Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.9

* Allow external packages to provide webview implementations for new platforms.

## 0.3.8+1

* Suppress deprecation warning for BinaryMessages. See: https://github.com/flutter/flutter/issues/33446
Expand Down
42 changes: 25 additions & 17 deletions packages/webview_flutter/lib/platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ import 'package:flutter/widgets.dart';

import 'webview_flutter.dart';

/// Interface for callbacks made by [WebViewPlatform].
/// Interface for callbacks made by [WebViewPlatformController].
///
/// The webview plugin implements this class, and passes an instance to the [WebViewPlatform].
/// [WebViewPlatform] is notifying this handler on events that happened on the platform's webview.
/// The webview plugin implements this class, and passes an instance to the [WebViewPlatformController].
/// [WebViewPlatformController] is notifying this handler on events that happened on the platform's webview.
abstract class WebViewPlatformCallbacksHandler {
/// Invoked by [WebViewPlatform] when a JavaScript channel message is received.
/// Invoked by [WebViewPlatformController] when a JavaScript channel message is received.
void onJavaScriptChannelMessage(String channel, String message);

/// Invoked by [WebViewPlatform] when a navigation request is pending.
/// Invoked by [WebViewPlatformController] when a navigation request is pending.
///
/// If true is returned the navigation is allowed, otherwise it is blocked.
bool onNavigationRequest({String url, bool isForMainFrame});

/// Invoked by [WebViewPlatform] when a page has finished loading.
/// Invoked by [WebViewPlatformController] when a page has finished loading.
void onPageFinished(String url);
}

