Merge pull request #85 from matthewlloyd/master

Add null checks around calls to InAppWebView callbacks
This commit is contained in:
Lorenzo Pichilli 2019-04-26 21:27:33 +02:00 committed by GitHub
commit b163e48c3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 40 deletions

View File

@ -750,6 +750,7 @@ class InAppWebViewController {
MethodChannel _channel; MethodChannel _channel;
Map<String, JavaScriptHandlerCallback> javaScriptHandlersMap = HashMap<String, JavaScriptHandlerCallback>(); Map<String, JavaScriptHandlerCallback> javaScriptHandlersMap = HashMap<String, JavaScriptHandlerCallback>();
bool _isOpened = false; bool _isOpened = false;
// ignore: unused_field
int _id; int _id;
String _inAppBrowserUuid; String _inAppBrowserUuid;
InAppBrowser _inAppBrowser; InAppBrowser _inAppBrowser;
@ -772,39 +773,39 @@ class InAppWebViewController {
switch(call.method) { switch(call.method) {
case "onLoadStart": case "onLoadStart":
String url = call.arguments["url"]; String url = call.arguments["url"];
if (_widget != null) if (_widget != null && _widget.onLoadStart != null)
_widget.onLoadStart(this, url); _widget.onLoadStart(this, url);
else else if (_inAppBrowser != null)
_inAppBrowser.onLoadStart(url); _inAppBrowser.onLoadStart(url);
break; break;
case "onLoadStop": case "onLoadStop":
String url = call.arguments["url"]; String url = call.arguments["url"];
if (_widget != null) if (_widget != null && _widget.onLoadStop != null)
_widget.onLoadStop(this, url); _widget.onLoadStop(this, url);
else else if (_inAppBrowser != null)
_inAppBrowser.onLoadStop(url); _inAppBrowser.onLoadStop(url);
break; break;
case "onLoadError": case "onLoadError":
String url = call.arguments["url"]; String url = call.arguments["url"];
int code = call.arguments["code"]; int code = call.arguments["code"];
String message = call.arguments["message"]; String message = call.arguments["message"];
if (_widget != null) if (_widget != null && _widget.onLoadError != null)
_widget.onLoadError(this, url, code, message); _widget.onLoadError(this, url, code, message);
else else if (_inAppBrowser != null)
_inAppBrowser.onLoadError(url, code, message); _inAppBrowser.onLoadError(url, code, message);
break; break;
case "onProgressChanged": case "onProgressChanged":
int progress = call.arguments["progress"]; int progress = call.arguments["progress"];
if (_widget != null) if (_widget != null && _widget.onProgressChanged != null)
_widget.onProgressChanged(this, progress); _widget.onProgressChanged(this, progress);
else else if (_inAppBrowser != null)
_inAppBrowser.onProgressChanged(progress); _inAppBrowser.onProgressChanged(progress);
break; break;
case "shouldOverrideUrlLoading": case "shouldOverrideUrlLoading":
String url = call.arguments["url"]; String url = call.arguments["url"];
if (_widget != null) if (_widget != null && _widget.shouldOverrideUrlLoading != null)
_widget.shouldOverrideUrlLoading(this, url); _widget.shouldOverrideUrlLoading(this, url);
else else if (_inAppBrowser != null)
_inAppBrowser.shouldOverrideUrlLoading(url); _inAppBrowser.shouldOverrideUrlLoading(url);
break; break;
case "onLoadResource": case "onLoadResource":
@ -829,9 +830,9 @@ class InAppWebViewController {
var response = new WebResourceResponse(urlResponse, headersResponse, statusCode, startTime, duration, data); var response = new WebResourceResponse(urlResponse, headersResponse, statusCode, startTime, duration, data);
var request = new WebResourceRequest(urlRequest, headersRequest, method); var request = new WebResourceRequest(urlRequest, headersRequest, method);
if (_widget != null) if (_widget != null && _widget.onLoadResource != null)
_widget.onLoadResource(this, response, request); _widget.onLoadResource(this, response, request);
else else if (_inAppBrowser != null)
_inAppBrowser.onLoadResource(response, request); _inAppBrowser.onLoadResource(response, request);
break; break;
case "onConsoleMessage": case "onConsoleMessage":
@ -845,17 +846,17 @@ class InAppWebViewController {
return; return;
} }
}); });
if (_widget != null) if (_widget != null && _widget.onConsoleMessage != null)
_widget.onConsoleMessage(this, ConsoleMessage(sourceURL, lineNumber, message, messageLevel)); _widget.onConsoleMessage(this, ConsoleMessage(sourceURL, lineNumber, message, messageLevel));
else else if (_inAppBrowser != null)
_inAppBrowser.onConsoleMessage(ConsoleMessage(sourceURL, lineNumber, message, messageLevel)); _inAppBrowser.onConsoleMessage(ConsoleMessage(sourceURL, lineNumber, message, messageLevel));
break; break;
case "onScrollChanged": case "onScrollChanged":
int x = call.arguments["x"]; int x = call.arguments["x"];
int y = call.arguments["y"]; int y = call.arguments["y"];
if (_widget != null) if (_widget != null && _widget.onScrollChanged != null)
_widget.onScrollChanged(this, x, y); _widget.onScrollChanged(this, x, y);
else else if (_inAppBrowser != null)
_inAppBrowser.onScrollChanged(x, y); _inAppBrowser.onScrollChanged(x, y);
break; break;
case "onCallJsHandler": case "onCallJsHandler":
@ -881,7 +882,7 @@ class InAppWebViewController {
///This is not always the same as the URL passed to [InAppWebView.onLoadStarted] because although the load for that URL has begun, the current page may not have changed. ///This is not always the same as the URL passed to [InAppWebView.onLoadStarted] because although the load for that URL has begun, the current page may not have changed.
Future<String> getUrl() async { Future<String> getUrl() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -891,7 +892,7 @@ class InAppWebViewController {
///Gets the title for the current page. ///Gets the title for the current page.
Future<String> getTitle() async { Future<String> getTitle() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -901,7 +902,7 @@ class InAppWebViewController {
///Gets the progress for the current page. The progress value is between 0 and 100. ///Gets the progress for the current page. The progress value is between 0 and 100.
Future<int> getProgress() async { Future<int> getProgress() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -932,7 +933,7 @@ class InAppWebViewController {
Future<void> loadUrl(String url, {Map<String, String> headers = const {}}) async { Future<void> loadUrl(String url, {Map<String, String> headers = const {}}) async {
assert(url != null && url.isNotEmpty); assert(url != null && url.isNotEmpty);
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(message: 'Cannot laod $url!'); _inAppBrowser._throwIsNotOpened(message: 'Cannot laod $url!');
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -946,7 +947,7 @@ class InAppWebViewController {
assert(url != null && url.isNotEmpty); assert(url != null && url.isNotEmpty);
assert(postData != null); assert(postData != null);
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(message: 'Cannot laod $url!'); _inAppBrowser._throwIsNotOpened(message: 'Cannot laod $url!');
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -961,7 +962,7 @@ class InAppWebViewController {
Future<void> loadData(String data, {String mimeType = "text/html", String encoding = "utf8", String baseUrl = "about:blank"}) async { Future<void> loadData(String data, {String mimeType = "text/html", String encoding = "utf8", String baseUrl = "about:blank"}) async {
assert(data != null); assert(data != null);
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1004,7 +1005,7 @@ class InAppWebViewController {
Future<void> loadFile(String assetFilePath, {Map<String, String> headers = const {}}) async { Future<void> loadFile(String assetFilePath, {Map<String, String> headers = const {}}) async {
assert(assetFilePath != null && assetFilePath.isNotEmpty); assert(assetFilePath != null && assetFilePath.isNotEmpty);
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(message: 'Cannot laod $assetFilePath!'); _inAppBrowser._throwIsNotOpened(message: 'Cannot laod $assetFilePath!');
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1016,7 +1017,7 @@ class InAppWebViewController {
///Reloads the [InAppWebView] window. ///Reloads the [InAppWebView] window.
Future<void> reload() async { Future<void> reload() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1026,7 +1027,7 @@ class InAppWebViewController {
///Goes back in the history of the [InAppWebView] window. ///Goes back in the history of the [InAppWebView] window.
Future<void> goBack() async { Future<void> goBack() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1036,7 +1037,7 @@ class InAppWebViewController {
///Returns a boolean value indicating whether the [InAppWebView] can move backward. ///Returns a boolean value indicating whether the [InAppWebView] can move backward.
Future<bool> canGoBack() async { Future<bool> canGoBack() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1046,7 +1047,7 @@ class InAppWebViewController {
///Goes forward in the history of the [InAppWebView] window. ///Goes forward in the history of the [InAppWebView] window.
Future<void> goForward() async { Future<void> goForward() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1056,7 +1057,7 @@ class InAppWebViewController {
///Returns a boolean value indicating whether the [InAppWebView] can move forward. ///Returns a boolean value indicating whether the [InAppWebView] can move forward.
Future<bool> canGoForward() async { Future<bool> canGoForward() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1068,7 +1069,7 @@ class InAppWebViewController {
assert(steps != null); assert(steps != null);
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1081,7 +1082,7 @@ class InAppWebViewController {
assert(steps != null); assert(steps != null);
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1097,7 +1098,7 @@ class InAppWebViewController {
///Check if the Web View of the [InAppWebView] instance is in a loading state. ///Check if the Web View of the [InAppWebView] instance is in a loading state.
Future<bool> isLoading() async { Future<bool> isLoading() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1107,7 +1108,7 @@ class InAppWebViewController {
///Stops the Web View of the [InAppWebView] instance from loading. ///Stops the Web View of the [InAppWebView] instance from loading.
Future<void> stopLoading() async { Future<void> stopLoading() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1117,7 +1118,7 @@ class InAppWebViewController {
///Injects JavaScript code into the [InAppWebView] window and returns the result of the evaluation. ///Injects JavaScript code into the [InAppWebView] window and returns the result of the evaluation.
Future<String> injectScriptCode(String source) async { Future<String> injectScriptCode(String source) async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1128,7 +1129,7 @@ class InAppWebViewController {
///Injects a JavaScript file into the [InAppWebView] window. ///Injects a JavaScript file into the [InAppWebView] window.
Future<void> injectScriptFile(String urlFile) async { Future<void> injectScriptFile(String urlFile) async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1139,7 +1140,7 @@ class InAppWebViewController {
///Injects CSS into the [InAppWebView] window. ///Injects CSS into the [InAppWebView] window.
Future<void> injectStyleCode(String source) async { Future<void> injectStyleCode(String source) async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1150,7 +1151,7 @@ class InAppWebViewController {
///Injects a CSS file into the [InAppWebView] window. ///Injects a CSS file into the [InAppWebView] window.
Future<void> injectStyleFile(String urlFile) async { Future<void> injectStyleFile(String urlFile) async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1220,7 +1221,7 @@ class InAppWebViewController {
///**NOTE for iOS**: available from iOS 11.0+. ///**NOTE for iOS**: available from iOS 11.0+.
Future<Uint8List> takeScreenshot() async { Future<Uint8List> takeScreenshot() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1230,7 +1231,7 @@ class InAppWebViewController {
///Sets the [InAppWebView] options with the new [options] and evaluates them. ///Sets the [InAppWebView] options with the new [options] and evaluates them.
Future<void> setOptions(Map<String, dynamic> options) async { Future<void> setOptions(Map<String, dynamic> options) async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1242,7 +1243,7 @@ class InAppWebViewController {
///Gets the current [InAppWebView] options. Returns `null` if the options are not setted yet. ///Gets the current [InAppWebView] options. Returns `null` if the options are not setted yet.
Future<Map<String, dynamic>> getOptions() async { Future<Map<String, dynamic>> getOptions() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }
@ -1258,7 +1259,7 @@ class InAppWebViewController {
///The object returned from this method will not be updated to reflect any new state. ///The object returned from this method will not be updated to reflect any new state.
Future<WebHistory> getCopyBackForwardList() async { Future<WebHistory> getCopyBackForwardList() async {
Map<String, dynamic> args = <String, dynamic>{}; Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) { if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser._throwIsNotOpened(); _inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid); args.putIfAbsent('uuid', () => _inAppBrowserUuid);
} }