diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-21 21:37:06 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-21 21:37:06 +0000 |
commit | fc368956c37b3f6447103d97d034c8dfa5f2ed5f (patch) | |
tree | 26c91201cb807544275af7e8d9423f7875e13bf7 | |
parent | 651f749bb3ee9a7d6a9c239f703351a499e16832 (diff) | |
download | chromium_src-fc368956c37b3f6447103d97d034c8dfa5f2ed5f.zip chromium_src-fc368956c37b3f6447103d97d034c8dfa5f2ed5f.tar.gz chromium_src-fc368956c37b3f6447103d97d034c8dfa5f2ed5f.tar.bz2 |
Use a NotificationRegistrar to listen for notifications.
BUG=2381
Review URL: http://codereview.chromium.org/113717
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16665 0039d316-1c4b-4281-b951-d872f2087c98
9 files changed, 34 insertions, 45 deletions
diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc index 868bc9c..df4ab5b 100644 --- a/chrome/browser/extensions/extension_browser_event_router.cc +++ b/chrome/browser/extensions/extension_browser_event_router.cc @@ -153,9 +153,8 @@ void ExtensionBrowserEventRouter::TabCreatedAt(TabContents* contents, DispatchEvent(contents->profile(), kOnTabCreated, json_args); - NotificationService::current()->AddObserver( - this, NotificationType::NAV_ENTRY_COMMITTED, - Source<NavigationController>(&contents->controller())); + registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, + Source<NavigationController>(&contents->controller())); } void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, @@ -225,9 +224,8 @@ void ExtensionBrowserEventRouter::TabClosingAt(TabContents* contents, int removed_count = tab_entries_.erase(tab_id); DCHECK(removed_count > 0); - NotificationService::current()->RemoveObserver( - this, NotificationType::NAV_ENTRY_COMMITTED, - Source<NavigationController>(&contents->controller())); + registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, + Source<NavigationController>(&contents->controller())); } void ExtensionBrowserEventRouter::TabSelectedAt(TabContents* old_contents, diff --git a/chrome/browser/extensions/extension_browser_event_router.h b/chrome/browser/extensions/extension_browser_event_router.h index 67dd0f4..a818d32 100644 --- a/chrome/browser/extensions/extension_browser_event_router.h +++ b/chrome/browser/extensions/extension_browser_event_router.h @@ -14,7 +14,7 @@ #include "chrome/browser/browser_list.h" #include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/browser/tabs/tab_strip_model.h" -#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_registrar.h" // The ExtensionBrowserEventRouter listens to Browser window & tab events // and routes them to listeners inside extension process renderers. @@ -69,6 +69,8 @@ class ExtensionBrowserEventRouter : public TabStripModelObserver, ExtensionBrowserEventRouter(); friend struct DefaultSingletonTraits<ExtensionBrowserEventRouter>; + NotificationRegistrar registrar_; + bool initialized_; // Maintain some information about known tabs, so we can: diff --git a/chrome/browser/extensions/extension_message_service.cc b/chrome/browser/extensions/extension_message_service.cc index 08dd703..86371e0 100644 --- a/chrome/browser/extensions/extension_message_service.cc +++ b/chrome/browser/extensions/extension_message_service.cc @@ -74,14 +74,10 @@ void ExtensionMessageService::Init() { ui_loop_ = MessageLoop::current(); - // Note: we never stop observing because we live longer than - // NotificationService. - NotificationService::current()->AddObserver(this, - NotificationType::RENDERER_PROCESS_TERMINATED, - NotificationService::AllSources()); - NotificationService::current()->AddObserver(this, - NotificationType::RENDERER_PROCESS_CLOSED, - NotificationService::AllSources()); + registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, + NotificationService::AllSources()); } void ExtensionMessageService::RegisterExtension( diff --git a/chrome/browser/extensions/extension_message_service.h b/chrome/browser/extensions/extension_message_service.h index 29990a9..ef6c1ec 100644 --- a/chrome/browser/extensions/extension_message_service.h +++ b/chrome/browser/extensions/extension_message_service.h @@ -11,7 +11,7 @@ #include "base/lock.h" #include "chrome/browser/renderer_host/render_process_host.h" -#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_registrar.h" class MessageLoop; class RenderProcessHost; @@ -94,6 +94,8 @@ class ExtensionMessageService : public NotificationObserver { int OpenChannelToExtensionImpl(const std::string& extension_id, IPC::Message::Sender* source); + NotificationRegistrar registrar_; + // The UI message loop, used for posting tasks. MessageLoop* ui_loop_; diff --git a/chrome/browser/extensions/extension_process_manager.cc b/chrome/browser/extensions/extension_process_manager.cc index 79b2e0f..503c099 100644 --- a/chrome/browser/extensions/extension_process_manager.cc +++ b/chrome/browser/extensions/extension_process_manager.cc @@ -18,29 +18,24 @@ static void CreateBackgroundHosts( for (ExtensionList::const_iterator extension = extensions->begin(); extension != extensions->end(); ++extension) { // Start the process for the master page, if it exists. - if ((*extension)->background_url().is_valid()) { + if ((*extension)->background_url().is_valid()) manager->CreateBackgroundHost(*extension, (*extension)->background_url()); - } } } ExtensionProcessManager::ExtensionProcessManager(Profile* profile) : browsing_instance_(new BrowsingInstance(profile)) { - // TODO: register notification, and load any hosts for existing exts. - NotificationService::current()->AddObserver(this, - NotificationType::EXTENSIONS_LOADED, - NotificationService::AllSources()); + registrar_.Add(this, NotificationType::EXTENSIONS_LOADED, + NotificationService::AllSources()); - if (profile->GetExtensionsService()) { + if (profile->GetExtensionsService()) CreateBackgroundHosts(this, profile->GetExtensionsService()->extensions()); - } } ExtensionProcessManager::~ExtensionProcessManager() { for (ExtensionHostList::iterator iter = background_hosts_.begin(); - iter != background_hosts_.end(); ++iter) { + iter != background_hosts_.end(); ++iter) delete *iter; - } } ExtensionView* ExtensionProcessManager::CreateView(Extension* extension, diff --git a/chrome/browser/extensions/extension_process_manager.h b/chrome/browser/extensions/extension_process_manager.h index 73be45d..ed4a536 100644 --- a/chrome/browser/extensions/extension_process_manager.h +++ b/chrome/browser/extensions/extension_process_manager.h @@ -8,7 +8,7 @@ #include <list> #include "base/ref_counted.h" -#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_registrar.h" class Browser; class BrowsingInstance; @@ -45,8 +45,11 @@ class ExtensionProcessManager : public NotificationObserver { const NotificationDetails& details); private: - // The list of running viewless background extensions. typedef std::list<ExtensionHost*> ExtensionHostList; + + NotificationRegistrar registrar_; + + // The list of running viewless background extensions. ExtensionHostList background_hosts_; // The BrowsingInstance shared by all extensions in this profile. This diff --git a/chrome/browser/extensions/extension_shelf.cc b/chrome/browser/extensions/extension_shelf.cc index 17623b6..59eecdb 100644 --- a/chrome/browser/extensions/extension_shelf.cc +++ b/chrome/browser/extensions/extension_shelf.cc @@ -146,10 +146,8 @@ ExtensionShelf::ExtensionShelf(Browser* browser) current_handle_view_(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(timer_factory_(this)) { // Watch extensions loaded notification. - NotificationService* ns = NotificationService::current(); - Source<Profile> ns_source(browser->profile()->GetOriginalProfile()); - ns->AddObserver(this, NotificationType::EXTENSIONS_LOADED, - NotificationService::AllSources()); + registrar_.Add(this, NotificationType::EXTENSIONS_LOADED, + NotificationService::AllSources()); // Add any already-loaded extensions now, since we missed the notification for // those. @@ -163,9 +161,6 @@ ExtensionShelf::ExtensionShelf(Browser* browser) } ExtensionShelf::~ExtensionShelf() { - NotificationService* ns = NotificationService::current(); - ns->RemoveObserver(this, NotificationType::EXTENSIONS_LOADED, - NotificationService::AllSources()); } BrowserBubble* ExtensionShelf::GetHandle() { diff --git a/chrome/browser/extensions/extension_shelf.h b/chrome/browser/extensions/extension_shelf.h index 4bc88eb..23d39dd 100644 --- a/chrome/browser/extensions/extension_shelf.h +++ b/chrome/browser/extensions/extension_shelf.h @@ -10,7 +10,7 @@ #include "chrome/browser/extensions/extension_view.h" #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/views/browser_bubble.h" -#include "chrome/common/notification_observer.h" +#include "chrome/common/notification_registrar.h" #include "views/view.h" class Browser; @@ -72,6 +72,8 @@ class ExtensionShelf : public views::View, // Adjust shelf handle size and position. void LayoutShelfHandle(); + NotificationRegistrar registrar_; + // Which browser window this shelf is in. Browser* browser_; diff --git a/chrome/browser/extensions/user_script_master_unittest.cc b/chrome/browser/extensions/user_script_master_unittest.cc index 0e1b5c4..3b92d24 100644 --- a/chrome/browser/extensions/user_script_master_unittest.cc +++ b/chrome/browser/extensions/user_script_master_unittest.cc @@ -11,6 +11,7 @@ #include "base/message_loop.h" #include "base/path_service.h" #include "base/string_util.h" +#include "chrome/common/notification_registrar.h" #include "chrome/common/notification_service.h" #include "testing/gtest/include/gtest/gtest.h" @@ -36,18 +37,11 @@ class UserScriptMasterTest : public testing::Test, file_util::CreateDirectory(script_dir_); // Register for all user script notifications. - NotificationService::current()->AddObserver( - this, - NotificationType::USER_SCRIPTS_LOADED, - NotificationService::AllSources()); + registrar_.Add(this, NotificationType::USER_SCRIPTS_LOADED, + NotificationService::AllSources()); } virtual void TearDown() { - NotificationService::current()->RemoveObserver( - this, - NotificationType::USER_SCRIPTS_LOADED, - NotificationService::AllSources()); - // Clean up test directory. ASSERT_TRUE(file_util::Delete(script_dir_, true)); ASSERT_FALSE(file_util::PathExists(script_dir_)); @@ -63,6 +57,8 @@ class UserScriptMasterTest : public testing::Test, MessageLoop::current()->Quit(); } + NotificationRegistrar registrar_; + // MessageLoop used in tests. MessageLoop message_loop_; |