0

[WebView] Rename to DocumentStartJavaScript

Per Java style guide https://google.github.io/styleguide/javaguide.html#s5.3-camel-case,
Javascript is better to be JavaScript.

We will need to do a 3-way patching for the AndroidX boundary interface,
so adding the addDocumentStartJavaScript() first in this CL, we need to
remove the original addDocumentStartJavascript() after aosp/1329732
landed.

We also upgrade the feature version to avoiding old AndroidX calling
newer WebView or vice versa.

Bug: 918065
Test: Covered by existing tests
Change-Id: I1f5eee713d521ac5f480eee670f5f5be674de553
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2240283
Reviewed-by: Richard Coles <torne@chromium.org>
Reviewed-by: Changwan Ryu <changwan@chromium.org>
Reviewed-by: Robert Sesek <rsesek@chromium.org>
Reviewed-by: Scott Violet <sky@chromium.org>
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#777493}
This commit is contained in:
Shimi Zhang
2020-06-11 21:22:06 +00:00
committed by Commit Bot
parent 5bc2e02253
commit cfdcb6f24a
14 changed files with 82 additions and 69 deletions
android_webview
browser
glue
java
src
com
android
java
src
org
javatests
src
org
chromium
android_webview
support_library
boundary_interfaces
src
org
java
src
org
chromium
components/js_injection

