Skip to content

Commit

Permalink
Remove ExecuteScriptWithoutUserGestureAndExtractBool
Browse files Browse the repository at this point in the history
Bug: 1157718
Change-Id: Ide48a8de26f1e9254da68d22c6e373a088c48601
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4368334
Reviewed-by: Avi Drissman <avi@chromium.org>
Auto-Submit: Chris Fredrickson <cfredric@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1122412}
  • Loading branch information
cfredric authored and Chromium LUCI CQ committed Mar 27, 2023
1 parent 4ac080c commit 3dc4ddb
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 170 deletions.
7 changes: 2 additions & 5 deletions chrome/browser/chrome_navigation_browsertest.cc
Expand Up @@ -1475,11 +1475,8 @@ IN_PROC_BROWSER_TEST_F(ChromeNavigationBrowserTest,
observer.Wait();

// Make sure popup attempt fails due to lack of transient user activation.
bool opened = false;
EXPECT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
main_contents, "window.domAutomationController.send(!!window.open());",
&opened));
EXPECT_FALSE(opened);
EXPECT_EQ(false, content::EvalJs(main_contents, "!!window.open();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));

EXPECT_EQ(embedded_test_server()->GetURL("/title1.html"),
main_contents->GetLastCommittedURL());
Expand Down
Expand Up @@ -220,9 +220,9 @@ IN_PROC_BROWSER_TEST_F(SiteEngagementHelperBrowserTest,
// Since the prerendered page couldn't have a user gesture, it runs JS with
// EXECUTE_SCRIPT_NO_USER_GESTURE. Requesting playing video without a user
// gesture results in the promise rejected.
EXPECT_FALSE(
content::ExecJs(prerendered_frame_host, "attemptPlay();",
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_EQ(false, content::EvalJs(
prerendered_frame_host, "attemptPlay();",
content::EvalJsOptions::EXECUTE_SCRIPT_NO_USER_GESTURE));

EXPECT_EQ(tester.last_updated_type(), EngagementType::kNavigation);
EXPECT_EQ(tester.last_updated_url(), url);
Expand Down
6 changes: 2 additions & 4 deletions chrome/browser/extensions/autoplay_browsertest.cc
Expand Up @@ -82,8 +82,6 @@ IN_PROC_BROWSER_TEST_F(AutoplayExtensionBrowserTest,
app_browser->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(content::WaitForLoadStop(web_contents));

bool result = false;
EXPECT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "runTest();", &result));
EXPECT_TRUE(result);
EXPECT_EQ(true, content::EvalJs(web_contents, "runTest();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}
14 changes: 6 additions & 8 deletions chrome/browser/media/unified_autoplay_browsertest.cc
Expand Up @@ -96,10 +96,9 @@ class UnifiedAutoplayBrowserTest : public InProcessBrowserTest {
}

bool AttemptPlay(content::WebContents* web_contents) {
bool played = false;
EXPECT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "attemptPlay();", &played));
return played;
return content::EvalJs(web_contents, "attemptPlay();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
}

bool NavigateInRenderer(content::WebContents* web_contents, const GURL& url) {
Expand Down Expand Up @@ -435,10 +434,9 @@ class UnifiedAutoplaySettingBrowserTest : public UnifiedAutoplayBrowserTest {
}

bool AutoplayAllowed(const content::ToRenderFrameHost& adapter) {
bool played = false;
EXPECT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
adapter, "tryPlayback();", &played));
return played;
return content::EvalJs(adapter, "tryPlayback();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
}

void NavigateFrameAndWait(content::RenderFrameHost* rfh, const GURL& url) {
Expand Down
18 changes: 8 additions & 10 deletions chrome/browser/password_manager/password_manager_browsertest.cc
Expand Up @@ -3002,11 +3002,10 @@ IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
std::string find_logs =
"var text = document.getElementById('log-entries').innerText;"
"var logs_found = /PasswordAutofillAgent::/.test(text);"
"window.domAutomationController.send(logs_found);";
bool logs_found = false;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
internals_web_contents->GetPrimaryMainFrame(), find_logs, &logs_found));
EXPECT_TRUE(logs_found);
"logs_found;";
EXPECT_EQ(true, content::EvalJs(internals_web_contents->GetPrimaryMainFrame(),
find_logs,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}

// Check that the internals page contains logs from the browser.
Expand All @@ -3025,11 +3024,10 @@ IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, InternalsPage_Browser) {
std::string find_logs =
"var text = document.getElementById('log-entries').innerText;"
"var logs_found = /PasswordManager::/.test(text);"
"window.domAutomationController.send(logs_found);";
bool logs_found = false;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
internals_web_contents->GetPrimaryMainFrame(), find_logs, &logs_found));
EXPECT_TRUE(logs_found);
"logs_found;";
EXPECT_EQ(true, content::EvalJs(internals_web_contents->GetPrimaryMainFrame(),
find_logs,
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}

// Tests that submitted credentials are saved on a password form without
Expand Down
17 changes: 7 additions & 10 deletions chrome/browser/policy/test/autoplay_policy_browsertest.cc
Expand Up @@ -14,6 +14,7 @@
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
Expand Down Expand Up @@ -57,12 +58,9 @@ class AutoplayPolicyTest : public PolicyTest {
}

bool TryAutoplay(content::RenderFrameHost* rfh) {
bool result = false;

EXPECT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
rfh, "tryPlayback();", &result));

return result;
return content::EvalJs(rfh, "tryPlayback();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
}

content::WebContents* GetWebContents() {
Expand Down Expand Up @@ -311,10 +309,9 @@ class AutoplayPolicyFencedFrameTest : public AutoplayPolicyTest {
}

bool TryAutoplay(content::RenderFrameHost* rfh) {
bool result = false;
EXPECT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
rfh, "attemptPlay();", &result));
return result;
return content::EvalJs(rfh, "attemptPlay();",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
}

protected:
Expand Down
Expand Up @@ -734,11 +734,8 @@ IN_PROC_BROWSER_TEST_F(ChromeSitePerProcessTest,
EXPECT_NE(popup, web_contents);

// Check that the window handle returned from window.open() was valid.
bool popup_handle_is_valid = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "window.domAutomationController.send(!!window.w)",
&popup_handle_is_valid));
EXPECT_TRUE(popup_handle_is_valid);
EXPECT_EQ(true, content::EvalJs(web_contents, "!!window.w",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}

// Check that when a frame sends two cross-process postMessages while having a
Expand Down Expand Up @@ -786,11 +783,8 @@ IN_PROC_BROWSER_TEST_F(ChromeSitePerProcessTest,

// Ensure that only one popup can be opened. The second window.open() call
// should've failed and stored null into window.w.
bool popup_handle_is_valid = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
child, "window.domAutomationController.send(!!window.w)",
&popup_handle_is_valid));
EXPECT_FALSE(popup_handle_is_valid);
EXPECT_EQ(false, content::EvalJs(child, "!!window.w",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}

// Check that when a frame sends two postMessages to iframes on different sites
Expand Down Expand Up @@ -843,14 +837,14 @@ IN_PROC_BROWSER_TEST_F(ChromeSitePerProcessTest,
EXPECT_NE(popup, web_contents);

// Ensure that only one renderer process has a valid popup handle.
bool root_frame_handle_is_valid = false;
bool frame_b_handle_is_valid = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "window.domAutomationController.send(!!window.w)",
&root_frame_handle_is_valid));
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
frame_b, "window.domAutomationController.send(!!window.w)",
&frame_b_handle_is_valid));
bool root_frame_handle_is_valid =
content::EvalJs(web_contents, "!!window.w",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
bool frame_b_handle_is_valid =
content::EvalJs(frame_b, "!!window.w",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
EXPECT_TRUE(root_frame_handle_is_valid != frame_b_handle_is_valid)
<< "root_frame_handle_is_valid = " << root_frame_handle_is_valid
<< ", frame_b_handle_is_valid = " << frame_b_handle_is_valid;
Expand Down Expand Up @@ -914,11 +908,8 @@ IN_PROC_BROWSER_TEST_F(ChromeSitePerProcessTest,

// Ensure that only one popup can be opened. The second window.open() call at
// top frame should fail, storing null into window.w.
bool popup_handle_is_valid = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "window.domAutomationController.send(!!window.w)",
&popup_handle_is_valid));
EXPECT_FALSE(popup_handle_is_valid);
EXPECT_EQ(false, content::EvalJs(web_contents, "!!window.w",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_EQ(2, browser()->tab_strip_model()->count());
}

Expand Down Expand Up @@ -967,14 +958,14 @@ IN_PROC_BROWSER_TEST_F(ChromeSitePerProcessTest,

// Ensure that only one popup was opened, from either the parent or the child
// frame, but not both.
bool parent_popup_handle_is_valid = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "window.domAutomationController.send(!!window.w)",
&parent_popup_handle_is_valid));
bool child_popup_handle_is_valid = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
child, "window.domAutomationController.send(!!window.w)",
&child_popup_handle_is_valid));
bool parent_popup_handle_is_valid =
content::EvalJs(web_contents, "!!window.w",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
bool child_popup_handle_is_valid =
content::EvalJs(child, "!!window.w",
content::EXECUTE_SCRIPT_NO_USER_GESTURE)
.ExtractBool();
EXPECT_NE(parent_popup_handle_is_valid, child_popup_handle_is_valid)
<< " parent_popup_handle_is_valid=" << parent_popup_handle_is_valid
<< " child_popup_handle_is_valid=" << child_popup_handle_is_valid;
Expand Down Expand Up @@ -1026,18 +1017,11 @@ IN_PROC_BROWSER_TEST_F(ChromeSitePerProcessTest,
EXPECT_NE(popup, web_contents);

// Confirm that only the root_frame opened the popup.
bool root_frame_popup_opened = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "window.domAutomationController.send(!!window.w);",
&root_frame_popup_opened));
EXPECT_TRUE(root_frame_popup_opened);

