-
Notifications
You must be signed in to change notification settings - Fork 937
Expand file tree
/
Copy pathbase.dart
More file actions
378 lines (325 loc) · 12.4 KB
/
base.dart
File metadata and controls
378 lines (325 loc) · 12.4 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_webview_plugin/src/javascript_channel.dart';
import 'javascript_message.dart';
const _kChannel = 'flutter_webview_plugin';
// TODO: more general state for iOS/android
enum WebViewState { shouldStart, startLoad, finishLoad, abortLoad }
// TODO: use an id by webview to be able to manage multiple webview
/// Singleton class that communicate with a Webview Instance
class FlutterWebviewPlugin {
factory FlutterWebviewPlugin() {
if (_instance == null) {
const MethodChannel methodChannel = const MethodChannel(_kChannel);
_instance = FlutterWebviewPlugin.private(methodChannel);
}
return _instance!;
}
@visibleForTesting
FlutterWebviewPlugin.private(this._channel) {
_channel.setMethodCallHandler(_handleMessages);
}
static FlutterWebviewPlugin? _instance;
final MethodChannel _channel;
final _onBack = StreamController<Null>.broadcast();
final _onDestroy = StreamController<Null>.broadcast();
final _onUrlChanged = StreamController<String>.broadcast();
final _onStateChanged = StreamController<WebViewStateChanged>.broadcast();
final _onScrollXChanged = StreamController<double>.broadcast();
final _onScrollYChanged = StreamController<double>.broadcast();
final _onProgressChanged = new StreamController<double>.broadcast();
final _onHttpError = StreamController<WebViewHttpError>.broadcast();
final _onPostMessage = StreamController<JavascriptMessage>.broadcast();
final Map<String, JavascriptChannel> _javascriptChannels =
<String, JavascriptChannel>{};
Future<Null> _handleMessages(MethodCall call) async {
switch (call.method) {
case 'onBack':
_onBack.add(null);
break;
case 'onDestroy':
_onDestroy.add(null);
break;
case 'onUrlChanged':
_onUrlChanged.add(call.arguments['url']);
break;
case 'onScrollXChanged':
_onScrollXChanged.add(call.arguments['xDirection']);
break;
case 'onScrollYChanged':
_onScrollYChanged.add(call.arguments['yDirection']);
break;
case 'onProgressChanged':
_onProgressChanged.add(call.arguments['progress']);
break;
case 'onState':
_onStateChanged.add(
WebViewStateChanged.fromMap(
Map<String, dynamic>.from(call.arguments),
),
);
break;
case 'onHttpError':
_onHttpError.add(
WebViewHttpError(call.arguments['code'], call.arguments['url']));
break;
case 'javascriptChannelMessage':
_handleJavascriptChannelMessage(
call.arguments['channel'], call.arguments['message']);
break;
}
}
/// Listening the OnDestroy LifeCycle Event for Android
Stream<Null> get onDestroy => _onDestroy.stream;
/// Listening the back key press Event for Android
Stream<Null> get onBack => _onBack.stream;
/// Listening url changed
Stream<String> get onUrlChanged => _onUrlChanged.stream;
/// Listening the onState Event for iOS WebView and Android
/// content is Map for type: {shouldStart(iOS)|startLoad|finishLoad}
/// more detail than other events
Stream<WebViewStateChanged> get onStateChanged => _onStateChanged.stream;
/// Listening web view loading progress estimation, value between 0.0 and 1.0
Stream<double> get onProgressChanged => _onProgressChanged.stream;
/// Listening web view y position scroll change
Stream<double> get onScrollYChanged => _onScrollYChanged.stream;
/// Listening web view x position scroll change
Stream<double> get onScrollXChanged => _onScrollXChanged.stream;
Stream<WebViewHttpError> get onHttpError => _onHttpError.stream;
/// Start the Webview with [url]
/// - [headers] specify additional HTTP headers
/// - [withJavascript] enable Javascript or not for the Webview
/// - [clearCache] clear the cache of the Webview
/// - [clearCookies] clear all cookies of the Webview
/// - [hidden] not show
/// - [rect]: show in rect, fullscreen if null
/// - [enableAppScheme]: false will enable all schemes, true only for httt/https/about
/// android: Not implemented yet
/// - [userAgent]: set the User-Agent of WebView
/// - [withZoom]: enable zoom on webview
/// - [withLocalStorage] enable localStorage API on Webview
/// Currently Android only.
/// It is always enabled in UIWebView of iOS and can not be disabled.
/// - [withLocalUrl]: allow url as a local path
/// Allow local files on iOs > 9.0
/// - [localUrlScope]: allowed folder for local paths
/// iOS only.
/// If null and withLocalUrl is true, then it will use the url as the scope,
/// allowing only itself to be read.
/// - [scrollBar]: enable or disable scrollbar
/// - [supportMultipleWindows] enable multiple windows support in Android
/// - [invalidUrlRegex] is the regular expression of URLs that web view shouldn't load.
/// For example, when webview is redirected to a specific URL, you want to intercept
/// this process by stopping loading this URL and replacing webview by another screen.
/// Android only settings:
/// - [displayZoomControls]: display zoom controls on webview
/// - [withOverviewMode]: enable overview mode for Android webview ( setLoadWithOverviewMode )
/// - [useWideViewPort]: use wide viewport for Android webview ( setUseWideViewPort )
/// - [ignoreSSLErrors]: use to bypass Android/iOS SSL checks e.g. for self-signed certificates
Future<void> launch(
String url, {
Map<String, String>? headers,
Set<JavascriptChannel> javascriptChannels = const <JavascriptChannel>{},
bool withJavascript = true,
bool clearCache = false,
bool clearCookies = false,
bool mediaPlaybackRequiresUserGesture = true,
bool hidden = false,
bool enableAppScheme = true,
Rect? rect,
String? userAgent,
bool withZoom = false,
bool displayZoomControls = false,
bool withLocalStorage = true,
bool withLocalUrl = false,
String? localUrlScope,
bool withOverviewMode = false,
bool scrollBar = true,
bool supportMultipleWindows = false,
bool appCacheEnabled = false,
bool allowFileURLs = false,
bool useWideViewPort = false,
String? invalidUrlRegex,
bool geolocationEnabled = false,
bool debuggingEnabled = false,
bool ignoreSSLErrors = false,
}) async {
final args = <String, dynamic>{
'url': url,
'withJavascript': withJavascript,
'clearCache': clearCache,
'hidden': hidden,
'clearCookies': clearCookies,
'mediaPlaybackRequiresUserGesture': mediaPlaybackRequiresUserGesture,
'enableAppScheme': enableAppScheme,
'userAgent': userAgent,
'withZoom': withZoom,
'displayZoomControls': displayZoomControls,
'withLocalStorage': withLocalStorage,
'withLocalUrl': withLocalUrl,
'localUrlScope': localUrlScope,
'scrollBar': scrollBar,
'supportMultipleWindows': supportMultipleWindows,
'appCacheEnabled': appCacheEnabled,
'allowFileURLs': allowFileURLs,
'useWideViewPort': useWideViewPort,
'invalidUrlRegex': invalidUrlRegex,
'geolocationEnabled': geolocationEnabled,
'withOverviewMode': withOverviewMode,
'debuggingEnabled': debuggingEnabled,
'ignoreSSLErrors': ignoreSSLErrors,
};
if (headers != null) {
args['headers'] = headers;
}
_assertJavascriptChannelNamesAreUnique(javascriptChannels);
javascriptChannels.forEach((channel) {
_javascriptChannels[channel.name] = channel;
});
args['javascriptChannelNames'] =
_extractJavascriptChannelNames(javascriptChannels).toList();
if (rect != null) {
args['rect'] = {
'left': rect.left,
'top': rect.top,
'width': rect.width,
'height': rect.height,
};
}
await _channel.invokeMethod('launch', args);
}
/// Execute Javascript inside webview
Future<String?> evalJavascript(String code) async {
final res = await _channel.invokeMethod<String>('eval', {'code': code});
return res;
}
/// Close the Webview
/// Will trigger the [onDestroy] event
Future<void> close() async {
_javascriptChannels.clear();
await _channel.invokeMethod('close');
}
/// Reloads the WebView.
Future<void> reload() async => await _channel.invokeMethod('reload');
/// Navigates back on the Webview.
Future<void> goBack() async => await _channel.invokeMethod('back');
/// Checks if webview can navigate back
Future<bool> canGoBack() async => await _channel.invokeMethod('canGoBack');
/// Checks if webview can navigate back
Future<bool> canGoForward() async =>
await _channel.invokeMethod('canGoForward');
/// Navigates forward on the Webview.
Future<void> goForward() async => await _channel.invokeMethod('forward');
// Hides the webview
Future<void> hide() async => await _channel.invokeMethod('hide');
// Shows the webview
Future<void> show() async => await _channel.invokeMethod('show');
// Clears browser cache
Future<void> clearCache() async => await _channel.invokeMethod('cleanCache');
// Reload webview with a url
Future<void> reloadUrl(String url, {Map<String, String>? headers}) async {
final args = <String, dynamic>{'url': url};
if (headers != null) {
args['headers'] = headers;
}
await _channel.invokeMethod('reloadUrl', args);
}
// Clean cookies on WebView
Future<void> cleanCookies() async {
// one liner to clear javascript cookies
await evalJavascript(
'document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });');
return await _channel.invokeMethod('cleanCookies');
}
// Stops current loading process
Future<void> stopLoading() async =>
await _channel.invokeMethod('stopLoading');
/// Close all Streams
void dispose() {
_onDestroy.close();
_onUrlChanged.close();
_onStateChanged.close();
_onProgressChanged.close();
_onScrollXChanged.close();
_onScrollYChanged.close();
_onHttpError.close();
_onPostMessage.close();
_instance = null;
}
Future<Map<String, String>> getCookies() async {
final cookiesString = await evalJavascript('document.cookie');
final cookies = <String, String>{};
if (cookiesString?.isNotEmpty == true) {
cookiesString!.split('; ').forEach((String cookie) {
final split = cookie.split('=');
cookies[split[0]] = split[1];
});
}
return cookies;
}
/// resize webview
Future<void> resize(Rect rect) async {
final args = {};
args['rect'] = {
'left': rect.left,
'top': rect.top,
'width': rect.width,
'height': rect.height,
};
await _channel.invokeMethod('resize', args);
}
Set<String> _extractJavascriptChannelNames(Set<JavascriptChannel> channels) {
final Set<String> channelNames =
channels.map((JavascriptChannel channel) => channel.name).toSet();
return channelNames;
}
void _handleJavascriptChannelMessage(
final String channelName, final String message) {
if (_javascriptChannels.containsKey(channelName))
_javascriptChannels[channelName]!
.onMessageReceived(JavascriptMessage(message));
else
print('Channel "$channelName" is not exstis');
}
void _assertJavascriptChannelNamesAreUnique(
final Set<JavascriptChannel>? channels) {
if (channels == null || channels.isEmpty) {
return;
}
assert(_extractJavascriptChannelNames(channels).length == channels.length);
}
}
class WebViewStateChanged {
WebViewStateChanged(this.type, this.url, this.navigationType);
factory WebViewStateChanged.fromMap(Map<String, dynamic> map) {
WebViewState t;
switch (map['type']) {
case 'shouldStart':
t = WebViewState.shouldStart;
break;
case 'startLoad':
t = WebViewState.startLoad;
break;
case 'finishLoad':
t = WebViewState.finishLoad;
break;
case 'abortLoad':
t = WebViewState.abortLoad;
break;
default:
throw UnimplementedError(
'WebViewState type "${map['type']}" is not supported.');
}
return WebViewStateChanged(t, map['url'], map['navigationType']);
}
final WebViewState type;
final String url;
final int navigationType;
}
class WebViewHttpError {
WebViewHttpError(this.code, this.url);
final String url;
final String code;
}