@@ -1323,7 +1323,7 @@ JsJavaConfiguratorHost* AwContents::GetJsJavaConfiguratorHost() {
return js_java_configurator_host_.get(); return js_java_configurator_host_.get();
} }
jint AwContents::AddDocumentStartJavascript( jint AwContents::AddDocumentStartJavaScript(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj, const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& script, const base::android::JavaParamRef<jstring>& script,
@@ -1331,7 +1331,7 @@ jint AwContents::AddDocumentStartJavascript(
std::vector<std::string> native_allowed_origin_rule_strings; std::vector<std::string> native_allowed_origin_rule_strings;
AppendJavaStringArrayToStringVector(env, allowed_origin_rules, AppendJavaStringArrayToStringVector(env, allowed_origin_rules,
&native_allowed_origin_rule_strings); &native_allowed_origin_rule_strings);
auto result = GetJsJavaConfiguratorHost()->AddDocumentStartJavascript( auto result = GetJsJavaConfiguratorHost()->AddDocumentStartJavaScript(
base::android::ConvertJavaStringToUTF16(env, script), base::android::ConvertJavaStringToUTF16(env, script),
native_allowed_origin_rule_strings); native_allowed_origin_rule_strings);
if (result.error_message) { if (result.error_message) {
@@ -1343,11 +1343,11 @@ jint AwContents::AddDocumentStartJavascript(
return result.script_id.value(); return result.script_id.value();
} }
void AwContents::RemoveDocumentStartJavascript( void AwContents::RemoveDocumentStartJavaScript(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj, const base::android::JavaParamRef<jobject>& obj,
jint script_id) { jint script_id) {
GetJsJavaConfiguratorHost()->RemoveDocumentStartJavascript(script_id); GetJsJavaConfiguratorHost()->RemoveDocumentStartJavaScript(script_id);
} }
base::android::ScopedJavaLocalRef<jstring> AwContents::AddWebMessageListener( base::android::ScopedJavaLocalRef<jstring> AwContents::AddWebMessageListener(

@@ -223,13 +223,13 @@ class AwContents : public FindHelper::Listener,
js_injection::JsJavaConfiguratorHost* GetJsJavaConfiguratorHost(); js_injection::JsJavaConfiguratorHost* GetJsJavaConfiguratorHost();
jint AddDocumentStartJavascript( jint AddDocumentStartJavaScript(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj, const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& script, const base::android::JavaParamRef<jstring>& script,
const base::android::JavaParamRef<jobjectArray>& allowed_origin_rules); const base::android::JavaParamRef<jobjectArray>& allowed_origin_rules);
void RemoveDocumentStartJavascript( void RemoveDocumentStartJavaScript(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj, const base::android::JavaParamRef<jobject>& obj,
jint script_id); jint script_id);

@@ -137,13 +137,13 @@ public class SharedWebViewChromium {
mAwContents.removeWebMessageListener(jsObjectName); mAwContents.removeWebMessageListener(jsObjectName);
} }
public ScriptReference addDocumentStartJavascript( public ScriptReference addDocumentStartJavaScript(
final String script, final String[] allowedOriginRules) { final String script, final String[] allowedOriginRules) {
if (checkNeedsPost()) { if (checkNeedsPost()) {
return mRunQueue.runOnUiThreadBlocking( return mRunQueue.runOnUiThreadBlocking(
() -> addDocumentStartJavascript(script, allowedOriginRules)); () -> addDocumentStartJavaScript(script, allowedOriginRules));
} }
return mAwContents.addDocumentStartJavascript(script, allowedOriginRules); return mAwContents.addDocumentStartJavaScript(script, allowedOriginRules);
} }
public void setWebViewRendererClientAdapter( public void setWebViewRendererClientAdapter(

@@ -2515,7 +2515,7 @@ public class AwContents implements SmartClipProvider {
* jsObjectName and allowedOriginRules is {@code null}. * jsObjectName and allowedOriginRules is {@code null}.
* @return A {@link ScriptReference} for removing the script. * @return A {@link ScriptReference} for removing the script.
*/ */
public ScriptReference addDocumentStartJavascript( public ScriptReference addDocumentStartJavaScript(
@NonNull String script, @NonNull String[] allowedOriginRules) { @NonNull String script, @NonNull String[] allowedOriginRules) {
if (script == null) { if (script == null) {
throw new IllegalArgumentException("script shouldn't be null."); throw new IllegalArgumentException("script shouldn't be null.");
@@ -2529,12 +2529,12 @@ public class AwContents implements SmartClipProvider {
} }
return new ScriptReference(AwContents.this, return new ScriptReference(AwContents.this,
AwContentsJni.get().addDocumentStartJavascript( AwContentsJni.get().addDocumentStartJavaScript(
mNativeAwContents, AwContents.this, script, allowedOriginRules)); mNativeAwContents, AwContents.this, script, allowedOriginRules));
} }
/* package */ void removeDocumentStartJavascript(int scriptId) { /* package */ void removeDocumentStartJavaScript(int scriptId) {
AwContentsJni.get().removeDocumentStartJavascript( AwContentsJni.get().removeDocumentStartJavaScript(
mNativeAwContents, AwContents.this, scriptId); mNativeAwContents, AwContents.this, scriptId);
} }
@@ -4088,9 +4088,9 @@ public class AwContents implements SmartClipProvider {
void grantFileSchemeAccesstoChildProcess(long nativeAwContents, AwContents caller); void grantFileSchemeAccesstoChildProcess(long nativeAwContents, AwContents caller);
void resumeLoadingCreatedPopupWebContents(long nativeAwContents, AwContents caller); void resumeLoadingCreatedPopupWebContents(long nativeAwContents, AwContents caller);
AwRenderProcess getRenderProcess(long nativeAwContents, AwContents caller); AwRenderProcess getRenderProcess(long nativeAwContents, AwContents caller);
int addDocumentStartJavascript(long nativeAwContents, AwContents caller, String script, int addDocumentStartJavaScript(long nativeAwContents, AwContents caller, String script,
String[] allowedOriginRules); String[] allowedOriginRules);
void removeDocumentStartJavascript(long nativeAwContents, AwContents caller, int scriptId); void removeDocumentStartJavaScript(long nativeAwContents, AwContents caller, int scriptId);
String addWebMessageListener(long nativeAwContents, AwContents caller, String addWebMessageListener(long nativeAwContents, AwContents caller,
WebMessageListenerHolder listener, String jsObjectName, String[] allowedOrigins); WebMessageListenerHolder listener, String jsObjectName, String[] allowedOrigins);
void removeWebMessageListener( void removeWebMessageListener(

@@ -27,6 +27,6 @@ public class ScriptReference {
AwContents awContents = mAwContentsRef.get(); AwContents awContents = mAwContentsRef.get();
if (awContents == null) return; if (awContents == null) return;
awContents.removeDocumentStartJavascript(mScriptId); awContents.removeDocumentStartJavaScript(mScriptId);
} }
} }

@@ -1011,7 +1011,7 @@ public class JsJavaInteractionTest {
javascriptInterfaceQueue.add(message); javascriptInterfaceQueue.add(message);
} }
}, "javaBridge"); }, "javaBridge");
addDocumentStartJavascriptOnUiThread( addDocumentStartJavaScriptOnUiThread(
mAwContents, "javaBridge.send('" + HELLO + "');", new String[] {"*"}); mAwContents, "javaBridge.send('" + HELLO + "');", new String[] {"*"});
loadUrlFromPath(HELLO_WORLD_HTML); loadUrlFromPath(HELLO_WORLD_HTML);
@@ -1026,7 +1026,7 @@ public class JsJavaInteractionTest {
@Feature({"AndroidWebView", "JsJavaInteraction"}) @Feature({"AndroidWebView", "JsJavaInteraction"})
public void testDocumentStartJavaScript_jsObjectShouldBeAvaliable() throws Throwable { public void testDocumentStartJavaScript_jsObjectShouldBeAvaliable() throws Throwable {
addWebMessageListenerOnUiThread(mAwContents, JS_OBJECT_NAME, new String[] {"*"}, mListener); addWebMessageListenerOnUiThread(mAwContents, JS_OBJECT_NAME, new String[] {"*"}, mListener);
addDocumentStartJavascriptOnUiThread( addDocumentStartJavaScriptOnUiThread(
mAwContents, JS_OBJECT_NAME + ".postMessage('" + HELLO + "');", new String[] {"*"}); mAwContents, JS_OBJECT_NAME + ".postMessage('" + HELLO + "');", new String[] {"*"});
String url = loadUrlFromPath(HELLO_WORLD_HTML); String url = loadUrlFromPath(HELLO_WORLD_HTML);
@@ -1045,7 +1045,7 @@ public class JsJavaInteractionTest {
@MediumTest @MediumTest
@Feature({"AndroidWebView", "JsJavaInteraction"}) @Feature({"AndroidWebView", "JsJavaInteraction"})
public void testDocumentStartJavaScript_runBeforeUserScript() throws Throwable { public void testDocumentStartJavaScript_runBeforeUserScript() throws Throwable {
addDocumentStartJavascriptOnUiThread(mAwContents, addDocumentStartJavaScriptOnUiThread(mAwContents,
JS_OBJECT_NAME + ".postMessage('" + HELLO + "1');", new String[] {"*"}); JS_OBJECT_NAME + ".postMessage('" + HELLO + "1');", new String[] {"*"});
addWebMessageListenerOnUiThread(mAwContents, JS_OBJECT_NAME, new String[] {"*"}, mListener); addWebMessageListenerOnUiThread(mAwContents, JS_OBJECT_NAME, new String[] {"*"}, mListener);
@@ -1071,9 +1071,9 @@ public class JsJavaInteractionTest {
@Feature({"AndroidWebView", "JsJavaInteraction"}) @Feature({"AndroidWebView", "JsJavaInteraction"})
public void testDocumentStartJavaScript_multipleScripts() throws Throwable { public void testDocumentStartJavaScript_multipleScripts() throws Throwable {
addWebMessageListenerOnUiThread(mAwContents, JS_OBJECT_NAME, new String[] {"*"}, mListener); addWebMessageListenerOnUiThread(mAwContents, JS_OBJECT_NAME, new String[] {"*"}, mListener);
addDocumentStartJavascriptOnUiThread(mAwContents, addDocumentStartJavaScriptOnUiThread(mAwContents,
JS_OBJECT_NAME + ".postMessage('" + HELLO + "0');", new String[] {"*"}); JS_OBJECT_NAME + ".postMessage('" + HELLO + "0');", new String[] {"*"});
addDocumentStartJavascriptOnUiThread(mAwContents, addDocumentStartJavaScriptOnUiThread(mAwContents,
JS_OBJECT_NAME + ".postMessage('" + HELLO + "1');", new String[] {"*"}); JS_OBJECT_NAME + ".postMessage('" + HELLO + "1');", new String[] {"*"});
String url = loadUrlFromPath(HELLO_WORLD_HTML); String url = loadUrlFromPath(HELLO_WORLD_HTML);
@@ -1095,12 +1095,12 @@ public class JsJavaInteractionTest {
@Feature({"AndroidWebView", "JsJavaInteraction"}) @Feature({"AndroidWebView", "JsJavaInteraction"})
public void testDocumentStartJavaScript_callAgainAfterPageLoad() throws Throwable { public void testDocumentStartJavaScript_callAgainAfterPageLoad() throws Throwable {
addWebMessageListenerOnUiThread(mAwContents, JS_OBJECT_NAME, new String[] {"*"}, mListener); addWebMessageListenerOnUiThread(mAwContents, JS_OBJECT_NAME, new String[] {"*"}, mListener);
addDocumentStartJavascriptOnUiThread(mAwContents, addDocumentStartJavaScriptOnUiThread(mAwContents,
JS_OBJECT_NAME + ".postMessage('" + HELLO + "0');", new String[] {"*"}); JS_OBJECT_NAME + ".postMessage('" + HELLO + "0');", new String[] {"*"});
String url = loadUrlFromPath(HELLO_WORLD_HTML); String url = loadUrlFromPath(HELLO_WORLD_HTML);
addDocumentStartJavascriptOnUiThread(mAwContents, addDocumentStartJavaScriptOnUiThread(mAwContents,
JS_OBJECT_NAME + ".postMessage('" + HELLO + "1');", new String[] {"*"}); JS_OBJECT_NAME + ".postMessage('" + HELLO + "1');", new String[] {"*"});
TestWebMessageListener.Data data = mListener.waitForOnPostMessage(); TestWebMessageListener.Data data = mListener.waitForOnPostMessage();
@@ -1129,7 +1129,7 @@ public class JsJavaInteractionTest {
throws Throwable { throws Throwable {
// With a standard origin rule. // With a standard origin rule.
final String testObjectName = "test"; final String testObjectName = "test";
addDocumentStartJavascriptOnUiThread(mAwContents, "let " + testObjectName + " = {};", addDocumentStartJavaScriptOnUiThread(mAwContents, "let " + testObjectName + " = {};",
new String[] {"https://www.example.com:443"}); new String[] {"https://www.example.com:443"});
Assert.assertTrue(didScriptRunWhenLoading("https://www.example.com", testObjectName)); Assert.assertTrue(didScriptRunWhenLoading("https://www.example.com", testObjectName));
@@ -1143,7 +1143,7 @@ public class JsJavaInteractionTest {
// Match all the origins. // Match all the origins.
final String testObjectName2 = testObjectName + "2"; final String testObjectName2 = testObjectName + "2";
addDocumentStartJavascriptOnUiThread( addDocumentStartJavaScriptOnUiThread(
mAwContents, "let " + testObjectName2 + " = {};", new String[] {"*"}); mAwContents, "let " + testObjectName2 + " = {};", new String[] {"*"});
Assert.assertTrue(didScriptRunWhenLoading("https://www.example.com", testObjectName2)); Assert.assertTrue(didScriptRunWhenLoading("https://www.example.com", testObjectName2));
@@ -1164,7 +1164,7 @@ public class JsJavaInteractionTest {
// Wrong origin rule. // Wrong origin rule.
final String testObjectName5 = testObjectName + "5"; final String testObjectName5 = testObjectName + "5";
try { try {
addDocumentStartJavascriptOnUiThread(mAwContents, "let " + testObjectName5 + " = {};", addDocumentStartJavaScriptOnUiThread(mAwContents, "let " + testObjectName5 + " = {};",
new String[] {"https://www.example.com/index.html"}); new String[] {"https://www.example.com/index.html"});
Assert.fail("You cannot use a full URL for allowedOriginRules."); Assert.fail("You cannot use a full URL for allowedOriginRules.");
} catch (RuntimeException e) { } catch (RuntimeException e) {
@@ -1185,7 +1185,7 @@ public class JsJavaInteractionTest {
+ "}"; + "}";
// Since we are matching both origins, the script will run in both iframe and main frame, // Since we are matching both origins, the script will run in both iframe and main frame,
// but it will send message in only iframe. // but it will send message in only iframe.
addDocumentStartJavascriptOnUiThread(mAwContents, script, new String[] {"*"}); addDocumentStartJavaScriptOnUiThread(mAwContents, script, new String[] {"*"});
final String frameUrl = mTestServer.getURL(HELLO_WORLD_HTML); final String frameUrl = mTestServer.getURL(HELLO_WORLD_HTML);
final String html = createCrossOriginAccessTestPageHtml(frameUrl); final String html = createCrossOriginAccessTestPageHtml(frameUrl);
@@ -1218,7 +1218,7 @@ public class JsJavaInteractionTest {
// Since we are matching both origins, the script will run in both iframe and main // Since we are matching both origins, the script will run in both iframe and main
// frame, but it will send message in only iframe. // frame, but it will send message in only iframe.
references[i] = references[i] =
addDocumentStartJavascriptOnUiThread(mAwContents, script, new String[] {"*"}); addDocumentStartJavaScriptOnUiThread(mAwContents, script, new String[] {"*"});
} }
final String url = loadUrlFromPath(HELLO_WORLD_HTML); final String url = loadUrlFromPath(HELLO_WORLD_HTML);
@@ -1252,7 +1252,7 @@ public class JsJavaInteractionTest {
final String script = JS_OBJECT_NAME + ".postMessage('" + HELLO + "');"; final String script = JS_OBJECT_NAME + ".postMessage('" + HELLO + "');";
ScriptReference reference = ScriptReference reference =
addDocumentStartJavascriptOnUiThread(mAwContents, script, new String[] {"*"}); addDocumentStartJavaScriptOnUiThread(mAwContents, script, new String[] {"*"});
final String url = loadUrlFromPath(HELLO_WORLD_HTML); final String url = loadUrlFromPath(HELLO_WORLD_HTML);
@@ -1334,10 +1334,10 @@ public class JsJavaInteractionTest {
+ "</body></html>"; + "</body></html>";
} }
private static ScriptReference addDocumentStartJavascriptOnUiThread( private static ScriptReference addDocumentStartJavaScriptOnUiThread(
final AwContents awContents, final String script, final String[] allowedOriginRules) { final AwContents awContents, final String script, final String[] allowedOriginRules) {
return TestThreadUtils.runOnUiThreadBlockingNoException( return TestThreadUtils.runOnUiThreadBlockingNoException(
() -> awContents.addDocumentStartJavascript(script, allowedOriginRules)); () -> awContents.addDocumentStartJavaScript(script, allowedOriginRules));
} }
private static void addWebMessageListenerOnUiThread(final AwContents awContents, private static void addWebMessageListenerOnUiThread(final AwContents awContents,

@@ -20,8 +20,11 @@ public interface WebViewProviderBoundaryInterface {
void addWebMessageListener(String jsObjectName, String[] allowedOriginRules, void addWebMessageListener(String jsObjectName, String[] allowedOriginRules,
/* WebMessageListener */ InvocationHandler listener); /* WebMessageListener */ InvocationHandler listener);
void removeWebMessageListener(String jsObjectName); void removeWebMessageListener(String jsObjectName);
// TODO(ctzsm): Remove the following interface after AndroidX patch landed.
/* ScriptReference */ InvocationHandler addDocumentStartJavascript( /* ScriptReference */ InvocationHandler addDocumentStartJavascript(
String script, String[] allowedOriginRules); String script, String[] allowedOriginRules);
/* ScriptReference */ InvocationHandler addDocumentStartJavaScript(
String script, String[] allowedOriginRules);
WebViewClient getWebViewClient(); WebViewClient getWebViewClient();
WebChromeClient getWebChromeClient(); WebChromeClient getWebChromeClient();
/* WebViewRenderer */ InvocationHandler getWebViewRenderer(); /* WebViewRenderer */ InvocationHandler getWebViewRenderer();

@@ -178,5 +178,5 @@ public class Features {
public static final String SET_SUPPORT_LIBRARY_VERSION = "SET_SUPPORT_LIBRARY_VERSION"; public static final String SET_SUPPORT_LIBRARY_VERSION = "SET_SUPPORT_LIBRARY_VERSION";
// WebViewCompat.addDocumentStartJavascript // WebViewCompat.addDocumentStartJavascript
public static final String DOCUMENT_START_SCRIPT = "DOCUMENT_START_SCRIPT"; public static final String DOCUMENT_START_SCRIPT = "DOCUMENT_START_SCRIPT:1";
} }

@@ -88,13 +88,23 @@ class SupportLibWebViewChromium implements WebViewProviderBoundaryInterface {
mSharedWebViewChromium.removeWebMessageListener(jsObjectName); mSharedWebViewChromium.removeWebMessageListener(jsObjectName);
} }
@Override
public /* ScriptReference */ InvocationHandler addDocumentStartJavaScript(
final String script, final String[] allowedOriginRules) {
recordApiCall(ApiCall.ADD_DOCUMENT_START_SCRIPT);
return BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
new SupportLibScriptReferenceAdapter(
mSharedWebViewChromium.addDocumentStartJavaScript(
script, allowedOriginRules)));
}
@Override @Override
public /* ScriptReference */ InvocationHandler addDocumentStartJavascript( public /* ScriptReference */ InvocationHandler addDocumentStartJavascript(
final String script, final String[] allowedOriginRules) { final String script, final String[] allowedOriginRules) {
recordApiCall(ApiCall.ADD_DOCUMENT_START_SCRIPT); recordApiCall(ApiCall.ADD_DOCUMENT_START_SCRIPT);
return BoundaryInterfaceReflectionUtil.createInvocationHandlerFor( return BoundaryInterfaceReflectionUtil.createInvocationHandlerFor(
new SupportLibScriptReferenceAdapter( new SupportLibScriptReferenceAdapter(
mSharedWebViewChromium.addDocumentStartJavascript( mSharedWebViewChromium.addDocumentStartJavaScript(
script, allowedOriginRules))); script, allowedOriginRules)));
} }

@@ -46,18 +46,18 @@ struct JsObject {
std::unique_ptr<WebMessageHostFactory> factory; std::unique_ptr<WebMessageHostFactory> factory;
}; };
struct DocumentStartJavascript { struct DocumentStartJavaScript {
DocumentStartJavascript(base::string16 script, DocumentStartJavaScript(base::string16 script,
AwOriginMatcher allowed_origin_rules, AwOriginMatcher allowed_origin_rules,
int32_t script_id) int32_t script_id)
: script_(std::move(script)), : script_(std::move(script)),
allowed_origin_rules_(allowed_origin_rules), allowed_origin_rules_(allowed_origin_rules),
script_id_(script_id) {} script_id_(script_id) {}
DocumentStartJavascript(DocumentStartJavascript&) = delete; DocumentStartJavaScript(DocumentStartJavaScript&) = delete;
DocumentStartJavascript& operator=(DocumentStartJavascript&) = delete; DocumentStartJavaScript& operator=(DocumentStartJavaScript&) = delete;
DocumentStartJavascript(DocumentStartJavascript&&) = default; DocumentStartJavaScript(DocumentStartJavaScript&&) = default;
DocumentStartJavascript& operator=(DocumentStartJavascript&&) = default; DocumentStartJavaScript& operator=(DocumentStartJavaScript&&) = default;
base::string16 script_; base::string16 script_;
AwOriginMatcher allowed_origin_rules_; AwOriginMatcher allowed_origin_rules_;
@@ -79,7 +79,7 @@ JsJavaConfiguratorHost::JsJavaConfiguratorHost(
JsJavaConfiguratorHost::~JsJavaConfiguratorHost() = default; JsJavaConfiguratorHost::~JsJavaConfiguratorHost() = default;
JsJavaConfiguratorHost::AddScriptResult JsJavaConfiguratorHost::AddScriptResult
JsJavaConfiguratorHost::AddDocumentStartJavascript( JsJavaConfiguratorHost::AddDocumentStartJavaScript(
const base::string16& script, const base::string16& script,
const std::vector<std::string>& allowed_origin_rules) { const std::vector<std::string>& allowed_origin_rules) {
AwOriginMatcher origin_matcher; AwOriginMatcher origin_matcher;
@@ -94,18 +94,18 @@ JsJavaConfiguratorHost::AddDocumentStartJavascript(
scripts_.emplace_back(script, origin_matcher, next_script_id_++); scripts_.emplace_back(script, origin_matcher, next_script_id_++);
web_contents()->ForEachFrame(base::BindRepeating( web_contents()->ForEachFrame(base::BindRepeating(
&JsJavaConfiguratorHost::NotifyFrameForAddDocumentStartJavascript, &JsJavaConfiguratorHost::NotifyFrameForAddDocumentStartJavaScript,
base::Unretained(this), &*scripts_.rbegin())); base::Unretained(this), &*scripts_.rbegin()));
result.script_id = scripts_.rbegin()->script_id_; result.script_id = scripts_.rbegin()->script_id_;
return result; return result;
} }
bool JsJavaConfiguratorHost::RemoveDocumentStartJavascript(int script_id) { bool JsJavaConfiguratorHost::RemoveDocumentStartJavaScript(int script_id) {
for (auto it = scripts_.begin(); it != scripts_.end(); ++it) { for (auto it = scripts_.begin(); it != scripts_.end(); ++it) {
if (it->script_id_ == script_id) { if (it->script_id_ == script_id) {
scripts_.erase(it); scripts_.erase(it);
web_contents()->ForEachFrame(base::BindRepeating( web_contents()->ForEachFrame(base::BindRepeating(
&JsJavaConfiguratorHost::NotifyFrameForRemoveDocumentStartJavascript, &JsJavaConfiguratorHost::NotifyFrameForRemoveDocumentStartJavaScript,
base::Unretained(this), script_id)); base::Unretained(this), script_id));
return true; return true;
} }
@@ -168,7 +168,7 @@ JsJavaConfiguratorHost::GetWebMessageHostFactories() {
void JsJavaConfiguratorHost::RenderFrameCreated( void JsJavaConfiguratorHost::RenderFrameCreated(
content::RenderFrameHost* render_frame_host) { content::RenderFrameHost* render_frame_host) {
NotifyFrameForWebMessageListener(render_frame_host); NotifyFrameForWebMessageListener(render_frame_host);
NotifyFrameForAllDocumentStartJavascripts(render_frame_host); NotifyFrameForAllDocumentStartJavaScripts(render_frame_host);
} }
void JsJavaConfiguratorHost::RenderFrameDeleted( void JsJavaConfiguratorHost::RenderFrameDeleted(
@@ -176,10 +176,10 @@ void JsJavaConfiguratorHost::RenderFrameDeleted(
js_to_java_messagings_.erase(render_frame_host); js_to_java_messagings_.erase(render_frame_host);
} }
void JsJavaConfiguratorHost::NotifyFrameForAllDocumentStartJavascripts( void JsJavaConfiguratorHost::NotifyFrameForAllDocumentStartJavaScripts(
content::RenderFrameHost* render_frame_host) { content::RenderFrameHost* render_frame_host) {
for (const auto& script : scripts_) { for (const auto& script : scripts_) {
NotifyFrameForAddDocumentStartJavascript(&script, render_frame_host); NotifyFrameForAddDocumentStartJavaScript(&script, render_frame_host);
} }
} }
@@ -204,19 +204,19 @@ void JsJavaConfiguratorHost::NotifyFrameForWebMessageListener(
configurator_remote->SetJsObjects(std::move(js_objects)); configurator_remote->SetJsObjects(std::move(js_objects));
} }
void JsJavaConfiguratorHost::NotifyFrameForAddDocumentStartJavascript( void JsJavaConfiguratorHost::NotifyFrameForAddDocumentStartJavaScript(
const DocumentStartJavascript* script, const DocumentStartJavaScript* script,
content::RenderFrameHost* render_frame_host) { content::RenderFrameHost* render_frame_host) {
DCHECK(script); DCHECK(script);
mojo::AssociatedRemote<mojom::JsJavaConfigurator> configurator_remote; mojo::AssociatedRemote<mojom::JsJavaConfigurator> configurator_remote;
render_frame_host->GetRemoteAssociatedInterfaces()->GetInterface( render_frame_host->GetRemoteAssociatedInterfaces()->GetInterface(
&configurator_remote); &configurator_remote);
configurator_remote->AddDocumentStartScript( configurator_remote->AddDocumentStartScript(
mojom::DocumentStartJavascript::New(script->script_id_, script->script_, mojom::DocumentStartJavaScript::New(script->script_id_, script->script_,
script->allowed_origin_rules_)); script->allowed_origin_rules_));
} }
void JsJavaConfiguratorHost::NotifyFrameForRemoveDocumentStartJavascript( void JsJavaConfiguratorHost::NotifyFrameForRemoveDocumentStartJavaScript(
int32_t script_id, int32_t script_id,
content::RenderFrameHost* render_frame_host) { content::RenderFrameHost* render_frame_host) {
mojo::AssociatedRemote<mojom::JsJavaConfigurator> configurator_remote; mojo::AssociatedRemote<mojom::JsJavaConfigurator> configurator_remote;

@@ -20,7 +20,7 @@ class RenderFrameHost;
namespace js_injection { namespace js_injection {
class AwOriginMatcher; class AwOriginMatcher;
struct DocumentStartJavascript; struct DocumentStartJavaScript;
struct JsObject; struct JsObject;
class JsToJavaMessaging; class JsToJavaMessaging;
class WebMessageHostFactory; class WebMessageHostFactory;
@@ -48,13 +48,13 @@ class JsJavaConfiguratorHost : public content::WebContentsObserver {
base::Optional<int> script_id; base::Optional<int> script_id;
}; };
// Native side AddDocumentStartJavascript, returns an error message if the // Native side AddDocumentStartJavaScript, returns an error message if the
// parameters didn't pass necessary checks. // parameters didn't pass necessary checks.
AddScriptResult AddDocumentStartJavascript( AddScriptResult AddDocumentStartJavaScript(
const base::string16& script, const base::string16& script,
const std::vector<std::string>& allowed_origin_rules); const std::vector<std::string>& allowed_origin_rules);
bool RemoveDocumentStartJavascript(int script_id); bool RemoveDocumentStartJavaScript(int script_id);
// Adds a new WebMessageHostFactory. For any urls that match // Adds a new WebMessageHostFactory. For any urls that match
// |allowed_origin_rules|, |js_object_name| is registered as a JS object that // |allowed_origin_rules|, |js_object_name| is registered as a JS object that
@@ -85,18 +85,18 @@ class JsJavaConfiguratorHost : public content::WebContentsObserver {
private: private:
void NotifyFrameForWebMessageListener( void NotifyFrameForWebMessageListener(
content::RenderFrameHost* render_frame_host); content::RenderFrameHost* render_frame_host);
void NotifyFrameForAllDocumentStartJavascripts( void NotifyFrameForAllDocumentStartJavaScripts(
content::RenderFrameHost* render_frame_host); content::RenderFrameHost* render_frame_host);
void NotifyFrameForAddDocumentStartJavascript( void NotifyFrameForAddDocumentStartJavaScript(
const DocumentStartJavascript* script, const DocumentStartJavaScript* script,
content::RenderFrameHost* render_frame_host); content::RenderFrameHost* render_frame_host);
void NotifyFrameForRemoveDocumentStartJavascript( void NotifyFrameForRemoveDocumentStartJavaScript(
int32_t script_id, int32_t script_id,
content::RenderFrameHost* render_frame_host); content::RenderFrameHost* render_frame_host);
int32_t next_script_id_ = 0; int32_t next_script_id_ = 0;
std::vector<DocumentStartJavascript> scripts_; std::vector<DocumentStartJavaScript> scripts_;
std::vector<std::unique_ptr<JsObject>> js_objects_; std::vector<std::unique_ptr<JsObject>> js_objects_;
std::map<content::RenderFrameHost*, std::map<content::RenderFrameHost*,
std::vector<std::unique_ptr<JsToJavaMessaging>>> std::vector<std::unique_ptr<JsToJavaMessaging>>>

@@ -19,10 +19,10 @@ struct JsObject {
js_injection.mojom.AwOriginMatcher origin_matcher; js_injection.mojom.AwOriginMatcher origin_matcher;
}; };
// DocumentStartJavascript struct contains the JavaScript snippet |script| and // DocumentStartJavaScript struct contains the JavaScript snippet |script| and
// the corresponding |origin_matcher|. We will run the script if the frame's // the corresponding |origin_matcher|. We will run the script if the frame's
// origin matches any rules in the |origin_matcher|. // origin matches any rules in the |origin_matcher|.
struct DocumentStartJavascript { struct DocumentStartJavaScript {
int32 script_id; int32 script_id;
mojo_base.mojom.String16 script; mojo_base.mojom.String16 script;
js_injection.mojom.AwOriginMatcher origin_matcher; js_injection.mojom.AwOriginMatcher origin_matcher;
@@ -58,7 +58,7 @@ interface JsJavaConfigurator {
// Called from browser, to add a script for a frame to run at document start // Called from browser, to add a script for a frame to run at document start
// stage. The script will run only if the frame's origin matches any of the // stage. The script will run only if the frame's origin matches any of the
// allowed_origin_rules. // allowed_origin_rules.
AddDocumentStartScript(js_injection.mojom.DocumentStartJavascript script); AddDocumentStartScript(js_injection.mojom.DocumentStartJavaScript script);
// Called from browser, to remove the script by the given script_id. // Called from browser, to remove the script by the given script_id.
RemoveDocumentStartScript(int32 script_id); RemoveDocumentStartScript(int32 script_id);

@@ -23,7 +23,7 @@ struct JsJavaConfigurator::JsObjectInfo {
mojo::AssociatedRemote<mojom::JsToJavaMessaging> js_to_java_messaging; mojo::AssociatedRemote<mojom::JsToJavaMessaging> js_to_java_messaging;
}; };
struct JsJavaConfigurator::DocumentStartJavascript { struct JsJavaConfigurator::DocumentStartJavaScript {
AwOriginMatcher origin_matcher; AwOriginMatcher origin_matcher;
blink::WebString script; blink::WebString script;
int32_t script_id; int32_t script_id;
@@ -55,11 +55,11 @@ void JsJavaConfigurator::SetJsObjects(
} }
void JsJavaConfigurator::AddDocumentStartScript( void JsJavaConfigurator::AddDocumentStartScript(
mojom::DocumentStartJavascriptPtr script_ptr) { mojom::DocumentStartJavaScriptPtr script_ptr) {
DocumentStartJavascript* script = new DocumentStartJavascript{ DocumentStartJavaScript* script = new DocumentStartJavaScript{
script_ptr->origin_matcher, script_ptr->origin_matcher,
blink::WebString::FromUTF16(script_ptr->script), script_ptr->script_id}; blink::WebString::FromUTF16(script_ptr->script), script_ptr->script_id};
scripts_.push_back(std::unique_ptr<DocumentStartJavascript>(script)); scripts_.push_back(std::unique_ptr<DocumentStartJavaScript>(script));
} }
void JsJavaConfigurator::RemoveDocumentStartScript(int32_t script_id) { void JsJavaConfigurator::RemoveDocumentStartScript(int32_t script_id) {

@@ -34,7 +34,7 @@ class JsJavaConfigurator
// mojom::JsJavaConfigurator implementation // mojom::JsJavaConfigurator implementation
void SetJsObjects(std::vector<mojom::JsObjectPtr> js_object_ptrs) override; void SetJsObjects(std::vector<mojom::JsObjectPtr> js_object_ptrs) override;
void AddDocumentStartScript( void AddDocumentStartScript(
mojom::DocumentStartJavascriptPtr script_ptr) override; mojom::DocumentStartJavaScriptPtr script_ptr) override;
void RemoveDocumentStartScript(int32_t script_id) override; void RemoveDocumentStartScript(int32_t script_id) override;
// RenderFrameObserver implementation // RenderFrameObserver implementation
@@ -50,7 +50,7 @@ class JsJavaConfigurator
private: private:
struct JsObjectInfo; struct JsObjectInfo;
struct DocumentStartJavascript; struct DocumentStartJavaScript;
void BindPendingReceiver( void BindPendingReceiver(
mojo::PendingAssociatedReceiver<mojom::JsJavaConfigurator> mojo::PendingAssociatedReceiver<mojom::JsJavaConfigurator>
@@ -63,7 +63,7 @@ class JsJavaConfigurator
// to prevent doing multiple injection in that case. // to prevent doing multiple injection in that case.
bool inside_did_clear_window_object_ = false; bool inside_did_clear_window_object_ = false;
std::vector<std::unique_ptr<DocumentStartJavascript>> scripts_; std::vector<std::unique_ptr<DocumentStartJavaScript>> scripts_;
std::vector<std::unique_ptr<JsBinding>> js_bindings_; std::vector<std::unique_ptr<JsBinding>> js_bindings_;
// Associated with legacy IPC channel. // Associated with legacy IPC channel.