|
| 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/services.dart'; |
| 8 | + |
| 9 | +import '../platform_interface/javascript_channel_registry.dart'; |
| 10 | +import '../platform_interface/platform_interface.dart'; |
| 11 | +import '../types/types.dart'; |
| 12 | + |
| 13 | +/// A [WebViewPlatformController] that uses a method channel to control the webview. |
| 14 | +class MethodChannelWebViewPlatform implements WebViewPlatformController { |
| 15 | + /// Constructs an instance that will listen for webviews broadcasting to the |
| 16 | + /// given [id], using the given [WebViewPlatformCallbacksHandler]. |
| 17 | + MethodChannelWebViewPlatform( |
| 18 | + int id, |
| 19 | + this._platformCallbacksHandler, |
| 20 | + this._javascriptChannelRegistry, |
| 21 | + ) : assert(_platformCallbacksHandler != null), |
| 22 | + _channel = MethodChannel('plugins.flutter.io/webview_$id') { |
| 23 | + _channel.setMethodCallHandler(_onMethodCall); |
| 24 | + } |
| 25 | + |
| 26 | + final JavascriptChannelRegistry _javascriptChannelRegistry; |
| 27 | + |
| 28 | + final WebViewPlatformCallbacksHandler _platformCallbacksHandler; |
| 29 | + |
| 30 | + final MethodChannel _channel; |
| 31 | + |
| 32 | + static const MethodChannel _cookieManagerChannel = |
| 33 | + MethodChannel('plugins.flutter.io/cookie_manager'); |
| 34 | + |
| 35 | + Future<bool?> _onMethodCall(MethodCall call) async { |
| 36 | + switch (call.method) { |
| 37 | + case 'javascriptChannelMessage': |
| 38 | + final String channel = call.arguments['channel']!; |
| 39 | + final String message = call.arguments['message']!; |
| 40 | + _javascriptChannelRegistry.onJavascriptChannelMessage(channel, message); |
| 41 | + return true; |
| 42 | + case 'navigationRequest': |
| 43 | + return await _platformCallbacksHandler.onNavigationRequest( |
| 44 | + url: call.arguments['url']!, |
| 45 | + isForMainFrame: call.arguments['isForMainFrame']!, |
| 46 | + ); |
| 47 | + case 'onPageFinished': |
| 48 | + _platformCallbacksHandler.onPageFinished(call.arguments['url']!); |
| 49 | + return null; |
| 50 | + case 'onProgress': |
| 51 | + _platformCallbacksHandler.onProgress(call.arguments['progress']); |
| 52 | + return null; |
| 53 | + case 'onPageStarted': |
| 54 | + _platformCallbacksHandler.onPageStarted(call.arguments['url']!); |
| 55 | + return null; |
| 56 | + case 'onWebResourceError': |
| 57 | + _platformCallbacksHandler.onWebResourceError( |
| 58 | + WebResourceError( |
| 59 | + errorCode: call.arguments['errorCode']!, |
| 60 | + description: call.arguments['description']!, |
| 61 | + // iOS doesn't support `failingUrl`. |
| 62 | + failingUrl: call.arguments['failingUrl'], |
| 63 | + domain: call.arguments['domain'], |
| 64 | + errorType: call.arguments['errorType'] == null |
| 65 | + ? null |
| 66 | + : WebResourceErrorType.values.firstWhere( |
| 67 | + (WebResourceErrorType type) { |
| 68 | + return type.toString() == |
| 69 | + '$WebResourceErrorType.${call.arguments['errorType']}'; |
| 70 | + }, |
| 71 | + ), |
| 72 | + ), |
| 73 | + ); |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + throw MissingPluginException( |
| 78 | + '${call.method} was invoked but has no handler', |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + Future<void> loadUrl( |
| 84 | + String url, |
| 85 | + Map<String, String>? headers, |
| 86 | + ) async { |
| 87 | + assert(url != null); |
| 88 | + return _channel.invokeMethod<void>('loadUrl', <String, dynamic>{ |
| 89 | + 'url': url, |
| 90 | + 'headers': headers, |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @override |
| 95 | + Future<String?> currentUrl() => _channel.invokeMethod<String>('currentUrl'); |
| 96 | + |
| 97 | + @override |
| 98 | + Future<bool> canGoBack() => |
| 99 | + _channel.invokeMethod<bool>("canGoBack").then((result) => result!); |
| 100 | + |
| 101 | + @override |
| 102 | + Future<bool> canGoForward() => |
| 103 | + _channel.invokeMethod<bool>("canGoForward").then((result) => result!); |
| 104 | + |
| 105 | + @override |
| 106 | + Future<void> goBack() => _channel.invokeMethod<void>("goBack"); |
| 107 | + |
| 108 | + @override |
| 109 | + Future<void> goForward() => _channel.invokeMethod<void>("goForward"); |
| 110 | + |
| 111 | + @override |
| 112 | + Future<void> reload() => _channel.invokeMethod<void>("reload"); |
| 113 | + |
| 114 | + @override |
| 115 | + Future<void> clearCache() => _channel.invokeMethod<void>("clearCache"); |
| 116 | + |
| 117 | + @override |
| 118 | + Future<void> updateSettings(WebSettings settings) async { |
| 119 | + final Map<String, dynamic> updatesMap = _webSettingsToMap(settings); |
| 120 | + if (updatesMap.isNotEmpty) { |
| 121 | + await _channel.invokeMethod<void>('updateSettings', updatesMap); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @override |
| 126 | + Future<String> evaluateJavascript(String javascriptString) { |
| 127 | + return _channel |
| 128 | + .invokeMethod<String>('evaluateJavascript', javascriptString) |
| 129 | + .then((result) => result!); |
| 130 | + } |
| 131 | + |
| 132 | + @override |
| 133 | + Future<void> addJavascriptChannels(Set<String> javascriptChannelNames) { |
| 134 | + return _channel.invokeMethod<void>( |
| 135 | + 'addJavascriptChannels', javascriptChannelNames.toList()); |
| 136 | + } |
| 137 | + |
| 138 | + @override |
| 139 | + Future<void> removeJavascriptChannels(Set<String> javascriptChannelNames) { |
| 140 | + return _channel.invokeMethod<void>( |
| 141 | + 'removeJavascriptChannels', javascriptChannelNames.toList()); |
| 142 | + } |
| 143 | + |
| 144 | + @override |
| 145 | + Future<String?> getTitle() => _channel.invokeMethod<String>("getTitle"); |
| 146 | + |
| 147 | + @override |
| 148 | + Future<void> scrollTo(int x, int y) { |
| 149 | + return _channel.invokeMethod<void>('scrollTo', <String, int>{ |
| 150 | + 'x': x, |
| 151 | + 'y': y, |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + @override |
| 156 | + Future<void> scrollBy(int x, int y) { |
| 157 | + return _channel.invokeMethod<void>('scrollBy', <String, int>{ |
| 158 | + 'x': x, |
| 159 | + 'y': y, |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + @override |
| 164 | + Future<int> getScrollX() => |
| 165 | + _channel.invokeMethod<int>("getScrollX").then((result) => result!); |
| 166 | + |
| 167 | + @override |
| 168 | + Future<int> getScrollY() => |
| 169 | + _channel.invokeMethod<int>("getScrollY").then((result) => result!); |
| 170 | + |
| 171 | + /// Method channel implementation for [WebViewPlatform.clearCookies]. |
| 172 | + static Future<bool> clearCookies() { |
| 173 | + return _cookieManagerChannel |
| 174 | + .invokeMethod<bool>('clearCookies') |
| 175 | + .then<bool>((dynamic result) => result!); |
| 176 | + } |
| 177 | + |
| 178 | + static Map<String, dynamic> _webSettingsToMap(WebSettings? settings) { |
| 179 | + final Map<String, dynamic> map = <String, dynamic>{}; |
| 180 | + void _addIfNonNull(String key, dynamic value) { |
| 181 | + if (value == null) { |
| 182 | + return; |
| 183 | + } |
| 184 | + map[key] = value; |
| 185 | + } |
| 186 | + |
| 187 | + void _addSettingIfPresent<T>(String key, WebSetting<T> setting) { |
| 188 | + if (!setting.isPresent) { |
| 189 | + return; |
| 190 | + } |
| 191 | + map[key] = setting.value; |
| 192 | + } |
| 193 | + |
| 194 | + _addIfNonNull('jsMode', settings!.javascriptMode?.index); |
| 195 | + _addIfNonNull('hasNavigationDelegate', settings.hasNavigationDelegate); |
| 196 | + _addIfNonNull('hasProgressTracking', settings.hasProgressTracking); |
| 197 | + _addIfNonNull('debuggingEnabled', settings.debuggingEnabled); |
| 198 | + _addIfNonNull( |
| 199 | + 'gestureNavigationEnabled', settings.gestureNavigationEnabled); |
| 200 | + _addIfNonNull( |
| 201 | + 'allowsInlineMediaPlayback', settings.allowsInlineMediaPlayback); |
| 202 | + _addSettingIfPresent('userAgent', settings.userAgent); |
| 203 | + return map; |
| 204 | + } |
| 205 | + |
| 206 | + /// Converts a [CreationParams] object to a map as expected by `platform_views` channel. |
| 207 | + /// |
| 208 | + /// This is used for the `creationParams` argument of the platform views created by |
| 209 | + /// [AndroidWebViewBuilder] and [CupertinoWebViewBuilder]. |
| 210 | + static Map<String, dynamic> creationParamsToMap( |
| 211 | + CreationParams creationParams, { |
| 212 | + bool usesHybridComposition = false, |
| 213 | + }) { |
| 214 | + return <String, dynamic>{ |
| 215 | + 'initialUrl': creationParams.initialUrl, |
| 216 | + 'settings': _webSettingsToMap(creationParams.webSettings), |
| 217 | + 'javascriptChannelNames': creationParams.javascriptChannelNames.toList(), |
| 218 | + 'userAgent': creationParams.userAgent, |
| 219 | + 'autoMediaPlaybackPolicy': creationParams.autoMediaPlaybackPolicy.index, |
| 220 | + 'usesHybridComposition': usesHybridComposition, |
| 221 | + }; |
| 222 | + } |
| 223 | +} |
0 commit comments