Skip to content

Commit

Permalink
Remove trivial uses of ExecuteScriptAndExtractString under
Browse files Browse the repository at this point in the history
chrome/browser

Low-Coverage-Reason: cleaning up deprecated API
Bug: 1157718
Change-Id: I2d086813bc5e9f7a9c78fb022e1f9b68f6417381
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4448689
Commit-Queue: Chris Fredrickson <cfredric@chromium.org>
Auto-Submit: Chris Fredrickson <cfredric@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1132696}
  • Loading branch information
cfredric authored and Chromium LUCI CQ committed Apr 19, 2023
1 parent 5976d05 commit cad0734
Show file tree
Hide file tree
Showing 39 changed files with 412 additions and 640 deletions.
19 changes: 9 additions & 10 deletions chrome/browser/about_flags_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@ void ToggleEnableDropdown(content::WebContents* contents,

std::string GetOriginListText(content::WebContents* contents,
const char* experiment_id) {
std::string text;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
contents,
base::StringPrintf(
"var k = document.getElementById('%s');"
"var s = k.getElementsByClassName('experiment-origin-list-value')[0];"
"window.domAutomationController.send(s.value );",
experiment_id),
&text));
return text;
return content::EvalJs(
contents,
base::StringPrintf(
"var k = document.getElementById('%s');"
"var s = "
"k.getElementsByClassName('experiment-origin-list-value')[0];"
"s.value;",
experiment_id))
.ExtractString();
}

bool IsDropdownEnabled(content::WebContents* contents,
Expand Down
84 changes: 36 additions & 48 deletions chrome/browser/apps/guest_view/web_view_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2565,21 +2565,16 @@ IN_PROC_BROWSER_TEST_F(WebViewTest, DOMStorageIsolation) {
{.launch_as_platform_app = true}));
// Verify that the browser tab's local/session storage does not have the same
// values which were stored by the webviews.
std::string output;
std::string get_local_storage(
"window.domAutomationController.send("
"window.localStorage.getItem('foo') || 'badval')");
"window.localStorage.getItem('foo') || 'badval'");
std::string get_session_storage(
"window.domAutomationController.send("
"window.localStorage.getItem('baz') || 'badval')");
ASSERT_TRUE(ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetWebContentsAt(0),
get_local_storage.c_str(), &output));
EXPECT_STREQ("badval", output.c_str());
ASSERT_TRUE(ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetWebContentsAt(0),
get_session_storage.c_str(), &output));
EXPECT_STREQ("badval", output.c_str());
"window.localStorage.getItem('baz') || 'badval'");
EXPECT_EQ("badval",
content::EvalJs(browser()->tab_strip_model()->GetWebContentsAt(0),
get_local_storage));
EXPECT_EQ("badval",
content::EvalJs(browser()->tab_strip_model()->GetWebContentsAt(0),
get_session_storage));
}

// This tests how guestviews should or should not be able to find each other
Expand Down Expand Up @@ -4487,48 +4482,45 @@ IN_PROC_BROWSER_TEST_F(WebViewTest,
// *TODO(https://crbug.com/1430991): The exact set of APIs and type of
// context this is is a bit fuzzy. In practice, it's basically the same set
// as is exposed to content scripts.
std::string accept_languages_result;
static constexpr char kGetAcceptLanguages[] =
R"(chrome.i18n.getAcceptLanguages((languages) => {
let result = 'success';
if (chrome.runtime.lastError) {
result = 'Error: ' + chrome.runtime.lastError;
} else if (!languages || !Array.isArray(languages) ||
!languages.includes('en')) {
result = 'Invalid return result: ' + JSON.stringify(languages);
}
domAutomationController.send(result);
R"(new Promise(resolve => {
chrome.i18n.getAcceptLanguages((languages) => {
let result = 'success';
if (chrome.runtime.lastError) {
result = 'Error: ' + chrome.runtime.lastError;
} else if (!languages || !Array.isArray(languages) ||
!languages.includes('en')) {
result = 'Invalid return result: ' + JSON.stringify(languages);
}
resolve(result);
});
});)";
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
web_view_frame, kGetAcceptLanguages, &accept_languages_result));
EXPECT_EQ("success", accept_languages_result);
EXPECT_EQ("success", content::EvalJs(web_view_frame, kGetAcceptLanguages));

