diff --git a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md index 8f329f1dbe04..3ece3a82e1df 100644 --- a/packages/webview_flutter/webview_flutter_android/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter_android/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.13.0 + +* Adds new method for accessing a native `WebView` from a `FlutterPluginBinding`. + ## 4.12.2 * Bumps the androidx group across 10 directories with 1 update. diff --git a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterAndroidExternalApi.java b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterAndroidExternalApi.java index 58d2da240e1d..8096869d9bcf 100644 --- a/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterAndroidExternalApi.java +++ b/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebViewFlutterAndroidExternalApi.java @@ -8,6 +8,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import io.flutter.embedding.engine.FlutterEngine; +import io.flutter.embedding.engine.plugins.FlutterPlugin; /** * App and package facing native API provided by the `webview_flutter_android` plugin. @@ -27,6 +28,27 @@ public interface WebViewFlutterAndroidExternalApi { *
See the Dart method `AndroidWebViewController.webViewIdentifier` to get the identifier of an * underlying `WebView`. * + * @param binding the plugin binding provided by the Flutter engine. If the binding doesn't + * contain an attached instance of {@link WebViewFlutterPlugin}, this method returns null. + * @param identifier the associated identifier of the `WebView`. + * @return the `WebView` associated with `identifier` or null if a `WebView` instance associated + * with `identifier` could not be found. + */ + @Nullable + static WebView getWebView(@NonNull FlutterPlugin.FlutterPluginBinding binding, long identifier) { + final WebViewFlutterPlugin webViewPlugin = + (WebViewFlutterPlugin) binding.getPlugin(WebViewFlutterPlugin.class); + + return getWebViewFromPlugin(webViewPlugin, identifier); + } + + /** + * Retrieves the {@link WebView} that is associated with `identifier`. + * + *
See the Dart method `AndroidWebViewController.webViewIdentifier` to get the identifier of an
+ * underlying `WebView`.
+ *
+ * @deprecated Use {@link #getWebView(FlutterPlugin.FlutterPluginBinding, long)} instead.
* @param engine the execution environment the {@link WebViewFlutterPlugin} should belong to. If
* the engine doesn't contain an attached instance of {@link WebViewFlutterPlugin}, this
* method returns null.
@@ -34,11 +56,16 @@ public interface WebViewFlutterAndroidExternalApi {
* @return the `WebView` associated with `identifier` or null if a `WebView` instance associated
* with `identifier` could not be found.
*/
+ @Deprecated
@Nullable
static WebView getWebView(@NonNull FlutterEngine engine, long identifier) {
final WebViewFlutterPlugin webViewPlugin =
(WebViewFlutterPlugin) engine.getPlugins().get(WebViewFlutterPlugin.class);
+ return getWebViewFromPlugin(webViewPlugin, identifier);
+ }
+
+ private static WebView getWebViewFromPlugin(WebViewFlutterPlugin webViewPlugin, long identifier) {
if (webViewPlugin != null && webViewPlugin.getInstanceManager() != null) {
final Object instance = webViewPlugin.getInstanceManager().getInstance(identifier);
if (instance instanceof WebView) {
diff --git a/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewFlutterAndroidExternalApiTest.java b/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewFlutterAndroidExternalApiTest.java
index f6d244f15da6..be91bac91618 100644
--- a/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewFlutterAndroidExternalApiTest.java
+++ b/packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewFlutterAndroidExternalApiTest.java
@@ -6,6 +6,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -33,6 +34,7 @@ public class WebViewFlutterAndroidExternalApiTest {
@Mock FlutterPlugin.FlutterPluginBinding mockPluginBinding;
+ @SuppressWarnings("deprecation")
@Test
public void getWebView() {
final WebViewFlutterPlugin webViewFlutterPlugin = new WebViewFlutterPlugin();
@@ -60,4 +62,34 @@ public void getWebView() {
webViewFlutterPlugin.onDetachedFromEngine(mockPluginBinding);
}
+
+ @Test
+ public void getWebViewFromBinding() {
+ final WebViewFlutterPlugin webViewFlutterPlugin = new WebViewFlutterPlugin();
+
+ when(mockPluginBinding.getApplicationContext()).thenReturn(mockContext);
+ when(mockPluginBinding.getPlatformViewRegistry()).thenReturn(mockViewRegistry);
+ when(mockPluginBinding.getBinaryMessenger()).thenReturn(mockBinaryMessenger);
+
+ webViewFlutterPlugin.onAttachedToEngine(mockPluginBinding);
+
+ final AndroidWebkitLibraryPigeonInstanceManager instanceManager =
+ webViewFlutterPlugin.getInstanceManager();
+ assertNotNull(instanceManager);
+
+ final WebView mockWebView = mock(WebView.class);
+ instanceManager.addDartCreatedInstance(mockWebView, 0);
+
+ when(mockPluginBinding.getPlugin(WebViewFlutterPlugin.class)).thenReturn(webViewFlutterPlugin);
+
+ assertEquals(WebViewFlutterAndroidExternalApi.getWebView(mockPluginBinding, 0), mockWebView);
+
+ webViewFlutterPlugin.onDetachedFromEngine(mockPluginBinding);
+ }
+
+ @Test
+ public void getWebViewFromBindingReturnsNullForUnknownPlugin() {
+ when(mockPluginBinding.getPlugin(WebViewFlutterPlugin.class)).thenReturn(null);
+ assertNull(WebViewFlutterAndroidExternalApi.getWebView(mockPluginBinding, 0));
+ }
}
diff --git a/packages/webview_flutter/webview_flutter_android/pubspec.yaml b/packages/webview_flutter/webview_flutter_android/pubspec.yaml
index 73b171853ece..0aa602bf8976 100644
--- a/packages/webview_flutter/webview_flutter_android/pubspec.yaml
+++ b/packages/webview_flutter/webview_flutter_android/pubspec.yaml
@@ -2,7 +2,7 @@ name: webview_flutter_android
description: A Flutter plugin that provides a WebView widget on Android.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
-version: 4.12.2
+version: 4.13.0
environment:
sdk: ^3.12.0
diff --git a/packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md b/packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md
index 168c08e8cbed..f5a9fd6ef336 100644
--- a/packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md
+++ b/packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 3.26.0
+
+* Adds new method for accessing a native `WKWebView` from a `FlutterPluginRegistrar`.
+* Updates minimum supported SDK version to Flutter 3.44/Dart 3.12.
+
## 3.25.1
* Relands update to prevent message calls when application will terminate.
diff --git a/packages/webview_flutter/webview_flutter_wkwebview/darwin/Tests/FWFWebViewFlutterWKWebViewExternalAPITests.swift b/packages/webview_flutter/webview_flutter_wkwebview/darwin/Tests/FWFWebViewFlutterWKWebViewExternalAPITests.swift
index 0e28185185cd..ab09029b345a 100644
--- a/packages/webview_flutter/webview_flutter_wkwebview/darwin/Tests/FWFWebViewFlutterWKWebViewExternalAPITests.swift
+++ b/packages/webview_flutter/webview_flutter_wkwebview/darwin/Tests/FWFWebViewFlutterWKWebViewExternalAPITests.swift
@@ -27,11 +27,11 @@ class FWFWebViewFlutterWKWebViewExternalAPITests: XCTestCase {
WebViewFlutterPlugin.register(with: registrar)
- let plugin = registry.registrar.plugin as! WebViewFlutterPlugin?
+ let plugin = registry.registrar.publishedValue as! WebViewFlutterPlugin
let webView = WKWebView(frame: .zero)
let webViewIdentifier = 0
- plugin?.proxyApiRegistrar?.instanceManager.addDartCreatedInstance(
+ plugin.proxyApiRegistrar?.instanceManager.addDartCreatedInstance(
webView, withIdentifier: Int64(webViewIdentifier))
let result = FWFWebViewFlutterWKWebViewExternalAPI.webView(
@@ -40,26 +40,54 @@ class FWFWebViewFlutterWKWebViewExternalAPITests: XCTestCase {
}
@MainActor func testWebViewForIdentifierHandlesIncorrectRegistry() {
- let registry = TestRegistry(publishedValue: false)
+ let registry = TestRegistry()
// Ensure that passing an empty registry, such as the FlutterAppDelegate
// in an app that has adopted UIScene, gracefully returns nil.
let result = FWFWebViewFlutterWKWebViewExternalAPI.webView(
forIdentifier: 0, withPluginRegistry: registry)
XCTAssertEqual(result, nil)
}
+
+ // FlutterPluginRegistrar.valuePublished(byPlugin:) is not available on macOS. This
+ // can be removed once this method becomes available.
+ // See https://github.com/flutter/flutter/issues/186911.
+ #if os(iOS)
+ @MainActor func testWebViewForIdentifierFromRegistrar() {
+ let registry = TestRegistry()
+
+ #if os(iOS)
+ let registrar = registry.registrar(forPlugin: "")!
+ #elseif os(macOS)
+ let registrar = registry.registrar(forPlugin: "")
+ #endif
+
+ WebViewFlutterPlugin.register(with: registrar)
+
+ let plugin = registry.registrar.publishedValue as! WebViewFlutterPlugin
+
+ let webView = WKWebView(frame: .zero)
+ let webViewIdentifier = 0
+ plugin.proxyApiRegistrar?.instanceManager.addDartCreatedInstance(
+ webView, withIdentifier: Int64(webViewIdentifier))
+
+ let result = FWFWebViewFlutterWKWebViewExternalAPI.webView(
+ forIdentifier: Int64(webViewIdentifier), withPluginRegistrar: registrar)
+ XCTAssertEqual(result, webView)
+ }
+
+ @MainActor func testWebViewForIdentifierHandlesIncorrectRegistrar() {
+ let registrar = TestFlutterPluginRegistrar()
+ // Ensure that passing an empty registry, such as the FlutterAppDelegate
+ // in an app that has adopted UIScene, gracefully returns nil.
+ let result = FWFWebViewFlutterWKWebViewExternalAPI.webView(
+ forIdentifier: 0, withPluginRegistrar: registrar)
+ XCTAssertEqual(result, nil)
+ }
+ #endif
}
class TestRegistry: NSObject, FlutterPluginRegistry {
let registrar = TestFlutterPluginRegistrar()
- let publishedValue: Bool
-
- init(publishedValue: Bool) {
- self.publishedValue = publishedValue
- }
-
- convenience override init() {
- self.init(publishedValue: true)
- }
#if os(iOS)
func registrar(forPlugin pluginKey: String) -> FlutterPluginRegistrar? {
@@ -76,10 +104,7 @@ class TestRegistry: NSObject, FlutterPluginRegistry {
}
func valuePublished(byPlugin pluginKey: String) -> NSObject? {
- if publishedValue && pluginKey == "WebViewFlutterPlugin" {
- return registrar.plugin
- }
- return nil
+ return registrar.publishedValue
}
}
@@ -98,7 +123,7 @@ class TestFlutterTextureRegistry: NSObject, FlutterTextureRegistry {
}
class TestFlutterPluginRegistrar: NSObject, FlutterPluginRegistrar {
- var plugin: WebViewFlutterPlugin? = nil
+ var publishedValue: NSObject? = nil
#if os(iOS)
var viewController: UIViewController?
@@ -144,7 +169,7 @@ class TestFlutterPluginRegistrar: NSObject, FlutterPluginRegistrar {
}
func publish(_ value: NSObject) {
- plugin = (value as! WebViewFlutterPlugin)
+ publishedValue = value
}
func addMethodCallDelegate(_ delegate: FlutterPlugin, channel: FlutterMethodChannel) {
@@ -159,7 +184,10 @@ class TestFlutterPluginRegistrar: NSObject, FlutterPluginRegistrar {
return ""
}
- func valuePublished(byPlugin: String) -> NSObject? {
+ func valuePublished(byPlugin pluginKey: String) -> NSObject? {
+ if pluginKey == "WebViewFlutterPlugin" {
+ return publishedValue
+ }
return nil
}
}
diff --git a/packages/webview_flutter/webview_flutter_wkwebview/darwin/webview_flutter_wkwebview/Sources/webview_flutter_wkwebview/WebViewFlutterWKWebViewExternalAPI.swift b/packages/webview_flutter/webview_flutter_wkwebview/darwin/webview_flutter_wkwebview/Sources/webview_flutter_wkwebview/WebViewFlutterWKWebViewExternalAPI.swift
index 8e81f6027311..b0c4c3275d67 100644
--- a/packages/webview_flutter/webview_flutter_wkwebview/darwin/webview_flutter_wkwebview/Sources/webview_flutter_wkwebview/WebViewFlutterWKWebViewExternalAPI.swift
+++ b/packages/webview_flutter/webview_flutter_wkwebview/darwin/webview_flutter_wkwebview/Sources/webview_flutter_wkwebview/WebViewFlutterWKWebViewExternalAPI.swift
@@ -24,6 +24,11 @@ public class FWFWebViewFlutterWKWebViewExternalAPI: NSObject {
///
/// See the Dart method `WebKitWebViewController.webViewIdentifier` to get the identifier of an
/// underlying `WKWebView`.
+ #if os(iOS)
+ // Only deprecate this method on iOS until FlutterPluginRegistrar.valuePublished(byPlugin:) is
+ // available on macOS. See https://github.com/flutter/flutter/issues/186911.
+ @available(*, deprecated, message: "Use webView(forIdentifier:withPluginRegistrar:) instead.")
+ #endif
@objc(webViewForIdentifier:withPluginRegistry:)
public static func webView(
forIdentifier identifier: Int64, withPluginRegistry registry: FlutterPluginRegistry
@@ -33,7 +38,33 @@ public class FWFWebViewFlutterWKWebViewExternalAPI: NSObject {
return nil
}
- let webView: WKWebView? = webviewPlugin.proxyApiRegistrar?.instanceManager.instance(
+ return webview(forIdentifier: identifier, withPlugin: webviewPlugin)
+ }
+
+ // This method is only available on iOS until FlutterPluginRegistrar.valuePublished(byPlugin:) is
+ // available on macOS. See https://github.com/flutter/flutter/issues/186911.
+ #if os(iOS)
+ /// Retrieves the `WKWebView` that is associated with `identifier` using a FlutterPluginRegistrar
+ ///
+ /// See the Dart method `WebKitWebViewController.webViewIdentifier` to get the identifier of an
+ /// underlying `WKWebView`.
+ @objc(webViewForIdentifier:withPluginRegistrar:)
+ public static func webView(
+ forIdentifier identifier: Int64, withPluginRegistrar registrar: FlutterPluginRegistrar
+ ) -> WKWebView? {
+ let plugin = registrar.valuePublished(byPlugin: "WebViewFlutterPlugin")
+ guard let webviewPlugin = plugin as? WebViewFlutterPlugin else {
+ return nil
+ }
+
+ return webview(forIdentifier: identifier, withPlugin: webviewPlugin)
+ }
+ #endif
+
+ private static func webview(forIdentifier identifier: Int64, withPlugin: WebViewFlutterPlugin)
+ -> WKWebView?
+ {
+ let webView: WKWebView? = withPlugin.proxyApiRegistrar?.instanceManager.instance(
forIdentifier: identifier)
return webView
}
diff --git a/packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_ssl_auth_error.dart b/packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_ssl_auth_error.dart
index a46e96dd3fd3..685f37eef18d 100644
--- a/packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_ssl_auth_error.dart
+++ b/packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_ssl_auth_error.dart
@@ -14,16 +14,11 @@ class WebKitSslAuthError extends PlatformSslAuthError {
WebKitSslAuthError({
required super.certificate,
required super.description,
- required SecTrust trust,
+ required this._trust,
required this.host,
required this.port,
- required Future