Skip to content

Commit

Permalink
Created WebUri class to replace Uri dart core type, fix #1402, fix #1328
Browse files Browse the repository at this point in the history
, fix #1350
  • Loading branch information
pichillilorenzo committed Oct 27, 2022
1 parent 52fb4b3 commit fa5449a
Show file tree
Hide file tree
Showing 92 changed files with 579 additions and 307 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 6.0.0-beta.10

- Created `WebUri` class to replace `Uri` dart core type. Related to:
- "Uri.tryParse will make the host to be lowercase" [#1402](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1402)
- "An error occurs when using a specific intent" [#1328](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1328)
- "Android shouldOverrideUrlLoading not working" [#1350](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1350)

### BREAKING CHANGES

- Replaced the usage of `Uri` type with the new `WebUri` type

## 6.0.0-beta.9

- Added `headers`, `otherLikelyURLs`, `referrer` arguments on `ChromeSafariBrowser.open` method for Android
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.pichillilorenzo.flutter_inappwebview.types;

import android.net.Uri;
import android.os.Build;
import android.webkit.WebResourceRequest;

Expand All @@ -14,14 +13,14 @@

public class WebResourceRequestExt {
@NonNull
private Uri url;
private String url;
private Map<String, String> headers;
private boolean isRedirect;
private boolean hasGesture;
private boolean isForMainFrame;
private String method;

public WebResourceRequestExt(@NonNull Uri url, Map<String, String> headers, boolean isRedirect, boolean hasGesture, boolean isForMainFrame, String method) {
public WebResourceRequestExt(@NonNull String url, Map<String, String> headers, boolean isRedirect, boolean hasGesture, boolean isForMainFrame, String method) {
this.url = url;
this.headers = headers;
this.isRedirect = isRedirect;
Expand All @@ -38,7 +37,7 @@ static public WebResourceRequestExt fromWebResourceRequest(@NonNull WebResourceR
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
isRedirect = request.isRedirect();
}
return new WebResourceRequestExt(request.getUrl(),
return new WebResourceRequestExt(request.getUrl().toString(),
request.getRequestHeaders(),
isRedirect,
request.hasGesture(),
Expand All @@ -49,7 +48,7 @@ static public WebResourceRequestExt fromWebResourceRequest(@NonNull WebResourceR

public Map<String, Object> toMap() {
Map<String, Object> webResourceRequestMap = new HashMap<>();
webResourceRequestMap.put("url", url.toString());
webResourceRequestMap.put("url", url);
webResourceRequestMap.put("headers", headers);
webResourceRequestMap.put("isRedirect", isRedirect);
webResourceRequestMap.put("hasGesture", hasGesture);
Expand All @@ -59,11 +58,11 @@ public Map<String, Object> toMap() {
}

@NonNull
public Uri getUrl() {
public String getUrl() {
return url;
}

public void setUrl(@NonNull Uri url) {
public void setUrl(@NonNull String url) {
this.url = url;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
}

WebResourceRequestExt request = new WebResourceRequestExt(
Uri.parse(failingUrl),
failingUrl,
null,
false,
false,
Expand Down Expand Up @@ -627,7 +627,8 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque

if (webView.webViewAssetLoaderExt != null && webView.webViewAssetLoaderExt.loader != null) {
try {
WebResourceResponse webResourceResponse = webView.webViewAssetLoaderExt.loader.shouldInterceptRequest(request.getUrl());
final Uri uri = Uri.parse(request.getUrl());
WebResourceResponse webResourceResponse = webView.webViewAssetLoaderExt.loader.shouldInterceptRequest(uri);
if (webResourceResponse != null) {
return webResourceResponse;
}
Expand Down Expand Up @@ -667,8 +668,11 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
return null;
}

final String url = request.getUrl().toString();
String scheme = request.getUrl().getScheme();
final String url = request.getUrl();
String scheme = url.split(":")[0].toLowerCase();
try {
scheme = Uri.parse(request.getUrl()).getScheme();
} catch (Exception ignored) {}

if (webView.customSettings.resourceCustomSchemes != null && webView.customSettings.resourceCustomSchemes.contains(scheme)) {
CustomSchemeResponse customSchemeResponse = null;
Expand Down Expand Up @@ -710,7 +714,7 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, final String url) {
WebResourceRequestExt requestExt = new WebResourceRequestExt(
Uri.parse(url), null, false,
url, null, false,
false, true, "GET"
);
return shouldInterceptRequest(view, requestExt);
Expand Down
15 changes: 15 additions & 0 deletions dev_packages/generators/lib/src/exchangeable_object_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ class ExchangeableObjectGenerator
} else if (hasFromValue && deprecatedHasToValue) {
classBuffer.write(fieldTypeElement.name!.replaceFirst("_", "") +
'.fromValue($deprecatedFieldName${deprecatedIsNullable ? '?' : ''}.toValue())${!isNullable ? '!' : ''}');
} else if (deprecatedField.type.getDisplayString(withNullability: false) == "Uri" &&
fieldElement.type.getDisplayString(withNullability: false) == "WebUri") {
if (deprecatedIsNullable) {
classBuffer.write("($deprecatedFieldName != null ? WebUri.uri($deprecatedFieldName!) : ${isNullable ? "null" : "WebUri('')"})");
} else {
classBuffer.write("WebUri.uri($deprecatedFieldName)");
}
} else {
classBuffer.write(deprecatedFieldName);
}
Expand Down Expand Up @@ -496,6 +503,12 @@ class ExchangeableObjectGenerator
} else {
return "$value != null ? Uri.tryParse($value) : null";
}
} else if (elementType.getDisplayString(withNullability: false) == "WebUri") {
if (!isNullable) {
return "WebUri($value)";
} else {
return "$value != null ? WebUri($value) : null";
}
} else if (elementType.getDisplayString(withNullability: false) ==
"Color") {
if (!isNullable) {
Expand Down Expand Up @@ -573,6 +586,8 @@ class ExchangeableObjectGenerator
final isNullable = Util.typeIsNullable(elementType);
if (elementType.getDisplayString(withNullability: false) == "Uri") {
return fieldName + (isNullable ? '?' : '') + '.toString()';
} else if (elementType.getDisplayString(withNullability: false) == "WebUri") {
return fieldName + (isNullable ? '?' : '') + '.toString()';
} else if (elementType.getDisplayString(withNullability: false) ==
"Color") {
return fieldName + (isNullable ? '?' : '') + '.toHex()';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void customTabs() {

await chromeSafariBrowser.open(
url: TEST_URL_1,
referrer: Uri.parse("android-app://custom-referrer"),
referrer: WebUri("android-app://custom-referrer"),
settings: ChromeSafariBrowserSettings(isSingleInstance: true));
await expectLater(chromeSafariBrowser.opened.future, completes);
expect(chromeSafariBrowser.isOpened(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void openAndClose() {
presentationStyle: ModalPresentationStyle.OVER_FULL_SCREEN,
eventAttribution: UIEventAttribution(
sourceIdentifier: 4,
destinationURL: Uri.parse("https://shop.example/test.html"),
destinationURL: WebUri("https://shop.example/test.html"),
sourceDescription: "Banner ad for Test.",
purchaser: "Shop Example, Inc."),
activityButton: ActivityButton(
Expand Down
40 changes: 21 additions & 19 deletions example/integration_test/constants.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
final TEST_URL_ABOUT_BLANK = Uri.parse('about:blank');
final TEST_CROSS_PLATFORM_URL_1 = Uri.parse('https://flutter.dev/');
final TEST_CROSS_PLATFORM_URL_2 = Uri.parse('https://www.bing.com/');
final TEST_URL_1 = Uri.parse('https://github.com/flutter');
final TEST_URL_2 = Uri.parse('https://www.google.com/');
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

final TEST_URL_ABOUT_BLANK = WebUri('about:blank');
final TEST_CROSS_PLATFORM_URL_1 = WebUri('https://flutter.dev/');
final TEST_CROSS_PLATFORM_URL_2 = WebUri('https://www.bing.com/');
final TEST_URL_1 = WebUri('https://github.com/flutter');
final TEST_URL_2 = WebUri('https://www.google.com/');
final TEST_URL_3 =
Uri.parse('https://github.com/pichillilorenzo/flutter_inappwebview');
final TEST_URL_4 = Uri.parse('https://www.youtube.com/');
final TEST_URL_EXAMPLE = Uri.parse('https://www.example.com/');
final TEST_URL_HTTP_EXAMPLE = Uri.parse('http://www.example.com/');
final TEST_URL_404 = Uri.parse('https://google.com/404');
WebUri('https://github.com/pichillilorenzo/flutter_inappwebview');
final TEST_URL_4 = WebUri('https://www.youtube.com/');
final TEST_URL_EXAMPLE = WebUri('https://www.example.com/');
final TEST_URL_HTTP_EXAMPLE = WebUri('http://www.example.com/');
final TEST_URL_404 = WebUri('https://google.com/404');
final TEST_WEB_PLATFORM_BASE_URL =
Uri.parse(Uri.base.toString().replaceFirst("/#/", "/"));
WebUri(Uri.base.toString().replaceFirst("/#/", "/"));
final TEST_WEB_PLATFORM_URL_1 =
Uri.parse(TEST_WEB_PLATFORM_BASE_URL.toString() + 'page.html');
WebUri(TEST_WEB_PLATFORM_BASE_URL.toString() + 'page.html');
final TEST_WEB_PLATFORM_URL_2 =
Uri.parse(TEST_WEB_PLATFORM_BASE_URL.toString() + 'page-2.html');
WebUri(TEST_WEB_PLATFORM_BASE_URL.toString() + 'page-2.html');
final TEST_WEB_PLATFORM_URL_3 =
Uri.parse(TEST_WEB_PLATFORM_BASE_URL.toString() + 'heavy-page.html');
final TEST_NOT_A_WEBSITE_URL = Uri.parse('https://www.notawebsite..com/');
WebUri(TEST_WEB_PLATFORM_BASE_URL.toString() + 'heavy-page.html');
final TEST_NOT_A_WEBSITE_URL = WebUri('https://www.notawebsite..com/');
final TEST_CHROME_SAFE_BROWSING_MALWARE =
Uri.parse('chrome://safe-browsing/match?type=malware');
final TEST_PERMISSION_SITE = Uri.parse('https://permission.site/');
final TEST_SERVICE_WORKER_URL = Uri.parse(
WebUri('chrome://safe-browsing/match?type=malware');
final TEST_PERMISSION_SITE = WebUri('https://permission.site/');
final TEST_SERVICE_WORKER_URL = WebUri(
'https://mdn.github.io/dom-examples/service-worker/simple-service-worker/');
final TEST_WEBVIEW_ASSET_LOADER_DOMAIN = 'my.custom.domain.com';
final TEST_WEBVIEW_ASSET_LOADER_URL = Uri.parse(
final TEST_WEBVIEW_ASSET_LOADER_URL = WebUri(
'https://$TEST_WEBVIEW_ASSET_LOADER_DOMAIN/assets/flutter_assets/assets/website/index.html');
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void setGetDelete() {
);
}

final url = Uri.parse(await pageLoaded.future);
final url = WebUri(await pageLoaded.future);

await cookieManager.setCookie(url: url, name: "myCookie", value: "myValue");
List<Cookie> cookies = await cookieManager.getCookies(url: url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void loadAssetFile(InAppLocalhostServer localhostServer) {

var headlessWebView = new HeadlessInAppWebView(
initialUrlRequest: URLRequest(
url: Uri.parse('http://localhost:8080/test_assets/index.html')),
url: WebUri('http://localhost:8080/test_assets/index.html')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
Expand All @@ -37,7 +37,7 @@ void loadAssetFile(InAppLocalhostServer localhostServer) {
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url: Uri.parse('http://localhost:8080/test_assets/index.html')),
url: WebUri('http://localhost:8080/test_assets/index.html')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void audioPlaybackPolicy() {
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url: Uri.parse(
url: WebUri(
'data:text/html;charset=utf-8;base64,$audioTestBase64')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
Expand Down Expand Up @@ -96,7 +96,7 @@ void audioPlaybackPolicy() {
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url: Uri.parse(
url: WebUri(
'data:text/html;charset=utf-8;base64,$audioTestBase64')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
Expand Down
2 changes: 1 addition & 1 deletion example/integration_test/in_app_webview/get_title.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void getTitle() {
base64Encode(const Utf8Encoder().convert(getTitleTest));

var url = !kIsWeb
? Uri.parse('data:text/html;charset=utf-8;base64,$getTitleTestBase64')
? WebUri('data:text/html;charset=utf-8;base64,$getTitleTestBase64')
: TEST_WEB_PLATFORM_URL_1;
var expectedValue = !kIsWeb ? 'Some title' : 'page';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ void httpAuthCredentialDatabase() {
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url:
Uri.parse("http://${environment["NODE_SERVER_IP"]}:8081/")),
url: WebUri("http://${environment["NODE_SERVER_IP"]}:8081/")),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
Expand Down Expand Up @@ -95,8 +94,7 @@ void httpAuthCredentialDatabase() {
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url:
Uri.parse("http://${environment["NODE_SERVER_IP"]}:8081/")),
url: WebUri("http://${environment["NODE_SERVER_IP"]}:8081/")),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
Expand Down
2 changes: 1 addition & 1 deletion example/integration_test/in_app_webview/inject_css.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void injectCSS() {
await pageLoaded.future;

await controller.injectCSSFileFromUrl(
urlFile: Uri.parse(
urlFile: WebUri(
'https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css'),
cssLinkHtmlTagAttributes: CSSLinkHtmlTagAttributes(id: 'bootstrap'));
await Future.delayed(Duration(seconds: 2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ void injectJavascriptFile() {
await pageLoaded.future;

await controller.injectJavascriptFileFromUrl(
urlFile:
Uri.parse('https://www.notawebsite..com/jquery-3.3.1.min.js'),
urlFile: WebUri('https://www.notawebsite..com/jquery-3.3.1.min.js'),
scriptHtmlTagAttributes: ScriptHtmlTagAttributes(
id: 'jquery-error',
onError: () {
Expand All @@ -65,7 +64,7 @@ void injectJavascriptFile() {
true);

await controller.injectJavascriptFileFromUrl(
urlFile: Uri.parse('https://code.jquery.com/jquery-3.3.1.min.js'),
urlFile: WebUri('https://code.jquery.com/jquery-3.3.1.min.js'),
scriptHtmlTagAttributes: ScriptHtmlTagAttributes(
id: 'jquery',
onLoad: () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void isSecureContext() {

if (!kIsWeb) {
await controller.loadUrl(
urlRequest: URLRequest(url: Uri.parse('http://example.com/')));
urlRequest: URLRequest(url: WebUri('http://example.com/')));
await pageLoads.stream.first;
expect(await controller.isSecureContext(), false);
}
Expand Down
13 changes: 6 additions & 7 deletions example/integration_test/in_app_webview/load_file_url.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void loadFileUrl() {
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: Uri.parse('file://${fileHtml.path}')),
URLRequest(url: WebUri('file://${fileHtml.path}')),
onConsoleMessage: (controller, consoleMessage) {
consoleMessageShouldNotComplete.complete(consoleMessage);
},
Expand All @@ -82,10 +82,9 @@ void loadFileUrl() {
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: Uri.parse('file://${fileHtml.path}')),
URLRequest(url: WebUri('file://${fileHtml.path}')),
initialSettings: InAppWebViewSettings(
allowingReadAccessTo:
Uri.parse('file://${appSupportDir.path}/')),
allowingReadAccessTo: WebUri('file://${appSupportDir.path}/')),
onConsoleMessage: (controller, consoleMessage) {
consoleMessageCompleter.complete(consoleMessage);
},
Expand All @@ -110,7 +109,7 @@ void loadFileUrl() {
onWebViewCreated: (controller) {
controller.loadUrl(
urlRequest:
URLRequest(url: Uri.parse('file://${fileHtml.path}')));
URLRequest(url: WebUri('file://${fileHtml.path}')));
},
onConsoleMessage: (controller, consoleMessage) {
consoleMessageShouldNotComplete.complete(consoleMessage);
Expand All @@ -132,9 +131,9 @@ void loadFileUrl() {
onWebViewCreated: (controller) {
controller.loadUrl(
urlRequest:
URLRequest(url: Uri.parse('file://${fileHtml.path}')),
URLRequest(url: WebUri('file://${fileHtml.path}')),
allowingReadAccessTo:
Uri.parse('file://${appSupportDir.path}/'));
WebUri('file://${appSupportDir.path}/'));
},
onConsoleMessage: (controller, consoleMessage) {
consoleMessageCompleter.complete(consoleMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void onReceivedError() {
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url: Uri.parse(
url: WebUri(
'data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+')),
onReceivedError: (controller, request, error) {
onReceivedErrorCompleter.complete();
Expand Down
Loading

0 comments on commit fa5449a

Please sign in to comment.