Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter] [url_launcher] Handle Multiwindows in WebViews #2991

Merged
merged 25 commits into from Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
68f8848
webview portion
bparrishMines Sep 2, 2020
7337002
url_launcher
bparrishMines Sep 2, 2020
3fa09aa
formatting
bparrishMines Sep 2, 2020
39b030b
Seperate to a class
bparrishMines Sep 2, 2020
bb8154b
Add documentation
bparrishMines Sep 2, 2020
625f673
Change doc location
bparrishMines Sep 2, 2020
9f8014f
load with no navigation delegate
bparrishMines Sep 4, 2020
ef55f08
formatting
bparrishMines Sep 4, 2020
9bd470a
test for window open
bparrishMines Sep 5, 2020
3c6de98
Merge branch 'master' of github.com:flutter/plugins into eat_js_webview
bparrishMines Sep 8, 2020
833bb0a
Add test to check for http/https
bparrishMines Sep 8, 2020
dde8c7f
dont filter url and set test to only run on android
bparrishMines Sep 9, 2020
c12449a
Merge branch 'master' of github.com:flutter/plugins into eat_js_webview
bparrishMines Sep 9, 2020
c5bf9d1
version bump
bparrishMines Sep 10, 2020
7657a2c
Merge branch 'master' of github.com:flutter/plugins into eat_js_webview
bparrishMines Sep 11, 2020
380c7dd
javascript test
bparrishMines Sep 11, 2020
9bfc1d2
Add iframe
bparrishMines Sep 12, 2020
dd4deec
Merge branch 'master' of github.com:flutter/plugins into eat_js_webview
bparrishMines Sep 12, 2020
022e717
Merge branch 'master' of github.com:flutter/plugins into eat_js_webview
bparrishMines Sep 15, 2020
9d6a594
replace onLoad
bparrishMines Sep 17, 2020
2a6d766
Merge branch 'master' of github.com:flutter/plugins into eat_js_webview
bparrishMines Sep 17, 2020
94c7b5a
use iframeLoaded variable
bparrishMines Sep 17, 2020
fab93d7
fix iframe test
bparrishMines Sep 18, 2020
f6bf112
Test name change
bparrishMines Sep 18, 2020
6d53eda
update test
bparrishMines Sep 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,17 +1,21 @@
package io.flutter.plugins.urllauncher;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.provider.Browser;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -60,6 +64,48 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request

private IntentFilter closeIntentFilter = new IntentFilter(ACTION_CLOSE);

// Verifies that a url opened by `Window.open` has a secure url.
private class FlutterWebChromeClient extends WebChromeClient {
@Override
public boolean onCreateWindow(
final WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
final WebViewClient webViewClient =
new WebViewClient() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what API needs this particular version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

understood

@Override
public boolean shouldOverrideUrlLoading(
@NonNull WebView view, @NonNull WebResourceRequest request) {
final String url = request.getUrl().toString();
if (isSecure(url)) {
webview.loadUrl(url);
}
return true;
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isSecure(url)) {
webview.loadUrl(url);
}
return true;
}
};

final WebView newWebView = new WebView(webview.getContext());
newWebView.setWebViewClient(webViewClient);

final WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();

return true;
}

private boolean isSecure(String url) {
return url.startsWith("https://") || url.startsWith("http://");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest we rename this as: isJavaScriptScheme(String url) and check for url.startsWith("javascript:"). Link to https://tools.ietf.org/html/draft-hoehrmann-javascript-scheme-03 would be useful too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would only do this after we get an ok from the internal team. This method is currently just following their suggestion.

}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -81,6 +127,10 @@ public void onCreate(Bundle savedInstanceState) {
// Open new urls inside the webview itself.
webview.setWebViewClient(webViewClient);

// Multi windows is set with FlutterWebChromeClient by default to handle internal bug: b/159892679.
webview.getSettings().setSupportMultipleWindows(true);
webview.setWebChromeClient(new FlutterWebChromeClient());

// Register receiver that may finish this Activity.
registerReceiver(broadcastReceiver, closeIntentFilter);
}
Expand Down
Expand Up @@ -9,9 +9,14 @@
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand All @@ -29,6 +34,48 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
private final FlutterWebViewClient flutterWebViewClient;
private final Handler platformThreadHandler;

// Verifies that a url opened by `Window.open` has a secure url.
private class FlutterWebChromeClient extends WebChromeClient {
blasten marked this conversation as resolved.
Show resolved Hide resolved
@Override
public boolean onCreateWindow(
final WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
final WebViewClient webViewClient =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand this part, it seems like we're creating a new WebViewClient type and delegate some of the calls to the existing flutterWebViewClient, do we not want all functionality implemented by FlutterWebViewClient to work for the new WebView as well? (e.g should we not just use FlutterWebViewClient as the client for the new webview?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that we only wanted the new WebView to filter out non https/http calls and load any secure url. I'm not sure what else we would need it to do? Im assuming you're talking about returning WebResourceErrors and onPageStarted/onPageFinished callbacks. Won't our FlutterWebViewClient receive these after we call loadUrl?

new WebViewClient() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(
@NonNull WebView view, @NonNull WebResourceRequest request) {
final String url = request.getUrl().toString();
if (isSecure(url)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow the logic here, can you comment on why we only delegate calls for http and https URLs?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flutterWebViewClient.shouldOverrideUrlLoading(FlutterWebView.this.webView, request);
}
return true;
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isSecure(url)) {
flutterWebViewClient.shouldOverrideUrlLoading(FlutterWebView.this.webView, url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does invoking shouldOVerrideUrlLoading has a side effect of loading the URL? It seems like our implementation of shouldOVerrideUrlLoading introduces such a side effect on older webview versions only if a navigation delegate is set. Does this work when no navigation delegate is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test, so it does still work without a navigation delegate.

}
return true;
}
};

final WebView newWebView = new WebView(view.getContext());
amirh marked this conversation as resolved.
Show resolved Hide resolved
newWebView.setWebViewClient(webViewClient);

final WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();

return true;
}

private boolean isSecure(String url) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a little confused by the name isSecure, my initial guess when looking on the call site would have been "is it https"? though I'm not sure even why we're checking on this condition here.

return url.startsWith("https://") || url.startsWith("http://");
}
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("unchecked")
FlutterWebView(
Expand All @@ -50,6 +97,10 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

// Multi windows is set with FlutterWebChromeClient by default to handle internal bug: b/159892679.
webView.getSettings().setSupportMultipleWindows(true);
webView.setWebChromeClient(new FlutterWebChromeClient());

methodChannel = new MethodChannel(messenger, "plugins.flutter.io/webview_" + id);
methodChannel.setMethodCallHandler(this);

Expand Down
Expand Up @@ -77,7 +77,7 @@ private static String errorCodeToString(int errorCode) {
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (!hasNavigationDelegate) {
return false;
}
Expand All @@ -97,7 +97,7 @@ private boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest reques
return request.isForMainFrame();
}

private boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!hasNavigationDelegate) {
return false;
}
Expand Down