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