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 2 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
@@ -0,0 +1,60 @@
// 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 'types/types.dart';
import 'webview_platform.dart';

/// Interface for callbacks made by [NavigationCallbackHandlerDelegate].
///
/// The webview plugin implements this class, and passes an instance to the
/// [NavigationCallbackHandlerDelegate].
/// [NavigationCallbackHandlerDelegate] is notifying this handler on events that
/// happened on the platform's webview.
abstract class NavigationCallbackHandlerDelegate extends PlatformInterface {
/// Creates a new [NavigationCallbacksHandlerDelegate]
factory NavigationCallbackHandlerDelegate() {
final NavigationCallbackHandlerDelegate callbackHandlerDelegate =
WebViewPlatform.instance!.createNavigationCallbackHandlerDelegate();
PlatformInterface.verify(callbackHandlerDelegate, _token);
return callbackHandlerDelegate;
}

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

static final Object _token = Object();

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

/// Invoked by [WebViewPlatformControllerDelegate] when a page has started
/// loading.
void onPageStarted(String url);

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

/// Invoked by [WebViewPlatformControllerDelegate] when a page is loading.
///
/// Only works when [WebSettings.hasProgressTracking] is set to `true`.
void onProgress(int progress);

/// Report web resource loading error to the host application.
void onWebResourceError(WebResourceError error);
}
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,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// 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 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:webview_flutter_platform_interface/src/v4/webview_controller_delegate.dart';

import '../webview_platform.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 and provide a factory method that takes the
/// [LoadRequestParamsDelegate] as a parameter.
///
/// {@tool sample}
/// This example demonstrates how to extend the [LoadRequestParamsDelegate] to
/// provide additional platform specific parameters.
///
/// Note that the additional parameters should always accept `null` or have a
/// default value to prevent breaking changes.
///
/// ```dart
/// class AndroidLoadRequestParamsDelegate extends LoadRequestParamsDelegate {
/// AndroidLoadRequestParamsDelegate._(
/// LoadRequestParamsDelegate loadRequestParams,
/// this.historyUrl,
/// ) : super(
/// uri: loadRequestParams.uri,
/// method: loadRequestParams.method,
/// headers: loadRequestParams.headers,
/// body: loadRequestParams.body,
/// );
///
/// factory AndroidLoadRequestParamsDelegate.fromLoadRequestParamsDelegate(
/// LoadRequestParamsDelegate loadRequestParams, {
/// Uri? historyUrl,
/// }) {
/// return AndroidLoadRequestParamsDelegate._(
/// loadRequestParams: loadRequestParams,
/// historyUrl: historyUrl,
/// );
/// }
///
/// final Uri? historyUrl;
/// }
/// ```
/// {@end-tool}
class LoadRequestParamsDelegate extends PlatformInterface {
/// Creates a new [LoadRequestParamsDelegate].
factory LoadRequestParamsDelegate({
required Uri uri,
required LoadRequestMethod method,
required Map<String, String> headers,
Uint8List? body,
}) {
final LoadRequestParamsDelegate loadRequestParamsDelegate =
WebViewPlatform.instance!.createLoadRequestParamsDelegate(
uri: uri,
method: method,
headers: headers,
body: body,
);
PlatformInterface.verify(loadRequestParamsDelegate, _token);
return loadRequestParamsDelegate;
}

/// Used by the platform implementation to create a new
/// [LoadRequestParamsDelegate].
///
/// Should only be used by platform implementations because they can't extend
/// a class that only contains a factory constructor.
@protected
LoadRequestParamsDelegate.implementation({
required this.uri,
required this.method,
required this.headers,
this.body,
}) : super(token: _token);

static final Object _token = Object();

/// 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;

/// Serializes the [WebViewRequest] to JSON.
Map<String, dynamic> toJson() => <String, dynamic>{
'uri': uri.toString(),
'method': method.serialize(),
'headers': headers,
'body': body,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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_mode.dart';
export 'load_request_params.dart';
export 'web_resource_error.dart';
export 'web_resource_error_type.dart';
export 'web_settings.dart';
export 'webview_cookie.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 'web_resource_error_type.dart';

/// Error returned in `WebView.onWebResourceError` when a web resource loading error has occurred.
@sealed
class WebResourceError {
/// Creates a new [WebResourceError]
///
/// A user should not need to instantiate this class, but will receive one in
/// [WebResourceErrorCallback].
WebResourceError({
required this.errorCode,
required this.description,
this.domain,
this.errorType,
this.failingUrl,
}) : assert(errorCode != null),
assert(description != null);

/// Raw code of the error from the respective platform.
///
/// On Android, the error code will be a constant from a
/// [WebViewClient](https://developer.android.com/reference/android/webkit/WebViewClient#summary) and
/// will have a corresponding [errorType].
///
/// On iOS, the error code will be a constant from `NSError.code` in
/// Objective-C. See
/// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorObjectsDomains/ErrorObjectsDomains.html
/// for more information on error handling on iOS. Some possible error codes
/// can be found at https://developer.apple.com/documentation/webkit/wkerrorcode?language=objc.
final int errorCode;

/// The domain of where to find the error code.
///
/// This field is only available on iOS and represents a "domain" from where
/// the [errorCode] is from. This value is taken directly from an `NSError`
/// in Objective-C. See
/// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorObjectsDomains/ErrorObjectsDomains.html
/// for more information on error handling on iOS.
final String? domain;

/// Description of the error that can be used to communicate the problem to the user.
final String description;

/// The type this error can be categorized as.
///
/// This will never be `null` on Android, but can be `null` on iOS.
final WebResourceErrorType? errorType;

/// Gets the URL for which the resource request was made.
///
/// This value is not provided on iOS. Alternatively, you can keep track of
/// the last values provided to [WebViewPlatformController.loadUrl].
final String? failingUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.

/// Possible error type categorizations used by [WebResourceError].
enum WebResourceErrorType {
/// User authentication failed on server.
authentication,

/// Malformed URL.
badUrl,

/// Failed to connect to the server.
connect,

/// Failed to perform SSL handshake.
failedSslHandshake,

/// Generic file error.
file,

/// File not found.
fileNotFound,

/// Server or proxy hostname lookup failed.
hostLookup,

/// Failed to read or write to the server.
io,

/// User authentication failed on proxy.
proxyAuthentication,

/// Too many redirects.
redirectLoop,

/// Connection timed out.
timeout,

/// Too many requests during this load.
tooManyRequests,

/// Generic error.
unknown,

/// Resource load was canceled by Safe Browsing.
unsafeResource,

/// Unsupported authentication scheme (not basic or digest).
unsupportedAuthScheme,

/// Unsupported URI scheme.
unsupportedScheme,

/// The web content process was terminated.
webContentProcessTerminated,

/// The web view was invalidated.
webViewInvalidated,

/// A JavaScript exception occurred.
javaScriptExceptionOccurred,

/// The result of JavaScript execution could not be returned.
javaScriptResultTypeIsUnsupported,
}
Loading