| Index: content/public/test/browser_test_utils.cc
|
| diff --git a/content/public/test/browser_test_utils.cc b/content/public/test/browser_test_utils.cc
|
| index 3198c44524a85b4b9c200bdea2270d6497124942..69e329df7fde08b56f76b170230b74a27b2078a6 100644
|
| --- a/content/public/test/browser_test_utils.cc
|
| +++ b/content/public/test/browser_test_utils.cc
|
| @@ -92,49 +92,6 @@
|
| namespace content {
|
| namespace {
|
|
|
| -class DOMOperationObserver : public NotificationObserver,
|
| - public WebContentsObserver {
|
| - public:
|
| - explicit DOMOperationObserver(RenderFrameHost* rfh)
|
| - : WebContentsObserver(WebContents::FromRenderFrameHost(rfh)),
|
| - did_respond_(false) {
|
| - registrar_.Add(this, NOTIFICATION_DOM_OPERATION_RESPONSE,
|
| - Source<WebContents>(web_contents()));
|
| - message_loop_runner_ = new MessageLoopRunner;
|
| - }
|
| -
|
| - void Observe(int type,
|
| - const NotificationSource& source,
|
| - const NotificationDetails& details) override {
|
| - DCHECK(type == NOTIFICATION_DOM_OPERATION_RESPONSE);
|
| - Details<std::string> dom_op_result(details);
|
| - if (!did_respond_) {
|
| - response_ = *dom_op_result.ptr();
|
| - did_respond_ = true;
|
| - message_loop_runner_->Quit();
|
| - }
|
| - }
|
| -
|
| - // Overridden from WebContentsObserver:
|
| - void RenderProcessGone(base::TerminationStatus status) override {
|
| - message_loop_runner_->Quit();
|
| - }
|
| -
|
| - bool WaitAndGetResponse(std::string* response) WARN_UNUSED_RESULT {
|
| - message_loop_runner_->Run();
|
| - *response = response_;
|
| - return did_respond_;
|
| - }
|
| -
|
| - private:
|
| - NotificationRegistrar registrar_;
|
| - std::string response_;
|
| - bool did_respond_;
|
| - scoped_refptr<MessageLoopRunner> message_loop_runner_;
|
| -
|
| - DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
|
| -};
|
| -
|
| class InterstitialObserver : public content::WebContentsObserver {
|
| public:
|
| InterstitialObserver(content::WebContents* web_contents,
|
| @@ -173,23 +130,13 @@ bool ExecuteScriptHelper(RenderFrameHost* render_frame_host,
|
| // automation id.
|
| std::string script =
|
| "window.domAutomationController.setAutomationId(0);" + original_script;
|
| - DOMOperationObserver dom_op_observer(render_frame_host);
|
| + DOMAutomationWaiter dom_op_observer(
|
| + WebContents::FromRenderFrameHost(render_frame_host));
|
| render_frame_host->ExecuteJavaScriptWithUserGestureForTests(
|
| base::UTF8ToUTF16(script));
|
| std::string json;
|
| - if (!dom_op_observer.WaitAndGetResponse(&json)) {
|
| - DLOG(ERROR) << "Cannot communicate with DOMOperationObserver.";
|
| - return false;
|
| - }
|
| -
|
| - // Nothing more to do for callers that ignore the returned JS value.
|
| - if (!result)
|
| - return true;
|
| -
|
| - base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
|
| - *result = reader.ReadToValue(json);
|
| - if (!*result) {
|
| - DLOG(ERROR) << reader.GetErrorMessage();
|
| + if (!dom_op_observer.WaitAndGetResponse(result)) {
|
| + DLOG(ERROR) << "Cannot communicate with DOMAutomationWaiter.";
|
| return false;
|
| }
|
|
|
| @@ -207,26 +154,18 @@ bool ExecuteScriptInIsolatedWorldHelper(RenderFrameHost* render_frame_host,
|
| const int world_id,
|
| const std::string& original_script,
|
| std::unique_ptr<base::Value>* result) {
|
| + // TODO(jcampan): we should make the domAutomationController not require an
|
| + // automation id.
|
| std::string script =
|
| "window.domAutomationController.setAutomationId(0);" + original_script;
|
| - DOMOperationObserver dom_op_observer(render_frame_host);
|
| + DOMAutomationWaiter dom_op_observer(
|
| + WebContents::FromRenderFrameHost(render_frame_host));
|
| render_frame_host->ExecuteJavaScriptInIsolatedWorld(
|
| base::UTF8ToUTF16(script),
|
| content::RenderFrameHost::JavaScriptResultCallback(), world_id);
|
| std::string json;
|
| - if (!dom_op_observer.WaitAndGetResponse(&json)) {
|
| - DLOG(ERROR) << "Cannot communicate with DOMOperationObserver.";
|
| - return false;
|
| - }
|
| -
|
| - // Nothing more to do for callers that ignore the returned JS value.
|
| - if (!result)
|
| - return true;
|
| -
|
| - base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
|
| - *result = reader.ReadToValue(json);
|
| - if (!*result) {
|
| - DLOG(ERROR) << reader.GetErrorMessage();
|
| + if (!dom_op_observer.WaitAndGetResponse(result)) {
|
| + DLOG(ERROR) << "Cannot communicate with DOMAutomationWaiter.";
|
| return false;
|
| }
|
|
|
| @@ -796,6 +735,73 @@ RenderFrameHost* ConvertToRenderFrameHost(RenderFrameHost* render_frame_host) {
|
| return render_frame_host;
|
| }
|
|
|
| +DOMAutomationWaiter::DOMAutomationWaiter(WebContents* web_contents)
|
| + : WebContentsObserver(web_contents),
|
| + message_loop_runner_(new MessageLoopRunner) {
|
| + registrar_.Add(this, NOTIFICATION_DOM_OPERATION_RESPONSE,
|
| + Source<WebContents>(web_contents));
|
| +}
|
| +
|
| +DOMAutomationWaiter::~DOMAutomationWaiter() {}
|
| +
|
| +bool DOMAutomationWaiter::WaitAndGetResponse(
|
| + std::unique_ptr<base::Value>* response) {
|
| + message_loop_runner_->Run();
|
| + if (!response_)
|
| + return false;
|
| +
|
| + if (response)
|
| + *response = std::move(response_);
|
| + else
|
| + response_.reset();
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool DOMAutomationWaiter::ShouldStopWaiting(const base::Value& value) {
|
| + return true;
|
| +}
|
| +
|
| +void DOMAutomationWaiter::Observe(int type,
|
| + const NotificationSource& source,
|
| + const NotificationDetails& details) {
|
| + DCHECK(type == NOTIFICATION_DOM_OPERATION_RESPONSE);
|
| + Details<std::string> dom_op_result(details);
|
| +
|
| + // Return if already received a response.
|
| + if (response_)
|
| + return;
|
| +
|
| + // Parse JSON from notification data into |candidate_response|.
|
| + const std::string& json_string = *dom_op_result.ptr();
|
| + std::unique_ptr<base::Value> candidate_response;
|
| + base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
|
| + candidate_response = reader.ReadToValue(json_string);
|
| + if (!candidate_response) {
|
| + DLOG(ERROR) << reader.GetErrorMessage();
|
| + return;
|
| + }
|
| +
|
| + // Allow subclasses to ignore this response and to continue waiting.
|
| + if (!ShouldStopWaiting(*candidate_response))
|
| + return;
|
| +
|
| + // Store the response and stop waiting.
|
| + response_ = std::move(candidate_response);
|
| + message_loop_runner_->Quit();
|
| +}
|
| +
|
| +void DOMAutomationWaiter::RenderProcessGone(base::TerminationStatus status) {
|
| + switch (status) {
|
| + case base::TERMINATION_STATUS_NORMAL_TERMINATION:
|
| + case base::TERMINATION_STATUS_STILL_RUNNING:
|
| + break;
|
| + default:
|
| + message_loop_runner_->Quit();
|
| + break;
|
| + }
|
| +}
|
| +
|
| bool ExecuteScript(const ToRenderFrameHost& adapter,
|
| const std::string& script) {
|
| std::string new_script =
|
|
|