0

Kill EvalJsWithManualReply

It already exists as an EvalJsOption.

Bug: 1157718
Change-Id: Idb1cbc25eca44eb09dbf5845906005974bad47d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2815622
Owners-Override: Avi Drissman <avi@chromium.org>
Reviewed-by: John Abd-El-Malek <jam@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#871017}
This commit is contained in:
Avi Drissman
2021-04-09 17:23:30 +00:00
committed by Chromium LUCI CQ
parent 022fc996b0
commit 3e40e53511
13 changed files with 79 additions and 89 deletions

@@ -310,7 +310,7 @@ class CookieSettingsTest
// Read a cookie with JavaScript cookie-store API // Read a cookie with JavaScript cookie-store API
std::string JSAsyncReadCookie(Browser* browser) { std::string JSAsyncReadCookie(Browser* browser) {
return content::EvalJsWithManualReply( return content::EvalJs(
browser->tab_strip_model()->GetActiveWebContents(), browser->tab_strip_model()->GetActiveWebContents(),
"async function doGet() {" "async function doGet() {"
" const cookies = await window.cookieStore.getAll();" " const cookies = await window.cookieStore.getAll();"
@@ -319,7 +319,8 @@ class CookieSettingsTest
" cookie_str += `${cookie.name}=${cookie.value};`;" " cookie_str += `${cookie.name}=${cookie.value};`;"
" window.domAutomationController.send(cookie_str);" " window.domAutomationController.send(cookie_str);"
"}" "}"
"doGet()") "doGet()",
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
.ExtractString(); .ExtractString();
} }
@@ -343,17 +344,18 @@ class CookieSettingsTest
// Set a cookie with JavaScript cookie-store api. // Set a cookie with JavaScript cookie-store api.
void JSAsyncWriteCookie(Browser* browser) { void JSAsyncWriteCookie(Browser* browser) {
content::EvalJsResult result = content::EvalJsWithManualReply( content::EvalJsResult result =
browser->tab_strip_model()->GetActiveWebContents(), content::EvalJs(browser->tab_strip_model()->GetActiveWebContents(),
"async function doSet() {" "async function doSet() {"
" await window.cookieStore.set(" " await window.cookieStore.set("
" { name: 'name'," " { name: 'name',"
" value: 'Good'," " value: 'Good',"
" expires: Date.now() + 3600*1000," " expires: Date.now() + 3600*1000,"
" sameSite: 'none' });" " sameSite: 'none' });"
" window.domAutomationController.send(true);" " window.domAutomationController.send(true);"
"}" "}"
"doSet()"); "doSet()",
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY);
// Failure ignored here since some tests purposefully try to set disallowed // Failure ignored here since some tests purposefully try to set disallowed
// cookies. // cookies.
} }

@@ -368,10 +368,9 @@ IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
EXPECT_TRUE(content::WaitForLoadStop(contents)); EXPECT_TRUE(content::WaitForLoadStop(contents));
// Execute in isolated world; where all distiller scripts are run. // Execute in isolated world; where all distiller scripts are run.
EXPECT_EQ(true, content::EvalJsWithManualReply( EXPECT_EQ(true, content::EvalJs(contents, kTestDistillerObject,
contents, kTestDistillerObject, content::EXECUTE_SCRIPT_USE_MANUAL_REPLY,
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS, ISOLATED_WORLD_ID_CHROME_INTERNAL));
ISOLATED_WORLD_ID_CHROME_INTERNAL));
} }
IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,

@@ -553,7 +553,8 @@ IN_PROC_BROWSER_TEST_F(TabStatsTrackerBrowserTest, AddObserverAudibleTab) {
// Start the audio. // Start the audio.
base::RunLoop run_loop; base::RunLoop run_loop;
AudioStartObserver audio_start_observer(web_contents, run_loop.QuitClosure()); AudioStartObserver audio_start_observer(web_contents, run_loop.QuitClosure());
EXPECT_EQ("OK", EvalJsWithManualReply(web_contents, "StartOscillator();")); EXPECT_EQ("OK", content::EvalJs(web_contents, "StartOscillator();",
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY));
run_loop.Run(); run_loop.Run();
// Adding an observer now should receive the OnTabIsAudibleChanged() call. // Adding an observer now should receive the OnTabIsAudibleChanged() call.

@@ -1134,7 +1134,8 @@ IN_PROC_BROWSER_TEST_F(AdsPageLoadMetricsObserverBrowserTest,
"video.onplaying = () => { " "video.onplaying = () => { "
"window.domAutomationController.send('true'); };" "window.domAutomationController.send('true'); };"
"video.play();"; "video.play();";
EXPECT_EQ("true", content::EvalJsWithManualReply(ad_frame, play_script)); EXPECT_EQ("true", content::EvalJs(ad_frame, play_script,
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY));
ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL)); ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL));

