diff options
author | finnur@chromium.org <finnur@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-15 20:21:58 +0000 |
---|---|---|
committer | finnur@chromium.org <finnur@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-15 20:21:58 +0000 |
commit | 4577622518be7c7ddd4f3b45016d4fa68d253802 (patch) | |
tree | e09db89fdb34063d7dcb6ffc013821792ab3f38e /chrome/browser/extensions | |
parent | 631d1ab9de83c531ca43b8134226eb247a4ee3aa (diff) | |
download | chromium_src-4577622518be7c7ddd4f3b45016d4fa68d253802.zip chromium_src-4577622518be7c7ddd4f3b45016d4fa68d253802.tar.gz chromium_src-4577622518be7c7ddd4f3b45016d4fa68d253802.tar.bz2 |
Retrying this patch. This was already reviewed by mpcomplete and the
only change is a fix for the UI test that broke and a small change to
event_bindings.cc (reviewed in-person).
ExtensionFunctionDispatcher now notifies ExtensionProcessManager of
renderviews created, which in turn notifies the renderer of page
actions that it knows about.
Remove generic event "page-action-executed" in favor of page action
specific event (sent as extension_id/page_action_id).
In the bindings, we now setup events for each page action we know about
so we can register for specific events, and not receive broadcast events
from all page actions. To setup these events I added a
GetCurrentPageActions() to extension_process_bindings.cc and a helper
function GetCurrentExtensionId().
And, finally, I simplified the page action background page by removing
the check to see if we are already subscribed to the feed (since we now
support multiple feed readers, it doesn't make sense anymore to always
check Google Reader). This check might make a comeback later in a
different form.
TBR=mpcomplete
BUG=13936
TEST=The RSS sample extension should work as before.
Review URL: http://codereview.chromium.org/149683
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20782 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
8 files changed, 78 insertions, 7 deletions
diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc index 3bd807a..c7a5b33 100644 --- a/chrome/browser/extensions/extension_browser_event_router.cc +++ b/chrome/browser/extensions/extension_browser_event_router.cc @@ -329,6 +329,7 @@ void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, void ExtensionBrowserEventRouter::TabStripEmpty() { } void ExtensionBrowserEventRouter::PageActionExecuted(Profile* profile, + std::string extension_id, std::string page_action_id, int tab_id, std::string url) { @@ -346,5 +347,6 @@ void ExtensionBrowserEventRouter::PageActionExecuted(Profile* profile, std::string json_args; JSONWriter::Write(&args, false, &json_args); - DispatchEvent(profile, events::kOnPageActionExecuted, json_args); + std::string event_name = extension_id + std::string("/") + page_action_id; + DispatchEvent(profile, event_name.c_str(), json_args); } diff --git a/chrome/browser/extensions/extension_browser_event_router.h b/chrome/browser/extensions/extension_browser_event_router.h index 0cfd075..d2f7613 100644 --- a/chrome/browser/extensions/extension_browser_event_router.h +++ b/chrome/browser/extensions/extension_browser_event_router.h @@ -50,6 +50,7 @@ class ExtensionBrowserEventRouter : public TabStripModelObserver, // PageActions. void PageActionExecuted(Profile* profile, + std::string extension_id, std::string page_action_id, int tab_id, std::string url); diff --git a/chrome/browser/extensions/extension_event_names.cc b/chrome/browser/extensions/extension_event_names.cc index f4fd6e7..214d37b 100644 --- a/chrome/browser/extensions/extension_event_names.cc +++ b/chrome/browser/extensions/extension_event_names.cc @@ -6,7 +6,6 @@ namespace extension_event_names { -const char kOnPageActionExecuted[] = "page-action-executed"; const char kOnTabAttached[] = "tab-attached"; const char kOnTabCreated[] = "tab-created"; const char kOnTabDetached[] = "tab-detached"; diff --git a/chrome/browser/extensions/extension_event_names.h b/chrome/browser/extensions/extension_event_names.h index 9b9e3f2..3e3673a 100644 --- a/chrome/browser/extensions/extension_event_names.h +++ b/chrome/browser/extensions/extension_event_names.h @@ -9,7 +9,6 @@ namespace extension_event_names { -extern const char kOnPageActionExecuted[]; extern const char kOnTabAttached[]; extern const char kOnTabCreated[]; extern const char kOnTabDetached[]; diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 8238595..e486f89 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -13,6 +13,7 @@ #include "chrome/browser/extensions/extension_message_service.h" #include "chrome/browser/extensions/extension_page_actions_module.h" #include "chrome/browser/extensions/extension_page_actions_module_constants.h" +#include "chrome/browser/extensions/extension_process_manager.h" #include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/browser/extensions/extension_tabs_module_constants.h" #include "chrome/browser/profile.h" @@ -188,6 +189,11 @@ ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( // Ensure the message service is initialized. ExtensionMessageService::GetInstance(profile()->GetRequestContext())->Init(); + + // Notify the ExtensionProcessManager that the view was created. + ExtensionProcessManager* epm = profile()->GetExtensionProcessManager(); + epm->RegisterExtensionProcess(extension_id(), + render_view_host->process()->pid()); } ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { diff --git a/chrome/browser/extensions/extension_process_manager.cc b/chrome/browser/extensions/extension_process_manager.cc index 4c2047b..3018ea8 100644 --- a/chrome/browser/extensions/extension_process_manager.cc +++ b/chrome/browser/extensions/extension_process_manager.cc @@ -9,9 +9,11 @@ #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/profile.h" #include "chrome/browser/renderer_host/site_instance.h" +#include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/notification_service.h" #include "chrome/common/notification_type.h" +#include "chrome/common/render_messages.h" static void CreateBackgroundHosts( ExtensionProcessManager* manager, const ExtensionList* extensions) { @@ -33,6 +35,10 @@ ExtensionProcessManager::ExtensionProcessManager(Profile* profile) NotificationService::AllSources()); registrar_.Add(this, NotificationType::EXTENSION_HOST_DESTROYED, Source<Profile>(profile)); + registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, + NotificationService::AllSources()); } ExtensionProcessManager::~ExtensionProcessManager() { @@ -78,6 +84,39 @@ ExtensionHost* ExtensionProcessManager::CreateBackgroundHost( return host; } +void ExtensionProcessManager::RegisterExtensionProcess( + std::string extension_id, int process_id) { + ProcessIDMap::const_iterator it = process_ids_.find(extension_id); + if (it != process_ids_.end() && (*it).second == process_id) + return; + + process_ids_[extension_id] = process_id; + + ExtensionsService* extension_service = + browsing_instance_->profile()->GetExtensionsService(); + + std::vector<std::string> page_action_ids; + Extension* extension = extension_service->GetExtensionById(extension_id); + for (PageActionMap::const_iterator i = extension->page_actions().begin(); + i != extension->page_actions().end(); ++i) { + page_action_ids.push_back(i->first); + } + + RenderProcessHost* rph = RenderProcessHost::FromID(process_id); + rph->Send(new ViewMsg_Extension_UpdatePageActions(extension_id, + page_action_ids)); +} + +void ExtensionProcessManager::UnregisterExtensionProcess(int process_id) { + ProcessIDMap::iterator it = process_ids_.begin(); + while (it != process_ids_.end()) { + if (it->second == process_id) + process_ids_.erase(it++); + else + ++it; + } +} + SiteInstance* ExtensionProcessManager::GetSiteInstanceForURL(const GURL& url) { return browsing_instance_->GetSiteInstanceForURL(url); } @@ -122,6 +161,13 @@ void ExtensionProcessManager::Observe(NotificationType type, break; } + case NotificationType::RENDERER_PROCESS_TERMINATED: + case NotificationType::RENDERER_PROCESS_CLOSED: { + RenderProcessHost* host = Source<RenderProcessHost>(source).ptr(); + UnregisterExtensionProcess(host->pid()); + break; + } + default: NOTREACHED(); } diff --git a/chrome/browser/extensions/extension_process_manager.h b/chrome/browser/extensions/extension_process_manager.h index 58f6639..e3d6b76 100644 --- a/chrome/browser/extensions/extension_process_manager.h +++ b/chrome/browser/extensions/extension_process_manager.h @@ -5,7 +5,9 @@ #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ +#include <map> #include <set> +#include <string> #include "base/ref_counted.h" #include "chrome/common/notification_registrar.h" @@ -43,6 +45,13 @@ class ExtensionProcessManager : public NotificationObserver { // Returns the SiteInstance that the given URL belongs to. SiteInstance* GetSiteInstanceForURL(const GURL& url); + // Register an extension process by |extension_id| and specifying which + // |process_id| it belongs to. + void RegisterExtensionProcess(std::string extension_id, int process_id); + + // Unregister an extension process with specified |process_id|. + void UnregisterExtensionProcess(int process_id); + // NotificationObserver: virtual void Observe(NotificationType type, const NotificationSource& source, @@ -69,6 +78,10 @@ class ExtensionProcessManager : public NotificationObserver { // controls process grouping. scoped_refptr<BrowsingInstance> browsing_instance_; + // A map of extension ID to the render_process_id that the extension lives in. + typedef std::map<std::string, int> ProcessIDMap; + ProcessIDMap process_ids_; + DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); }; diff --git a/chrome/browser/extensions/extension_uitest.cc b/chrome/browser/extensions/extension_uitest.cc index 6d7bc8c..6945446 100644 --- a/chrome/browser/extensions/extension_uitest.cc +++ b/chrome/browser/extensions/extension_uitest.cc @@ -86,7 +86,7 @@ class ExtensionUITest : public ParentTestType { } private: - DISALLOW_EVIL_CONSTRUCTORS(ExtensionUITest); + DISALLOW_COPY_AND_ASSIGN(ExtensionUITest); }; // For tests that only need to check for a single postMessage @@ -331,9 +331,6 @@ const char* BrowserEventAutomationProxy::event_names_[] = { "tab-detached", "tab-removed", - // Page action events. - "page-action-executed", - // Bookmark events. "bookmark-added", "bookmark-removed", @@ -445,6 +442,10 @@ class BrowserEventExtensionTest // ExternalTabMessageLoop is ported. #if defined(OS_WIN) TEST_F(BrowserEventExtensionTest, RunTest) { + // This test loads an HTML file that tries to add listeners to a bunch of + // chrome.* events and upon adding a listener it posts the name of the event + // to the automation layer, which we'll count to make sure the events work. + // // The extension for this test does not specify a "key" property in its // manifest file. Therefore, the extension system will automatically assign // it an Id. To make this test consistent and non-flaky, the genetated Id @@ -456,6 +457,10 @@ TEST_F(BrowserEventExtensionTest, RunTest) { BrowserEventAutomationProxy* proxy = static_cast<BrowserEventAutomationProxy*>(automation()); + // If this assert hits and the actual size is 0 then you need to look at: + // src\chrome\test\data\extensions\uitest\event_sink\test.html and see if + // all the events we are attaching to are valid. Also compare the list against + // the event_names_ string array above. EXPECT_EQ(arraysize(BrowserEventAutomationProxy::event_names_), proxy->event_count_.size()); for (std::map<std::string, int>::iterator i = proxy->event_count_.begin(); |