This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[webview_flutter] Initial v4.0 platform interface implementation #5109
Merged
fluttergithubbot
merged 39 commits into
flutter-team-archive:main
from
Baseflow:issue/94051_webview_4.0
May 18, 2022
Merged
Changes from 32 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
ee662ec
Refactor to v4
mvanbeusekom 06bb7b6
First definition of platform interface v4
mvanbeusekom d8ce25f
Converted WebResourceError to full delegate
mvanbeusekom 75cc8e5
Merge branch 'issue/94051_webview_4.0' of github.com:Baseflow/flutter…
mvanbeusekom 2abdd4a
Processed feedback on pull request
mvanbeusekom 78c56e6
Fixed formatting
mvanbeusekom 3b5bad5
Removed obsolete import statements
mvanbeusekom 8684ac3
Applied feedback on WebViewControllerDelegate pull request
mvanbeusekom 5302592
Implemented PR feedback. (Unit tests not yet updated)
BeMacized 19c4580
Update tests
BeMacized 196b4d8
Process PR feedback
BeMacized 9361b8c
Process PR feedback
BeMacized fd81633
Add missing license block
BeMacized 3936ff5
Add missing comments
BeMacized 405219c
Applied feedback on pull requests.
mvanbeusekom 8495c24
Fixed formatting
mvanbeusekom c772654
Regenerate mock classes after rename
mvanbeusekom f42f022
Fixed formatting
mvanbeusekom dca96f0
Removed WebSettings object.
mvanbeusekom 221e386
Fixed formatting
mvanbeusekom 9760d52
Bump version number
mvanbeusekom b13b4dd
Added dependency on meta package
mvanbeusekom d488453
Rebased on main
mvanbeusekom 978121e
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom 46bca5f
Applied feedback from review
mvanbeusekom e2be2b8
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom fa54703
Rearrange folder structure to allow easier migration final version
mvanbeusekom d065f93
Update example in documentation
mvanbeusekom 76b2a02
Make ...CreationParams immutable
mvanbeusekom 4cc0f56
Made JavaScriptChannelRegistry available
mvanbeusekom 44d7782
Added PR feedback
mvanbeusekom 789fd5a
Added clearLocalStorage method and test
mvanbeusekom 51a1f76
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom ca99630
Refactored 'Delegate' postfix according to latest discussion
mvanbeusekom 241d3b4
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom 3e22600
Apply feedback from PR
mvanbeusekom dfb48ca
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom 1db2d18
Apply feedback from PR
mvanbeusekom 1487730
Merge remote-tracking branch 'upstream/main' into issue/94051_webview…
mvanbeusekom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ew_flutter/webview_flutter_platform_interface/lib/v4/src/javascript_channel_registry.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // 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 'package:meta/meta.dart'; | ||
|
|
||
| import 'types/javascript_channel.dart'; | ||
| import 'types/javascript_message.dart'; | ||
|
|
||
| /// Utility class for managing named JavaScript channels and forwarding incoming | ||
| /// messages on the correct channel. | ||
| @sealed | ||
| class JavaScriptChannelRegistry { | ||
| /// Constructs a [JavaScriptChannelRegistry] initializing it with the given | ||
| /// set of [JavaScriptChannel]s. | ||
| JavaScriptChannelRegistry(Set<JavaScriptChannel>? channels) { | ||
| updateJavaScriptChannelsFromSet(channels); | ||
| } | ||
|
|
||
| /// Maps a channel name to a channel. | ||
| final Map<String, JavaScriptChannel> channels = <String, JavaScriptChannel>{}; | ||
|
|
||
| /// Invoked when a JavaScript channel message is received. | ||
| void onJavaScriptChannelMessage(String channel, String message) { | ||
| final JavaScriptChannel? javaScriptChannel = channels[channel]; | ||
|
|
||
| if (javaScriptChannel == null) { | ||
| throw ArgumentError('No channel registered with name $channel.'); | ||
| } | ||
|
|
||
| javaScriptChannel.onMessageReceived(JavaScriptMessage(message: message)); | ||
| } | ||
|
|
||
| /// Replaces the set of [JavaScriptChannel]s with the new set. | ||
| void updateJavaScriptChannelsFromSet(Set<JavaScriptChannel>? channels) { | ||
| this.channels.clear(); | ||
| if (channels == null) { | ||
| return; | ||
| } | ||
|
|
||
| for (final JavaScriptChannel channel in channels) { | ||
| this.channels[channel.name] = channel; | ||
| } | ||
| } | ||
| } | ||
88 changes: 88 additions & 0 deletions
88
...w_flutter/webview_flutter_platform_interface/lib/v4/src/navigation_callback_delegate.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // 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 [WebViewControllerDelegate] 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 [WebViewControllerDelegate]. | ||
| abstract class NavigationCallbackDelegate extends PlatformInterface { | ||
|
mvanbeusekom marked this conversation as resolved.
Outdated
|
||
| /// Creates a new [NavigationCallbackDelegate] | ||
| factory NavigationCallbackDelegate(NavigationCallbackCreationParams params) { | ||
| final NavigationCallbackDelegate callbackDelegate = | ||
| WebViewPlatform.instance!.createNavigationCallbackDelegate(params); | ||
| PlatformInterface.verify(callbackDelegate, _token); | ||
| return callbackDelegate; | ||
| } | ||
|
|
||
| /// Used by the platform implementation to create a new [NavigationCallbackDelegate]. | ||
| /// | ||
| /// Should only be used by platform implementations because they can't extend | ||
| /// a class that only contains a factory constructor. | ||
| @protected | ||
| NavigationCallbackDelegate.implementation(this.params) : super(token: _token); | ||
|
|
||
| static final Object _token = Object(); | ||
|
|
||
| /// The parameters used to initialize the [NavigationCallbackDelegate]. | ||
| final NavigationCallbackCreationParams params; | ||
|
|
||
| /// Invoked when a navigation request is pending. | ||
| /// | ||
| /// See [WebViewControllerDelegate.setNavigationCallbackDelegate]. | ||
| 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 [WebViewControllerDelegate.setNavigationCallbackDelegate]. | ||
| 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 [WebViewControllerDelegate.setNavigationCallbackDelegate]. | ||
| 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 [WebViewControllerDelegate.setNavigationCallbackDelegate]. | ||
| 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 [WebViewControllerDelegate.setNavigationCallbackDelegate]. | ||
| Future<void> setOnWebResourceError( | ||
| void Function(WebResourceError error) onWebResourceError, | ||
| ) { | ||
| throw UnimplementedError( | ||
| 'setOnWebResourceError is not implemented on the current platform.'); | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
...bview_flutter/webview_flutter_platform_interface/lib/v4/src/types/javascript_channel.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // 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 'javascript_message.dart'; | ||
|
|
||
| /// Callback type for handling messages sent from JavaScript running in a web view. | ||
| typedef JavaScriptMessageHandler = void Function(JavaScriptMessage message); | ||
|
|
||
| final RegExp _validChannelNames = RegExp(r'^[a-zA-Z_][a-zA-Z0-9_]*$'); | ||
|
|
||
| /// A named channel for receiving messaged from JavaScript code running inside a web view. | ||
| class JavaScriptChannel { | ||
| /// Constructs a JavaScript channel. | ||
| /// | ||
| /// The parameters `name` and `onMessageReceived` must not be null. | ||
| JavaScriptChannel({ | ||
| required this.name, | ||
| required this.onMessageReceived, | ||
| }) : assert(name != null), | ||
| assert(onMessageReceived != null), | ||
| assert(_validChannelNames.hasMatch(name)); | ||
|
|
||
| /// The channel's name. | ||
| /// | ||
| /// The name must start with a letter or underscore(_), followed by any | ||
| /// combination of alphabetic characters plus digits. | ||
| /// | ||
| /// Note that any JavaScript existing `window` property with this name will be | ||
| /// overriden. | ||
| final String name; | ||
|
|
||
| /// A callback that's invoked when a message is received through the channel. | ||
| final JavaScriptMessageHandler onMessageReceived; | ||
| } |
51 changes: 51 additions & 0 deletions
51
...bview_flutter/webview_flutter_platform_interface/lib/v4/src/types/javascript_message.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // 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 'package:flutter/foundation.dart'; | ||
|
|
||
| /// A message that was sent by JavaScript code running in a [WebView]. | ||
| /// | ||
| /// Platform specific implementations can add additional fields by extending | ||
| /// this class and providing a factory method that takes the | ||
| /// [JavaScriptMessage] as a parameter. | ||
| /// | ||
| /// {@tool sample} | ||
| /// This example demonstrates how to extend the [JavaScriptMessage] to | ||
| /// provide additional platform specific parameters. | ||
| /// | ||
| /// When extending [JavaScriptMessage] additional parameters should always | ||
| /// accept `null` or have a default value to prevent breaking changes. | ||
| /// | ||
| /// ```dart | ||
| /// @immutable | ||
| /// class WKWebViewScriptMessage extends JavaScriptMessage { | ||
| /// WKWebViewScriptMessage._( | ||
| /// JavaScriptMessage javaScriptMessage, | ||
| /// this.extraData, | ||
| /// ) : super(javaScriptMessage.message); | ||
| /// | ||
| /// factory WKWebViewScriptMessage.fromJavaScripMessage( | ||
| /// JavaScriptMessage javaScripMessage, { | ||
| /// String? extraData, | ||
| /// }) { | ||
| /// return WKWebViewScriptMessage._( | ||
| /// javaScriptMessage, | ||
| /// extraData: extraData, | ||
| /// ); | ||
| /// } | ||
| /// | ||
| /// final String? extraData; | ||
| /// } | ||
| /// ``` | ||
| /// {@end-tool} | ||
| @immutable | ||
| class JavaScriptMessage { | ||
| /// Creates a new JavaScript message object. | ||
| const JavaScriptMessage({ | ||
| required this.message, | ||
| }); | ||
|
|
||
| /// The contents of the message that was sent by the JavaScript code. | ||
| final String message; | ||
| } |
12 changes: 12 additions & 0 deletions
12
.../webview_flutter/webview_flutter_platform_interface/lib/v4/src/types/javascript_mode.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // 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. | ||
|
|
||
| /// Describes the state of JavaScript support in a given web view. | ||
| enum JavaScriptMode { | ||
| /// JavaScript execution is disabled. | ||
| disabled, | ||
|
|
||
| /// JavaScript execution is not restricted. | ||
| unrestricted, | ||
| } |
88 changes: 88 additions & 0 deletions
88
...view_flutter/webview_flutter_platform_interface/lib/v4/src/types/load_request_params.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // 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:typed_data'; | ||
| import 'package:flutter/foundation.dart'; | ||
|
|
||
| import '../webview_controller_delegate.dart'; | ||
|
|
||
| /// Defines the supported HTTP methods for loading a page in [WebViewControllerDelegate]. | ||
| enum LoadRequestMethod { | ||
| /// HTTP GET method. | ||
| get, | ||
|
|
||
| /// HTTP POST method. | ||
| post, | ||
| } | ||
|
|
||
| /// Extension methods on the [LoadRequestMethod] enum. | ||
| extension LoadRequestMethodExtensions on LoadRequestMethod { | ||
| /// Converts [LoadRequestMethod] to [String] format. | ||
| String serialize() { | ||
| switch (this) { | ||
| case LoadRequestMethod.get: | ||
| return 'get'; | ||
| case LoadRequestMethod.post: | ||
| return 'post'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Defines the parameters that can be used to load a page with the [WebViewControllerDelegate]. | ||
| /// | ||
| /// Platform specific implementations can add additional fields by extending | ||
| /// this class. | ||
| /// | ||
| /// {@tool sample} | ||
| /// This example demonstrates how to extend the [LoadRequestParams] to | ||
| /// provide additional platform specific parameters. | ||
| /// | ||
| /// When extending [LoadRequestParams] additional parameters should always | ||
| /// accept `null` or have a default value to prevent breaking changes. | ||
| /// | ||
| /// ```dart | ||
| /// class AndroidLoadRequestParams extends LoadRequestParams { | ||
| /// AndroidLoadRequestParams._({ | ||
| /// required LoadRequestParams params, | ||
| /// this.historyUrl, | ||
| /// }) : super( | ||
| /// uri: params.uri, | ||
| /// method: params.method, | ||
| /// body: params.body, | ||
| /// headers: params.headers, | ||
| /// ); | ||
| /// | ||
| /// factory AndroidLoadRequestParams.fromLoadRequestParams( | ||
| /// LoadRequestParams params, { | ||
| /// Uri? historyUrl, | ||
| /// }) { | ||
| /// return AndroidLoadRequestParams._(params, historyUrl: historyUrl); | ||
| /// } | ||
| /// | ||
| /// final Uri? historyUrl; | ||
| /// } | ||
| /// ``` | ||
| /// {@end-tool} | ||
| @immutable | ||
| class LoadRequestParams { | ||
| /// Used by the platform implementation to create a new [LoadRequestParams]. | ||
| const LoadRequestParams({ | ||
| required this.uri, | ||
| required this.method, | ||
| required this.headers, | ||
| this.body, | ||
| }); | ||
|
|
||
| /// URI for the request. | ||
| final Uri uri; | ||
|
|
||
| /// HTTP method used to make the request. | ||
| final LoadRequestMethod method; | ||
|
|
||
| /// Headers for the request. | ||
| final Map<String, String> headers; | ||
|
|
||
| /// HTTP body for the request. | ||
| final Uint8List? body; | ||
| } |
44 changes: 44 additions & 0 deletions
44
...view_flutter_platform_interface/lib/v4/src/types/navigation_callback_creation_params.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // 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 'package:flutter/material.dart'; | ||
|
|
||
| /// Object specifying creation parameters for creating a [NavigationCallbackDelegate]. | ||
| /// | ||
| /// Platform specific implementations can add additional fields by extending | ||
| /// this class. | ||
| /// | ||
| /// {@tool sample} | ||
| /// This example demonstrates how to extend the [NavigationCallbackCreationParams] to | ||
| /// provide additional platform specific parameters. | ||
| /// | ||
| /// When extending [NavigationCallbackCreationParams] additional | ||
| /// parameters should always accept `null` or have a default value to prevent | ||
| /// breaking changes. | ||
| /// | ||
| /// ```dart | ||
| /// class AndroidNavigationCallbackCreationParams extends NavigationCallbackCreationParams { | ||
| /// AndroidNavigationCallbackCreationParams._( | ||
| /// // This parameter prevents breaking changes later. | ||
| /// // ignore: avoid_unused_constructor_parameters | ||
| /// NavigationCallbackCreationParams params, { | ||
| /// this.filter, | ||
| /// }) : super(); | ||
| /// | ||
| /// factory AndroidNavigationCallbackCreationParams.fromNavigationCallbackCreationParams( | ||
| /// NavigationCallbackCreationParams params, { | ||
| /// String? filter, | ||
| /// }) { | ||
| /// return AndroidNavigationCallbackCreationParams._(params, filter: filter); | ||
| /// } | ||
| /// | ||
| /// final String? filter; | ||
| /// } | ||
| /// ``` | ||
| /// {@end-tool} | ||
| @immutable | ||
| class NavigationCallbackCreationParams { | ||
|
mvanbeusekom marked this conversation as resolved.
Outdated
|
||
| /// Used by the platform implementation to create a new [NavigationCallbackDelegate]. | ||
| const NavigationCallbackCreationParams(); | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
packages/webview_flutter/webview_flutter_platform_interface/lib/v4/src/types/types.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // 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. | ||
|
|
||
| export 'javascript_channel.dart'; | ||
| export 'javascript_message.dart'; | ||
| export 'javascript_mode.dart'; | ||
| export 'load_request_params.dart'; | ||
| export 'navigation_callback_creation_params.dart'; | ||
| export 'web_resource_error.dart'; | ||
| export 'webview_controller_creation_params.dart'; | ||
| export 'webview_cookie.dart'; | ||
| export 'webview_cookie_manager_creation_params.dart'; | ||
| export 'webview_widget_creation_params.dart'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.