@@ -3121,11 +3121,11 @@ IN_PROC_BROWSER_TEST_F(PageLoadMetricsBrowserTest, PortalActivation) {
EXPECT_TRUE( EXPECT_TRUE(
ExecJs(outer_contents, "document.querySelector('portal').activate()")); ExecJs(outer_contents, "document.querySelector('portal').activate()"));
EXPECT_EQ(true, EXPECT_EQ(true, content::EvalJs(portal_contents,
EvalJsWithManualReply(portal_contents,
"activatePromise.then(r => { " "activatePromise.then(r => { "
" window.domAutomationController.send(r);" " window.domAutomationController.send(r);"
"});")); "});",
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY));
// The activated portal contents should be the currently active contents. // The activated portal contents should be the currently active contents.
EXPECT_EQ(portal_contents, EXPECT_EQ(portal_contents,

@@ -72,17 +72,18 @@ class PaymentHandlerChangeShippingAddressOptionTest
IN_PROC_BROWSER_TEST_P(PaymentHandlerChangeShippingAddressOptionTest, Test) { IN_PROC_BROWSER_TEST_P(PaymentHandlerChangeShippingAddressOptionTest, Test) {
EXPECT_EQ("instruments.set(): Payment handler installed.", EXPECT_EQ("instruments.set(): Payment handler installed.",
content::EvalJsWithManualReply( content::EvalJs(
GetActiveWebContents(), GetActiveWebContents(),
"install('change_shipping_" + getTestType() + "_app.js');")); "install('change_shipping_" + getTestType() + "_app.js');",
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY));
EXPECT_TRUE( EXPECT_TRUE(
content::ExecJs(GetActiveWebContents(), GetParam().init_test_code)); content::ExecJs(GetActiveWebContents(), GetParam().init_test_code));
std::string actual_output = std::string actual_output =
content::EvalJsWithManualReply( content::EvalJs(GetActiveWebContents(),
GetActiveWebContents(), "outputChangeShippingAddressOptionReturnValue(request);",
"outputChangeShippingAddressOptionReturnValue(request);") content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
.ExtractString(); .ExtractString();
// The test expectations are hard-coded, but the embedded test server changes // The test expectations are hard-coded, but the embedded test server changes

@@ -883,15 +883,16 @@ IN_PROC_BROWSER_TEST_F(
permissions::PermissionRequestManager::AutoResponseType::ACCEPT_ONCE); permissions::PermissionRequestManager::AutoResponseType::ACCEPT_ONCE);
// Request 'geolocation' permission. // Request 'geolocation' permission.
std::string result = content::EvalJsWithManualReply(GetActiveMainFrame(), std::string result =
kQueryCurrentPosition) content::EvalJs(GetActiveMainFrame(), kQueryCurrentPosition,
.ExtractString(); content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
.ExtractString();
EXPECT_EQ("success", result); EXPECT_EQ("success", result);
EXPECT_EQ(1, bubble_factory()->TotalRequestCount()); EXPECT_EQ(1, bubble_factory()->TotalRequestCount());
// Request 'geolocation' permission. There should not be a 2nd prompt. // Request 'geolocation' permission. There should not be a 2nd prompt.
result = content::EvalJsWithManualReply(GetActiveMainFrame(), result = content::EvalJs(GetActiveMainFrame(), kQueryCurrentPosition,
kQueryCurrentPosition) content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
.ExtractString(); .ExtractString();
EXPECT_EQ("success", result); EXPECT_EQ("success", result);
EXPECT_EQ(1, bubble_factory()->TotalRequestCount()); EXPECT_EQ(1, bubble_factory()->TotalRequestCount());
@@ -909,8 +910,8 @@ IN_PROC_BROWSER_TEST_F(
GetPermissionRequestManager())); GetPermissionRequestManager()));
// Request 'geolocation' permission. // Request 'geolocation' permission.
result = content::EvalJsWithManualReply(GetActiveMainFrame(), result = content::EvalJs(GetActiveMainFrame(), kQueryCurrentPosition,
kQueryCurrentPosition) content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
.ExtractString(); .ExtractString();
EXPECT_EQ("success", result); EXPECT_EQ("success", result);
// There should be no permission prompt. // There should be no permission prompt.
@@ -946,8 +947,8 @@ IN_PROC_BROWSER_TEST_F(
permissions::PermissionRequestManager::AutoResponseType::ACCEPT_ONCE); permissions::PermissionRequestManager::AutoResponseType::ACCEPT_ONCE);
// Request 'geolocation' permission. We should get a prompt. // Request 'geolocation' permission. We should get a prompt.
result = content::EvalJsWithManualReply(GetActiveMainFrame(), result = content::EvalJs(GetActiveMainFrame(), kQueryCurrentPosition,
kQueryCurrentPosition) content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
.ExtractString(); .ExtractString();
EXPECT_EQ("success", result); EXPECT_EQ("success", result);

@@ -164,10 +164,11 @@ IN_PROC_BROWSER_TEST_F(CreateShortcutBrowserTest, WorksAfterDelayedIFrameLoad) {
iframe.srcdoc = 'inner page'; iframe.srcdoc = 'inner page';
document.body.appendChild(iframe); document.body.appendChild(iframe);
)"; )";
EXPECT_EQ(content::EvalJsWithManualReply( EXPECT_EQ(
browser()->tab_strip_model()->GetActiveWebContents(), script) content::EvalJs(browser()->tab_strip_model()->GetActiveWebContents(),
.ExtractString(), script, content::EXECUTE_SCRIPT_USE_MANUAL_REPLY)
"success"); .ExtractString(),
"success");
InstallShortcutAppForCurrentUrl(); InstallShortcutAppForCurrentUrl();
} }

@@ -242,10 +242,8 @@ IN_PROC_BROWSER_TEST_F(SourceUrlRecorderWebContentsObserverBrowserTest,
window.domAutomationController.send(true); window.domAutomationController.send(true);
}, 10); }, 10);
)"; )";
// EvalJsWithManualReply returns an EvalJsResult, whose docs say to use EXPECT_EQ(true, EvalJs(portal_contents, activated_poll,
// EXPECT_EQ(true, ...) rather than EXPECT_TRUE(), as the latter does not content::EXECUTE_SCRIPT_USE_MANUAL_REPLY));
// compile.
EXPECT_EQ(true, EvalJsWithManualReply(portal_contents, activated_poll));
// The activated portal contents should be the currently active contents. // The activated portal contents should be the currently active contents.
EXPECT_EQ(portal_contents, shell()->web_contents()); EXPECT_EQ(portal_contents, shell()->web_contents());

