diff options
author | mpcomplete@google.com <mpcomplete@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-13 22:15:29 +0000 |
---|---|---|
committer | mpcomplete@google.com <mpcomplete@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-04-13 22:15:29 +0000 |
commit | 9665fa68fd7f65d34ee1796641649466e74f6cd7 (patch) | |
tree | 3219e368f2597813d46eb88c678fc76f2e08fe3c | |
parent | 34cdd0557b61c9c98aa0510cadfcd814a8156b44 (diff) | |
download | chromium_src-9665fa68fd7f65d34ee1796641649466e74f6cd7.zip chromium_src-9665fa68fd7f65d34ee1796641649466e74f6cd7.tar.gz chromium_src-9665fa68fd7f65d34ee1796641649466e74f6cd7.tar.bz2 |
Hook up extension events to the json parser, and change the extension message
passing stuff to use json.
Also made sure to lowercase extension IDs when looking them up in
ExtensionMessageService.
Review URL: http://codereview.chromium.org/67054
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13612 0039d316-1c4b-4281-b951-d872f2087c98
-rwxr-xr-x | chrome/browser/extensions/extension_message_service.cc | 3 | ||||
-rwxr-xr-x | chrome/browser/extensions/extension_view_unittest.cc | 4 | ||||
-rwxr-xr-x | chrome/renderer/extensions/event_bindings.cc | 6 | ||||
-rw-r--r-- | chrome/renderer/extensions/extension_process_bindings.cc | 5 | ||||
-rw-r--r-- | chrome/renderer/render_view_unittest.cc | 27 | ||||
-rw-r--r-- | chrome/renderer/resources/event_bindings.js | 4 | ||||
-rw-r--r-- | chrome/renderer/resources/renderer_extension_bindings.js | 5 | ||||
-rw-r--r-- | chrome/test/in_process_browser_test.cc | 2 | ||||
-rw-r--r-- | chrome/test/in_process_browser_test.h | 3 |
9 files changed, 41 insertions, 18 deletions
diff --git a/chrome/browser/extensions/extension_message_service.cc b/chrome/browser/extensions/extension_message_service.cc index ef2530a..8a4ef73 100755 --- a/chrome/browser/extensions/extension_message_service.cc +++ b/chrome/browser/extensions/extension_message_service.cc @@ -73,7 +73,8 @@ int ExtensionMessageService::OpenChannelToExtension( ResourceMessageFilter* dest = NULL; { AutoLock lock(renderers_lock_); - ProcessIDMap::iterator process_id = process_ids_.find(extension_id); + ProcessIDMap::iterator process_id = process_ids_.find( + StringToLowerASCII(extension_id)); if (process_id == process_ids_.end()) return -1; diff --git a/chrome/browser/extensions/extension_view_unittest.cc b/chrome/browser/extensions/extension_view_unittest.cc index 5784b8a..a1dd565 100755 --- a/chrome/browser/extensions/extension_view_unittest.cc +++ b/chrome/browser/extensions/extension_view_unittest.cc @@ -11,6 +11,7 @@ #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/extensions/test_extension_loader.h" #include "chrome/browser/tab_contents/site_instance.h" +#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_paths.h" #include "chrome/test/in_process_browser_test.h" #include "chrome/test/ui_test_utils.h" @@ -72,6 +73,9 @@ class ExtensionViewTest : public InProcessBrowserTest { InProcessBrowserTest::SetUp(); } + virtual void SetUpCommandLine(CommandLine* command_line) { + command_line->AppendSwitch(switches::kEnableExtensions); + } }; // Tests that ExtensionView starts an extension process and runs the script diff --git a/chrome/renderer/extensions/event_bindings.cc b/chrome/renderer/extensions/event_bindings.cc index 0b45abba..bfa9e48 100755 --- a/chrome/renderer/extensions/event_bindings.cc +++ b/chrome/renderer/extensions/event_bindings.cc @@ -8,6 +8,7 @@ #include "base/singleton.h" #include "chrome/renderer/extensions/bindings_utils.h" #include "chrome/renderer/extensions/event_bindings.h" +#include "chrome/renderer/js_only_v8_extensions.h" #include "chrome/renderer/render_thread.h" #include "grit/renderer_resources.h" @@ -23,11 +24,14 @@ ContextList& GetRegisteredContexts() { return Singleton<ExtensionData>::get()->contexts; } +const char* kExtensionDeps[] = { JsonJsV8Extension::kName }; + class ExtensionImpl : public v8::Extension { public: ExtensionImpl() : v8::Extension(EventBindings::kName, - GetStringResource<IDR_EVENT_BINDINGS_JS>()) { + GetStringResource<IDR_EVENT_BINDINGS_JS>(), + arraysize(kExtensionDeps), kExtensionDeps) { } ~ExtensionImpl() {} diff --git a/chrome/renderer/extensions/extension_process_bindings.cc b/chrome/renderer/extensions/extension_process_bindings.cc index af8d83e..2d8f3db 100644 --- a/chrome/renderer/extensions/extension_process_bindings.cc +++ b/chrome/renderer/extensions/extension_process_bindings.cc @@ -6,6 +6,7 @@ #include "chrome/common/render_messages.h" #include "chrome/renderer/extensions/bindings_utils.h" +#include "chrome/renderer/js_only_v8_extensions.h" #include "chrome/renderer/render_view.h" #include "grit/renderer_resources.h" #include "third_party/WebKit/WebKit/chromium/public/WebScriptSource.h" @@ -17,12 +18,14 @@ using WebKit::WebString; namespace { const char kExtensionName[] = "chrome/ExtensionProcessBindings"; +const char* kExtensionDeps[] = { JsonJsV8Extension::kName }; class ExtensionImpl : public v8::Extension { public: ExtensionImpl() : v8::Extension( kExtensionName, GetStringResource<IDR_EXTENSION_PROCESS_BINDINGS_JS>(), - NULL, NULL) {} + arraysize(kExtensionDeps), kExtensionDeps) { + } static void SetFunctionNames(const std::vector<std::string>& names) { function_names_ = new std::set<std::string>(); diff --git a/chrome/renderer/render_view_unittest.cc b/chrome/renderer/render_view_unittest.cc index a70480c..f28b8d0 100644 --- a/chrome/renderer/render_view_unittest.cc +++ b/chrome/renderer/render_view_unittest.cc @@ -6,6 +6,7 @@ #include "chrome/common/render_messages.h" #include "chrome/renderer/extensions/event_bindings.h" #include "chrome/renderer/extensions/renderer_extension_bindings.h" +#include "chrome/renderer/js_only_v8_extensions.h" #include "chrome/renderer/mock_render_process.h" #include "chrome/renderer/mock_render_thread.h" #include "chrome/renderer/render_view.h" @@ -67,6 +68,8 @@ class RenderViewTest : public testing::Test { // testing::Test virtual void SetUp() { WebKit::initialize(&webkitclient_); + WebKit::registerExtension(BaseJsV8Extension::Get()); + WebKit::registerExtension(JsonJsV8Extension::Get()); WebKit::registerExtension(EventBindings::Get()); WebKit::registerExtension(RendererExtensionBindings::Get(&render_thread_)); @@ -381,9 +384,9 @@ TEST_F(RenderViewTest, ExtensionMessagesOpenChannel) { "var e = new chromium.Extension('foobar');" "var port = e.connect();" "port.onmessage.addListener(doOnMessage);" - "port.postMessage('content ready');" + "port.postMessage({message: 'content ready'});" "function doOnMessage(msg, port) {" - " alert('content got: ' + msg);" + " alert('content got: ' + msg.val);" "}"); // Verify that we opened a channel and sent a message through it. @@ -395,21 +398,21 @@ TEST_F(RenderViewTest, ExtensionMessagesOpenChannel) { const IPC::Message* post_msg = render_thread_.sink().GetUniqueMessageMatching( ViewHostMsg_ExtensionPostMessage::ID); - EXPECT_TRUE(post_msg); + ASSERT_TRUE(post_msg); ViewHostMsg_ExtensionPostMessage::Param post_params; ViewHostMsg_ExtensionPostMessage::Read(post_msg, &post_params); - EXPECT_EQ("content ready", post_params.b); + EXPECT_EQ("{\"message\":\"content ready\"}", post_params.b); // Now simulate getting a message back from the other side. render_thread_.sink().ClearMessages(); const int kPortId = 0; - RendererExtensionBindings::HandleMessage("42", kPortId); + RendererExtensionBindings::HandleMessage("{\"val\": 42}", kPortId); // Verify that we got it. const IPC::Message* alert_msg = render_thread_.sink().GetUniqueMessageMatching( ViewHostMsg_RunJavaScriptMessage::ID); - EXPECT_TRUE(alert_msg); + ASSERT_TRUE(alert_msg); void* iter = IPC::SyncMessage::GetDataIterator(alert_msg); ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; IPC::ReadParam(alert_msg, &iter, &alert_param); @@ -423,10 +426,10 @@ TEST_F(RenderViewTest, ExtensionMessagesOnConnect) { ExecuteJavaScript( "chromium.onconnect.addListener(function (port) {" " port.onmessage.addListener(doOnMessage);" - " port.postMessage('onconnect');" + " port.postMessage({message: 'onconnect'});" "});" "function doOnMessage(msg, port) {" - " alert('got: ' + msg);" + " alert('got: ' + msg.val);" "}"); render_thread_.sink().ClearMessages(); @@ -439,20 +442,20 @@ TEST_F(RenderViewTest, ExtensionMessagesOnConnect) { const IPC::Message* post_msg = render_thread_.sink().GetUniqueMessageMatching( ViewHostMsg_ExtensionPostMessage::ID); - EXPECT_TRUE(post_msg); + ASSERT_TRUE(post_msg); ViewHostMsg_ExtensionPostMessage::Param post_params; ViewHostMsg_ExtensionPostMessage::Read(post_msg, &post_params); - EXPECT_EQ("onconnect", post_params.b); + EXPECT_EQ("{\"message\":\"onconnect\"}", post_params.b); // Now simulate getting a message back from the channel opener. render_thread_.sink().ClearMessages(); - RendererExtensionBindings::HandleMessage("42", kPortId); + RendererExtensionBindings::HandleMessage("{\"val\": 42}", kPortId); // Verify that we got it. const IPC::Message* alert_msg = render_thread_.sink().GetUniqueMessageMatching( ViewHostMsg_RunJavaScriptMessage::ID); - EXPECT_TRUE(alert_msg); + ASSERT_TRUE(alert_msg); void* iter = IPC::SyncMessage::GetDataIterator(alert_msg); ViewHostMsg_RunJavaScriptMessage::SendParam alert_param; IPC::ReadParam(alert_msg, &iter, &alert_param); diff --git a/chrome/renderer/resources/event_bindings.js b/chrome/renderer/resources/event_bindings.js index 42f1175..f8a6517 100644 --- a/chrome/renderer/resources/event_bindings.js +++ b/chrome/renderer/resources/event_bindings.js @@ -25,9 +25,9 @@ var chromium = chromium || {}; chromium.Event.dispatchJSON_ = function(name, data) { if (chromium.Event.attached_[name]) { if (data) { - data = chromium.json.parse(data); + data = goog.json.parse(data); } - chromium.Event.attached_[name].dispatch_(data); + chromium.Event.attached_[name].dispatch(data); } }; diff --git a/chrome/renderer/resources/renderer_extension_bindings.js b/chrome/renderer/resources/renderer_extension_bindings.js index deb5c40..a392cf0 100644 --- a/chrome/renderer/resources/renderer_extension_bindings.js +++ b/chrome/renderer/resources/renderer_extension_bindings.js @@ -30,6 +30,9 @@ var chromium = chromium || {}; chromium.Port.dispatchOnMessage_ = function(msg, portId) { var port = chromium.Port.ports_[portId]; if (port) { + if (msg) { + msg = goog.json.parse(msg); + } port.onmessage.dispatch(msg, port); } }; @@ -37,7 +40,7 @@ var chromium = chromium || {}; // Sends a message asynchronously to the context on the other end of this // port. chromium.Port.prototype.postMessage = function(msg) { - PostMessage(this.portId_, msg); + PostMessage(this.portId_, goog.json.serialize(msg)); }; // Extension object. diff --git a/chrome/test/in_process_browser_test.cc b/chrome/test/in_process_browser_test.cc index 6699144..47e0e81 100644 --- a/chrome/test/in_process_browser_test.cc +++ b/chrome/test/in_process_browser_test.cc @@ -86,6 +86,8 @@ void InProcessBrowserTest::SetUp() { CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); original_command_line_.reset(new CommandLine(*command_line)); + SetUpCommandLine(command_line); + // Hide windows on show. if (!command_line->HasSwitch(kUnitTestShowWindows) && !show_window_) BrowserView::SetShowState(SW_HIDE); diff --git a/chrome/test/in_process_browser_test.h b/chrome/test/in_process_browser_test.h index 457516e..25e18a4 100644 --- a/chrome/test/in_process_browser_test.h +++ b/chrome/test/in_process_browser_test.h @@ -67,6 +67,9 @@ class InProcessBrowserTest : public testing::Test, public NotificationObserver { // Override this rather than TestBody. virtual void RunTestOnMainThread() = 0; + // Override this to add command line flags specific to your test. + virtual void SetUpCommandLine(CommandLine* command_line) {} + // Allows subclasses to configure the host mapper. By default this blocks // requests to google.com as Chrome pings that on startup and we don't want to // do that during testing. |