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.7k
[webview_flutter_android] Adds Android implementation of PlatformNavigationDelegate #6495
Merged
mvanbeusekom
merged 8 commits into
flutter:v4_webview
from
Baseflow:v4_webview_android_navigation_delegate
Oct 27, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cab51be
Adds the Android implementation of PlatformNavigationDelegate.
mvanbeusekom eb3320b
Merge remote-tracking branch 'upstream/v4_webview' into v4_webview_an…
mvanbeusekom affdfd7
Fix analyzer warnings (all in existing code)
mvanbeusekom 45ba5e1
Fix formatting
mvanbeusekom ff44ef4
Add missing license header
mvanbeusekom ffc1a7d
Merge branch 'v4_webview' of github.com:flutter/plugins into v4_webvi…
mvanbeusekom 88a4f0b
Added PR feedback
mvanbeusekom 3bedbc0
Fixed formatting
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
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
279 changes: 279 additions & 0 deletions
279
...s/webview_flutter/webview_flutter_android/lib/src/v4/src/android_navigation_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,279 @@ | ||
| // 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:webview_flutter_platform_interface/v4/webview_flutter_platform_interface.dart'; | ||
|
|
||
| import '../../android_webview.dart' as android_webview; | ||
| import 'android_proxy.dart'; | ||
|
|
||
| /// Signature for the `loadUrl` callback responsible for loading the [url] | ||
| /// after a navigation request has been approved. | ||
| typedef LoadUrlCallback = Future<void> Function( | ||
| String url, Map<String, String>? headers); | ||
|
|
||
| /// Error returned in `WebView.onWebResourceError` when a web resource loading error has occurred. | ||
| class AndroidWebResourceError extends WebResourceError { | ||
| /// Creates a new [AndroidWebResourceError]. | ||
| AndroidWebResourceError._({ | ||
| required super.errorCode, | ||
| required super.description, | ||
| this.failingUrl, | ||
| }) : super( | ||
| errorType: _errorCodeToErrorType(errorCode), | ||
| ); | ||
|
|
||
| /// Gets the URL for which the failing resource request was made. | ||
| final String? failingUrl; | ||
|
|
||
| static WebResourceErrorType? _errorCodeToErrorType(int errorCode) { | ||
| switch (errorCode) { | ||
| case android_webview.WebViewClient.errorAuthentication: | ||
| return WebResourceErrorType.authentication; | ||
| case android_webview.WebViewClient.errorBadUrl: | ||
| return WebResourceErrorType.badUrl; | ||
| case android_webview.WebViewClient.errorConnect: | ||
| return WebResourceErrorType.connect; | ||
| case android_webview.WebViewClient.errorFailedSslHandshake: | ||
| return WebResourceErrorType.failedSslHandshake; | ||
| case android_webview.WebViewClient.errorFile: | ||
| return WebResourceErrorType.file; | ||
| case android_webview.WebViewClient.errorFileNotFound: | ||
| return WebResourceErrorType.fileNotFound; | ||
| case android_webview.WebViewClient.errorHostLookup: | ||
| return WebResourceErrorType.hostLookup; | ||
| case android_webview.WebViewClient.errorIO: | ||
| return WebResourceErrorType.io; | ||
| case android_webview.WebViewClient.errorProxyAuthentication: | ||
| return WebResourceErrorType.proxyAuthentication; | ||
| case android_webview.WebViewClient.errorRedirectLoop: | ||
| return WebResourceErrorType.redirectLoop; | ||
| case android_webview.WebViewClient.errorTimeout: | ||
| return WebResourceErrorType.timeout; | ||
| case android_webview.WebViewClient.errorTooManyRequests: | ||
| return WebResourceErrorType.tooManyRequests; | ||
| case android_webview.WebViewClient.errorUnknown: | ||
| return WebResourceErrorType.unknown; | ||
| case android_webview.WebViewClient.errorUnsafeResource: | ||
| return WebResourceErrorType.unsafeResource; | ||
| case android_webview.WebViewClient.errorUnsupportedAuthScheme: | ||
| return WebResourceErrorType.unsupportedAuthScheme; | ||
| case android_webview.WebViewClient.errorUnsupportedScheme: | ||
| return WebResourceErrorType.unsupportedScheme; | ||
| } | ||
|
|
||
| throw ArgumentError( | ||
| 'Could not find a WebResourceErrorType for errorCode: $errorCode', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Object specifying creation parameters for creating a [AndroidNavigationDelegate]. | ||
| /// | ||
| /// When adding additional fields make sure they can be null or have a default | ||
| /// value to avoid breaking changes. See [PlatformNavigationDelegateCreationParams] for | ||
| /// more information. | ||
| @immutable | ||
| class AndroidNavigationDelegateCreationParams | ||
| extends PlatformNavigationDelegateCreationParams { | ||
| /// Creates a new [AndroidNavigationDelegateCreationParams] instance. | ||
| const AndroidNavigationDelegateCreationParams._({ | ||
| @visibleForTesting this.androidWebViewProxy = const AndroidWebViewProxy(), | ||
| }) : super(); | ||
|
|
||
| /// Creates a [AndroidNavigationDelegateCreationParams] instance based on [PlatformNavigationDelegateCreationParams]. | ||
| factory AndroidNavigationDelegateCreationParams.fromPlatformNavigationDelegateCreationParams( | ||
| // Recommended placeholder to prevent being broken by platform interface. | ||
| // ignore: avoid_unused_constructor_parameters | ||
| PlatformNavigationDelegateCreationParams params, { | ||
| @visibleForTesting | ||
| AndroidWebViewProxy androidWebViewProxy = const AndroidWebViewProxy(), | ||
| }) { | ||
| return AndroidNavigationDelegateCreationParams._( | ||
| androidWebViewProxy: androidWebViewProxy, | ||
| ); | ||
| } | ||
|
|
||
| /// Handles constructing objects and calling static methods for the Android WebView | ||
| /// native library. | ||
| @visibleForTesting | ||
| final AndroidWebViewProxy androidWebViewProxy; | ||
| } | ||
|
|
||
| /// A place to register callback methods responsible to handle navigation events | ||
| /// triggered by the [android_webview.WebView]. | ||
| class AndroidNavigationDelegate extends PlatformNavigationDelegate { | ||
| /// Creates a new [AndroidNavigationkDelegate]. | ||
| AndroidNavigationDelegate(PlatformNavigationDelegateCreationParams params) | ||
| : super.implementation(params is AndroidNavigationDelegateCreationParams | ||
| ? params | ||
| : AndroidNavigationDelegateCreationParams | ||
| .fromPlatformNavigationDelegateCreationParams(params)) { | ||
| final WeakReference<AndroidNavigationDelegate> weakThis = | ||
| WeakReference<AndroidNavigationDelegate>(this); | ||
|
|
||
| _webChromeClient = (params as AndroidNavigationDelegateCreationParams) | ||
| .androidWebViewProxy | ||
| .createAndroidWebChromeClient( | ||
| onProgressChanged: (android_webview.WebView webView, int progress) { | ||
| if (weakThis.target?._onProgress != null) { | ||
| weakThis.target!._onProgress!(progress); | ||
| } | ||
| }); | ||
|
|
||
| _webViewClient = params.androidWebViewProxy.createAndroidWebViewClient( | ||
| onPageFinished: (android_webview.WebView webView, String url) { | ||
| if (weakThis.target?._onPageFinished != null) { | ||
| weakThis.target!._onPageFinished!(url); | ||
| } | ||
| }, | ||
| onPageStarted: (android_webview.WebView webView, String url) { | ||
| if (weakThis.target?._onPageStarted != null) { | ||
| weakThis.target!._onPageStarted!(url); | ||
| } | ||
| }, | ||
| onReceivedRequestError: ( | ||
| android_webview.WebView webView, | ||
| android_webview.WebResourceRequest request, | ||
| android_webview.WebResourceError error, | ||
| ) { | ||
| if (weakThis.target?._onWebResourceError != null && | ||
| request.isForMainFrame) { | ||
| weakThis.target!._onWebResourceError!(AndroidWebResourceError._( | ||
| errorCode: error.errorCode, | ||
| description: error.description, | ||
| failingUrl: request.url, | ||
| )); | ||
| } | ||
| }, | ||
| onReceivedError: ( | ||
| android_webview.WebView webView, | ||
| int errorCode, | ||
| String description, | ||
| String failingUrl, | ||
| ) { | ||
| if (weakThis.target?._onWebResourceError != null) { | ||
| weakThis.target!._onWebResourceError!(AndroidWebResourceError._( | ||
| errorCode: errorCode, | ||
| description: description, | ||
| failingUrl: failingUrl, | ||
| )); | ||
| } | ||
| }, | ||
| requestLoading: ( | ||
| android_webview.WebView webView, | ||
| android_webview.WebResourceRequest request, | ||
| ) { | ||
| if (weakThis.target != null) { | ||
| weakThis.target!._handleNavigation( | ||
| request.url, | ||
| request.requestHeaders, | ||
| ); | ||
| } | ||
| }, | ||
| urlLoading: ( | ||
| android_webview.WebView webView, | ||
| String url, | ||
| ) { | ||
| if (weakThis.target != null) { | ||
| weakThis.target!._handleNavigation(url, <String, String>{}); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| late final android_webview.WebChromeClient _webChromeClient; | ||
|
|
||
| /// Gets the native [android_webview.WebChromeClient] that is bridged by this [AndroidNavigationDelegate]. | ||
| /// | ||
| /// Used by the [AndroidWebViewController] to set the `android_webview.WebView.setWebChromeClient`. | ||
| android_webview.WebChromeClient get androidWebChromeClient => | ||
| _webChromeClient; | ||
|
|
||
| late final android_webview.WebViewClient _webViewClient; | ||
|
|
||
| /// Gets the native [android_webview.WebViewClient] that is bridged by this [AndroidNavigationDelegate]. | ||
| /// | ||
| /// Used by the [AndroidWebViewController] to set the `android_webview.WebView.setWebViewClient`. | ||
| android_webview.WebViewClient get androidWebViewClient => _webViewClient; | ||
|
|
||
| PageEventCallback? _onPageFinished; | ||
| PageEventCallback? _onPageStarted; | ||
| ProgressCallback? _onProgress; | ||
| WebResourceErrorCallback? _onWebResourceError; | ||
| NavigationRequestCallback? _onNavigationRequest; | ||
| LoadUrlCallback? _onLoadUrl; | ||
|
|
||
| void _handleNavigation(String url, Map<String, String> headers) { | ||
| final LoadUrlCallback? onLoadUrl = _onLoadUrl; | ||
| final NavigationRequestCallback? onNavigationRequest = _onNavigationRequest; | ||
|
|
||
| if (onNavigationRequest == null || onLoadUrl == null) { | ||
| return; | ||
| } | ||
|
|
||
| final FutureOr<NavigationDecision> returnValue = onNavigationRequest( | ||
| NavigationRequest( | ||
| isMainFrame: true, | ||
| url: url, | ||
| ), | ||
| ); | ||
|
|
||
| if (returnValue is NavigationDecision && | ||
| returnValue == NavigationDecision.navigate) { | ||
| onLoadUrl(url, headers); | ||
| } else if (returnValue is Future<NavigationDecision>) { | ||
| returnValue.then((NavigationDecision shouldLoadUrl) { | ||
| if (shouldLoadUrl == NavigationDecision.navigate) { | ||
| onLoadUrl(url, headers); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// Invoked when loading the [url] after a navigation request is approved. | ||
| Future<void> setOnLoadUrl( | ||
| LoadUrlCallback onLoadUrl, | ||
| ) async { | ||
| _onLoadUrl = onLoadUrl; | ||
| } | ||
|
|
||
| @override | ||
| Future<void> setOnNavigationRequest( | ||
| NavigationRequestCallback onNavigationRequest, | ||
| ) async { | ||
| _onNavigationRequest = onNavigationRequest; | ||
| _webViewClient.setSynchronousReturnValueForShouldOverrideUrlLoading(true); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> setOnPageStarted( | ||
| PageEventCallback onPageStarted, | ||
| ) async { | ||
| _onPageStarted = onPageStarted; | ||
| } | ||
|
|
||
| @override | ||
| Future<void> setOnPageFinished( | ||
| PageEventCallback onPageFinished, | ||
| ) async { | ||
| _onPageFinished = onPageFinished; | ||
| } | ||
|
|
||
| @override | ||
| Future<void> setOnProgress( | ||
| ProgressCallback onProgress, | ||
| ) async { | ||
| _onProgress = onProgress; | ||
| } | ||
|
|
||
| @override | ||
| Future<void> setOnWebResourceError( | ||
| WebResourceErrorCallback onWebResourceError, | ||
| ) async { | ||
| _onWebResourceError = onWebResourceError; | ||
| } | ||
| } | ||
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.