diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-05 03:34:53 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-05 03:34:53 +0000 |
commit | 882b7b2ac4db4348b28f0de68e48febbfd814a70 (patch) | |
tree | aa53683904901bebe5f012851bede0edb20f757c /chrome/browser | |
parent | 428f54bcc8e53882375b7fb9f30b2bc577c59b8a (diff) | |
download | chromium_src-882b7b2ac4db4348b28f0de68e48febbfd814a70.zip chromium_src-882b7b2ac4db4348b28f0de68e48febbfd814a70.tar.gz chromium_src-882b7b2ac4db4348b28f0de68e48febbfd814a70.tar.bz2 |
Adds RenderViewHost::ExecuteJavascriptInWebFrameNotifyResult which
executes script and uses a notification to post the results back.
BUG=54833
TEST=none
Review URL: http://codereview.chromium.org/3591008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@61475 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
4 files changed, 126 insertions, 6 deletions
diff --git a/chrome/browser/debugger/debugger_remote_service.cc b/chrome/browser/debugger/debugger_remote_service.cc index 4c3341b..3d102d5 100644 --- a/chrome/browser/debugger/debugger_remote_service.cc +++ b/chrome/browser/debugger/debugger_remote_service.cc @@ -331,9 +331,7 @@ bool DebuggerRemoteService::DispatchEvaluateJavascript( } std::string javascript; content->GetString(kDataKey, &javascript); - render_view_host->Send( - new ViewMsg_ScriptEvalRequest(render_view_host->routing_id(), - L"", - UTF8ToWide(javascript))); + render_view_host->ExecuteJavascriptInWebFrame(std::wstring(), + UTF8ToWide(javascript)); return false; } diff --git a/chrome/browser/renderer_host/render_view_host.cc b/chrome/browser/renderer_host/render_view_host.cc index de32b0c..a702b08 100644 --- a/chrome/browser/renderer_host/render_view_host.cc +++ b/chrome/browser/renderer_host/render_view_host.cc @@ -14,6 +14,7 @@ #include "base/stats_counters.h" #include "base/string_util.h" #include "base/time.h" +#include "base/values.h" #include "chrome/browser/blocked_plugin_manager.h" #include "chrome/browser/browser_list.h" #include "chrome/browser/child_process_security_policy.h" @@ -490,8 +491,20 @@ void RenderViewHost::ReservePageIDRange(int size) { } void RenderViewHost::ExecuteJavascriptInWebFrame( - const std::wstring& frame_xpath, const std::wstring& jscript) { - Send(new ViewMsg_ScriptEvalRequest(routing_id(), frame_xpath, jscript)); + const std::wstring& frame_xpath, + const std::wstring& jscript) { + Send(new ViewMsg_ScriptEvalRequest(routing_id(), WideToUTF16(frame_xpath), + WideToUTF16(jscript), + 0, false)); +} + +int RenderViewHost::ExecuteJavascriptInWebFrameNotifyResult( + const string16& frame_xpath, + const string16& jscript) { + static int next_id = 1; + Send(new ViewMsg_ScriptEvalRequest(routing_id(), frame_xpath, jscript, + next_id, true)); + return next_id++; } void RenderViewHost::InsertCSSInWebFrame( @@ -871,6 +884,7 @@ void RenderViewHost::OnMessageReceived(const IPC::Message& msg) { IPC_MESSAGE_HANDLER(ViewHostMsg_SetSuggestResult, OnSetSuggestResult) IPC_MESSAGE_HANDLER(ViewHostMsg_DetectedPhishingSite, OnDetectedPhishingSite) + IPC_MESSAGE_HANDLER(ViewHostMsg_ScriptEvalResponse, OnScriptEvalResponse) // Have the super handle all other messages. IPC_MESSAGE_UNHANDLED(RenderWidgetHost::OnMessageReceived(msg)) IPC_END_MESSAGE_MAP_EX() @@ -2102,3 +2116,12 @@ void RenderViewHost::OnDetectedPhishingSite(const GURL& phishing_url, // TODO(noelutz): send an HTTP request to the client-side detection frontends // to confirm that the URL is really phishing. } + +void RenderViewHost::OnScriptEvalResponse(int id, bool result) { + scoped_ptr<Value> result_value(Value::CreateBooleanValue(result)); + std::pair<int, Value*> details(id, result_value.get()); + NotificationService::current()->Notify( + NotificationType::EXECUTE_JAVASCRIPT_RESULT, + Source<RenderViewHost>(this), + Details<std::pair<int, Value*> >(&details)); +} diff --git a/chrome/browser/renderer_host/render_view_host.h b/chrome/browser/renderer_host/render_view_host.h index e5f09ff..b9bf703 100644 --- a/chrome/browser/renderer_host/render_view_host.h +++ b/chrome/browser/renderer_host/render_view_host.h @@ -252,6 +252,11 @@ class RenderViewHost : public RenderWidgetHost { void ExecuteJavascriptInWebFrame(const std::wstring& frame_xpath, const std::wstring& jscript); + // Runs some javascript within the context of a frame in the page. The result + // is sent back via the notification EXECUTE_JAVASCRIPT_RESULT. + int ExecuteJavascriptInWebFrameNotifyResult(const string16& frame_xpath, + const string16& jscript); + // Insert some css into a frame in the page. |id| is optional, and specifies // the element id given when inserting/replacing the style element. void InsertCSSInWebFrame(const std::wstring& frame_xpath, @@ -688,6 +693,7 @@ class RenderViewHost : public RenderWidgetHost { void OnDetectedPhishingSite(const GURL& phishing_url, double phishing_score, const SkBitmap& thumbnail); + void OnScriptEvalResponse(int id, bool result); private: friend class TestRenderViewHost; diff --git a/chrome/browser/renderer_host/test/render_view_host_browsertest.cc b/chrome/browser/renderer_host/test/render_view_host_browsertest.cc new file mode 100644 index 0000000..b75c09b --- /dev/null +++ b/chrome/browser/renderer_host/test/render_view_host_browsertest.cc @@ -0,0 +1,93 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/utf_string_conversions.h" +#include "base/values.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/renderer_host/render_view_host.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/test/in_process_browser_test.h" +#include "chrome/test/ui_test_utils.h" +#include "net/test/test_server.h" + +typedef InProcessBrowserTest RenderViewHostTest; + +typedef std::pair<int, Value*> ExecuteDetailType; + +namespace { + +// NotificationObserver used to listen for EXECUTE_JAVASCRIPT_RESULT +// notifications. +class ExecuteNotificationObserver : public NotificationObserver { + public: + ExecuteNotificationObserver() : id_(0) {} + + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + id_ = (static_cast<Details<ExecuteDetailType > >(details))->first; + Value* value = (static_cast<Details<ExecuteDetailType > >(details))->second; + if (value) + value_.reset(value->DeepCopy()); + MessageLoopForUI::current()->Quit(); + } + + int id() const { return id_; } + + Value* value() const { return value_.get(); } + + private: + int id_; + scoped_ptr<Value> value_; + + DISALLOW_COPY_AND_ASSIGN(ExecuteNotificationObserver); +}; + +} // namespace + +// Makes sure ExecuteJavascriptInWebFrameNotifyResult works. +IN_PROC_BROWSER_TEST_F(RenderViewHostTest, + ExecuteJavascriptInWebFrameNotifyResult) { + ASSERT_TRUE(test_server()->Start()); + GURL empty_url(test_server()->GetURL("files/empty.html")); + ui_test_utils::NavigateToURL(browser(), empty_url); + RenderViewHost* rvh = browser()->GetSelectedTabContents()->render_view_host(); + ASSERT_TRUE(rvh); + + // Execute the script 'true' and make sure we get back true. + int execute_id = rvh->ExecuteJavascriptInWebFrameNotifyResult( + string16(), + ASCIIToUTF16("true;")); + { + ExecuteNotificationObserver observer; + ui_test_utils::RegisterAndWait( + &observer, + NotificationType::EXECUTE_JAVASCRIPT_RESULT, + Source<RenderViewHost>(rvh)); + EXPECT_EQ(execute_id, observer.id()); + ASSERT_TRUE(observer.value()); + bool bool_value; + ASSERT_TRUE(observer.value()->GetAsBoolean(&bool_value)); + EXPECT_TRUE(bool_value); + } + + // Execute the script 'false' and make sure we get back false. + int execute_id2 = rvh->ExecuteJavascriptInWebFrameNotifyResult( + string16(), + ASCIIToUTF16("false;")); + // The ids should change. + EXPECT_NE(execute_id, execute_id2); + { + ExecuteNotificationObserver observer; + ui_test_utils::RegisterAndWait( + &observer, + NotificationType::EXECUTE_JAVASCRIPT_RESULT, + Source<RenderViewHost>(rvh)); + EXPECT_EQ(execute_id2, observer.id()); + ASSERT_TRUE(observer.value()); + bool bool_value; + ASSERT_TRUE(observer.value()->GetAsBoolean(&bool_value)); + EXPECT_FALSE(bool_value); + } +} |