Commit 5283ad46 authored by Jiawei Li's avatar Jiawei Li Committed by Commit Bot

[chromecast] Add ExecuteJavaScript method for CastWebContents

Adds a ExecuteJavaScript method for CastWebContents to run script on the
main frame's context.
The method would accept string script and a OnceCallback that is used to
receive a result later.

Bug: Internal b/150822089
Test: cast_web_contents_browsertest
Change-Id: I15e43f62036907e667278f954d06b0bb035c59cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2123639
Commit-Queue: Jiawei Li <lijiawei@chromium.org>
Reviewed-by: default avatarSean Topping <seantopping@chromium.org>
Reviewed-by: default avatarYuchen Liu <yucliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754163}
parent 5976e4c1
...@@ -371,6 +371,16 @@ class CastWebContents { ...@@ -371,6 +371,16 @@ class CastWebContents {
const std::string& data, const std::string& data,
std::vector<blink::WebMessagePort> ports) = 0; std::vector<blink::WebMessagePort> ports) = 0;
// Executes a string of JavaScript in the main frame's context.
// This is no-op if the main frame is not available.
// Pass in a callback to receive a result when it is available.
// If there is no need to receive the result, pass in a
// default-constructed callback. If provided, the callback
// will be invoked on the UI thread.
virtual void ExecuteJavaScript(
const base::string16& javascript,
base::OnceCallback<void(base::Value)> callback) = 0;
// =========================================================================== // ===========================================================================
// Utility Methods // Utility Methods
// =========================================================================== // ===========================================================================
......
...@@ -1143,6 +1143,59 @@ IN_PROC_BROWSER_TEST_F(CastWebContentsBrowserTest, ...@@ -1143,6 +1143,59 @@ IN_PROC_BROWSER_TEST_F(CastWebContentsBrowserTest,
} }
} }
IN_PROC_BROWSER_TEST_F(CastWebContentsBrowserTest, ExecuteJavaScript) {
// Start test server for hosting test HTML pages.
embedded_test_server()->ServeFilesFromSourceDirectory(GetTestDataPath());
StartTestServer();
auto run_loop = std::make_unique<base::RunLoop>();
auto quit_closure = [&run_loop]() {
if (run_loop->running()) {
run_loop->QuitWhenIdle();
}
};
// ===========================================================================
// Test: Set a value using ExecuteJavaScript with empty callback, and then use
// ExecuteJavaScript with callback to retrieve that value.
// ===========================================================================
constexpr char kSoyMilkJsonStringLiteral[] = "\"SoyMilk\"";
// Load page with title "hello":
GURL gurl{embedded_test_server()->GetURL("/title1.html")};
{
InSequence seq;
EXPECT_CALL(
mock_cast_wc_observer_,
OnPageStateChanged(CheckPageState(
cast_web_contents_.get(), CastWebContents::PageState::LOADING)));
EXPECT_CALL(
mock_cast_wc_observer_,
OnPageStateChanged(CheckPageState(cast_web_contents_.get(),
CastWebContents::PageState::LOADED)))
.WillOnce(InvokeWithoutArgs(quit_closure));
}
cast_web_contents_->LoadUrl(gurl);
run_loop->Run();
// Execute with empty callback.
cast_web_contents_->ExecuteJavaScript(
base::UTF8ToUTF16(
base::StringPrintf("const the_var = %s;", kSoyMilkJsonStringLiteral)),
base::DoNothing());
// Execute a script snippet to return the variable's value.
base::RunLoop run_loop2;
cast_web_contents_->ExecuteJavaScript(
base::UTF8ToUTF16("the_var;"),
base::BindLambdaForTesting([&](base::Value result_value) {
std::string result_json;
ASSERT_TRUE(base::JSONWriter::Write(result_value, &result_json));
EXPECT_EQ(result_json, kSoyMilkJsonStringLiteral);
run_loop2.Quit();
}));
run_loop2.Run();
}
} // namespace chromecast } // namespace chromecast
#endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_BROWSERTEST_H_ #endif // CHROMECAST_BROWSER_CAST_WEB_CONTENTS_BROWSERTEST_H_
...@@ -104,8 +104,7 @@ CastWebContents* CastWebContents::FromWebContents( ...@@ -104,8 +104,7 @@ CastWebContents* CastWebContents::FromWebContents(
return *it; return *it;
} }
void CastWebContentsImpl::RenderProcessReady( void CastWebContentsImpl::RenderProcessReady(content::RenderProcessHost* host) {
content::RenderProcessHost* host) {
DCHECK(host->IsReady()); DCHECK(host->IsReady());
const base::Process& process = host->GetProcess(); const base::Process& process = host->GetProcess();
for (auto& observer : observer_list_) { for (auto& observer : observer_list_) {
...@@ -431,6 +430,18 @@ void CastWebContentsImpl::PostMessageToMainFrame( ...@@ -431,6 +430,18 @@ void CastWebContentsImpl::PostMessageToMainFrame(
std::move(ports)); std::move(ports));
} }
void CastWebContentsImpl::ExecuteJavaScript(
const base::string16& javascript,
base::OnceCallback<void(base::Value)> callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!web_contents_ || closing_ || !main_frame_loaded_ ||
!web_contents_->GetMainFrame())
return;
web_contents_->GetMainFrame()->ExecuteJavaScript(javascript,
std::move(callback));
}
void CastWebContentsImpl::AddObserver(CastWebContents::Observer* observer) { void CastWebContentsImpl::AddObserver(CastWebContents::Observer* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(observer); DCHECK(observer);
......
...@@ -82,6 +82,9 @@ class CastWebContentsImpl : public CastWebContents, ...@@ -82,6 +82,9 @@ class CastWebContentsImpl : public CastWebContents,
const std::string& target_origin, const std::string& target_origin,
const std::string& data, const std::string& data,
std::vector<blink::WebMessagePort> ports) override; std::vector<blink::WebMessagePort> ports) override;
void ExecuteJavaScript(
const base::string16& javascript,
base::OnceCallback<void(base::Value)> callback) override;
void AddObserver(Observer* observer) override; void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override; void RemoveObserver(Observer* observer) override;
bool is_websql_enabled() override; bool is_websql_enabled() override;
......
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