bool frame_c_popup_opened = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
frame_c, "window.domAutomationController.send(!!window.w);",
&frame_c_popup_opened));

EXPECT_FALSE(frame_c_popup_opened);
EXPECT_EQ(true, content::EvalJs(web_contents, "!!window.w;",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));

EXPECT_EQ(false, content::EvalJs(frame_c, "!!window.w",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}

// Test that when an activation is consumed, no frames in the frame tree can
Expand Down Expand Up @@ -1090,23 +1074,14 @@ IN_PROC_BROWSER_TEST_F(ChromeSitePerProcessTest,
EXPECT_NE(popup, web_contents);

// Confirm that only frame_b opened the popup.
bool root_frame_popup_opened = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "window.domAutomationController.send(!!window.w);",
&root_frame_popup_opened));
EXPECT_FALSE(root_frame_popup_opened);

bool frame_b_popup_opened = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
frame_b, "window.domAutomationController.send(!!window.w);",
&frame_b_popup_opened));
EXPECT_TRUE(frame_b_popup_opened);

bool frame_c_popup_opened = false;
EXPECT_TRUE(ExecuteScriptWithoutUserGestureAndExtractBool(
frame_c, "window.domAutomationController.send(!!window.w);",
&frame_c_popup_opened));
EXPECT_FALSE(frame_c_popup_opened);
EXPECT_EQ(false, content::EvalJs(web_contents, "!!window.w;",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));

EXPECT_EQ(true, content::EvalJs(frame_b, "!!window.w;",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));

EXPECT_EQ(false, content::EvalJs(frame_c, "!!window.w;",
content::EXECUTE_SCRIPT_NO_USER_GESTURE));
}

// Test that opening a window with `noopener` consumes user activation.
Expand Down

0 comments on commit 3dc4ddb

Please sign in to comment.