Commit 2d369b47 authored by Shimi Zhang's avatar Shimi Zhang Committed by Commit Bot

[JJI] Port {add,remove}WebMessageListener to glue layer

See the corresponding AndroidX change: http://aosp/1099421

Bug: 918065
Change-Id: Iad8cd0cd273235de1105f04844891494f793af1b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1745462
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Reviewed-by: default avatarNate Fischer <ntfschr@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Reviewed-by: default avatarChangwan Ryu <changwan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#700368}
parent cef58076
......@@ -9,6 +9,7 @@ import android.webkit.WebViewClient;
import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwRenderProcess;
import org.chromium.android_webview.WebMessageListener;
import org.chromium.android_webview.WebViewChromiumRunQueue;
import org.chromium.base.ThreadUtils;
import org.chromium.content_public.browser.MessagePort;
......@@ -117,6 +118,24 @@ public class SharedWebViewChromium {
mAwContents.postMessageToMainFrame(message, targetOrigin, sentPorts);
}
public void addWebMessageListener(final String jsObjectName, final String[] allowedOriginRules,
final WebMessageListener listener) {
if (checkNeedsPost()) {
mRunQueue.addTask(
() -> addWebMessageListener(jsObjectName, allowedOriginRules, listener));
return;
}
mAwContents.addWebMessageListener(jsObjectName, allowedOriginRules, listener);
}
public void removeWebMessageListener(final String jsObjectName) {
if (checkNeedsPost()) {
mRunQueue.addTask(() -> removeWebMessageListener(jsObjectName));
return;
}
mAwContents.removeWebMessageListener(jsObjectName);
}
public void setWebViewRendererClientAdapter(
SharedWebViewRendererClientAdapter webViewRendererClientAdapter) {
if (checkNeedsPost()) {
......
......@@ -18,7 +18,7 @@ import org.chromium.content_public.browser.UiThreadTaskTraits;
* since developer could hold a reference to it. So just cut the connection between native and Java.
*/
@JNINamespace("android_webview")
public class JsReplyProxy {
public class JsReplyProxy extends AwSupportLibIsomorphic {
private long mNativeJsReplyProxy;
private JsReplyProxy(long nativeJsReplyProxy) {
......
......@@ -8,13 +8,16 @@ import("//build/config/android/rules.gni")
android_library("support_lib_glue_java") {
java_files = [
"java/src/org/chromium/support_lib_glue/IsomorphicAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibJsReplyProxyAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibProxyControllerAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibReflectionUtil.java",
"java/src/org/chromium/support_lib_glue/SupportLibServiceWorkerClientAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibServiceWorkerControllerAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibServiceWorkerSettingsAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibTracingControllerAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibWebMessageAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibWebMessageCallbackAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibWebMessageListenerAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibWebMessagePortAdapter.java",
"java/src/org/chromium/support_lib_glue/SupportLibWebResourceRequest.java",
"java/src/org/chromium/support_lib_glue/SupportLibWebSettingsAdapter.java",
......
......@@ -9,6 +9,7 @@ android_library("boundary_interface_java") {
java_files = [
"src/org/chromium/support_lib_boundary/FeatureFlagHolderBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/IsomorphicObjectBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/JsReplyProxyBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/ProxyControllerBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/SafeBrowsingResponseBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/ServiceWorkerClientBoundaryInterface.java",
......@@ -19,6 +20,7 @@ android_library("boundary_interface_java") {
"src/org/chromium/support_lib_boundary/VisualStateCallbackBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/WebMessageBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/WebMessageCallbackBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/WebMessageListenerBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/WebMessagePortBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/WebResourceErrorBoundaryInterface.java",
"src/org/chromium/support_lib_boundary/WebResourceRequestBoundaryInterface.java",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.support_lib_boundary;
/**
* Boundary interface for org.chromium.android_webview.WebMessageListener.
*/
public interface JsReplyProxyBoundaryInterface extends IsomorphicObjectBoundaryInterface {
void postMessage(String message);
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.support_lib_boundary;
import android.net.Uri;
import android.webkit.WebView;
import java.lang.reflect.InvocationHandler;
/**
* Boundary interface for org.chromium.android_webview.WebMessageListener.
*/
public interface WebMessageListenerBoundaryInterface extends FeatureFlagHolderBoundaryInterface {
void onPostMessage(WebView view, /* WebMessage */ InvocationHandler message, Uri sourceOrigin,
boolean isMainFrame, /* JsReplyProxy */ InvocationHandler replyProxy);
}
......@@ -17,6 +17,9 @@ public interface WebViewProviderBoundaryInterface {
/* VisualStateCallback */ InvocationHandler callback);
/* WebMessagePort */ InvocationHandler[] createWebMessageChannel();
void postMessageToMainFrame(/* WebMessage */ InvocationHandler message, Uri targetOrigin);
void addWebMessageListener(String jsObjectName, String[] allowedOriginRules,
/* WebMessageListener */ InvocationHandler listener);
void removeWebMessageListener(String jsObjectName);
WebViewClient getWebViewClient();
WebChromeClient getWebChromeClient();
/* WebViewRenderer */ InvocationHandler getWebViewRenderer();
......
......@@ -169,4 +169,8 @@ public class Features {
// WebSettingsCompat.setForceDarkBehavior
// WebSettingsCompat.getForceDarkBehavior
public static final String FORCE_DARK_BEHAVIOR = "FORCE_DARK_BEHAVIOR";
// WebViewCompat.addWebMessageListener
// WebViewCompat.removeWebMessageListener
public static final String WEB_MESSAGE_LISTENER = "WEB_MESSAGE_LISTENER";
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.support_lib_glue;
import org.chromium.android_webview.AwSupportLibIsomorphic;
import org.chromium.android_webview.JsReplyProxy;
import org.chromium.support_lib_boundary.JsReplyProxyBoundaryInterface;
/**
* Adapter between JsReplyProxyBoundaryInterface and JsReplyProxy.
*/
class SupportLibJsReplyProxyAdapter
extends IsomorphicAdapter implements JsReplyProxyBoundaryInterface {
private JsReplyProxy mReplyProxy;
public SupportLibJsReplyProxyAdapter(JsReplyProxy replyProxy) {
mReplyProxy = replyProxy;
}
@Override
public void postMessage(String message) {
mReplyProxy.postMessage(message);
}
@Override
/* package */ AwSupportLibIsomorphic getPeeredObject() {
return mReplyProxy;
}
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.support_lib_glue;
import org.chromium.content_public.browser.MessagePort;
import org.chromium.support_lib_boundary.WebMessageBoundaryInterface;
import java.lang.reflect.InvocationHandler;
/**
* Utility class for creating a WebMessageBoundaryInterface (this is necessary to pass a
* WebMessage back across the boundary).
*/
public class SupportLibWebMessageAdapter implements WebMessageBoundaryInterface {
private String mData;
private MessagePort[] mPorts;
/* package */ SupportLibWebMessageAdapter(String data, MessagePort[] ports) {
mData = data;
mPorts = ports;
}
@Override
public String getData() {
return mData;
}
@Override
public /* WebMessagePort */ InvocationHandler[] getPorts() {
return SupportLibWebMessagePortAdapter.fromMessagePorts(mPorts);
}
@Override
public String[] getSupportedFeatures() {
// getData() and getPorts() are not covered by feature flags.
return new String[0];
}
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.support_lib_glue;
import android.net.Uri;
import android.webkit.WebView;
import org.chromium.android_webview.JsReplyProxy;
import org.chromium.android_webview.WebMessageListener;
import org.chromium.base.Log;
import org.chromium.content_public.browser.MessagePort;
import org.chromium.support_lib_boundary.WebMessageListenerBoundaryInterface;
import org.chromium.support_lib_boundary.util.BoundaryInterfaceReflectionUtil;
import org.chromium.support_lib_boundary.util.Features;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* Support library glue WebMessageListener.
*
* A new instance of this class is created transiently for every shared library WebViewCompat call.
* Do not store state here.
*/
class SupportLibWebMessageListenerAdapter implements WebMessageListener {
private static final String TAG = "WebMsgLtrAdptr";
private final WebView mWebView;
private WebMessageListenerBoundaryInterface mImpl;
private String[] mSupportedFeatures;
public SupportLibWebMessageListenerAdapter(
WebView webView, /* WebMessageListener */ InvocationHandler handler) {
mWebView = webView;
mImpl = BoundaryInterfaceReflectionUtil.castToSuppLibClass(
WebMessageListenerBoundaryInterface.class, handler);
mSupportedFeatures = mImpl.getSupportedFeatures();
}
public /* WebMessageListener */ InvocationHandler getSupportLibInvocationHandler() {
return Proxy.getInvocationHandler(mImpl);
}
@Override
public void onPostMessage(final String message, final Uri sourceOrigin,
final boolean isMainFrame, final JsReplyProxy replyProxy, final MessagePort[] ports) {
if (!BoundaryInterfaceReflectionUtil.containsFeature(
mSupportedFeatures, Features.WEB_MESSAGE_LISTENER)) {
Log.e(TAG, "The AndroidX doesn't have feature: " + Features.WEB_MESSAGE_LISTENER);
return;
}
mImpl.onPostMessage(mWebView,
BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
new SupportLibWebMessageAdapter(message, ports)),
sourceOrigin, isMainFrame,
BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
new SupportLibJsReplyProxyAdapter(replyProxy)));
}
}
......@@ -62,36 +62,6 @@ class SupportLibWebMessagePortAdapter implements WebMessagePortBoundaryInterface
}, handler);
}
/**
* Utility class for creating a WebMessageBoundaryInterface (this is necessary to pass a
* WebMessage back across the boundary).
*/
private static class SupportLibWebMessageAdapter implements WebMessageBoundaryInterface {
private String mData;
private MessagePort[] mPorts;
SupportLibWebMessageAdapter(String data, MessagePort[] ports) {
mData = data;
mPorts = ports;
}
@Override
public String getData() {
return mData;
}
@Override
public /* WebMessagePort */ InvocationHandler[] getPorts() {
return SupportLibWebMessagePortAdapter.fromMessagePorts(mPorts);
}
@Override
public String[] getSupportedFeatures() {
// getData() and getPorts() are not covered by feature flags.
return new String[0];
}
}
public static /* WebMessagePort */ InvocationHandler[] fromMessagePorts(
MessagePort[] messagePorts) {
if (messagePorts == null) return null;
......
......@@ -6,10 +6,12 @@ package org.chromium.support_lib_glue;
import android.net.Uri;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.android.webview.chromium.SharedWebViewChromium;
import com.android.webview.chromium.SharedWebViewRendererClientAdapter;
import com.android.webview.chromium.WebkitToSharedGlueConverter;
import org.chromium.android_webview.AwContents;
import org.chromium.support_lib_boundary.VisualStateCallbackBoundaryInterface;
......@@ -26,10 +28,12 @@ import java.lang.reflect.InvocationHandler;
* WebViewCompat call. Do not store state here.
*/
class SupportLibWebViewChromium implements WebViewProviderBoundaryInterface {
private final WebView mWebView;
private final SharedWebViewChromium mSharedWebViewChromium;
public SupportLibWebViewChromium(SharedWebViewChromium sharedWebViewChromium) {
mSharedWebViewChromium = sharedWebViewChromium;
public SupportLibWebViewChromium(WebView webView) {
mWebView = webView;
mSharedWebViewChromium = WebkitToSharedGlueConverter.getSharedWebViewChromium(webView);
}
@Override
......@@ -65,6 +69,18 @@ class SupportLibWebViewChromium implements WebViewProviderBoundaryInterface {
messageBoundaryInterface.getPorts()));
}
@Override
public void addWebMessageListener(String jsObjectName, String[] allowedOriginRules,
/* WebMessageListener */ InvocationHandler listener) {
mSharedWebViewChromium.addWebMessageListener(jsObjectName, allowedOriginRules,
new SupportLibWebMessageListenerAdapter(mWebView, listener));
}
@Override
public void removeWebMessageListener(final String jsObjectName) {
mSharedWebViewChromium.removeWebMessageListener(jsObjectName);
}
@Override
public WebViewClient getWebViewClient() {
return mSharedWebViewChromium.getWebViewClient();
......
......@@ -73,6 +73,7 @@ class SupportLibWebViewChromiumFactory implements WebViewProviderFactoryBoundary
Features.MULTI_PROCESS_QUERY,
Features.FORCE_DARK,
Features.FORCE_DARK_BEHAVIOR + Features.DEV_SUFFIX,
Features.WEB_MESSAGE_LISTENER + Features.DEV_SUFFIX,
};
// clang-format on
......@@ -89,10 +90,9 @@ class SupportLibWebViewChromiumFactory implements WebViewProviderFactoryBoundary
}
@Override
public InvocationHandler createWebView(WebView webview) {
public /* WebViewProvider */ InvocationHandler createWebView(WebView webView) {
return BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
new SupportLibWebViewChromium(
WebkitToSharedGlueConverter.getSharedWebViewChromium(webview)));
new SupportLibWebViewChromium(webView));
}
@Override
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment