diff options
author | yoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-28 17:36:10 +0000 |
---|---|---|
committer | yoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-28 17:36:10 +0000 |
commit | f825df2b1438559506e0a8511ad371daac41d2d1 (patch) | |
tree | 36a7964f87d0b6ab6c6ee64344c165112948d410 /chrome/browser | |
parent | be843e26b00f611d3bcf3b2652b8eccf95310346 (diff) | |
download | chromium_src-f825df2b1438559506e0a8511ad371daac41d2d1.zip chromium_src-f825df2b1438559506e0a8511ad371daac41d2d1.tar.gz chromium_src-f825df2b1438559506e0a8511ad371daac41d2d1.tar.bz2 |
Revert "Revert "Change extension event routers to not be singletons and to be more profile-aware.""
This time, we handle incognito profiles correctly (see comment in 57186).
This reverts commit 4b62f4fb0fe15eee4e573f7182552a7bd2125262.
BUG=57186, 87145
TEST=existing tests
Review URL: http://codereview.chromium.org/7230035
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90789 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/extensions/extension_bookmarks_module.cc | 23 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_bookmarks_module.h | 17 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_cookies_api.cc | 23 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_cookies_api.h | 12 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_history_api.cc | 58 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_history_api.h | 16 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_management_api.cc | 29 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_management_api.h | 13 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_service.cc | 18 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_service.h | 15 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_webnavigation_api.cc | 19 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_webnavigation_api.h | 14 |
12 files changed, 116 insertions, 141 deletions
diff --git a/chrome/browser/extensions/extension_bookmarks_module.cc b/chrome/browser/extensions/extension_bookmarks_module.cc index e325aa0..c61c8dd 100644 --- a/chrome/browser/extensions/extension_bookmarks_module.cc +++ b/chrome/browser/extensions/extension_bookmarks_module.cc @@ -118,22 +118,18 @@ void BookmarksFunction::Observe(NotificationType type, Release(); // Balanced in Run(). } -// static -ExtensionBookmarkEventRouter* ExtensionBookmarkEventRouter::GetInstance() { - return Singleton<ExtensionBookmarkEventRouter>::get(); -} - -ExtensionBookmarkEventRouter::ExtensionBookmarkEventRouter() { +ExtensionBookmarkEventRouter::ExtensionBookmarkEventRouter( + BookmarkModel* model) : model_(model) { } ExtensionBookmarkEventRouter::~ExtensionBookmarkEventRouter() { + if (model_) { + model_->RemoveObserver(this); + } } -void ExtensionBookmarkEventRouter::Observe(BookmarkModel* model) { - if (models_.find(model) == models_.end()) { - model->AddObserver(this); - models_.insert(model); - } +void ExtensionBookmarkEventRouter::Init() { + model_->AddObserver(this); } void ExtensionBookmarkEventRouter::DispatchEvent(Profile *profile, @@ -150,6 +146,11 @@ void ExtensionBookmarkEventRouter::Loaded(BookmarkModel* model) { // so they know when it's safe to use the API? } +void ExtensionBookmarkEventRouter::BookmarkModelBeingDeleted( + BookmarkModel* model) { + model_ = NULL; +} + void ExtensionBookmarkEventRouter::BookmarkNodeMoved( BookmarkModel* model, const BookmarkNode* old_parent, diff --git a/chrome/browser/extensions/extension_bookmarks_module.h b/chrome/browser/extensions/extension_bookmarks_module.h index 48be1b7..079656e 100644 --- a/chrome/browser/extensions/extension_bookmarks_module.h +++ b/chrome/browser/extensions/extension_bookmarks_module.h @@ -12,7 +12,6 @@ #include "base/compiler_specific.h" #include "base/memory/ref_counted.h" -#include "base/memory/singleton.h" #include "chrome/browser/bookmarks/bookmark_model_observer.h" #include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/ui/shell_dialogs.h" @@ -25,16 +24,14 @@ class FilePath; // the extension system. class ExtensionBookmarkEventRouter : public BookmarkModelObserver { public: - static ExtensionBookmarkEventRouter* GetInstance(); + explicit ExtensionBookmarkEventRouter(BookmarkModel* model); virtual ~ExtensionBookmarkEventRouter(); - // Call this for each model to observe. Safe to call multiple times per - // model. - void Observe(BookmarkModel* model); + void Init(); // BookmarkModelObserver: virtual void Loaded(BookmarkModel* model) OVERRIDE; - virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE {} + virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; virtual void BookmarkNodeMoved(BookmarkModel* model, const BookmarkNode* old_parent, int old_index, @@ -57,18 +54,12 @@ class ExtensionBookmarkEventRouter : public BookmarkModelObserver { virtual void BookmarkImportEnding(BookmarkModel* model) OVERRIDE; private: - ExtensionBookmarkEventRouter(); - friend struct DefaultSingletonTraits<ExtensionBookmarkEventRouter>; - // Helper to actually dispatch an event to extension listeners. void DispatchEvent(Profile* profile, const char* event_name, const std::string& json_args); - // These are stored so that Observe can be called multiple times safely. - // This way the caller doesn't have to know whether it's already observing - // a particular model or not. The pointers are not owned by this object. - std::set<BookmarkModel*> models_; + BookmarkModel* model_; DISALLOW_COPY_AND_ASSIGN(ExtensionBookmarkEventRouter); }; diff --git a/chrome/browser/extensions/extension_cookies_api.cc b/chrome/browser/extensions/extension_cookies_api.cc index 83c535d..7e7cf5b 100644 --- a/chrome/browser/extensions/extension_cookies_api.cc +++ b/chrome/browser/extensions/extension_cookies_api.cc @@ -25,26 +25,29 @@ namespace keys = extension_cookies_api_constants; -// static -ExtensionCookiesEventRouter* ExtensionCookiesEventRouter::GetInstance() { - return Singleton<ExtensionCookiesEventRouter>::get(); -} +ExtensionCookiesEventRouter::ExtensionCookiesEventRouter(Profile* profile) + : profile_(profile) {} + +ExtensionCookiesEventRouter::~ExtensionCookiesEventRouter() {} void ExtensionCookiesEventRouter::Init() { - if (registrar_.IsEmpty()) { - registrar_.Add(this, - NotificationType::COOKIE_CHANGED, - NotificationService::AllSources()); - } + CHECK(registrar_.IsEmpty()); + registrar_.Add(this, + NotificationType::COOKIE_CHANGED, + NotificationService::AllSources()); } void ExtensionCookiesEventRouter::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { + Profile* profile = Source<Profile>(source).ptr(); + if (!profile_->IsSameProfile(profile)) { + return; + } switch (type.value) { case NotificationType::COOKIE_CHANGED: CookieChanged( - Source<Profile>(source).ptr(), + profile, Details<ChromeCookieDetails>(details).ptr()); break; diff --git a/chrome/browser/extensions/extension_cookies_api.h b/chrome/browser/extensions/extension_cookies_api.h index 286c8ba..5d4fc13 100644 --- a/chrome/browser/extensions/extension_cookies_api.h +++ b/chrome/browser/extensions/extension_cookies_api.h @@ -12,7 +12,6 @@ #include <string> #include "base/memory/ref_counted.h" -#include "base/memory/singleton.h" #include "base/time.h" #include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/net/chrome_cookie_notification_details.h" @@ -31,17 +30,12 @@ class URLRequestContextGetter; // extension system. class ExtensionCookiesEventRouter : public NotificationObserver { public: - // Single instance of the event router. - static ExtensionCookiesEventRouter* GetInstance(); + explicit ExtensionCookiesEventRouter(Profile* profile); + virtual ~ExtensionCookiesEventRouter(); void Init(); private: - friend struct DefaultSingletonTraits<ExtensionCookiesEventRouter>; - - ExtensionCookiesEventRouter() {} - virtual ~ExtensionCookiesEventRouter() {} - // NotificationObserver implementation. virtual void Observe(NotificationType type, const NotificationSource& source, @@ -61,6 +55,8 @@ class ExtensionCookiesEventRouter : public NotificationObserver { // Used for tracking registrations to CookieMonster notifications. NotificationRegistrar registrar_; + Profile* profile_; + DISALLOW_COPY_AND_ASSIGN(ExtensionCookiesEventRouter); }; diff --git a/chrome/browser/extensions/extension_history_api.cc b/chrome/browser/extensions/extension_history_api.cc index 9ead422..ea36ecd 100644 --- a/chrome/browser/extensions/extension_history_api.cc +++ b/chrome/browser/extensions/extension_history_api.cc @@ -64,49 +64,37 @@ void AddVisitNode(const history::VisitRow& row, ListValue* list) { } // namespace -ExtensionHistoryEventRouter* ExtensionHistoryEventRouter::GetInstance() { - return Singleton<ExtensionHistoryEventRouter>::get(); -} +ExtensionHistoryEventRouter::ExtensionHistoryEventRouter() {} + +ExtensionHistoryEventRouter::~ExtensionHistoryEventRouter() {} void ExtensionHistoryEventRouter::ObserveProfile(Profile* profile) { NotificationSource source = Source<Profile>(profile); - if (profiles_.find(source.map_key()) == profiles_.end()) - profiles_[source.map_key()] = profile; - - if (registrar_.IsEmpty()) { - registrar_.Add(this, - NotificationType::HISTORY_URL_VISITED, - NotificationService::AllSources()); - registrar_.Add(this, - NotificationType::HISTORY_URLS_DELETED, - NotificationService::AllSources()); - } + CHECK(registrar_.IsEmpty()); + registrar_.Add(this, + NotificationType::HISTORY_URL_VISITED, + source); + registrar_.Add(this, + NotificationType::HISTORY_URLS_DELETED, + source); } -ExtensionHistoryEventRouter::ExtensionHistoryEventRouter() {} - -ExtensionHistoryEventRouter::~ExtensionHistoryEventRouter() {} - void ExtensionHistoryEventRouter::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { - ProfileMap::iterator it = profiles_.find(source.map_key()); - if (it != profiles_.end()) { - Profile* profile = it->second; - switch (type.value) { - case NotificationType::HISTORY_URL_VISITED: - HistoryUrlVisited( - profile, - Details<const history::URLVisitedDetails>(details).ptr()); - break; - case NotificationType::HISTORY_URLS_DELETED: - HistoryUrlsRemoved( - profile, - Details<const history::URLsDeletedDetails>(details).ptr()); - break; - default: - NOTREACHED(); - } + switch (type.value) { + case NotificationType::HISTORY_URL_VISITED: + HistoryUrlVisited( + Source<Profile>(source).ptr(), + Details<const history::URLVisitedDetails>(details).ptr()); + break; + case NotificationType::HISTORY_URLS_DELETED: + HistoryUrlsRemoved( + Source<Profile>(source).ptr(), + Details<const history::URLsDeletedDetails>(details).ptr()); + break; + default: + NOTREACHED(); } } diff --git a/chrome/browser/extensions/extension_history_api.h b/chrome/browser/extensions/extension_history_api.h index f6ddcdc..bb6f55a 100644 --- a/chrome/browser/extensions/extension_history_api.h +++ b/chrome/browser/extensions/extension_history_api.h @@ -6,10 +6,8 @@ #define CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_ #pragma once -#include <map> #include <string> -#include "base/memory/singleton.h" #include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/history/history.h" #include "chrome/browser/history/history_notifications.h" @@ -19,18 +17,12 @@ // extension system. class ExtensionHistoryEventRouter : public NotificationObserver { public: - // Single instance of the event router. - static ExtensionHistoryEventRouter* GetInstance(); + explicit ExtensionHistoryEventRouter(); + virtual ~ExtensionHistoryEventRouter(); - // Safe to call multiple times. void ObserveProfile(Profile* profile); private: - friend struct DefaultSingletonTraits<ExtensionHistoryEventRouter>; - - ExtensionHistoryEventRouter(); - virtual ~ExtensionHistoryEventRouter(); - // NotificationObserver::Observe. virtual void Observe(NotificationType type, const NotificationSource& source, @@ -49,10 +41,6 @@ class ExtensionHistoryEventRouter : public NotificationObserver { // Used for tracking registrations to history service notifications. NotificationRegistrar registrar_; - // Registered profiles. - typedef std::map<uintptr_t, Profile*> ProfileMap; - ProfileMap profiles_; - DISALLOW_COPY_AND_ASSIGN(ExtensionHistoryEventRouter); }; diff --git a/chrome/browser/extensions/extension_management_api.cc b/chrome/browser/extensions/extension_management_api.cc index b67b03f..4dd6726 100644 --- a/chrome/browser/extensions/extension_management_api.cc +++ b/chrome/browser/extensions/extension_management_api.cc @@ -250,12 +250,8 @@ bool UninstallFunction::RunImpl() { return true; } -// static -ExtensionManagementEventRouter* ExtensionManagementEventRouter::GetInstance() { - return Singleton<ExtensionManagementEventRouter>::get(); -} - -ExtensionManagementEventRouter::ExtensionManagementEventRouter() {} +ExtensionManagementEventRouter::ExtensionManagementEventRouter(Profile* profile) + : profile_(profile) {} ExtensionManagementEventRouter::~ExtensionManagementEventRouter() {} @@ -267,13 +263,11 @@ void ExtensionManagementEventRouter::Init() { NotificationType::EXTENSION_UNLOADED }; - // Don't re-init (eg in the case of multiple profiles). - if (registrar_.IsEmpty()) { - for (size_t i = 0; i < arraysize(types); i++) { - registrar_.Add(this, - types[i], - NotificationService::AllSources()); - } + CHECK(registrar_.IsEmpty()); + for (size_t i = 0; i < arraysize(types); i++) { + registrar_.Add(this, + types[i], + NotificationService::AllSources()); } } @@ -282,6 +276,12 @@ void ExtensionManagementEventRouter::Observe( const NotificationSource& source, const NotificationDetails& details) { const char* event_name = NULL; + Profile* profile = Source<Profile>(source).ptr(); + CHECK(profile); + if (!profile_->IsSameProfile(profile)) { + return; + } + switch (type.value) { case NotificationType::EXTENSION_INSTALLED: event_name = events::kOnExtensionInstalled; @@ -300,9 +300,6 @@ void ExtensionManagementEventRouter::Observe( return; } - Profile* profile = Source<Profile>(source).ptr(); - CHECK(profile); - ListValue args; if (event_name == events::kOnExtensionUninstalled) { const std::string& extension_id = diff --git a/chrome/browser/extensions/extension_management_api.h b/chrome/browser/extensions/extension_management_api.h index 04b2c8c..92099a3 100644 --- a/chrome/browser/extensions/extension_management_api.h +++ b/chrome/browser/extensions/extension_management_api.h @@ -6,7 +6,6 @@ #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_API_H__ #pragma once -#include "base/memory/singleton.h" #include "chrome/browser/extensions/extension_function.h" #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" @@ -50,18 +49,12 @@ class UninstallFunction : public ExtensionManagementFunction { class ExtensionManagementEventRouter : public NotificationObserver { public: - // Get the singleton instance of the event router. - static ExtensionManagementEventRouter* GetInstance(); + explicit ExtensionManagementEventRouter(Profile* profile); + virtual ~ExtensionManagementEventRouter(); - // Performs one-time initialization of our singleton. void Init(); private: - friend struct DefaultSingletonTraits<ExtensionManagementEventRouter>; - - ExtensionManagementEventRouter(); - virtual ~ExtensionManagementEventRouter(); - // NotificationObserver implementation. virtual void Observe(NotificationType type, const NotificationSource& source, @@ -69,6 +62,8 @@ class ExtensionManagementEventRouter : public NotificationObserver { NotificationRegistrar registrar_; + Profile* profile_; + DISALLOW_COPY_AND_ASSIGN(ExtensionManagementEventRouter); }; diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc index c477405..d3f9512 100644 --- a/chrome/browser/extensions/extension_service.cc +++ b/chrome/browser/extensions/extension_service.cc @@ -658,17 +658,23 @@ void ExtensionService::InitEventRouters() { if (event_routers_initialized_) return; - ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_); + history_event_router_.reset(new ExtensionHistoryEventRouter()); + history_event_router_->ObserveProfile(profile_); ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_); browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_)); browser_event_router_->Init(); preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_)); - ExtensionBookmarkEventRouter::GetInstance()->Observe( - profile_->GetBookmarkModel()); - ExtensionCookiesEventRouter::GetInstance()->Init(); - ExtensionManagementEventRouter::GetInstance()->Init(); + bookmark_event_router_.reset(new ExtensionBookmarkEventRouter( + profile_->GetBookmarkModel())); + bookmark_event_router_->Init(); + cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_)); + cookies_event_router_->Init(); + management_event_router_.reset(new ExtensionManagementEventRouter(profile_)); + management_event_router_->Init(); ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_); - ExtensionWebNavigationEventRouter::GetInstance()->Init(); + web_navigation_event_router_.reset( + new ExtensionWebNavigationEventRouter(profile_)); + web_navigation_event_router_->Init(); #if defined(OS_CHROMEOS) ExtensionFileBrowserEventRouter::GetInstance()->ObserveFileSystemEvents( diff --git a/chrome/browser/extensions/extension_service.h b/chrome/browser/extensions/extension_service.h index 0c5e8e7..401eef4 100644 --- a/chrome/browser/extensions/extension_service.h +++ b/chrome/browser/extensions/extension_service.h @@ -40,14 +40,19 @@ #include "content/common/property_bag.h" class CrxInstaller; +class ExtensionBookmarkEventRouter; class ExtensionBrowserEventRouter; class ExtensionContentSettingsStore; +class ExtensionCookiesEventRouter; +class ExtensionHistoryEventRouter; class ExtensionInstallUI; +class ExtensionManagementEventRouter; class ExtensionPreferenceEventRouter; class ExtensionServiceBackend; struct ExtensionSyncData; class ExtensionToolbarModel; class ExtensionUpdater; +class ExtensionWebNavigationEventRouter; class GURL; class PendingExtensionManager; class Profile; @@ -705,10 +710,20 @@ class ExtensionService // Flag to make sure event routers are only initialized once. bool event_routers_initialized_; + scoped_ptr<ExtensionHistoryEventRouter> history_event_router_; + scoped_ptr<ExtensionBrowserEventRouter> browser_event_router_; scoped_ptr<ExtensionPreferenceEventRouter> preference_event_router_; + scoped_ptr<ExtensionBookmarkEventRouter> bookmark_event_router_; + + scoped_ptr<ExtensionCookiesEventRouter> cookies_event_router_; + + scoped_ptr<ExtensionManagementEventRouter> management_event_router_; + + scoped_ptr<ExtensionWebNavigationEventRouter> web_navigation_event_router_; + // A collection of external extension providers. Each provider reads // a source of external extension information. Examples include the // windows registry and external_extensions.json. diff --git a/chrome/browser/extensions/extension_webnavigation_api.cc b/chrome/browser/extensions/extension_webnavigation_api.cc index 04da675..513760e 100644 --- a/chrome/browser/extensions/extension_webnavigation_api.cc +++ b/chrome/browser/extensions/extension_webnavigation_api.cc @@ -247,16 +247,11 @@ void FrameNavigationState::RemoveTabContentsState( // ExtensionWebNavigtionEventRouter ------------------------------------------- -ExtensionWebNavigationEventRouter::ExtensionWebNavigationEventRouter() {} +ExtensionWebNavigationEventRouter::ExtensionWebNavigationEventRouter( + Profile* profile) : profile_(profile) {} ExtensionWebNavigationEventRouter::~ExtensionWebNavigationEventRouter() {} -// static -ExtensionWebNavigationEventRouter* -ExtensionWebNavigationEventRouter::GetInstance() { - return Singleton<ExtensionWebNavigationEventRouter>::get(); -} - void ExtensionWebNavigationEventRouter::Init() { if (registrar_.IsEmpty()) { registrar_.Add(this, @@ -284,10 +279,12 @@ void ExtensionWebNavigationEventRouter::Observe( void ExtensionWebNavigationEventRouter::CreatingNewWindow( TabContents* tab_contents, const ViewHostMsg_CreateWindow_Params* details) { - DispatchOnBeforeRetarget(tab_contents, - tab_contents->profile(), - details->opener_url, - details->target_url); + if (profile_->IsSameProfile(tab_contents->profile())) { + DispatchOnBeforeRetarget(tab_contents, + tab_contents->profile(), + details->opener_url, + details->target_url); + } } diff --git a/chrome/browser/extensions/extension_webnavigation_api.h b/chrome/browser/extensions/extension_webnavigation_api.h index 9f475dc..431507d 100644 --- a/chrome/browser/extensions/extension_webnavigation_api.h +++ b/chrome/browser/extensions/extension_webnavigation_api.h @@ -12,8 +12,8 @@ #include <map> -#include "base/memory/singleton.h" #include "chrome/browser/extensions/extension_function.h" +#include "chrome/browser/profiles/profile.h" #include "content/browser/tab_contents/tab_contents_observer.h" #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" @@ -132,19 +132,14 @@ class ExtensionWebNavigationTabObserver : public TabContentsObserver { // system. class ExtensionWebNavigationEventRouter : public NotificationObserver { public: - // Returns the singleton instance of the event router. - static ExtensionWebNavigationEventRouter* GetInstance(); + explicit ExtensionWebNavigationEventRouter(Profile* profile); + virtual ~ExtensionWebNavigationEventRouter(); // Invoked by the extensions service once the extension system is fully set // up and can start dispatching events to extensions. void Init(); private: - friend struct DefaultSingletonTraits<ExtensionWebNavigationEventRouter>; - - ExtensionWebNavigationEventRouter(); - virtual ~ExtensionWebNavigationEventRouter(); - // NotificationObserver implementation. virtual void Observe(NotificationType type, const NotificationSource& source, @@ -159,6 +154,9 @@ class ExtensionWebNavigationEventRouter : public NotificationObserver { // Used for tracking registrations to navigation notifications. NotificationRegistrar registrar_; + // The profile that owns us via ExtensionService. + Profile* profile_; + DISALLOW_COPY_AND_ASSIGN(ExtensionWebNavigationEventRouter); }; |