Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter] Initial v4.0 platform interface implementation #5109

Merged
merged 39 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ee662ec
Refactor to v4
mvanbeusekom Mar 24, 2022
06bb7b6
First definition of platform interface v4
mvanbeusekom Mar 25, 2022
d8ce25f
Converted WebResourceError to full delegate
mvanbeusekom Mar 31, 2022
75cc8e5
Merge branch 'issue/94051_webview_4.0' of github.com:Baseflow/flutter…
mvanbeusekom Mar 31, 2022
2abdd4a
Processed feedback on pull request
mvanbeusekom Apr 1, 2022
78c56e6
Fixed formatting
mvanbeusekom Apr 1, 2022
3b5bad5
Removed obsolete import statements
mvanbeusekom Apr 1, 2022
8684ac3
Applied feedback on WebViewControllerDelegate pull request
mvanbeusekom Apr 4, 2022
5302592
Implemented PR feedback. (Unit tests not yet updated)
BeMacized Apr 6, 2022
19c4580
Update tests
BeMacized Apr 6, 2022
196b4d8
Process PR feedback
BeMacized Apr 11, 2022
9361b8c
Process PR feedback
BeMacized Apr 11, 2022
fd81633
Add missing license block
BeMacized Apr 11, 2022
3936ff5
Add missing comments
BeMacized Apr 12, 2022
405219c
Applied feedback on pull requests.
mvanbeusekom Apr 12, 2022
8495c24
Fixed formatting
mvanbeusekom Apr 12, 2022
c772654
Regenerate mock classes after rename
mvanbeusekom Apr 12, 2022
f42f022
Fixed formatting
mvanbeusekom Apr 12, 2022
dca96f0
Removed WebSettings object.
mvanbeusekom Apr 13, 2022
221e386
Fixed formatting
mvanbeusekom Apr 13, 2022
9760d52
Bump version number
mvanbeusekom Apr 13, 2022
b13b4dd
Added dependency on meta package
mvanbeusekom Apr 13, 2022
d488453
Rebased on main
mvanbeusekom Apr 13, 2022
978121e
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom Apr 14, 2022
46bca5f
Applied feedback from review
mvanbeusekom Apr 14, 2022
e2be2b8
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom Apr 14, 2022
fa54703
Rearrange folder structure to allow easier migration final version
mvanbeusekom Apr 14, 2022
d065f93
Update example in documentation
mvanbeusekom Apr 14, 2022
76b2a02
Make ...CreationParams immutable
mvanbeusekom Apr 14, 2022
4cc0f56
Made JavaScriptChannelRegistry available
mvanbeusekom Apr 15, 2022
44d7782
Added PR feedback
mvanbeusekom Apr 19, 2022
789fd5a
Added clearLocalStorage method and test
mvanbeusekom Apr 20, 2022
51a1f76
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom May 10, 2022
ca99630
Refactored 'Delegate' postfix according to latest discussion
mvanbeusekom May 11, 2022
241d3b4
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom May 11, 2022
3e22600
Apply feedback from PR
mvanbeusekom May 11, 2022
dfb48ca
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom May 11, 2022
1db2d18
Apply feedback from PR
mvanbeusekom May 13, 2022
1487730
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom May 18, 2022
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 1.9.0

* Adds the first iteration of the v4 webview_flutter interface implementation.
* Removes unnecessary imports.

## 1.8.2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// 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:plugin_platform_interface/plugin_platform_interface.dart';

import 'webview_platform.dart';

