-
Notifications
You must be signed in to change notification settings - Fork 9
/
browser_controller.dart
276 lines (242 loc) · 7.44 KB
/
browser_controller.dart
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
// Copyright 2020-2023 Gohilla.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../web_browser.dart';
/// Controls [Browser].
class BrowserController extends ChangeNotifier {
static bool get _isAndroid => !kIsWeb && Platform.isAndroid;
final _onPageStarted = StreamController<String>.broadcast();
final _onPageFinished = StreamController<String>.broadcast();
bool _isLoading = false;
String? _userAgent;
String _uriString = '';
Uri? _uri;
bool _canGoBack = true;
bool _canGoForward = true;
final WebViewController _webViewController;
/// Optional delegate for listening to navigation events.
@protected
final NavigationDelegate? webViewNavigationDelegate;
WebResourceError? _error;
BrowserPolicy? policy;
bool _isZoomEnabled = true;
BrowserController({
String uriString = '',
String? userAgent,
WebViewController? webViewController,
this.webViewNavigationDelegate,
}) : _uriString = uriString,
_userAgent = userAgent,
_webViewController = webViewController ?? WebViewController() {
try {
_webViewController.setNavigationDelegate(NavigationDelegate(
onNavigationRequest: (request) async {
final result = await webViewNavigationDelegate?.onNavigationRequest
?.call(request);
if (result != null && result == NavigationDecision.prevent) {
return result;
}
final policy = this.policy;
if (policy != null && request.isMainFrame) {
final parsedUri = Uri.tryParse(request.url);
if (parsedUri == null) {
return NavigationDecision.navigate;
}
if (!policy.isUriAllowed(parsedUri)) {
return NavigationDecision.prevent;
}
}
return NavigationDecision.navigate;
},
onPageStarted: (uri) {
_uriString = uri;
_isLoading = true;
// By default, assume this was a navigation to a new page.
_canGoBack = true;
_canGoForward = false;
if (!kIsWeb) {
try {
if (_isAndroid) {
_webViewController.canGoBack().then((value) {
if (value != _canGoBack) {
_canGoBack = value;
notifyListeners();
}
}, onError: (error) {});
}
_webViewController.canGoForward().then((value) {
if (value != _canGoForward) {
_canGoForward = value;
notifyListeners();
}
}, onError: (error) {});
} catch (error) {
// Ignore error
}
}
notifyListeners();
webViewNavigationDelegate?.onPageStarted?.call(uri);
},
onProgress: (progress) {
notifyListeners();
webViewNavigationDelegate?.onProgress?.call(progress);
},
onPageFinished: (uri) {
_uriString = uri;
_isLoading = false;
notifyListeners();
webViewNavigationDelegate?.onPageFinished?.call(uri);
},
onWebResourceError: (error) {
if (error.isForMainFrame ?? true) {
_error = error;
_isLoading = false;
notifyListeners();
}
webViewNavigationDelegate?.onWebResourceError?.call(error);
},
));
} catch (error) {
// Ignore
}
try {
_webViewController.setJavaScriptMode(
JavaScriptMode.unrestricted,
);
} on UnimplementedError {
// Ignore errors in browser
}
try {
_webViewController.enableZoom(true);
} catch (error) {
// Ignore error
}
}
/// Whether [goBack] may succeed.
bool get canGoBack => !kIsWeb && _canGoBack;
/// Whether [goForward] may succeed.
bool get canGoForward => !kIsWeb && _canGoForward;
WebResourceError? get error => _error;
bool get isLoading => _isLoading;
/// Whether real navigation events are received from the browser.
bool get isNavigationEventsReceivedFromBrowser => !kIsWeb;
bool get isZoomEnabled => _isZoomEnabled;
set isZoomEnabled(bool newValue) {
if (newValue != _isZoomEnabled) {
_isZoomEnabled = newValue;
_webViewController.enableZoom(newValue);
notifyListeners();
}
}
/// Broadcast stream of page loads that finished.
///
/// The stream will be closed when [dispose] is called.
Stream<String> get onPageFinished => _onPageFinished.stream;
/// Broadcast stream of page loads that started.
///
/// The stream will be closed when [dispose] is called.
Stream<String> get onPageStarted => _onPageStarted.stream;
/// Parsed [uriString].
Uri get uri => _uri ??= Uri.parse(uriString);
/// Current URI string.
String get uriString => _uriString;
/// User agent string.
///
/// This is ignored in browsers.
String? get userAgent => _userAgent;
set userAgent(String? newValue) {
_userAgent = newValue;
try {
_webViewController.setUserAgent(newValue);
} catch (error) {
// Ignore error
}
notifyListeners();
}
/// Web view controller NOT meant to be used developers directly.
WebViewController get webViewController => _webViewController;
@override
void dispose() {
super.dispose();
_onPageStarted.close();
_onPageFinished.close();
}
/// Go back in the browser history.
bool goBack({bool fixRedirectIssues = false}) {
final result = canGoBack;
try {
_webViewController.goBack();
} catch (error) {
// Ignore error
}
_canGoBack = true;
_canGoForward = true;
notifyListeners();
return result;
}
/// Go forward in the browser history.
bool goForward() {
final result = canGoForward;
try {
_webViewController.goForward();
} catch (error) {
// Ignore error
}
_canGoBack = true;
_canGoForward = true;
notifyListeners();
return result;
}
/// Go to the specified URI.
///
/// If the URL is the same, does nothing.
void goTo(String uri) {
if (uri != uriString) {
if (kIsWeb) {
_error = null;
}
_isLoading = true;
final parsedUri = Uri.parse(uri);
_webViewController.loadRequest(parsedUri);
_uriString = uri;
_canGoBack = true;
_canGoForward = false;
_maybeFakeNavigationEvents(uri);
notifyListeners();
}
}
/// Refreshes the current page.
void refresh() {
if (kIsWeb) {
_error = null;
}
_isLoading = true;
if (kIsWeb) {
webViewController.loadRequest(uri);
} else {
webViewController.reload();
}
_maybeFakeNavigationEvents(uriString);
notifyListeners();
}
void _maybeFakeNavigationEvents(String uri) {
if (!isNavigationEventsReceivedFromBrowser) {
_onPageStarted.add(uri);
_onPageFinished.add(uri);
}
}
}