// Finally, try accessing a privileged API, which shouldn't be available to
// the embedded resource.
// TODO(https://crbug.com/1430991): Even though the API call should fail, the
// bindings are unexpectedly available here. Instead, we should just be able
// to test `!chrome.app.window`.
std::string app_window_result;
static constexpr char kCallAppWindowCreate[] =
R"(if (chrome.app && chrome.app.window && chrome.app.window.create) {
chrome.app.window.create('embedder.html', {}, (res) => {
let reply = 'success';
if (!chrome.runtime.lastError) {
reply = 'No last error found. Result Type: ' + typeof res;
} else if (chrome.runtime.lastError.message !=
'Access to extension API denied.') {
reply = 'Unexpected last error: ' +
chrome.runtime.lastError.message;
}
domAutomationController.send(reply);
new Promise(resolve => {
chrome.app.window.create('embedder.html', {}, (res) => {
let reply = 'success';
if (!chrome.runtime.lastError) {
reply = 'No last error found. Result Type: ' + typeof res;
} else if (chrome.runtime.lastError.message !=
'Access to extension API denied.') {
reply = 'Unexpected last error: ' +
chrome.runtime.lastError.message;
}
resolve(reply);
});
});
} else {
domAutomationController.send(
'Unexpectedly missing `chrome.app.window.create`');
'Unexpectedly missing `chrome.app.window.create`';
})";
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
web_view_frame, kCallAppWindowCreate, &app_window_result));
EXPECT_EQ("success", app_window_result);
EXPECT_EQ("success", content::EvalJs(web_view_frame, kCallAppWindowCreate));
}

// Tests that a WebView can navigate an iframe to a blob URL that it creates
Expand All @@ -4552,12 +4544,8 @@ IN_PROC_BROWSER_TEST_F(WebViewTest, BlobInWebviewAccessibleResource) {
content::RenderFrameHost* blob_frame = ChildFrameAt(webview_rfh, 0);
EXPECT_TRUE(blob_frame->GetLastCommittedURL().SchemeIsBlob());

std::string result;
EXPECT_TRUE(ExecuteScriptAndExtractString(
blob_frame,
"window.domAutomationController.send(document.body.innerText);",
&result));
EXPECT_EQ("Blob content", result);
EXPECT_EQ("Blob content",
content::EvalJs(blob_frame, "document.body.innerText;"));
}

// Tests that a WebView cannot load a webview-inaccessible resource. See
Expand Down
33 changes: 10 additions & 23 deletions chrome/browser/apps/guest_view/web_view_interactive_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1527,13 +1527,9 @@ IN_PROC_BROWSER_TEST_F(WebViewImeInteractiveTest,
EXPECT_TRUE(focus_listener.WaitUntilSatisfied());

// Verify the text inside the <input> is "A B X D".
std::string value;
ASSERT_TRUE(ExecuteScriptAndExtractString(guest_rfh,
"window.domAutomationController."
"send(document.querySelector('"
"input').value)",
&value));
EXPECT_EQ("A B X D", value);
EXPECT_EQ("A B X D", content::EvalJs(guest_rfh,
"document.querySelector('"
"input').value"));

// Now commit "C" to to replace the range (4, 5).
// For OOPIF guests, the target for IME is the RWH for the guest's main frame.
Expand All @@ -1545,13 +1541,9 @@ IN_PROC_BROWSER_TEST_F(WebViewImeInteractiveTest,
EXPECT_TRUE(input_listener.WaitUntilSatisfied());

// Get the input value from the guest.
value.clear();
ASSERT_TRUE(ExecuteScriptAndExtractString(guest_rfh,
"window.domAutomationController."
"send(document.querySelector('"
"input').value)",
&value));
EXPECT_EQ("A B C D", value);
EXPECT_EQ("A B C D", content::EvalJs(guest_rfh,
"document.querySelector('"
"input').value"));
}
#endif // BUILDFLAG(IS_MAC)

Expand Down Expand Up @@ -1587,15 +1579,10 @@ IN_PROC_BROWSER_TEST_F(WebViewImeInteractiveTest, CompositionRangeUpdates) {

// Clear the string as it already contains some text. Then verify the text in
// the <input> is empty.
std::string value;
ASSERT_TRUE(ExecuteScriptAndExtractString(
guest_view->GetGuestMainFrame(),
"var input = document.querySelector('input');"
"input.value = '';"
"window.domAutomationController.send("
" document.querySelector('input').value)",
&value));
EXPECT_EQ("", value);
EXPECT_EQ("", content::EvalJs(guest_view->GetGuestMainFrame(),
"var input = document.querySelector('input');"
"input.value = '';"
"document.querySelector('input').value"));

// Now set some composition text which should lead to an update in composition
// range information.
Expand Down
14 changes: 5 additions & 9 deletions chrome/browser/ash/accessibility/dictation_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,28 +466,24 @@ class DictationTestBase : public InProcessBrowserTest,
}

std::string GetEditableValue() {
std::string output;
std::string script;
switch (editable_type()) {
case EditableType::kTextArea:
case EditableType::kInput:
script =
"window.domAutomationController.send("
"document.getElementById('input').value)";
script = "document.getElementById('input').value";
break;
case EditableType::kContentEditable:
case EditableType::kFormattedContentEditable:
// Replace all non-breaking spaces with regular spaces. Otherwise,
// string comparisons will unexpectedly fail.
script =
"window.domAutomationController.send("
"document.getElementById('input').innerText.replaceAll("
"'\u00a0', ' '));";
"'\u00a0', ' ');";
break;
}
CHECK(ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetWebContentsAt(0), script, &output));
return output;
return content::EvalJs(browser()->tab_strip_model()->GetWebContentsAt(0),
script)
.ExtractString();
}