/// Interface for talking to the webview's platform implementation.
///
/// An instance implementing this interface is passed to the `onWebViewPlatformCreated` callback that is
/// passed to [WebViewPlatformBuilder#onWebViewPlatformCreated].
abstract class WebViewPlatform {
abstract class WebViewPlatformController {
/// Creates a new WebViewPlatform.
///
/// Callbacks made by the WebView will be delegated to `handler`.
///
/// The `handler` parameter must not be null.
WebViewPlatform(WebViewPlatformCallbacksHandler handler);
WebViewPlatformController(WebViewPlatformCallbacksHandler handler);

/// Loads the specified URL.
///
Expand Down Expand Up @@ -182,7 +182,7 @@ class WebSettings {
}
}

/// Configuration to use when creating a new [WebViewPlatform].
/// Configuration to use when creating a new [WebViewPlatformController].
class CreationParams {
CreationParams(
{this.initialUrl, this.webSettings, this.javascriptChannelNames});
Expand All @@ -194,7 +194,7 @@ class CreationParams {

/// The initial [WebSettings] for the new webview.
///
/// This can later be updated with [WebViewPlatform.updateSettings].
/// This can later be updated with [WebViewPlatformController.updateSettings].
final WebSettings webSettings;

/// The initial set of JavaScript channels that are configured for this webview.
Expand All @@ -217,25 +217,25 @@ class CreationParams {
}

typedef WebViewPlatformCreatedCallback = void Function(
WebViewPlatform webViewPlatform);
WebViewPlatformController webViewPlatformController);

/// Interface building a platform WebView implementation.
/// Interface for a platform implementation of a WebView.
///
/// [WebView#platformBuilder] controls the builder that is used by [WebView].
/// [WebView.platform] controls the builder that is used by [WebView].
/// [AndroidWebViewPlatform] and [CupertinoWebViewPlatform] are the default implementations
/// for Android and iOS respectively.
abstract class WebViewBuilder {
abstract class WebViewPlatform {
/// Builds a new WebView.
///
/// Returns a Widget tree that embeds the created webview.
///
/// `creationParams` are the initial parameters used to setup the webview.
///
/// `webViewPlatformHandler` will be used for handling callbacks that are made by the created
/// [WebViewPlatform].
/// [WebViewPlatformController].
///
/// `onWebViewPlatformCreated` will be invoked after the platform specific [WebViewPlatform]
/// implementation is created with the [WebViewPlatform] instance as a parameter.
/// `onWebViewPlatformCreated` will be invoked after the platform specific [WebViewPlatformController]
/// implementation is created with the [WebViewPlatformController] instance as a parameter.
///
/// `gestureRecognizers` specifies which gestures should be consumed by the web view.
/// It is possible for other gesture recognizers to be competing with the web view on pointer
Expand All @@ -256,4 +256,12 @@ abstract class WebViewBuilder {
WebViewPlatformCreatedCallback onWebViewPlatformCreated,
Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers,
});

/// Clears all cookies for all [WebView] instances.
///
/// Returns true if cookies were present before clearing, else false.
Future<bool> clearCookies() {
throw UnimplementedError(
"WebView clearCookies is not implemented on the current platform");
}
}
9 changes: 7 additions & 2 deletions packages/webview_flutter/lib/src/webview_android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart';
Expand All @@ -12,10 +14,10 @@ import 'webview_method_channel.dart';

/// Builds an Android webview.
///
/// This is used as the default implementation for [WebView.platformBuilder] on Android. It uses
/// This is used as the default implementation for [WebView.platform] on Android. It uses
/// an [AndroidView] to embed the webview in the widget hierarchy, and uses a method channel to
/// communicate with the platform code.
class AndroidWebViewBuilder implements WebViewBuilder {
class AndroidWebView implements WebViewPlatform {
@override
Widget build({
BuildContext context,
Expand Down Expand Up @@ -55,4 +57,7 @@ class AndroidWebViewBuilder implements WebViewBuilder {
),
);
}

@override
Future<bool> clearCookies() => MethodChannelWebViewPlatform.clearCookies();
}
9 changes: 7 additions & 2 deletions packages/webview_flutter/lib/src/webview_cupertino.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart';
Expand All @@ -12,10 +14,10 @@ import 'webview_method_channel.dart';

/// Builds an iOS webview.
///
/// This is used as the default implementation for [WebView.platformBuilder] on iOS. It uses
/// This is used as the default implementation for [WebView.platform] on iOS. It uses
/// a [UiKitView] to embed the webview in the widget hierarchy, and uses a method channel to
/// communicate with the platform code.
class CupertinoWebViewBuilder implements WebViewBuilder {
class CupertinoWebView implements WebViewPlatform {
@override
Widget build({
BuildContext context,
Expand All @@ -39,4 +41,7 @@ class CupertinoWebViewBuilder implements WebViewBuilder {
creationParamsCodec: const StandardMessageCodec(),
);
}

@override
Future<bool> clearCookies() => MethodChannelWebViewPlatform.clearCookies();
}
17 changes: 15 additions & 2 deletions packages/webview_flutter/lib/src/webview_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'package:flutter/services.dart';

import '../platform_interface.dart';

/// A [WebViewPlatform] that uses a method channel to control the webview.
class MethodChannelWebViewPlatform implements WebViewPlatform {
/// A [WebViewPlatformController] that uses a method channel to control the webview.
class MethodChannelWebViewPlatform implements WebViewPlatformController {
MethodChannelWebViewPlatform(int id, this._platformCallbacksHandler)
: assert(_platformCallbacksHandler != null),
_channel = MethodChannel('plugins.flutter.io/webview_$id') {
Expand All @@ -20,6 +20,9 @@ class MethodChannelWebViewPlatform implements WebViewPlatform {

final MethodChannel _channel;

static const MethodChannel _cookieManagerChannel =
MethodChannel('plugins.flutter.io/cookie_manager');

Future<bool> _onMethodCall(MethodCall call) async {
switch (call.method) {
case 'javascriptChannelMessage':
Expand Down Expand Up @@ -105,6 +108,16 @@ class MethodChannelWebViewPlatform implements WebViewPlatform {
'removeJavascriptChannels', javascriptChannelNames.toList());
}

/// Method channel mplementation for [WebViewPlatform.clearCookies].
static Future<bool> clearCookies() {
return _cookieManagerChannel
// TODO(amirh): remove this when the invokeMethod update makes it to stable Flutter.
// https://github.com/flutter/flutter/issues/26431
// ignore: strong_mode_implicit_dynamic_method
.invokeMethod('clearCookies')
.then<bool>((dynamic result) => result);
}

static Map<String, dynamic> _webSettingsToMap(WebSettings settings) {
final Map<String, dynamic> map = <String, dynamic>{};
void _addIfNonNull(String key, dynamic value) {
Expand Down
Loading