/// An interface defining navigation events that occur on the native platform.
///
/// The [PlatformWebViewController] is notifying this delegate on events that
/// happened on the platform's webview. Platform implementations should
/// implement this class and pass an instance to the [PlatformWebViewController].
abstract class PlatformNavigationDelegate extends PlatformInterface {
/// Creates a new [PlatformNavigationDelegate]
factory PlatformNavigationDelegate(
PlatformNavigationDelegateCreationParams params) {
final PlatformNavigationDelegate callbackDelegate =
WebViewPlatform.instance!.createPlatformNavigationDelegate(params);
PlatformInterface.verify(callbackDelegate, _token);
return callbackDelegate;
}

/// Used by the platform implementation to create a new [PlatformNavigationDelegate].
///
/// Should only be used by platform implementations because they can't extend
/// a class that only contains a factory constructor.
@protected
PlatformNavigationDelegate.implementation(this.params) : super(token: _token);

static final Object _token = Object();

/// The parameters used to initialize the [PlatformNavigationDelegate].
final PlatformNavigationDelegateCreationParams params;

/// Invoked when a navigation request is pending.
///
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
Future<void> setOnNavigationRequest(
FutureOr<bool> Function({required String url, required bool isForMainFrame})
onNavigationRequest,
) {
throw UnimplementedError(
'setOnNavigationRequest is not implemented on the current platform.');
}

/// Invoked when a page has started loading.
///
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
Future<void> setOnPageStarted(
void Function(String url) onPageStarted,
) {
throw UnimplementedError(
'setOnPageStarted is not implemented on the current platform.');
}

/// Invoked when a page has finished loading.
///
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
Future<void> setOnPageFinished(
void Function(String url) onPageFinished,
) {
throw UnimplementedError(
'setOnPageFinished is not implemented on the current platform.');
}

/// Invoked when a page is loading to report the progress.
///
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
Future<void> setOnProgress(
void Function(int progress) onProgress,
) {
throw UnimplementedError(
'setOnProgress is not implemented on the current platform.');
}

/// Invoked when a resource loading error occurred.
///
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
Future<void> setOnWebResourceError(
void Function(WebResourceError error) onWebResourceError,
) {
throw UnimplementedError(
'setOnWebResourceError is not implemented on the current platform.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math';
import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'platform_navigation_delegate.dart';
import 'webview_platform.dart';

/// Interface for a platform implementation of a web view controller.
///
/// Platform implementations should extend this class rather than implement it
/// as `webview_flutter` does not consider newly added methods to be breaking
/// changes. Extending this class (using `extends`) ensures that the subclass
/// will get the default implementation, while platform implementations that
/// `implements` this interface will be broken by newly added
/// [PlatformWebViewCookieManager] methods.
abstract class PlatformWebViewController extends PlatformInterface {
/// Creates a new [PlatformWebViewController]
factory PlatformWebViewController(
PlatformWebViewControllerCreationParams params) {
final PlatformWebViewController webViewControllerDelegate =
WebViewPlatform.instance!.createPlatformWebViewController(params);
PlatformInterface.verify(webViewControllerDelegate, _token);
return webViewControllerDelegate;
}

/// Used by the platform implementation to create a new [PlatformWebViewController].
///
/// Should only be used by platform implementations because they can't extend
/// a class that only contains a factory constructor.
@protected
PlatformWebViewController.implementation(this.params) : super(token: _token);

static final Object _token = Object();

/// The parameters used to initialize the [PlatformWebViewController].
final PlatformWebViewControllerCreationParams params;

/// Loads the file located on the specified [absoluteFilePath].
///
/// The [absoluteFilePath] parameter should contain the absolute path to the
/// file as it is stored on the device. For example:
/// `/Users/username/Documents/www/index.html`.
///
/// Throws an ArgumentError if the [absoluteFilePath] does not exist.
Future<void> loadFile(
String absoluteFilePath,
) {
throw UnimplementedError(
'loadFile is not implemented on the current platform');
}

/// Loads the Flutter asset specified in the pubspec.yaml file.
///
/// Throws an ArgumentError if [key] is not part of the specified assets
/// in the pubspec.yaml file.
Future<void> loadFlutterAsset(
String key,
) {
throw UnimplementedError(
'loadFlutterAsset is not implemented on the current platform');
}

/// Loads the supplied HTML string.
///
/// The [baseUrl] parameter is used when resolving relative URLs within the
/// HTML string.
Future<void> loadHtmlString(
String html, {
String? baseUrl,
}) {
throw UnimplementedError(
'loadHtmlString is not implemented on the current platform');
}

/// Makes a specific HTTP request ands loads the response in the webview.
///
/// [WebViewRequest.method] must be one of the supported HTTP methods
/// in [WebViewRequestMethod].
///
/// If [WebViewRequest.headers] is not empty, its key-value pairs will be
/// added as the headers for the request.
///
/// If [WebViewRequest.body] is not null, it will be added as the body
/// for the request.
///
/// Throws an ArgumentError if [WebViewRequest.uri] has empty scheme.
Future<void> loadRequest(
LoadRequestParams params,
) {
throw UnimplementedError(
'loadRequest is not implemented on the current platform');
}

/// Accessor to the current URL that the WebView is displaying.
///
/// If no URL was ever loaded, returns `null`.
Future<String?> currentUrl() {
throw UnimplementedError(
'currentUrl is not implemented on the current platform');
}

/// Checks whether there's a back history item.
Future<bool> canGoBack() {
throw UnimplementedError(
'canGoBack is not implemented on the current platform');
}

/// Checks whether there's a forward history item.
Future<bool> canGoForward() {
throw UnimplementedError(
'canGoForward is not implemented on the current platform');
}

/// Goes back in the history of this WebView.
///
/// If there is no back history item this is a no-op.
Future<void> goBack() {
throw UnimplementedError(
'goBack is not implemented on the current platform');
}

/// Goes forward in the history of this WebView.
///
/// If there is no forward history item this is a no-op.
Future<void> goForward() {
throw UnimplementedError(
'goForward is not implemented on the current platform');
}

/// Reloads the current URL.
Future<void> reload() {
throw UnimplementedError(
'reload is not implemented on the current platform');
}

/// Clears all caches used by the [WebView].
///
/// The following caches are cleared:
/// 1. Browser HTTP Cache.
/// 2. [Cache API](https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/cache-api) caches.
/// These are not yet supported in iOS WkWebView. Service workers tend to use this cache.
/// 3. Application cache.
Future<void> clearCache() {
throw UnimplementedError(
'clearCache is not implemented on the current platform');
}

/// Clears the local storage used by the [WebView].
Future<void> clearLocalStorage() {
throw UnimplementedError(
'clearLocalStorage is not implemented on the current platform');
}

/// Sets the [PlatformNavigationDelegate] containing the callback methods that
/// are called during navigation events.
Future<void> setPlatformNavigationDelegate(
PlatformNavigationDelegate handler) {
throw UnimplementedError(
'setPlatformNavigationDelegate is not implemented on the current platform');
}

/// Runs the given JavaScript in the context of the current page.
///
/// The Future completes with an error if a JavaScript error occurred.
Future<void> runJavaScript(String javaScript) {
throw UnimplementedError(
'runJavaScript is not implemented on the current platform');
}

/// Runs the given JavaScript in the context of the current page, and returns the result.
///
/// The Future completes with an error if a JavaScript error occurred, or if the
/// type the given expression evaluates to is unsupported. Unsupported values include
/// certain non-primitive types on iOS, as well as `undefined` or `null` on iOS 14+.
Future<String> runJavaScriptReturningResult(String javaScript) {
throw UnimplementedError(
'runJavaScriptReturningResult is not implemented on the current platform');
}

/// Adds a new JavaScript channel to the set of enabled channels.
Future<void> addJavaScriptChannel(
JavaScriptChannelParams javaScriptChannelParams,
) {
throw UnimplementedError(
'addJavaScriptChannel is not implemented on the current platform');
}

/// Removes the JavaScript channel with the matching name from the set of
/// enabled channels.
///
/// This disables the channel with the matching name if it was previously
/// enabled through the [addJavaScriptChannel].
Future<void> removeJavaScriptChannel(String javaScriptChannelName) {
throw UnimplementedError(
'removeJavaScriptChannel is not implemented on the current platform');
}

/// Returns the title of the currently loaded page.
Future<String?> getTitle() {
throw UnimplementedError(
'getTitle is not implemented on the current platform');
}

/// Set the scrolled position of this view.
///
/// The parameters `x` and `y` specify the position to scroll to in WebView pixels.
Future<void> scrollTo(int x, int y) {
throw UnimplementedError(
'scrollTo is not implemented on the current platform');
}

/// Move the scrolled position of this view.
///
/// The parameters `x` and `y` specify the amount of WebView pixels to scroll by.
Future<void> scrollBy(int x, int y) {
throw UnimplementedError(
'scrollBy is not implemented on the current platform');
}

/// Return the current scroll position of this view.
///
/// Scroll position is measured from the top left.
Future<Point<int>> getScrollPosition() {
throw UnimplementedError(
'getScrollPosition is not implemented on the current platform');
}

/// Whether to enable the platform's webview content debugging tools.
Future<void> enableDebugging(bool enabled) {
throw UnimplementedError(
'enableDebugging is not implemented on the current platform');
}

/// Whether to allow swipe based navigation on supported platforms.
Future<void> enableGestureNavigation(bool enabled) {
throw UnimplementedError(
'enableGestureNavigation is not implemented on the current platform');
}

/// Whhether to support zooming using its on-screen zoom controls and gestures.
Future<void> enableZoom(bool enabled) {
throw UnimplementedError(
'enableZoom is not implemented on the current platform');
}

/// Set the current background color of this view.
Future<void> setBackgroundColor(Color color) {
throw UnimplementedError(
'setBackgroundColor is not implemented on the current platform');
}

/// Sets the JavaScript execution mode to be used by the webview.
Future<void> setJavaScriptMode(JavaScriptMode javaScriptMode) {
throw UnimplementedError(
'setJavaScriptMode is not implemented on the current platform');
}

/// Sets the value used for the HTTP `User-Agent:` request header.
Future<void> setUserAgent(String? userAgent) {
throw UnimplementedError(
'setUserAgent is not implemented on the current platform');
}
}

/// Describes the parameters necessary for registering a JavaScript channel.
class JavaScriptChannelParams {
/// Creates a new [JavaScriptChannelParams] object.
JavaScriptChannelParams({
required this.name,
required this.onMessageReceived,
});

/// The name that identifies the JavaScript channel.
final String name;

/// The callback method that is invoked when a [JavaScriptMessage] is
/// received.
final void Function(JavaScriptMessage) onMessageReceived;
}
Loading