|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:flutter/foundation.dart'; |
| 8 | +import 'package:flutter/widgets.dart'; |
| 9 | +import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'; |
| 10 | + |
| 11 | +import 'web_kit/web_kit.dart'; |
| 12 | + |
| 13 | +/// A [Widget] that displays a [WKWebView]. |
| 14 | +class WebKitWebViewWidget extends StatefulWidget { |
| 15 | + /// Constructs a [WebKitWebViewWidget]. |
| 16 | + const WebKitWebViewWidget({ |
| 17 | + required this.creationParams, |
| 18 | + required this.callbacksHandler, |
| 19 | + required this.javascriptChannelRegistry, |
| 20 | + required this.onBuildWidget, |
| 21 | + this.configuration, |
| 22 | + @visibleForTesting this.webViewProxy = const WebViewProxy(), |
| 23 | + }); |
| 24 | + |
| 25 | + /// The initial parameters used to setup the WebView. |
| 26 | + final CreationParams creationParams; |
| 27 | + |
| 28 | + /// The handler of callbacks made made by [NavigationDelegate]. |
| 29 | + final WebViewPlatformCallbacksHandler callbacksHandler; |
| 30 | + |
| 31 | + /// Manager of named JavaScript channels and forwarding incoming messages on the correct channel. |
| 32 | + final JavascriptChannelRegistry javascriptChannelRegistry; |
| 33 | + |
| 34 | + /// A collection of properties used to initialize a web view. |
| 35 | + /// |
| 36 | + /// If null, a default configuration is used. |
| 37 | + final WKWebViewConfiguration? configuration; |
| 38 | + |
| 39 | + /// The handler for constructing [WKWebView]s and calling static methods. |
| 40 | + /// |
| 41 | + /// This should only be changed for testing purposes. |
| 42 | + final WebViewProxy webViewProxy; |
| 43 | + |
| 44 | + /// A callback to build a widget once [WKWebView] has been initialized. |
| 45 | + final Widget Function(WebKitWebViewPlatformController controller) |
| 46 | + onBuildWidget; |
| 47 | + |
| 48 | + @override |
| 49 | + State<StatefulWidget> createState() => _WebKitWebViewWidgetState(); |
| 50 | +} |
| 51 | + |
| 52 | +class _WebKitWebViewWidgetState extends State<WebKitWebViewWidget> { |
| 53 | + late final WebKitWebViewPlatformController controller; |
| 54 | + |
| 55 | + @override |
| 56 | + void initState() { |
| 57 | + super.initState(); |
| 58 | + controller = WebKitWebViewPlatformController( |
| 59 | + creationParams: widget.creationParams, |
| 60 | + callbacksHandler: widget.callbacksHandler, |
| 61 | + javascriptChannelRegistry: widget.javascriptChannelRegistry, |
| 62 | + configuration: widget.configuration, |
| 63 | + webViewProxy: widget.webViewProxy, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + @override |
| 68 | + Widget build(BuildContext context) { |
| 69 | + return widget.onBuildWidget(controller); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// An implementation of [WebViewPlatformController] with the WebKit api. |
| 74 | +class WebKitWebViewPlatformController extends WebViewPlatformController { |
| 75 | + /// Construct a [WebKitWebViewPlatformController]. |
| 76 | + WebKitWebViewPlatformController({ |
| 77 | + required CreationParams creationParams, |
| 78 | + required this.callbacksHandler, |
| 79 | + required this.javascriptChannelRegistry, |
| 80 | + WKWebViewConfiguration? configuration, |
| 81 | + @visibleForTesting this.webViewProxy = const WebViewProxy(), |
| 82 | + }) : super(callbacksHandler) { |
| 83 | + _setCreationParams( |
| 84 | + creationParams, |
| 85 | + configuration: configuration ?? WKWebViewConfiguration(), |
| 86 | + ).then((_) => _initializationCompleter.complete()); |
| 87 | + } |
| 88 | + |
| 89 | + final Completer<void> _initializationCompleter = Completer<void>(); |
| 90 | + |
| 91 | + /// Handles callbacks that are made by navigation. |
| 92 | + final WebViewPlatformCallbacksHandler callbacksHandler; |
| 93 | + |
| 94 | + /// Manages named JavaScript channels and forwarding incoming messages on the correct channel. |
| 95 | + final JavascriptChannelRegistry javascriptChannelRegistry; |
| 96 | + |
| 97 | + /// Handles constructing a [WKWebView]. |
| 98 | + /// |
| 99 | + /// This should only be changed when used for testing. |
| 100 | + final WebViewProxy webViewProxy; |
| 101 | + |
| 102 | + /// Represents the WebView maintained by platform code. |
| 103 | + late final WKWebView webView; |
| 104 | + |
| 105 | + Future<void> _setCreationParams( |
| 106 | + CreationParams params, { |
| 107 | + required WKWebViewConfiguration configuration, |
| 108 | + }) async { |
| 109 | + _setWebViewConfiguration( |
| 110 | + configuration, |
| 111 | + allowsInlineMediaPlayback: params.webSettings?.allowsInlineMediaPlayback, |
| 112 | + autoMediaPlaybackPolicy: params.autoMediaPlaybackPolicy, |
| 113 | + ); |
| 114 | + |
| 115 | + webView = webViewProxy.createWebView(configuration); |
| 116 | + } |
| 117 | + |
| 118 | + void _setWebViewConfiguration( |
| 119 | + WKWebViewConfiguration configuration, { |
| 120 | + required bool? allowsInlineMediaPlayback, |
| 121 | + required AutoMediaPlaybackPolicy autoMediaPlaybackPolicy, |
| 122 | + }) { |
| 123 | + if (allowsInlineMediaPlayback != null) { |
| 124 | + configuration.allowsInlineMediaPlayback = allowsInlineMediaPlayback; |
| 125 | + } |
| 126 | + |
| 127 | + late final bool requiresUserAction; |
| 128 | + switch (autoMediaPlaybackPolicy) { |
| 129 | + case AutoMediaPlaybackPolicy.require_user_action_for_all_media_types: |
| 130 | + requiresUserAction = true; |
| 131 | + break; |
| 132 | + case AutoMediaPlaybackPolicy.always_allow: |
| 133 | + requiresUserAction = false; |
| 134 | + break; |
| 135 | + } |
| 136 | + |
| 137 | + configuration.mediaTypesRequiringUserActionForPlayback = |
| 138 | + <WKAudiovisualMediaType>{ |
| 139 | + if (requiresUserAction) WKAudiovisualMediaType.all, |
| 140 | + if (!requiresUserAction) WKAudiovisualMediaType.none, |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/// Handles constructing [WKWebView]s and calling static methods. |
| 146 | +/// |
| 147 | +/// This should only be used for testing purposes. |
| 148 | +@visibleForTesting |
| 149 | +class WebViewProxy { |
| 150 | + /// Creates a [WebViewProxy]. |
| 151 | + const WebViewProxy(); |
| 152 | + |
| 153 | + /// Constructs a [WKWebView]. |
| 154 | + WKWebView createWebView(WKWebViewConfiguration configuration) { |
| 155 | + return WKWebView(configuration); |
| 156 | + } |
| 157 | +} |
0 commit comments