Commit ebb3e071 authored by Shimi Zhang's avatar Shimi Zhang Committed by Commit Bot

[JJI] Reorder and rename the interfaces

Per AndroidX API design guide, put single abstract method (SAM) to be
the last parameter so kotlin could interop with them.

Rename removeWebMessageListener() to unsetWebMessageListener().

Bug: 918065
Change-Id: Ief2d7fa80a4cd48185487a39b0463511cc9c6311
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1764260Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Reviewed-by: default avatarChangwan Ryu <changwan@chromium.org>
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#689604}
parent eb3b2ec0
...@@ -1309,14 +1309,14 @@ void AwContents::OnPostMessage( ...@@ -1309,14 +1309,14 @@ void AwContents::OnPostMessage(
const base::android::JavaRef<jstring>& message, const base::android::JavaRef<jstring>& message,
const base::android::JavaRef<jstring>& origin, const base::android::JavaRef<jstring>& origin,
jboolean is_main_frame, jboolean is_main_frame,
const base::android::JavaRef<jobject>& reply_proxy, const base::android::JavaRef<jintArray>& ports,
const base::android::JavaRef<jintArray>& ports) { const base::android::JavaRef<jobject>& reply_proxy) {
const ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); const ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) if (obj.is_null())
return; return;
Java_AwContents_onPostMessage(env, obj, message, origin, is_main_frame, Java_AwContents_onPostMessage(env, obj, message, origin, is_main_frame, ports,
reply_proxy, ports); reply_proxy);
} }
void AwContents::ClearView(JNIEnv* env, const JavaParamRef<jobject>& obj) { void AwContents::ClearView(JNIEnv* env, const JavaParamRef<jobject>& obj) {
......
...@@ -231,8 +231,8 @@ class AwContents : public FindHelper::Listener, ...@@ -231,8 +231,8 @@ class AwContents : public FindHelper::Listener,
const base::android::JavaRef<jstring>& message, const base::android::JavaRef<jstring>& message,
const base::android::JavaRef<jstring>& origin, const base::android::JavaRef<jstring>& origin,
jboolean is_main_frame, jboolean is_main_frame,
const base::android::JavaRef<jobject>& reply_proxy, const base::android::JavaRef<jintArray>& ports,
const base::android::JavaRef<jintArray>& ports); const base::android::JavaRef<jobject>& reply_proxy);
bool GetViewTreeForceDarkState() { return view_tree_force_dark_state_; } bool GetViewTreeForceDarkState() { return view_tree_force_dark_state_; }
......
...@@ -55,8 +55,8 @@ void JsApiHandler::PostMessage( ...@@ -55,8 +55,8 @@ void JsApiHandler::PostMessage(
env, base::android::ConvertUTF16ToJavaString(env, message), env, base::android::ConvertUTF16ToJavaString(env, message),
base::android::ConvertUTF8ToJavaString(env, source_origin.Serialize()), base::android::ConvertUTF8ToJavaString(env, source_origin.Serialize()),
web_contents->GetMainFrame() == render_frame_host_, web_contents->GetMainFrame() == render_frame_host_,
reply_proxy_->GetJavaPeer(), base::android::ToJavaIntArray(env, int_ports.data(), int_ports.size()),
base::android::ToJavaIntArray(env, int_ports.data(), int_ports.size())); reply_proxy_->GetJavaPeer());
} }
void JsApiHandler::SetJavaToJsMessaging( void JsApiHandler::SetJavaToJsMessaging(
......
...@@ -2466,19 +2466,19 @@ public class AwContents implements SmartClipProvider { ...@@ -2466,19 +2466,19 @@ public class AwContents implements SmartClipProvider {
* new {@code listener} receives messages immediately if the message's origin also matches the * new {@code listener} receives messages immediately if the message's origin also matches the
* new {@code allowedOriginRules}. {@code jsObjectName} will take effect since next navigation. * new {@code allowedOriginRules}. {@code jsObjectName} will take effect since next navigation.
* *
* @param listener The {@link WebMessageListener} to be called when received
* onPostMessage().
* @param jsObjectName The name for the injected JavaScript object. * @param jsObjectName The name for the injected JavaScript object.
* @param allowedOrigins A list of matching rules for the allowed origins. * @param allowedOrigins A list of matching rules for the allowed origins.
* The JavaScript object will be injected when the frame's origin matches * The JavaScript object will be injected when the frame's origin matches
* any one of the allowed origins. If a wildcard "*" is provided, it will * any one of the allowed origins. If a wildcard "*" is provided, it will
* inject JavaScript object to all frames. * inject JavaScript object to all frames.
* @param listener The {@link WebMessageListener} to be called when received
* onPostMessage().
* @throws IllegalArgumentException if one of the allowedOriginRules is invalid or one of * @throws IllegalArgumentException if one of the allowedOriginRules is invalid or one of
* listener, jsObjectName and allowedOriginRules is {@code * listener, jsObjectName and allowedOriginRules is {@code
* null}. * null}.
*/ */
public void setWebMessageListener(@NonNull WebMessageListener listener, public void setWebMessageListener(@NonNull String jsObjectName,
@NonNull String jsObjectName, @NonNull String[] allowedOriginRules) { @NonNull String[] allowedOriginRules, @NonNull WebMessageListener listener) {
if (listener == null) { if (listener == null) {
throw new IllegalArgumentException("listener shouldn't be null"); throw new IllegalArgumentException("listener shouldn't be null");
} }
...@@ -2508,7 +2508,7 @@ public class AwContents implements SmartClipProvider { ...@@ -2508,7 +2508,7 @@ public class AwContents implements SmartClipProvider {
* Removes the {@link WebMessageListener} sets by {@link setWebMessageListener}. It then won't * Removes the {@link WebMessageListener} sets by {@link setWebMessageListener}. It then won't
* inject JavaScript object for navigation since next navigation. * inject JavaScript object for navigation since next navigation.
*/ */
public void removeWebMessageListener() { public void unsetWebMessageListener() {
mWebMessageListener = null; mWebMessageListener = null;
nativeSetJsApiService( nativeSetJsApiService(
mNativeAwContents, /* needToInjectJsObject */ false, "", new String[0]); mNativeAwContents, /* needToInjectJsObject */ false, "", new String[0]);
...@@ -2519,8 +2519,8 @@ public class AwContents implements SmartClipProvider { ...@@ -2519,8 +2519,8 @@ public class AwContents implements SmartClipProvider {
* WebMessageListener}. * WebMessageListener}.
*/ */
@CalledByNative @CalledByNative
public void onPostMessage(String message, String sourceOrigin, boolean isMainFrame, public void onPostMessage(String message, String sourceOrigin, boolean isMainFrame, int[] ports,
JsReplyProxy replyProxy, int[] ports) { JsReplyProxy replyProxy) {
if (mWebMessageListener == null) return; if (mWebMessageListener == null) return;
MessagePort[] messagePorts = new MessagePort[ports.length]; MessagePort[] messagePorts = new MessagePort[ports.length];
for (int i = 0; i < ports.length; ++i) { for (int i = 0; i < ports.length; ++i) {
......
...@@ -375,8 +375,7 @@ public class JsJavaInteractionTest { ...@@ -375,8 +375,7 @@ public class JsJavaInteractionTest {
@Test @Test
@SmallTest @SmallTest
@Feature({"AndroidWebView", "JsJavaInterfaction"}) @Feature({"AndroidWebView", "JsJavaInterfaction"})
public void testRemoveWebMessageListenerCouldPreventInjectionForNextPageLoad() public void testUnsetWebMessageListenerCouldPreventInjectionForNextPageLoad() throws Throwable {
throws Throwable {
setWebMessageListenerOnUiThread(mAwContents, mListener, new String[] {"*"}); setWebMessageListenerOnUiThread(mAwContents, mListener, new String[] {"*"});
// Load the the page. // Load the the page.
...@@ -388,7 +387,7 @@ public class JsJavaInteractionTest { ...@@ -388,7 +387,7 @@ public class JsJavaInteractionTest {
Assert.assertTrue(mListener.hasNoMoreOnPostMessage()); Assert.assertTrue(mListener.hasNoMoreOnPostMessage());
// Remove WebMessageListener will disable injection for next page load. // Remove WebMessageListener will disable injection for next page load.
TestThreadUtils.runOnUiThreadBlocking(() -> mAwContents.removeWebMessageListener()); TestThreadUtils.runOnUiThreadBlocking(() -> mAwContents.unsetWebMessageListener());
loadUrlFromPath(POST_MESSAGE_SIMPLE_HTML); loadUrlFromPath(POST_MESSAGE_SIMPLE_HTML);
...@@ -686,9 +685,10 @@ public class JsJavaInteractionTest { ...@@ -686,9 +685,10 @@ public class JsJavaInteractionTest {
private static void setWebMessageListenerOnUiThread(final AwContents awContents, private static void setWebMessageListenerOnUiThread(final AwContents awContents,
final WebMessageListener listener, final String[] allowedOriginRules) { final WebMessageListener listener, final String[] allowedOriginRules) {
TestThreadUtils.runOnUiThreadBlocking(() TestThreadUtils.runOnUiThreadBlocking(
-> awContents.setWebMessageListener(listener, ()
JS_OBJECT_NAME, allowedOriginRules)); -> awContents.setWebMessageListener(
JS_OBJECT_NAME, allowedOriginRules, listener));
} }
private static boolean hasJavaScriptObject(final String jsObjectName, private static boolean hasJavaScriptObject(final String jsObjectName,
......
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