diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-27 19:13:17 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-27 19:13:17 +0000 |
commit | f82d57b5aac005760209900be768192f66a38361 (patch) | |
tree | c6fd653cf6f6f6b7f6b19ce34ad3492e4758d42d | |
parent | fa71e8aecde3d31a2a8e75df8034bdef6e237f35 (diff) | |
download | chromium_src-f82d57b5aac005760209900be768192f66a38361.zip chromium_src-f82d57b5aac005760209900be768192f66a38361.tar.gz chromium_src-f82d57b5aac005760209900be768192f66a38361.tar.bz2 |
Revert "Revert 83168 - Revert "Revert 83100 - Remove weird dependency on extensions from webui.Re-plumb extension request messages in a more sane way.Before, each RVH had ProcessWebUIMessage(), which wasserving as a manual way of plumbing both WebUI andextension messages to the right place, even though onlya few RVHD responded to either message.Instead of this, we now just teach more of the stack howto handle IPC messages in general, and delegate them upthrough the stack, giving each layer a chance to handlethem if it knows how.The result is simpler and smaller:179 insertions(+), 252 deletions(-)BUG=80311Review URL: http://codereview.chromium.org/6901021"This reverts commit 643b280cedd9f0b76948686f39f50f295aba362f.TBR=mpcomplete@chromium.org"
This reverts commit ba6603c3cc46f1b83219be906ab36f8f2b3372e2.
TBR=mpcomplete@chromium.org
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@83188 0039d316-1c4b-4281-b951-d872f2087c98
33 files changed, 178 insertions, 185 deletions
diff --git a/chrome/browser/chromeos/notifications/balloon_view_host.cc b/chrome/browser/chromeos/notifications/balloon_view_host.cc index 1e9be4f..a341f18 100644 --- a/chrome/browser/chromeos/notifications/balloon_view_host.cc +++ b/chrome/browser/chromeos/notifications/balloon_view_host.cc @@ -7,6 +7,9 @@ #include "base/stl_util-inl.h" #include "base/values.h" #include "chrome/common/extensions/extension_messages.h" +#include "content/common/view_messages.h" +#include "ipc/ipc_message.h" +#include "ipc/ipc_message_macros.h" namespace chromeos { @@ -15,6 +18,15 @@ BalloonViewHost::~BalloonViewHost() { message_callbacks_.end()); } +bool BalloonViewHost::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(BalloonViewHost, message) + IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + bool BalloonViewHost::AddWebUIMessageCallback( const std::string& message, MessageCallback* callback) { @@ -25,18 +37,17 @@ bool BalloonViewHost::AddWebUIMessageCallback( return ret.second; } -void BalloonViewHost::ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) { - ::BalloonViewHost::ProcessWebUIMessage(params); - +void BalloonViewHost::OnWebUISend(const GURL& source_url, + const std::string& name, + const ListValue& args) { // Look up the callback for this message. MessageCallbackMap::const_iterator callback = - message_callbacks_.find(params.name); + message_callbacks_.find(name); if (callback == message_callbacks_.end()) return; // Run callback. - callback->second->Run(¶ms.arguments); + callback->second->Run(&args); } } // namespace chromeos diff --git a/chrome/browser/chromeos/notifications/balloon_view_host.h b/chrome/browser/chromeos/notifications/balloon_view_host.h index 5b6a3ca5..7b1a3e5 100644 --- a/chrome/browser/chromeos/notifications/balloon_view_host.h +++ b/chrome/browser/chromeos/notifications/balloon_view_host.h @@ -12,6 +12,7 @@ #include <string> #include "base/callback.h" +#include "base/compiler_specific.h" #include "ui/gfx/native_widget_types.h" class ListValue; @@ -33,15 +34,19 @@ class BalloonViewHost : public ::BalloonViewHost { bool AddWebUIMessageCallback(const std::string& message, MessageCallback* callback); - // Process WebUI message. - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params); - private: + // RenderViewHostDelegate + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + // A map of message name -> message handling callback. typedef std::map<std::string, MessageCallback*> MessageCallbackMap; MessageCallbackMap message_callbacks_; + // Message handlers. + virtual void OnWebUISend(const GURL& source_url, + const std::string& name, + const ListValue& args); + DISALLOW_COPY_AND_ASSIGN(BalloonViewHost); }; diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 26b466b..efca9b6 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -26,7 +26,6 @@ #include "chrome/browser/extensions/extension_idle_api.h" #include "chrome/browser/extensions/extension_infobar_module.h" #include "chrome/browser/extensions/extension_management_api.h" -#include "chrome/browser/extensions/extension_message_service.h" #include "chrome/browser/extensions/extension_metrics_module.h" #include "chrome/browser/extensions/extension_module.h" #include "chrome/browser/extensions/extension_omnibox_api.h" @@ -54,10 +53,13 @@ #include "chrome/browser/ui/webui/favicon_source.h" #include "chrome/common/extensions/extension_messages.h" #include "chrome/common/url_constants.h" +#include "content/browser/child_process_security_policy.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_view_host.h" #include "content/common/notification_service.h" #include "content/common/result_codes.h" +#include "ipc/ipc_message.h" +#include "ipc/ipc_message_macros.h" #include "third_party/skia/include/core/SkBitmap.h" #if defined(TOOLKIT_VIEWS) @@ -473,8 +475,28 @@ Browser* ExtensionFunctionDispatcher::GetCurrentBrowser( return browser; } -void ExtensionFunctionDispatcher::HandleRequest( - const ExtensionHostMsg_DomMessage_Params& params) { +bool ExtensionFunctionDispatcher::OnMessageReceived( + const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(ExtensionFunctionDispatcher, message) + IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +void ExtensionFunctionDispatcher::OnRequest( + const ExtensionHostMsg_Request_Params& params) { + if (!ChildProcessSecurityPolicy::GetInstance()-> + HasExtensionBindings(render_view_host_->process()->id())) { + // This can happen if someone uses window.open() to open an extension URL + // from a non-extension context. + render_view_host_->Send(new ExtensionMsg_Response( + render_view_host_->routing_id(), params.request_id, false, + std::string(), "Access to extension API denied.")); + return; + } + scoped_refptr<ExtensionFunction> function( FactoryRegistry::GetInstance()->NewFunction(params.name)); function->set_dispatcher_peer(peer_); diff --git a/chrome/browser/extensions/extension_function_dispatcher.h b/chrome/browser/extensions/extension_function_dispatcher.h index cb67988..9f91d9d 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.h +++ b/chrome/browser/extensions/extension_function_dispatcher.h @@ -11,6 +11,7 @@ #include "base/memory/ref_counted.h" #include "googleurl/src/gurl.h" +#include "ipc/ipc_channel.h" #include "ui/gfx/native_widget_types.h" class Browser; @@ -20,7 +21,7 @@ class ListValue; class Profile; class RenderViewHost; class TabContents; -struct ExtensionHostMsg_DomMessage_Params; +struct ExtensionHostMsg_Request_Params; // A factory function for creating new ExtensionFunction instances. typedef ExtensionFunction* (*ExtensionFunctionFactory)(); @@ -28,7 +29,7 @@ typedef ExtensionFunction* (*ExtensionFunctionFactory)(); // ExtensionFunctionDispatcher receives requests to execute functions from // Chromium extensions running in a RenderViewHost and dispatches them to the // appropriate handler. It lives entirely on the UI thread. -class ExtensionFunctionDispatcher { +class ExtensionFunctionDispatcher : public IPC::Channel::Listener { public: class Delegate { public: @@ -85,8 +86,8 @@ class ExtensionFunctionDispatcher { Delegate* delegate() { return delegate_; } - // Handle a request to execute an extension function. - void HandleRequest(const ExtensionHostMsg_DomMessage_Params& params); + // If |message| is an extension request, handle it. Returns true if handled. + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; // Send a response to a function. void SendResponse(ExtensionFunction* api, bool success); @@ -121,6 +122,9 @@ class ExtensionFunctionDispatcher { const Extension* extension, const GURL& url); + // Message handlers. + void OnRequest(const ExtensionHostMsg_Request_Params& params); + // We need to keep a pointer to the profile because we use it in the dtor // in sending EXTENSION_FUNCTION_DISPATCHER_DESTROYED, but by that point // the render_view_host_ has been deleted. diff --git a/chrome/browser/extensions/extension_host.cc b/chrome/browser/extensions/extension_host.cc index 74f28e3..d2e5bec 100644 --- a/chrome/browser/extensions/extension_host.cc +++ b/chrome/browser/extensions/extension_host.cc @@ -546,13 +546,6 @@ WebPreferences ExtensionHost::GetWebkitPrefs() { return webkit_prefs; } -void ExtensionHost::ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) { - if (extension_function_dispatcher_.get()) { - extension_function_dispatcher_->HandleRequest(params); - } -} - RenderViewHostDelegate::View* ExtensionHost::GetViewDelegate() { return this; } @@ -784,6 +777,11 @@ ViewType::Type ExtensionHost::GetRenderViewType() const { } bool ExtensionHost::OnMessageReceived(const IPC::Message& message) { + if (extension_function_dispatcher_.get() && + extension_function_dispatcher_->OnMessageReceived(message)) { + return true; + } + bool handled = true; IPC_BEGIN_MESSAGE_MAP(ExtensionHost, message) IPC_MESSAGE_HANDLER(ViewHostMsg_RunFileChooser, OnRunFileChooser) diff --git a/chrome/browser/extensions/extension_host.h b/chrome/browser/extensions/extension_host.h index 63adef8..1a23d20 100644 --- a/chrome/browser/extensions/extension_host.h +++ b/chrome/browser/extensions/extension_host.h @@ -128,8 +128,6 @@ class ExtensionHost : public RenderViewHostDelegate, // RenderViewHostDelegate implementation. virtual RenderViewHostDelegate::View* GetViewDelegate(); virtual WebPreferences GetWebkitPrefs(); - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params); virtual void RunJavaScriptMessage(const std::wstring& message, const std::wstring& default_prompt, const GURL& frame_url, diff --git a/chrome/browser/extensions/extension_message_handler.cc b/chrome/browser/extensions/extension_message_handler.cc index 52517cb..4447dac 100644 --- a/chrome/browser/extensions/extension_message_handler.cc +++ b/chrome/browser/extensions/extension_message_handler.cc @@ -10,7 +10,6 @@ #include "content/browser/child_process_security_policy.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_view_host.h" -#include "content/browser/renderer_host/render_view_host_delegate.h" ExtensionMessageHandler::ExtensionMessageHandler( RenderViewHost* render_view_host) @@ -25,7 +24,6 @@ bool ExtensionMessageHandler::OnMessageReceived( bool handled = true; IPC_BEGIN_MESSAGE_MAP(ExtensionMessageHandler, message) IPC_MESSAGE_HANDLER(ExtensionHostMsg_PostMessage, OnPostMessage) - IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP() return handled; @@ -39,18 +37,3 @@ void ExtensionMessageHandler::OnPostMessage(int port_id, port_id, message); } } - -void ExtensionMessageHandler::OnRequest( - const ExtensionHostMsg_DomMessage_Params& params) { - if (!ChildProcessSecurityPolicy::GetInstance()-> - HasExtensionBindings(render_view_host()->process()->id())) { - // This can happen if someone uses window.open() to open an extension URL - // from a non-extension context. - Send(new ExtensionMsg_Response( - routing_id(), params.request_id, false, std::string(), - "Access to extension API denied.")); - return; - } - - render_view_host()->delegate()->ProcessWebUIMessage(params); -} diff --git a/chrome/browser/extensions/extension_message_handler.h b/chrome/browser/extensions/extension_message_handler.h index 3a5c050..0ba4940 100644 --- a/chrome/browser/extensions/extension_message_handler.h +++ b/chrome/browser/extensions/extension_message_handler.h @@ -14,6 +14,12 @@ struct ExtensionHostMsg_DomMessage_Params; // Filters and dispatches extension-related IPC messages that arrive from // renderers. There is one of these objects for each RenderViewHost in Chrome. // Contrast this with ExtensionTabHelper, which is only created for TabContents. +// +// TODO(aa): Handling of content script messaging should be able to move to EFD +// once there is an EFD for every RVHD where extension code can run. Then we +// could eliminate this class. Right now, we don't end up with an EFD for tab +// contents unless that tab contents is hosting chrome-extension:// URLs. That +// still leaves content scripts. See also: crbug.com/80307. class ExtensionMessageHandler : public RenderViewHostObserver { public: // |sender| is guaranteed to outlive this object. @@ -26,7 +32,6 @@ class ExtensionMessageHandler : public RenderViewHostObserver { private: // Message handlers. void OnPostMessage(int port_id, const std::string& message); - void OnRequest(const ExtensionHostMsg_DomMessage_Params& params); DISALLOW_COPY_AND_ASSIGN(ExtensionMessageHandler); }; diff --git a/chrome/browser/extensions/extension_web_ui.cc b/chrome/browser/extensions/extension_web_ui.cc index 1a9b122..7192c03 100644 --- a/chrome/browser/extensions/extension_web_ui.cc +++ b/chrome/browser/extensions/extension_web_ui.cc @@ -28,6 +28,7 @@ #include "content/browser/tab_contents/tab_contents.h" #include "content/common/bindings_policy.h" #include "content/common/page_transition_types.h" +#include "ipc/ipc_message.h" #include "net/base/file_stream.h" #include "third_party/skia/include/core/SkBitmap.h" #include "ui/gfx/codec/png_codec.h" @@ -184,9 +185,11 @@ void ExtensionWebUI::RenderViewReused(RenderViewHost* render_view_host) { ResetExtensionBookmarkManagerEventRouter(); } -void ExtensionWebUI::ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) { - extension_function_dispatcher_->HandleRequest(params); +bool ExtensionWebUI::OnMessageReceived(const IPC::Message& message) { + if (extension_function_dispatcher_.get()) + return extension_function_dispatcher_->OnMessageReceived(message); + + return false; } Browser* ExtensionWebUI::GetBrowser() { diff --git a/chrome/browser/extensions/extension_web_ui.h b/chrome/browser/extensions/extension_web_ui.h index 34f0899..f41ead3 100644 --- a/chrome/browser/extensions/extension_web_ui.h +++ b/chrome/browser/extensions/extension_web_ui.h @@ -14,6 +14,7 @@ #include "chrome/browser/favicon_service.h" #include "chrome/common/extensions/extension.h" #include "content/browser/webui/web_ui.h" +#include "ipc/ipc_channel.h" class GURL; class ListValue; @@ -21,7 +22,7 @@ class PrefService; class Profile; class RenderViewHost; class TabContents; -struct ExtensionHostMsg_DomMessage_Params; +struct ExtensionHostMsg_Request_Params; // This class implements WebUI for extensions and allows extensions to put UI in // the main tab contents area. For example, each extension can specify an @@ -42,10 +43,9 @@ class ExtensionWebUI } // WebUI + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; virtual void RenderViewCreated(RenderViewHost* render_view_host); virtual void RenderViewReused(RenderViewHost* render_view_host); - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params); // ExtensionFunctionDispatcher::Delegate virtual Browser* GetBrowser(); diff --git a/chrome/browser/notifications/balloon_host.cc b/chrome/browser/notifications/balloon_host.cc index ff6a035..3d600a0 100644 --- a/chrome/browser/notifications/balloon_host.cc +++ b/chrome/browser/notifications/balloon_host.cc @@ -22,6 +22,7 @@ #include "content/common/notification_type.h" #include "content/common/renderer_preferences.h" #include "content/common/view_messages.h" +#include "ipc/ipc_message.h" #include "webkit/glue/webpreferences.h" BalloonHost::BalloonHost(Balloon* balloon) @@ -131,11 +132,11 @@ RenderViewHostDelegate::View* BalloonHost::GetViewDelegate() { return this; } -void BalloonHost::ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) { - if (extension_function_dispatcher_.get()) { - extension_function_dispatcher_->HandleRequest(params); - } +bool BalloonHost::OnMessageReceived(const IPC::Message& message) { + if (extension_function_dispatcher_.get()) + return extension_function_dispatcher_->OnMessageReceived(message); + + return false; } // RenderViewHostDelegate::View methods implemented to allow links to diff --git a/chrome/browser/notifications/balloon_host.h b/chrome/browser/notifications/balloon_host.h index 475d6bd..9a70e61 100644 --- a/chrome/browser/notifications/balloon_host.h +++ b/chrome/browser/notifications/balloon_host.h @@ -9,6 +9,7 @@ #include <string> #include <vector> +#include "base/compiler_specific.h" #include "base/scoped_ptr.h" #include "chrome/browser/extensions/extension_function_dispatcher.h" #include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" @@ -23,6 +24,10 @@ class SiteInstance; struct RendererPreferences; struct WebPreferences; +namespace IPC { +class Message; +} + class BalloonHost : public RenderViewHostDelegate, public RenderViewHostDelegate::View, public ExtensionFunctionDispatcher::Delegate, @@ -61,8 +66,6 @@ class BalloonHost : public RenderViewHostDelegate, virtual int GetBrowserWindowID() const; virtual ViewType::Type GetRenderViewType() const; virtual RenderViewHostDelegate::View* GetViewDelegate(); - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params); // NotificationObserver override. virtual void Observe(NotificationType type, @@ -131,6 +134,9 @@ class BalloonHost : public RenderViewHostDelegate, RenderViewHost* render_view_host_; private: + // RenderViewHostDelegate + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + // Called to send an event that the balloon has been disconnected from // a renderer (if should_notify_on_disconnect_ is true). void NotifyDisconnect(); diff --git a/chrome/browser/tab_contents/background_contents.cc b/chrome/browser/tab_contents/background_contents.cc index c994fad..e5f98b8 100644 --- a/chrome/browser/tab_contents/background_contents.cc +++ b/chrome/browser/tab_contents/background_contents.cc @@ -10,7 +10,6 @@ #include "chrome/browser/renderer_preferences_util.h" #include "chrome/browser/ui/webui/chrome_web_ui_factory.h" #include "chrome/common/extensions/extension_constants.h" -#include "chrome/common/extensions/extension_messages.h" #include "chrome/common/url_constants.h" #include "chrome/common/view_types.h" #include "content/browser/browsing_instance.h" @@ -208,15 +207,6 @@ WebPreferences BackgroundContents::GetWebkitPrefs() { false); // is_web_ui } -void BackgroundContents::ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) { - // TODO(rafaelw): It may make sense for extensions to be able to open - // BackgroundContents to chrome-extension://<id> pages. Consider implementing. - render_view_host_->Send(new ExtensionMsg_Response( - render_view_host_->routing_id(), params.request_id, false, - std::string(), "Access to extension API denied.")); -} - void BackgroundContents::CreateNewWindow( int route_id, const ViewHostMsg_CreateWindow_Params& params) { diff --git a/chrome/browser/tab_contents/background_contents.h b/chrome/browser/tab_contents/background_contents.h index 1e7a451e..f4e8874 100644 --- a/chrome/browser/tab_contents/background_contents.h +++ b/chrome/browser/tab_contents/background_contents.h @@ -19,7 +19,6 @@ #include "webkit/glue/window_open_disposition.h" class TabContents; -struct ExtensionHostMsg_DomMessage_Params; struct WebPreferences; namespace gfx { @@ -68,8 +67,6 @@ class BackgroundContents : public RenderViewHostDelegate, virtual void DidNavigate(RenderViewHost* render_view_host, const ViewHostMsg_FrameNavigate_Params& params); virtual WebPreferences GetWebkitPrefs(); - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params); virtual void RunJavaScriptMessage(const std::wstring& message, const std::wstring& default_prompt, const GURL& frame_url, diff --git a/chrome/browser/ui/webui/sync_internals_ui.cc b/chrome/browser/ui/webui/sync_internals_ui.cc index 8ab946e1..e11eb23 100644 --- a/chrome/browser/ui/webui/sync_internals_ui.cc +++ b/chrome/browser/ui/webui/sync_internals_ui.cc @@ -42,10 +42,10 @@ SyncInternalsUI::~SyncInternalsUI() { } } -void SyncInternalsUI::ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) { - const std::string& name = params.name; - browser_sync::JsArgList args(params.arguments); +void SyncInternalsUI::OnWebUISend(const GURL& source_url, + const std::string& name, + const ListValue& content) { + browser_sync::JsArgList args(content); VLOG(1) << "Received message: " << name << " with args " << args.ToString(); // We handle this case directly because it needs to work even if diff --git a/chrome/browser/ui/webui/sync_internals_ui.h b/chrome/browser/ui/webui/sync_internals_ui.h index 589b712..e5a8118 100644 --- a/chrome/browser/ui/webui/sync_internals_ui.h +++ b/chrome/browser/ui/webui/sync_internals_ui.h @@ -8,6 +8,7 @@ #include <string> +#include "base/compiler_specific.h" #include "base/basictypes.h" #include "chrome/browser/sync/js_event_handler.h" #include "content/browser/webui/web_ui.h" @@ -35,12 +36,13 @@ class SyncInternalsUI : public WebUI, public browser_sync::JsEventHandler { // // TODO(akalin): Add a simple isSyncEnabled() message and make // getAboutInfo() be handled by the sync service. - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params); + virtual void OnWebUISend(const GURL& source_url, + const std::string& name, + const ListValue& args) OVERRIDE; // browser_sync::JsEventHandler implementation. virtual void HandleJsEvent(const std::string& name, - const browser_sync::JsArgList& args); + const browser_sync::JsArgList& args) OVERRIDE; private: // Returns the sync service's JsFrontend object, or NULL if the sync diff --git a/chrome/browser/ui/webui/sync_internals_ui_unittest.cc b/chrome/browser/ui/webui/sync_internals_ui_unittest.cc index 1407f10..27b46a7 100644 --- a/chrome/browser/ui/webui/sync_internals_ui_unittest.cc +++ b/chrome/browser/ui/webui/sync_internals_ui_unittest.cc @@ -12,7 +12,6 @@ #include "chrome/browser/sync/js_arg_list.h" #include "chrome/browser/sync/js_test_util.h" #include "chrome/browser/sync/profile_sync_service_mock.h" -#include "chrome/common/extensions/extension_messages.h" #include "chrome/test/profile_mock.h" #include "content/browser/browser_thread.h" #include "content/browser/renderer_host/test_render_view_host.h" @@ -161,33 +160,33 @@ TEST_F(SyncInternalsUITest, HandleJsEventNullService) { GetTestSyncInternalsUI()->HandleJsEvent("testMessage", JsArgList(args)); } -TEST_F(SyncInternalsUITest, ProcessWebUIMessageBasic) { +TEST_F(SyncInternalsUITest, OnWebUISendBasic) { ExpectSetupTeardownCalls(); - ExtensionHostMsg_DomMessage_Params params; - params.name = "testName"; - params.arguments.Append(Value::CreateIntegerValue(10)); + std::string name = "testName"; + ListValue args; + args.Append(Value::CreateIntegerValue(10)); EXPECT_CALL(mock_js_backend_, - ProcessMessage(params.name, HasArgsAsList(params.arguments), + ProcessMessage(name, HasArgsAsList(args), GetTestSyncInternalsUIAddress())); ConstructTestSyncInternalsUI(); - GetTestSyncInternalsUI()->ProcessWebUIMessage(params); + GetTestSyncInternalsUI()->OnWebUISend(GURL(), name, args); } -TEST_F(SyncInternalsUITest, ProcessWebUIMessageBasicNullService) { +TEST_F(SyncInternalsUITest, OnWebUISendBasicNullService) { ExpectSetupTeardownCallsNullService(); ConstructTestSyncInternalsUI(); - ExtensionHostMsg_DomMessage_Params params; - params.name = "testName"; - params.arguments.Append(Value::CreateIntegerValue(5)); + std::string name = "testName"; + ListValue args; + args.Append(Value::CreateIntegerValue(5)); // Should drop the message. - GetTestSyncInternalsUI()->ProcessWebUIMessage(params); + GetTestSyncInternalsUI()->OnWebUISend(GURL(), name, args); } namespace { @@ -195,32 +194,28 @@ const char kAboutInfoCall[] = "onGetAboutInfoFinished({\"summary\":\"SYNC DISABLED\"});"; } // namespace -TEST_F(SyncInternalsUITest, ProcessWebUIMessageGetAboutInfo) { +TEST_F(SyncInternalsUITest, OnWebUISendGetAboutInfo) { ExpectSetupTeardownCalls(); - ExtensionHostMsg_DomMessage_Params params; - params.name = "getAboutInfo"; - ConstructTestSyncInternalsUI(); EXPECT_CALL(*GetTestSyncInternalsUI(), ExecuteJavascript(ASCIIToUTF16(kAboutInfoCall))); - GetTestSyncInternalsUI()->ProcessWebUIMessage(params); + ListValue args; + GetTestSyncInternalsUI()->OnWebUISend(GURL(), "getAboutInfo", args); } -TEST_F(SyncInternalsUITest, ProcessWebUIMessageGetAboutInfoNullService) { +TEST_F(SyncInternalsUITest, OnWebUISendGetAboutInfoNullService) { ExpectSetupTeardownCallsNullService(); - ExtensionHostMsg_DomMessage_Params params; - params.name = "getAboutInfo"; - ConstructTestSyncInternalsUI(); EXPECT_CALL(*GetTestSyncInternalsUI(), ExecuteJavascript(ASCIIToUTF16(kAboutInfoCall))); - GetTestSyncInternalsUI()->ProcessWebUIMessage(params); + ListValue args; + GetTestSyncInternalsUI()->OnWebUISend(GURL(), "getAboutInfo", args); } } // namespace diff --git a/chrome/common/extensions/extension_messages.h b/chrome/common/extensions/extension_messages.h index f2f5c34..74378dd 100644 --- a/chrome/common/extensions/extension_messages.h +++ b/chrome/common/extensions/extension_messages.h @@ -16,7 +16,7 @@ #define IPC_MESSAGE_START ExtensionMsgStart // Parameters structure for ExtensionHostMsg_Request. -IPC_STRUCT_BEGIN(ExtensionHostMsg_DomMessage_Params) +IPC_STRUCT_BEGIN(ExtensionHostMsg_Request_Params) // Message name. IPC_STRUCT_MEMBER(std::string, name) @@ -214,7 +214,7 @@ IPC_MESSAGE_ROUTED1(ExtensionMsg_GetApplicationInfo, // A renderer sends this message when an extension process starts an API // request. The browser will always respond with a ExtensionMsg_Response. IPC_MESSAGE_ROUTED1(ExtensionHostMsg_Request, - ExtensionHostMsg_DomMessage_Params) + ExtensionHostMsg_Request_Params) // Notify the browser that the given extension added a listener to an event. IPC_MESSAGE_CONTROL2(ExtensionHostMsg_AddListener, diff --git a/chrome/renderer/extensions/extension_process_bindings.cc b/chrome/renderer/extensions/extension_process_bindings.cc index 8b7c5fa..dec277e 100644 --- a/chrome/renderer/extensions/extension_process_bindings.cc +++ b/chrome/renderer/extensions/extension_process_bindings.cc @@ -364,7 +364,7 @@ class ExtensionImpl : public ExtensionBase { GetPendingRequestMap()[request_id].reset(new PendingRequest( current_context, name)); - ExtensionHostMsg_DomMessage_Params params; + ExtensionHostMsg_Request_Params params; params.name = name; params.arguments.Swap(value_args); params.source_url = source_url; diff --git a/content/DEPS b/content/DEPS index bf29db6..991fd7d3 100644 --- a/content/DEPS +++ b/content/DEPS @@ -8,9 +8,13 @@ include_rules = [ # When the src\content refactoring is complete, this will be unnecessary (and # in fact, a layering violation). "+chrome", + # The following directories have been refactored, so no new dependencies # should be added from these directories. "-chrome/browser/printing", + "-chrome/browser/extensions", + "-chrome/common/extensions", + "-chrome/renderer/extensions", # The subdirectories in content/ will manually allow their own include # directories in content/ so we disallow all of them. diff --git a/content/browser/renderer_host/DEPS b/content/browser/renderer_host/DEPS index d8e9a30..b12c3bd 100644 --- a/content/browser/renderer_host/DEPS +++ b/content/browser/renderer_host/DEPS @@ -1,4 +1,5 @@ include_rules = [ "+content/renderer", # For single-process mode. "+third_party/zlib", + "+chrome/common/extensions" ] diff --git a/content/browser/renderer_host/browser_render_process_host.h b/content/browser/renderer_host/browser_render_process_host.h index 37be1f1..822da69 100644 --- a/content/browser/renderer_host/browser_render_process_host.h +++ b/content/browser/renderer_host/browser_render_process_host.h @@ -15,7 +15,6 @@ #include "base/platform_file.h" #include "base/process.h" #include "base/timer.h" -#include "chrome/common/extensions/extension.h" #include "content/browser/child_process_launcher.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/common/notification_observer.h" diff --git a/content/browser/renderer_host/render_message_filter.cc b/content/browser/renderer_host/render_message_filter.cc index ad28245..945dcfc 100644 --- a/content/browser/renderer_host/render_message_filter.cc +++ b/content/browser/renderer_host/render_message_filter.cc @@ -25,6 +25,7 @@ #include "chrome/browser/platform_util.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/chrome_switches.h" +#include "chrome/common/extensions/extension.h" #include "chrome/common/render_messages.h" #include "chrome/common/url_constants.h" #include "content/browser/browser_thread.h" diff --git a/content/browser/renderer_host/render_view_host.cc b/content/browser/renderer_host/render_view_host.cc index 0b7a4a4..b6fd42f 100644 --- a/content/browser/renderer_host/render_view_host.cc +++ b/content/browser/renderer_host/render_view_host.cc @@ -21,7 +21,6 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_switches.h" -#include "chrome/common/extensions/extension_messages.h" #include "chrome/common/icon_messages.h" #include "chrome/common/render_messages.h" #include "chrome/common/translate_errors.h" @@ -763,7 +762,6 @@ bool RenderViewHost::OnMessageReceived(const IPC::Message& msg) { OnMsgDidContentsPreferredSizeChange) IPC_MESSAGE_HANDLER(ViewHostMsg_DomOperationResponse, OnMsgDomOperationResponse) - IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnMsgWebUISend) IPC_MESSAGE_HANDLER(ViewHostMsg_ForwardMessageToExternalHost, OnMsgForwardMessageToExternalHost) IPC_MESSAGE_HANDLER(ViewHostMsg_SetTooltipText, OnMsgSetTooltipText) @@ -1088,40 +1086,6 @@ void RenderViewHost::OnMsgDomOperationResponse( Details<DomOperationNotificationDetails>(&details)); } -void RenderViewHost::OnMsgWebUISend( - const GURL& source_url, const std::string& message, - const std::string& content) { - if (!ChildProcessSecurityPolicy::GetInstance()-> - HasWebUIBindings(process()->id())) { - NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; - return; - } - - scoped_ptr<Value> value; - if (!content.empty()) { - value.reset(base::JSONReader::Read(content, false)); - if (!value.get() || !value->IsType(Value::TYPE_LIST)) { - // The page sent us something that we didn't understand. - // This probably indicates a programming error. - NOTREACHED() << "Invalid JSON argument in OnMsgWebUISend."; - return; - } - } - - ExtensionHostMsg_DomMessage_Params params; - params.name = message; - if (value.get()) - params.arguments.Swap(static_cast<ListValue*>(value.get())); - params.source_url = source_url; - // WebUI doesn't use these values yet. - // TODO(aa): When WebUI is ported to ExtensionFunctionDispatcher, send real - // values here. - params.request_id = -1; - params.has_callback = false; - params.user_gesture = false; - delegate_->ProcessWebUIMessage(params); -} - void RenderViewHost::OnMsgForwardMessageToExternalHost( const std::string& message, const std::string& origin, const std::string& target) { diff --git a/content/browser/renderer_host/render_view_host.h b/content/browser/renderer_host/render_view_host.h index c9e7027..281bfeb 100644 --- a/content/browser/renderer_host/render_view_host.h +++ b/content/browser/renderer_host/render_view_host.h @@ -544,9 +544,6 @@ class RenderViewHost : public RenderWidgetHost { void OnMsgDidContentsPreferredSizeChange(const gfx::Size& new_size); void OnMsgDomOperationResponse(const std::string& json_string, int automation_id); - void OnMsgWebUISend(const GURL& source_url, - const std::string& message, - const std::string& content); void OnMsgForwardMessageToExternalHost(const std::string& message, const std::string& origin, const std::string& target); diff --git a/content/browser/renderer_host/render_view_host_delegate.h b/content/browser/renderer_host/render_view_host_delegate.h index f4f0783..f95a76d 100644 --- a/content/browser/renderer_host/render_view_host_delegate.h +++ b/content/browser/renderer_host/render_view_host_delegate.h @@ -45,7 +45,6 @@ class SkBitmap; class SSLClientAuthHandler; class SSLAddCertHandler; class TabContents; -struct ExtensionHostMsg_DomMessage_Params; struct ViewHostMsg_CreateWindow_Params; struct ViewHostMsg_FrameNavigate_Params; struct WebApplicationInfo; @@ -489,11 +488,6 @@ class RenderViewHostDelegate : public IPC::Channel::Listener { virtual void DomOperationResponse(const std::string& json_string, int automation_id) {} - // A message was sent from HTML-based UI. - // By default we ignore such messages. - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) {} - // A message for external host. By default we ignore such messages. // |receiver| can be a receiving script and |message| is any // arbitrary string that makes sense to the receiver. diff --git a/content/browser/tab_contents/background_contents.h b/content/browser/tab_contents/background_contents.h index e544fde..ef03cd4 100644 --- a/content/browser/tab_contents/background_contents.h +++ b/content/browser/tab_contents/background_contents.h @@ -67,7 +67,6 @@ class BackgroundContents : public RenderViewHostDelegate, virtual void DidNavigate(RenderViewHost* render_view_host, const ViewHostMsg_FrameNavigate_Params& params); virtual WebPreferences GetWebkitPrefs(); - virtual void ProcessWebUIMessage(const ViewHostMsg_DomMessage_Params& params); virtual void RunJavaScriptMessage(const std::wstring& message, const std::wstring& default_prompt, const GURL& frame_url, diff --git a/content/browser/tab_contents/tab_contents.cc b/content/browser/tab_contents/tab_contents.cc index 6c72691..2a0a0d5 100644 --- a/content/browser/tab_contents/tab_contents.cc +++ b/content/browser/tab_contents/tab_contents.cc @@ -52,7 +52,6 @@ #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/content_restriction.h" -#include "chrome/common/extensions/extension_messages.h" #include "chrome/common/icon_messages.h" #include "chrome/common/pref_names.h" #include "chrome/common/render_messages.h" @@ -366,6 +365,9 @@ void TabContents::AddObservers() { } bool TabContents::OnMessageReceived(const IPC::Message& message) { + if (web_ui() && web_ui()->OnMessageReceived(message)) + return true; + ObserverListBase<TabContentsObserver>::Iterator it(observers_); TabContentsObserver* observer; while ((observer = it.GetNext()) != NULL) @@ -2007,19 +2009,6 @@ void TabContents::DomOperationResponse(const std::string& json_string, int automation_id) { } -void TabContents::ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) { - if (!render_manager_.web_ui()) { - // This can happen if someone uses window.open() to open an extension URL - // from a non-extension context. - render_view_host()->Send(new ExtensionMsg_Response( - render_view_host()->routing_id(), params.request_id, false, "", - "Access to extension API denied.")); - return; - } - render_manager_.web_ui()->ProcessWebUIMessage(params); -} - void TabContents::ProcessExternalHostMessage(const std::string& message, const std::string& origin, const std::string& target) { diff --git a/content/browser/tab_contents/tab_contents.h b/content/browser/tab_contents/tab_contents.h index 17992dc..f690393 100644 --- a/content/browser/tab_contents/tab_contents.h +++ b/content/browser/tab_contents/tab_contents.h @@ -73,7 +73,6 @@ class TabContentsObserver; class TabContentsSSLHelper; class TabContentsView; class URLPattern; -struct ExtensionHostMsg_DomMessage_Params; struct RendererPreferences; struct ThumbnailScore; struct ViewHostMsg_FrameNavigate_Params; @@ -807,8 +806,6 @@ class TabContents : public PageNavigator, WindowOpenDisposition disposition); virtual void DomOperationResponse(const std::string& json_string, int automation_id); - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params); virtual void ProcessExternalHostMessage(const std::string& message, const std::string& origin, const std::string& target); diff --git a/content/browser/webui/web_ui.cc b/content/browser/webui/web_ui.cc index 636f9e8..995a229 100644 --- a/content/browser/webui/web_ui.cc +++ b/content/browser/webui/web_ui.cc @@ -10,13 +10,17 @@ #include "base/string_number_conversions.h" #include "base/utf_string_conversions.h" #include "base/values.h" -#include "chrome/common/extensions/extension_messages.h" #include "chrome/common/render_messages.h" +#include "content/browser/child_process_security_policy.h" +#include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_view_host.h" #include "content/browser/tab_contents/tab_contents.h" #include "content/browser/tab_contents/tab_contents_view.h" #include "content/browser/webui/generic_handler.h" #include "content/common/bindings_policy.h" +#include "content/common/view_messages.h" +#include "ipc/ipc_message.h" +#include "ipc/ipc_message_macros.h" namespace { @@ -60,16 +64,32 @@ WebUI::~WebUI() { const WebUI::TypeID WebUI::kNoWebUI = NULL; -void WebUI::ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params) { +bool WebUI::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(WebUI, message) + IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +void WebUI::OnWebUISend(const GURL& source_url, + const std::string& message, + const ListValue& args) { + if (!ChildProcessSecurityPolicy::GetInstance()-> + HasWebUIBindings(tab_contents_->GetRenderProcessHost()->id())) { + NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; + return; + } + // Look up the callback for this message. MessageCallbackMap::const_iterator callback = - message_callbacks_.find(params.name); + message_callbacks_.find(message); if (callback == message_callbacks_.end()) return; // Forward this message and content on. - callback->second->Run(¶ms.arguments); + callback->second->Run(&args); } void WebUI::CallJavascriptFunction(const std::string& function_name) { diff --git a/content/browser/webui/web_ui.h b/content/browser/webui/web_ui.h index 402d2fd..e44f1bd 100644 --- a/content/browser/webui/web_ui.h +++ b/content/browser/webui/web_ui.h @@ -11,8 +11,10 @@ #include <vector> #include "base/callback.h" +#include "base/compiler_specific.h" #include "base/string16.h" #include "content/common/page_transition_types.h" +#include "ipc/ipc_channel.h" class DictionaryValue; class WebUIMessageHandler; @@ -22,15 +24,20 @@ class Profile; class RenderViewHost; class TabContents; class Value; -struct ExtensionHostMsg_DomMessage_Params; // A WebUI sets up the datasources and message handlers for a given HTML-based // UI. It is contained by a WebUIManager. -class WebUI { +class WebUI : public IPC::Channel::Listener { public: explicit WebUI(TabContents* contents); virtual ~WebUI(); + // IPC message handling. + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + virtual void OnWebUISend(const GURL& source_url, + const std::string& message, + const ListValue& args); + // Called by RenderViewHost when the RenderView is first created. This is // *not* called for every page load because in some cases // RenderViewHostManager will reuse RenderView instances. In those cases, @@ -52,10 +59,6 @@ class WebUI { // won't be run in that case. virtual void DidBecomeActiveForReusedRenderView() {} - // Called from TabContents. - virtual void ProcessWebUIMessage( - const ExtensionHostMsg_DomMessage_Params& params); - // Used by WebUIMessageHandlers. typedef Callback1<const ListValue*>::Type MessageCallback; void RegisterMessageCallback(const std::string& message, diff --git a/content/common/view_messages.h b/content/common/view_messages.h index 4602f17..a6d7997 100644 --- a/content/common/view_messages.h +++ b/content/common/view_messages.h @@ -1619,7 +1619,7 @@ IPC_MESSAGE_ROUTED1(ViewHostMsg_DidContentsPreferredSizeChange, IPC_MESSAGE_ROUTED3(ViewHostMsg_WebUISend, GURL /* source_url */, std::string /* message */, - std::string /* args (as a JSON string) */) + ListValue /* args */) // A renderer sends this to the browser process when it wants to // create a ppapi plugin. The browser will create the plugin process if diff --git a/content/renderer/web_ui_bindings.cc b/content/renderer/web_ui_bindings.cc index 868d17a..1c42dbb 100644 --- a/content/renderer/web_ui_bindings.cc +++ b/content/renderer/web_ui_bindings.cc @@ -4,7 +4,6 @@ #include "content/renderer/web_ui_bindings.h" -#include "base/json/json_writer.h" #include "base/memory/scoped_ptr.h" #include "base/stl_util-inl.h" #include "base/values.h" @@ -69,14 +68,17 @@ void WebUIBindings::send(const CppArgumentList& args, CppVariant* result) { return; const std::string message = args[0].ToString(); - // If they've provided an optional message parameter, convert that into JSON. - std::string content; + // If they've provided an optional message parameter, convert that into a + // Value to send to the browser process. + scoped_ptr<Value> content; if (args.size() == 2) { if (!args[1].isObject()) return; - scoped_ptr<Value> value(CreateValueFromCppVariant(args[1])); - base::JSONWriter::Write(value.get(), /* pretty_print= */ false, &content); + content.reset(CreateValueFromCppVariant(args[1])); + CHECK(content->IsType(Value::TYPE_LIST)); + } else { + content.reset(new ListValue()); } // Retrieve the source frame's url @@ -86,8 +88,11 @@ void WebUIBindings::send(const CppArgumentList& args, CppVariant* result) { source_url = webframe->url(); // Send the message up to the browser. - sender()->Send( - new ViewHostMsg_WebUISend(routing_id(), source_url, message, content)); + sender()->Send(new ViewHostMsg_WebUISend( + routing_id(), + source_url, + message, + *(static_cast<ListValue*>(content.get())))); } void DOMBoundBrowserObject::SetProperty(const std::string& name, |