| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/test/browser_test_utils.h" | 5 #include "content/public/test/browser_test_utils.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 bool did_respond_; | 75 bool did_respond_; |
| 76 scoped_refptr<MessageLoopRunner> message_loop_runner_; | 76 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 77 | 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); | 78 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. | 81 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. |
| 82 bool ExecuteScriptHelper(RenderViewHost* render_view_host, | 82 bool ExecuteScriptHelper(RenderViewHost* render_view_host, |
| 83 const std::string& frame_xpath, | 83 const std::string& frame_xpath, |
| 84 const std::string& original_script, | 84 const std::string& original_script, |
| 85 scoped_ptr<Value>* result) WARN_UNUSED_RESULT; | 85 scoped_ptr<base::Value>* result) WARN_UNUSED_RESULT; |
| 86 | 86 |
| 87 // Executes the passed |original_script| in the frame pointed to by | 87 // Executes the passed |original_script| in the frame pointed to by |
| 88 // |frame_xpath|. If |result| is not NULL, stores the value that the evaluation | 88 // |frame_xpath|. If |result| is not NULL, stores the value that the evaluation |
| 89 // of the script in |result|. Returns true on success. | 89 // of the script in |result|. Returns true on success. |
| 90 bool ExecuteScriptHelper(RenderViewHost* render_view_host, | 90 bool ExecuteScriptHelper(RenderViewHost* render_view_host, |
| 91 const std::string& frame_xpath, | 91 const std::string& frame_xpath, |
| 92 const std::string& original_script, | 92 const std::string& original_script, |
| 93 scoped_ptr<Value>* result) { | 93 scoped_ptr<base::Value>* result) { |
| 94 // TODO(jcampan): we should make the domAutomationController not require an | 94 // TODO(jcampan): we should make the domAutomationController not require an |
| 95 // automation id. | 95 // automation id. |
| 96 std::string script = | 96 std::string script = |
| 97 "window.domAutomationController.setAutomationId(0);" + original_script; | 97 "window.domAutomationController.setAutomationId(0);" + original_script; |
| 98 DOMOperationObserver dom_op_observer(render_view_host); | 98 DOMOperationObserver dom_op_observer(render_view_host); |
| 99 render_view_host->ExecuteJavascriptInWebFrame(UTF8ToUTF16(frame_xpath), | 99 render_view_host->ExecuteJavascriptInWebFrame(UTF8ToUTF16(frame_xpath), |
| 100 UTF8ToUTF16(script)); | 100 UTF8ToUTF16(script)); |
| 101 std::string json; | 101 std::string json; |
| 102 if (!dom_op_observer.WaitAndGetResponse(&json)) { | 102 if (!dom_op_observer.WaitAndGetResponse(&json)) { |
| 103 DLOG(ERROR) << "Cannot communicate with DOMOperationObserver."; | 103 DLOG(ERROR) << "Cannot communicate with DOMOperationObserver."; |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 return ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, | 408 return ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, |
| 409 NULL); | 409 NULL); |
| 410 } | 410 } |
| 411 | 411 |
| 412 bool ExecuteScriptInFrameAndExtractInt( | 412 bool ExecuteScriptInFrameAndExtractInt( |
| 413 const internal::ToRenderViewHost& adapter, | 413 const internal::ToRenderViewHost& adapter, |
| 414 const std::string& frame_xpath, | 414 const std::string& frame_xpath, |
| 415 const std::string& script, | 415 const std::string& script, |
| 416 int* result) { | 416 int* result) { |
| 417 DCHECK(result); | 417 DCHECK(result); |
| 418 scoped_ptr<Value> value; | 418 scoped_ptr<base::Value> value; |
| 419 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, | 419 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, |
| 420 &value) || !value.get()) | 420 &value) || !value.get()) |
| 421 return false; | 421 return false; |
| 422 | 422 |
| 423 return value->GetAsInteger(result); | 423 return value->GetAsInteger(result); |
| 424 } | 424 } |
| 425 | 425 |
| 426 bool ExecuteScriptInFrameAndExtractBool( | 426 bool ExecuteScriptInFrameAndExtractBool( |
| 427 const internal::ToRenderViewHost& adapter, | 427 const internal::ToRenderViewHost& adapter, |
| 428 const std::string& frame_xpath, | 428 const std::string& frame_xpath, |
| 429 const std::string& script, | 429 const std::string& script, |
| 430 bool* result) { | 430 bool* result) { |
| 431 DCHECK(result); | 431 DCHECK(result); |
| 432 scoped_ptr<Value> value; | 432 scoped_ptr<base::Value> value; |
| 433 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, | 433 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, |
| 434 &value) || !value.get()) | 434 &value) || !value.get()) |
| 435 return false; | 435 return false; |
| 436 | 436 |
| 437 return value->GetAsBoolean(result); | 437 return value->GetAsBoolean(result); |
| 438 } | 438 } |
| 439 | 439 |
| 440 bool ExecuteScriptInFrameAndExtractString( | 440 bool ExecuteScriptInFrameAndExtractString( |
| 441 const internal::ToRenderViewHost& adapter, | 441 const internal::ToRenderViewHost& adapter, |
| 442 const std::string& frame_xpath, | 442 const std::string& frame_xpath, |
| 443 const std::string& script, | 443 const std::string& script, |
| 444 std::string* result) { | 444 std::string* result) { |
| 445 DCHECK(result); | 445 DCHECK(result); |
| 446 scoped_ptr<Value> value; | 446 scoped_ptr<base::Value> value; |
| 447 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, | 447 if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, |
| 448 &value) || !value.get()) | 448 &value) || !value.get()) |
| 449 return false; | 449 return false; |
| 450 | 450 |
| 451 return value->GetAsString(result); | 451 return value->GetAsString(result); |
| 452 } | 452 } |
| 453 | 453 |
| 454 bool ExecuteScript(const internal::ToRenderViewHost& adapter, | 454 bool ExecuteScript(const internal::ToRenderViewHost& adapter, |
| 455 const std::string& script) { | 455 const std::string& script) { |
| 456 return ExecuteScriptInFrame(adapter, std::string(), script); | 456 return ExecuteScriptInFrame(adapter, std::string(), script); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 // The queue should not be empty, unless we were quit because of a timeout. | 634 // The queue should not be empty, unless we were quit because of a timeout. |
| 635 if (message_queue_.empty()) | 635 if (message_queue_.empty()) |
| 636 return false; | 636 return false; |
| 637 if (message) | 637 if (message) |
| 638 *message = message_queue_.front(); | 638 *message = message_queue_.front(); |
| 639 message_queue_.pop(); | 639 message_queue_.pop(); |
| 640 return true; | 640 return true; |
| 641 } | 641 } |
| 642 | 642 |
| 643 } // namespace content | 643 } // namespace content |
| OLD | NEW |