void WaitForEditableValue(const std::string& value) {
Expand Down
11 changes: 4 additions & 7 deletions chrome/browser/ash/accessibility/switch_access_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ class SwitchAccessTest : public InProcessBrowserTest {
}

std::string GetInputString() {
std::string output;
std::string script =
"window.domAutomationController.send("
"document.getElementById('in').value)";
CHECK(ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetWebContentsAt(0), script, &output));
return output;
std::string script = "document.getElementById('in').value";
return content::EvalJs(browser()->tab_strip_model()->GetWebContentsAt(0),
script)
.ExtractString();
}

void SetUpOnMainThread() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,12 @@ IN_PROC_BROWSER_TEST_F(KioskEnterpriseTest, EnterpriseKioskApp) {
EXPECT_TRUE(content::WaitForLoadStop(window->web_contents()));

// Check whether the app can retrieve an OAuth2 access token.
std::string result;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
window->web_contents(),
"chrome.identity.getAuthToken({ 'interactive': false }, function(token) {"
" window.domAutomationController.send(token);"
"});",
&result));
EXPECT_EQ(kTestAccessToken, result);
EXPECT_EQ(kTestAccessToken,
content::EvalJs(window->web_contents(),
"new Promise(resolve => {"
" chrome.identity.getAuthToken({ 'interactive': "
" false }, resolve);"
"});"));

// Verify that the session is not considered to be logged in with a GAIA
// account.
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/ash/login/test/js_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ void JSChecker::GetIntImpl(const std::string& expression, int* result) {
void JSChecker::GetStringImpl(const std::string& expression,
std::string* result) {
CHECK(web_contents_);
ASSERT_TRUE(content::ExecuteScriptAndExtractString(
web_contents_, WrapSend(expression), result));
*result = content::EvalJs(web_contents_, expression).ExtractString();
}

void JSChecker::ExpectVisiblePath(
Expand Down
9 changes: 2 additions & 7 deletions chrome/browser/ash/login/test/webview_content_extractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ std::string GetWebViewContentsById(const std::string& element_id) {
content::WaitForLoadStop(
content::WebContents::FromRenderFrameHost(FindFrame(element_id)));

std::string text;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
FindFrame(element_id),
"window.domAutomationController.send(document.body.textContent);",
&text));

return text;
return content::EvalJs(FindFrame(element_id), "document.body.textContent;")
.ExtractString();
}

} // namespace test
Expand Down
6 changes: 1 addition & 5 deletions chrome/browser/ash/login/wizard_controller_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,7 @@ class WizardControllerTest : public OobeBaseTest {
}

std::string JSExecuteStringExpression(const std::string& expression) {
std::string result;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
GetWebContents(),
"window.domAutomationController.send(" + expression + ");", &result));
return result;
return content::EvalJs(GetWebContents(), expression).ExtractString();
}

void CheckCurrentScreen(OobeScreenId screen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,7 @@ class SystemWebAppManagerFileHandlingBrowserTestBase

std::string GetJsStatementValueAsString(content::WebContents* web_contents,
const std::string& js_statement) {
std::string str;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
web_contents, "domAutomationController.send( " + js_statement + ");",
&str));
return str;
return content::EvalJs(web_contents, js_statement).ExtractString();
}

private:
Expand Down Expand Up @@ -433,17 +429,14 @@ class SystemWebAppManagerLaunchDirectoryBrowserTest
std::string ReadContentFromJsFileHandle(
content::WebContents* web_contents,
const std::string& file_handle_or_promise) {
std::string js_file_content;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
web_contents,
"Promise.resolve(" + file_handle_or_promise + ")" +
".then(async (fileHandle) => {"
" const file = await fileHandle.getFile();"
" const content = await file.text();"
" window.domAutomationController.send(content);"
"});",
&js_file_content));
return js_file_content;
return content::EvalJs(web_contents,
"Promise.resolve(" + file_handle_or_promise + ")" +
".then(async (fileHandle) => {"
" const file = await fileHandle.getFile();"
" const content = await file.text();"
" return content;"
"});")
.ExtractString();
}

// Writes |content_to_write| to |file_handle_or_promise| file handle. Returns
Expand Down Expand Up @@ -749,17 +742,17 @@ IN_PROC_BROWSER_TEST_P(
CheckFileIsGif(web_contents, "window.launchParams.files[1].getFile()"));

// Check we can list the directory.
std::string file_names;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
web_contents,
"(async function() {"
" let fileNames = [];"
" const files = await window.launchParams.files[0].keys();"
" for await (const name of files)"
" fileNames.push(name);"
" domAutomationController.send(fileNames.sort().join(';'));"
"})();",
&file_names));
std::string file_names =
content::EvalJs(
web_contents,
"(async function() {"
" let fileNames = [];"
" const files = await window.launchParams.files[0].keys();"
" for await (const name of files)"
" fileNames.push(name);"
" return fileNames.sort().join(';');"
"})();")
.ExtractString();
EXPECT_EQ(base::StrCat({kTestPngFile, ";", kTestGifFile}), file_names);

// Verify we can read a file (other than launch file) inside the directory.
Expand Down
Loading

0 comments on commit cad0734

Please sign in to comment.