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 14 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.5.4

* Handle WebView multi-window support.

## 5.5.3

* Suppress deprecation warning on the `shouldOverrideUrlLoading` method on Android.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 androidx.annotation.RequiresApi;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -67,6 +71,39 @@ 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) {
webview.loadUrl(request.getUrl().toString());
return true;
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String 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;
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -88,6 +125,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
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher
description: Flutter plugin for launching a URL on Android and iOS. Supports
web, phone, SMS, and email schemes.
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher
version: 5.5.3
version: 5.5.4

flutter:
plugin:
Expand Down
4 changes: 4 additions & 0 deletions packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.23

* Handle WebView multi-window support.

## 0.3.22+2

* Update package:e2e reference to use the local version in the flutter/plugins
Expand Down
Original file line number Diff line number Diff line change
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,46 @@ 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 (!flutterWebViewClient.shouldOverrideUrlLoading(
FlutterWebView.this.webView, request)) {
webView.loadUrl(url);
}
return true;
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!flutterWebViewClient.shouldOverrideUrlLoading(
FlutterWebView.this.webView, url)) {
webView.loadUrl(url);
}
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;
}
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("unchecked")
FlutterWebView(
Expand All @@ -50,6 +95,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,40 @@ void main() {
final String currentUrl = await controller.currentUrl();
expect(currentUrl, 'about:blank');
});

testWidgets(
'can open new window and go back',
Copy link

Choose a reason for hiding this comment

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

this one is good, but what about a test case like the one in the internal doc?

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'm not sure how to do a test like that. I would need to load a link that tries to open a window and test to see if javascript ran?

(WidgetTester tester) async {
final Completer<WebViewController> controllerCompleter =
Completer<WebViewController>();
final Completer<void> pageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: WebView(
key: GlobalKey(),
onWebViewCreated: (WebViewController controller) {
controllerCompleter.complete(controller);
},
javascriptMode: JavascriptMode.unrestricted,
onPageFinished: (String url) {
pageLoaded.complete();
},
initialUrl: 'https://flutter.dev',
),
),
);
final WebViewController controller = await controllerCompleter.future;
await controller
.evaluateJavascript('window.open("https://www.google.com")');
await pageLoaded.future;
expect(controller.currentUrl(), completion('https://www.google.com/'));

await controller.goBack();
expect(controller.currentUrl(), completion('https://www.flutter.dev'));
},
skip: !Platform.isAndroid,
);
}

// JavaScript booleans evaluate to different string values on Android and iOS.
Expand Down
2 changes: 1 addition & 1 deletion packages/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
version: 0.3.22+2
version: 0.3.23
homepage: https://github.com/flutter/plugins/tree/master/packages/webview_flutter

environment:
Expand Down