@@ -14759,7 +14759,8 @@ IN_PROC_BROWSER_TEST_P(SitePerProcessBrowserTestWithSadFrameTabReload,
GURL out_of_view_url( GURL out_of_view_url(
embedded_test_server()->GetURL("a.com", "/iframe_out_of_view.html")); embedded_test_server()->GetURL("a.com", "/iframe_out_of_view.html"));
EXPECT_TRUE(NavigateToURL(shell(), out_of_view_url)); EXPECT_TRUE(NavigateToURL(shell(), out_of_view_url));
EXPECT_EQ("LOADED", EvalJsWithManualReply(shell(), "notifyWhenLoaded();")); EXPECT_EQ("LOADED", EvalJs(shell(), "notifyWhenLoaded();",
EXECUTE_SCRIPT_USE_MANUAL_REPLY));
NavigateIframeToURL(web_contents(), "test_iframe", NavigateIframeToURL(web_contents(), "test_iframe",
embedded_test_server()->GetURL("b.com", "/title1.html")); embedded_test_server()->GetURL("b.com", "/title1.html"));
@@ -15801,18 +15802,19 @@ IN_PROC_BROWSER_TEST_P(SitePerProcessBrowserTest,
ASSERT_TRUE(NavigateToURL(shell(), main_frame_url)); ASSERT_TRUE(NavigateToURL(shell(), main_frame_url));
GURL cross_origin(embedded_test_server()->GetURL("b.com", "/title1.html")); GURL cross_origin(embedded_test_server()->GetURL("b.com", "/title1.html"));
std::string msg = std::string msg =
EvalJsWithManualReply( EvalJs(shell(),
shell(), JsReplace("var object = document.createElement('object');" JsReplace("var object = document.createElement('object');"
"document.body.appendChild(object);" "document.body.appendChild(object);"
"object.data = $1;" "object.data = $1;"
"object.type='text/html';" "object.type='text/html';"
"object.notify = true;" "object.notify = true;"
"object.onload = () => {" "object.onload = () => {"
" if (!object.notify) return;" " if (!object.notify) return;"
" object.notify = false;" " object.notify = false;"
" window.domAutomationController.send('done');" " window.domAutomationController.send('done');"
"};", "};",
cross_origin)) cross_origin),
EXECUTE_SCRIPT_USE_MANUAL_REPLY)
.ExtractString(); .ExtractString();
ASSERT_EQ("done", msg); ASSERT_EQ("done", msg);
// To track the frame's visibility an EmbeddedContentView is needed. The // To track the frame's visibility an EmbeddedContentView is needed. The

@@ -1601,10 +1601,11 @@ EvalJsResult EvalRunnerScript(const ToRenderFrameHost& execution_target,
<< "['" << token << "', [result]]; but got instead: " << *response << "['" << token << "', [result]]; but got instead: " << *response
<< " ... This is potentially because a script tried to call " << " ... This is potentially because a script tried to call "
"domAutomationController.send itself -- that is only allowed " "domAutomationController.send itself -- that is only allowed "
"when using EvalJsWithManualReply(). When using EvalJs(), result " "when using EXECUTE_SCRIPT_USE_MANUAL_REPLY. When using "
"values are just the result of calling eval() on the script -- " "EvalJs(), result values are just the result of calling eval() on "
"the completion value is the value of the last executed " "the script -- the completion value is the value of the last "
"statement. When using ExecJs(), there is no result value."; "executed statement. When using ExecJs(), there is no result "
"value.";
} }
} }
@@ -1698,14 +1699,6 @@ EvalJsResult EvalJs(const ToRenderFrameHost& execution_target,
token); token);
} }
EvalJsResult EvalJsWithManualReply(const ToRenderFrameHost& execution_target,
const std::string& script,
int options,
int32_t world_id) {
return EvalJs(execution_target, script,
options | EXECUTE_SCRIPT_USE_MANUAL_REPLY, world_id);
}
EvalJsResult EvalJsAfterLifecycleUpdate( EvalJsResult EvalJsAfterLifecycleUpdate(
const ToRenderFrameHost& execution_target, const ToRenderFrameHost& execution_target,
const std::string& raf_script, const std::string& raf_script,

@@ -445,7 +445,7 @@ class ToRenderFrameHost {
RenderFrameHost* ConvertToRenderFrameHost(RenderFrameHost* render_view_host); RenderFrameHost* ConvertToRenderFrameHost(RenderFrameHost* render_view_host);
RenderFrameHost* ConvertToRenderFrameHost(WebContents* web_contents); RenderFrameHost* ConvertToRenderFrameHost(WebContents* web_contents);
// Semi-deprecated: in new code, prefer ExecJs() -- it works the same, but has // Deprecated: in new code, prefer ExecJs() -- it works the same, but has
// better error handling. (Note: still use ExecuteScript() on pages with a // better error handling. (Note: still use ExecuteScript() on pages with a
// Content Security Policy). // Content Security Policy).
// //
@@ -490,9 +490,7 @@ void ExecuteScriptAsync(const ToRenderFrameHost& adapter,
// the executed script. They return true on success, false if the script // the executed script. They return true on success, false if the script
// execution failed or did not evaluate to the expected type. // execution failed or did not evaluate to the expected type.
// //
// Semi-deprecated: Consider using EvalJs() or EvalJsWithManualReply() instead, // Deprecated: Use EvalJs().
// which handle errors better and don't require an out-param. If the target
// document doesn't have a CSP. See the comment on EvalJs() for migration tips.
bool ExecuteScriptAndExtractDouble(const ToRenderFrameHost& adapter, bool ExecuteScriptAndExtractDouble(const ToRenderFrameHost& adapter,
const std::string& script, const std::string& script,
double* result) WARN_UNUSED_RESULT; double* result) WARN_UNUSED_RESULT;
@@ -788,8 +786,8 @@ enum EvalJsOptions {
// * Preferred, but more rewriting: Use EvalJs with a Promise which // * Preferred, but more rewriting: Use EvalJs with a Promise which
// resolves to the value you previously passed to send(). // resolves to the value you previously passed to send().
// * Less rewriting of |script|, but with some drawbacks: Use // * Less rewriting of |script|, but with some drawbacks: Use
// EXECUTE_SCRIPT_USE_MANUAL_REPLY in |options|, or EvalJsWithManualReply. // EXECUTE_SCRIPT_USE_MANUAL_REPLY in |options|. When specified, this
// When specified, this means that |script| must continue to call // means that |script| must continue to call
// domAutomationController.send(). Note that this option option disables // domAutomationController.send(). Note that this option option disables
// some error-catching safeguards, but you still get the benefit of having // some error-catching safeguards, but you still get the benefit of having
// an EvalJsResult that can be passed to EXPECT. // an EvalJsResult that can be passed to EXPECT.
@@ -819,15 +817,6 @@ EvalJsResult EvalJs(const ToRenderFrameHost& execution_target,
int32_t world_id = ISOLATED_WORLD_ID_GLOBAL) int32_t world_id = ISOLATED_WORLD_ID_GLOBAL)
WARN_UNUSED_RESULT; WARN_UNUSED_RESULT;
// Like EvalJs(), except that |script| must call domAutomationController.send()
// itself. This is the same as specifying the EXECUTE_SCRIPT_USE_MANUAL_REPLY
// option to EvalJs.
EvalJsResult EvalJsWithManualReply(const ToRenderFrameHost& execution_target,
const std::string& script,
int options = EXECUTE_SCRIPT_DEFAULT_OPTIONS,
int32_t world_id = ISOLATED_WORLD_ID_GLOBAL)
WARN_UNUSED_RESULT;
// Like EvalJs(), but runs |raf_script| inside a requestAnimationFrame handler, // Like EvalJs(), but runs |raf_script| inside a requestAnimationFrame handler,
// and runs |script| after the rendering update has completed. By the time // and runs |script| after the rendering update has completed. By the time
// this method returns, any IPCs sent from the renderer process to the browser // this method returns, any IPCs sent from the renderer process to the browser

@@ -155,8 +155,9 @@ IN_PROC_BROWSER_TEST_F(EvalJsBrowserTest, EvalJsWithManualReply) {
std::string script = "window.domAutomationController.send(20); 'hi';"; std::string script = "window.domAutomationController.send(20); 'hi';";
// Calling domAutomationController is required for EvalJsWithManualReply. // Calling domAutomationController is required for
EXPECT_EQ(20, EvalJsWithManualReply(shell(), script)); // EXECUTE_SCRIPT_USE_MANUAL_REPLY.
EXPECT_EQ(20, EvalJs(shell(), script, EXECUTE_SCRIPT_USE_MANUAL_REPLY));
// Calling domAutomationController is an error with EvalJs. // Calling domAutomationController is an error with EvalJs.
auto result = EvalJs(shell(), script); auto result = EvalJs(shell(), script);
@@ -169,9 +170,10 @@ IN_PROC_BROWSER_TEST_F(EvalJsBrowserTest, EvalJsWithManualReply) {
result.error, result.error,
::testing::EndsWith("This is potentially because a script tried to call " ::testing::EndsWith("This is potentially because a script tried to call "
"domAutomationController.send itself -- that is only " "domAutomationController.send itself -- that is only "
"allowed when using EvalJsWithManualReply(). When " "allowed when using "
"using EvalJs(), result values are just the result " "EXECUTE_SCRIPT_USE_MANUAL_REPLY. When using "
"of calling eval() on the script -- the completion " "EvalJs(), result values are just the result of "
"calling eval() on the script -- the completion "
"value is the value of the last executed statement. " "value is the value of the last executed statement. "
"When using ExecJs(), there is no result value.")); "When using ExecJs(), there is no result value."));
} }