diff options
author | torne@chromium.org <torne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-12 18:48:17 +0000 |
---|---|---|
committer | torne@chromium.org <torne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-12 18:48:17 +0000 |
commit | 7211f74d2f66c46507e235b39245f736420e68d1 (patch) | |
tree | ea8f2a932730dd902540f8e9ce2cb79db4b654c8 /chrome | |
parent | 413a8e22c2ff986d335cdc9b86f78a6716a52975 (diff) | |
download | chromium_src-7211f74d2f66c46507e235b39245f736420e68d1.zip chromium_src-7211f74d2f66c46507e235b39245f736420e68d1.tar.gz chromium_src-7211f74d2f66c46507e235b39245f736420e68d1.tar.bz2 |
Profile shouldn't own DesktopNotificationService.
DesktopNotificationService is now owned by
DesktopNotificationServiceFactory, using Profile as a key. This uses the
ProfileKeyedService infrastructure originally created for
ThemeServiceFactory.
BUG=77155
TEST=existing tests
Review URL: http://codereview.chromium.org/6803012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81277 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
27 files changed, 191 insertions, 106 deletions
diff --git a/chrome/browser/chromeos/notifications/balloon_view.cc b/chrome/browser/chromeos/notifications/balloon_view.cc index cac419a..6246e51 100644 --- a/chrome/browser/chromeos/notifications/balloon_view.cc +++ b/chrome/browser/chromeos/notifications/balloon_view.cc @@ -12,6 +12,7 @@ #include "chrome/browser/chromeos/notifications/notification_panel.h" #include "chrome/browser/notifications/balloon.h" #include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/views/notifications/balloon_view_host.h" @@ -338,7 +339,7 @@ void BalloonViewImpl::DelayedClose(bool by_user) { void BalloonViewImpl::DenyPermission() { DesktopNotificationService* service = - balloon_->profile()->GetDesktopNotificationService(); + DesktopNotificationServiceFactory::GetForProfile(balloon_->profile()); service->DenyPermission(balloon_->notification().origin_url()); } diff --git a/chrome/browser/content_settings/content_settings_notification_provider.cc b/chrome/browser/content_settings/content_settings_notification_provider.cc index 1d81fbe..835bb14 100644 --- a/chrome/browser/content_settings/content_settings_notification_provider.cc +++ b/chrome/browser/content_settings/content_settings_notification_provider.cc @@ -6,6 +6,7 @@ #include "chrome/browser/content_settings/content_settings_notification_provider.h" #include "base/string_util.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notifications_prefs_cache.h" #include "chrome/browser/notifications/notification_ui_manager.h" @@ -227,7 +228,7 @@ void NotificationProvider::NotifySettingsChange() { NotificationService::current()->Notify( NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, Source<DesktopNotificationService>( - profile_->GetDesktopNotificationService()), + DesktopNotificationServiceFactory::GetForProfile(profile_)), NotificationService::NoDetails()); } diff --git a/chrome/browser/desktop_notification_handler.cc b/chrome/browser/desktop_notification_handler.cc index 8bd2ef2..986425a 100644 --- a/chrome/browser/desktop_notification_handler.cc +++ b/chrome/browser/desktop_notification_handler.cc @@ -5,6 +5,7 @@ #include "chrome/browser/desktop_notification_handler.h" #include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/profiles/profile.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/browser/renderer_host/render_view_host.h" @@ -38,7 +39,7 @@ void DesktopNotificationHandler::OnShow( const DesktopNotificationHostMsg_Show_Params& params) { RenderProcessHost* process = render_view_host()->process(); DesktopNotificationService* service = - process->profile()->GetDesktopNotificationService(); + DesktopNotificationServiceFactory::GetForProfile(process->profile()); service->ShowDesktopNotification( params, @@ -50,7 +51,7 @@ void DesktopNotificationHandler::OnShow( void DesktopNotificationHandler::OnCancel(int notification_id) { RenderProcessHost* process = render_view_host()->process(); DesktopNotificationService* service = - process->profile()->GetDesktopNotificationService(); + DesktopNotificationServiceFactory::GetForProfile(process->profile()); service->CancelDesktopNotification( process->id(), @@ -67,7 +68,7 @@ void DesktopNotificationHandler::OnRequestPermission( RenderProcessHost* process = render_view_host()->process(); DesktopNotificationService* service = - process->profile()->GetDesktopNotificationService(); + DesktopNotificationServiceFactory::GetForProfile(process->profile()); service->RequestPermission( source_origin, process->id(), routing_id(), callback_context, NULL); } diff --git a/chrome/browser/extensions/notifications_apitest.cc b/chrome/browser/extensions/notifications_apitest.cc index 7e2a34d..22eea07 100644 --- a/chrome/browser/extensions/notifications_apitest.cc +++ b/chrome/browser/extensions/notifications_apitest.cc @@ -1,10 +1,11 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" @@ -16,11 +17,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FLAKY_Notifications) { ASSERT_TRUE(RunExtensionTest("notifications/has_not_permission")) << message_; ASSERT_TRUE(RunExtensionTest("notifications/has_permission_manifest")) << message_; - browser()->profile()->GetDesktopNotificationService() + DesktopNotificationServiceFactory::GetForProfile(browser()->profile()) ->GrantPermission(GURL( "chrome-extension://peoadpeiejnhkmpaakpnompolbglelel")); ASSERT_TRUE(RunExtensionTest("notifications/has_permission_prefs")) << message_; #endif } - diff --git a/chrome/browser/notifications/desktop_notification_service.cc b/chrome/browser/notifications/desktop_notification_service.cc index ef5bf76..bfe85ab 100644 --- a/chrome/browser/notifications/desktop_notification_service.cc +++ b/chrome/browser/notifications/desktop_notification_service.cc @@ -9,6 +9,7 @@ #include "base/utf_string_conversions.h" #include "chrome/browser/content_settings/content_settings_provider.h" #include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_object_proxy.h" #include "chrome/browser/notifications/notification_ui_manager.h" @@ -187,14 +188,16 @@ string16 NotificationPermissionInfoBarDelegate::GetButtonLabel( bool NotificationPermissionInfoBarDelegate::Accept() { UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Allowed", 1); - profile_->GetDesktopNotificationService()->GrantPermission(origin_); + DesktopNotificationServiceFactory::GetForProfile(profile_)-> + GrantPermission(origin_); action_taken_ = true; return true; } bool NotificationPermissionInfoBarDelegate::Cancel() { UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Denied", 1); - profile_->GetDesktopNotificationService()->DenyPermission(origin_); + DesktopNotificationServiceFactory::GetForProfile(profile_)-> + DenyPermission(origin_); action_taken_ = true; return true; } diff --git a/chrome/browser/notifications/desktop_notification_service.h b/chrome/browser/notifications/desktop_notification_service.h index a5c05a9..50f28f5 100644 --- a/chrome/browser/notifications/desktop_notification_service.h +++ b/chrome/browser/notifications/desktop_notification_service.h @@ -16,6 +16,7 @@ #include "chrome/browser/content_settings/content_settings_notification_provider.h" #include "chrome/browser/content_settings/content_settings_provider.h" #include "chrome/browser/prefs/pref_change_registrar.h" +#include "chrome/browser/profiles/profile_keyed_service.h" #include "chrome/common/content_settings.h" #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" @@ -32,7 +33,8 @@ struct DesktopNotificationHostMsg_Show_Params; // The DesktopNotificationService is an object, owned by the Profile, // which provides the creation of desktop "toasts" to web pages and workers. -class DesktopNotificationService : public NotificationObserver { +class DesktopNotificationService : public NotificationObserver, + public ProfileKeyedService { public: enum DesktopNotificationSource { PageNotification, diff --git a/chrome/browser/notifications/desktop_notification_service_factory.cc b/chrome/browser/notifications/desktop_notification_service_factory.cc new file mode 100644 index 0000000..1e24345 --- /dev/null +++ b/chrome/browser/notifications/desktop_notification_service_factory.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/notifications/desktop_notification_service_factory.h" + +#include "chrome/browser/browser_process.h" +#include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_dependency_manager.h" +#include "content/browser/browser_thread.h" + +// static +DesktopNotificationService* DesktopNotificationServiceFactory::GetForProfile( + Profile* profile) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + return static_cast<DesktopNotificationService*>( + GetInstance()->GetServiceForProfile(profile)); +} + +// static +DesktopNotificationServiceFactory* DesktopNotificationServiceFactory:: + GetInstance() { + return Singleton<DesktopNotificationServiceFactory>::get(); +} + +DesktopNotificationServiceFactory::DesktopNotificationServiceFactory() + : ProfileKeyedServiceFactory(ProfileDependencyManager::GetInstance()) { +} + +DesktopNotificationServiceFactory::~DesktopNotificationServiceFactory() { +} + +ProfileKeyedService* DesktopNotificationServiceFactory::BuildServiceInstanceFor( + Profile* profile) const { + DesktopNotificationService* service = new DesktopNotificationService(profile, + g_browser_process->notification_ui_manager()); + + return service; +} + +bool DesktopNotificationServiceFactory::ServiceHasOwnInstanceInIncognito() { + return true; +} diff --git a/chrome/browser/notifications/desktop_notification_service_factory.h b/chrome/browser/notifications/desktop_notification_service_factory.h new file mode 100644 index 0000000..10211b2 --- /dev/null +++ b/chrome/browser/notifications/desktop_notification_service_factory.h @@ -0,0 +1,39 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_FACTORY_H_ +#define CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_FACTORY_H_ + +#include "base/compiler_specific.h" +#include "base/memory/singleton.h" +#include "chrome/browser/profiles/profile_keyed_service_factory.h" + +class DesktopNotificationService; +class Profile; + +// Singleton that owns all DesktopNotificationServices and associates them with +// Profiles. Listens for the Profile's destruction notification and cleans up +// the associated DesktopNotificationService. +class DesktopNotificationServiceFactory : public ProfileKeyedServiceFactory { + public: + // Returns the DesktopNotificationService that provides desktop notifications + // for |profile|. + static DesktopNotificationService* GetForProfile(Profile* profile); + + static DesktopNotificationServiceFactory* GetInstance(); + + private: + friend struct DefaultSingletonTraits<DesktopNotificationServiceFactory>; + + DesktopNotificationServiceFactory(); + virtual ~DesktopNotificationServiceFactory(); + + // ProfileKeyedServiceFactory: + virtual ProfileKeyedService* BuildServiceInstanceFor( + Profile* profile) const OVERRIDE; + // Use a separate desktop notification service for incognito. + virtual bool ServiceHasOwnInstanceInIncognito() OVERRIDE; +}; + +#endif // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATION_SERVICE_FACTORY_H_ diff --git a/chrome/browser/notifications/desktop_notification_service_unittest.cc b/chrome/browser/notifications/desktop_notification_service_unittest.cc index 0ad39cb..a8fbbe3 100644 --- a/chrome/browser/notifications/desktop_notification_service_unittest.cc +++ b/chrome/browser/notifications/desktop_notification_service_unittest.cc @@ -7,6 +7,7 @@ #include "base/memory/ref_counted.h" #include "base/message_loop.h" #include "base/synchronization/waitable_event.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notifications_prefs_cache.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/prefs/scoped_user_pref_update.h" @@ -102,7 +103,7 @@ class DesktopNotificationServiceTest : public RenderViewHostTestHarness { // Creates the service, calls InitPrefs() on it which loads data from the // profile into the cache and then puts the cache in io thread mode. - service_ = profile()->GetDesktopNotificationService(); + service_ = DesktopNotificationServiceFactory::GetForProfile(profile()); cache_ = service_->prefs_cache(); } diff --git a/chrome/browser/notifications/notification_exceptions_table_model_unittest.cc b/chrome/browser/notifications/notification_exceptions_table_model_unittest.cc index 6c6c182..25f2680 100644 --- a/chrome/browser/notifications/notification_exceptions_table_model_unittest.cc +++ b/chrome/browser/notifications/notification_exceptions_table_model_unittest.cc @@ -1,10 +1,11 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/notifications/notification_exceptions_table_model.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/test/testing_profile.h" #include "content/browser/browser_thread.h" #include "content/browser/renderer_host/test_render_view_host.h" @@ -23,7 +24,7 @@ class NotificationExceptionsTableModelTest : public RenderViewHostTestHarness { virtual void SetUp() { RenderViewHostTestHarness::SetUp(); - service_ = profile()->GetDesktopNotificationService(); + service_ = DesktopNotificationServiceFactory::GetForProfile(profile()); ResetModel(); } diff --git a/chrome/browser/notifications/notification_options_menu_model.cc b/chrome/browser/notifications/notification_options_menu_model.cc index 0bf7533..7024142 100644 --- a/chrome/browser/notifications/notification_options_menu_model.cc +++ b/chrome/browser/notifications/notification_options_menu_model.cc @@ -12,6 +12,7 @@ #include "chrome/browser/notifications/balloon_collection.h" #include "chrome/browser/notifications/balloon_host.h" #include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_ui_manager.h" #include "chrome/browser/notifications/notifications_prefs_cache.h" @@ -160,7 +161,7 @@ string16 NotificationOptionsMenuModel::GetLabelForCommandId(int command_id) const GURL& origin = notification.origin_url(); DesktopNotificationService* service = - balloon_->profile()->GetDesktopNotificationService(); + DesktopNotificationServiceFactory::GetForProfile(balloon_->profile()); if (origin.SchemeIs(chrome::kExtensionScheme)) { ExtensionService* ext_service = balloon_->profile()->GetExtensionService(); @@ -210,7 +211,7 @@ bool NotificationOptionsMenuModel::GetAcceleratorForCommandId( void NotificationOptionsMenuModel::ExecuteCommand(int command_id) { DesktopNotificationService* service = - balloon_->profile()->GetDesktopNotificationService(); + DesktopNotificationServiceFactory::GetForProfile(balloon_->profile()); ExtensionService* ext_service = balloon_->profile()->GetExtensionService(); const GURL& origin = balloon_->notification().origin_url(); diff --git a/chrome/browser/profiles/profile.cc b/chrome/browser/profiles/profile.cc index 1bd2bd9..39460e5 100644 --- a/chrome/browser/profiles/profile.cc +++ b/chrome/browser/profiles/profile.cc @@ -23,7 +23,6 @@ #include "chrome/browser/extensions/extension_process_manager.h" #include "chrome/browser/extensions/extension_special_storage_policy.h" #include "chrome/browser/net/pref_proxy_config_service.h" -#include "chrome/browser/notifications/desktop_notification_service.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/off_the_record_profile_io_data.h" #include "chrome/browser/profiles/profile_dependency_manager.h" @@ -505,14 +504,6 @@ class OffTheRecordProfileImpl : public Profile, return profile_->GetProtocolHandlerRegistry(); } - virtual DesktopNotificationService* GetDesktopNotificationService() { - if (!desktop_notification_service_.get()) { - desktop_notification_service_.reset(new DesktopNotificationService( - this, g_browser_process->notification_ui_manager())); - } - return desktop_notification_service_.get(); - } - virtual TokenService* GetTokenService() { return NULL; } @@ -702,9 +693,6 @@ class OffTheRecordProfileImpl : public Profile, // The download manager that only stores downloaded items in memory. scoped_refptr<DownloadManager> download_manager_; - // Use a separate desktop notification service for OTR. - scoped_ptr<DesktopNotificationService> desktop_notification_service_; - // We use a non-writable content settings map for OTR. scoped_refptr<HostContentSettingsMap> host_content_settings_map_; diff --git a/chrome/browser/profiles/profile.h b/chrome/browser/profiles/profile.h index e7d1741..3bad14a 100644 --- a/chrome/browser/profiles/profile.h +++ b/chrome/browser/profiles/profile.h @@ -49,7 +49,6 @@ class ChromeAppCacheService; class ChromeBlobStorageContext; class ChromeURLDataManager; class CloudPrintProxyService; -class DesktopNotificationService; class DownloadManager; class Extension; class ExtensionDevToolsManager; @@ -451,9 +450,6 @@ class Profile { // Returns the WebKitContext assigned to this profile. virtual WebKitContext* GetWebKitContext() = 0; - // Returns the provider of desktop notifications for this profile. - virtual DesktopNotificationService* GetDesktopNotificationService() = 0; - // Returns the service that manages BackgroundContents for this profile. virtual BackgroundContentsService* GetBackgroundContentsService() const = 0; diff --git a/chrome/browser/profiles/profile_dependency_manager_unittest.cc b/chrome/browser/profiles/profile_dependency_manager_unittest.cc index c50de50..3b99963 100644 --- a/chrome/browser/profiles/profile_dependency_manager_unittest.cc +++ b/chrome/browser/profiles/profile_dependency_manager_unittest.cc @@ -6,7 +6,6 @@ #include "chrome/browser/profiles/profile_dependency_manager.h" #include "chrome/browser/profiles/profile_keyed_service_factory.h" -#include "chrome/test/testing_profile.h" class ProfileDependencyManagerUnittests : public ::testing::Test { protected: @@ -22,11 +21,6 @@ class ProfileDependencyManagerUnittests : public ::testing::Test { child->DependsOn(parent); } - void CreateAndDestroyTestProfile() { - TestingProfile profile; - profile.SetProfileDependencyManager(&dependency_manager_); - } - ProfileDependencyManager* manager() { return &dependency_manager_; } std::vector<std::string>* shutdown_order() { return &shutdown_order_; } @@ -66,7 +60,7 @@ class TestService : public ProfileKeyedServiceFactory { TEST_F(ProfileDependencyManagerUnittests, SingleCase) { TestService service("service", shutdown_order(), manager()); - CreateAndDestroyTestProfile(); + manager()->DestroyProfileServices(NULL); ASSERT_EQ(1U, shutdown_order()->size()); EXPECT_STREQ("service", (*shutdown_order())[0].c_str()); @@ -78,7 +72,7 @@ TEST_F(ProfileDependencyManagerUnittests, SimpleDependency) { TestService child("child", shutdown_order(), manager()); DependOn(&child, &parent); - CreateAndDestroyTestProfile(); + manager()->DestroyProfileServices(NULL); ASSERT_EQ(2U, shutdown_order()->size()); EXPECT_STREQ("child", (*shutdown_order())[0].c_str()); @@ -93,7 +87,7 @@ TEST_F(ProfileDependencyManagerUnittests, TwoChildrenOneParent) { DependOn(&child1, &parent); DependOn(&child2, &parent); - CreateAndDestroyTestProfile(); + manager()->DestroyProfileServices(NULL); ASSERT_EQ(3U, shutdown_order()->size()); EXPECT_STREQ("child2", (*shutdown_order())[0].c_str()); @@ -116,7 +110,7 @@ TEST_F(ProfileDependencyManagerUnittests, MConfiguration) { TestService child_of_2("child_of_2", shutdown_order(), manager()); DependOn(&child_of_2, &parent2); - CreateAndDestroyTestProfile(); + manager()->DestroyProfileServices(NULL); ASSERT_EQ(5U, shutdown_order()->size()); EXPECT_STREQ("child_of_2", (*shutdown_order())[0].c_str()); @@ -140,7 +134,7 @@ TEST_F(ProfileDependencyManagerUnittests, DiamondConfiguration) { DependOn(&bottom, &middle_row_1); DependOn(&bottom, &middle_row_2); - CreateAndDestroyTestProfile(); + manager()->DestroyProfileServices(NULL); ASSERT_EQ(4U, shutdown_order()->size()); EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str()); @@ -173,7 +167,7 @@ TEST_F(ProfileDependencyManagerUnittests, ComplexGraph) { DependOn(&bottom, &specialized_service); DependOn(&bottom, &other_intermediary); - CreateAndDestroyTestProfile(); + manager()->DestroyProfileServices(NULL); ASSERT_EQ(6U, shutdown_order()->size()); EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str()); diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc index b0148d1..2717989 100644 --- a/chrome/browser/profiles/profile_impl.cc +++ b/chrome/browser/profiles/profile_impl.cc @@ -46,7 +46,6 @@ #include "chrome/browser/net/net_pref_observer.h" #include "chrome/browser/net/pref_proxy_config_service.h" #include "chrome/browser/net/ssl_config_service_manager.h" -#include "chrome/browser/notifications/desktop_notification_service.h" #include "chrome/browser/password_manager/password_store_default.h" #include "chrome/browser/policy/configuration_policy_pref_store.h" #include "chrome/browser/policy/configuration_policy_provider.h" @@ -1237,15 +1236,6 @@ WebKitContext* ProfileImpl::GetWebKitContext() { return webkit_context_.get(); } -DesktopNotificationService* ProfileImpl::GetDesktopNotificationService() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (!desktop_notification_service_.get()) { - desktop_notification_service_.reset(new DesktopNotificationService( - this, g_browser_process->notification_ui_manager())); - } - return desktop_notification_service_.get(); -} - void ProfileImpl::MarkAsCleanShutdown() { if (prefs_.get()) { // The session cleanly exited, set kSessionExitedCleanly appropriately. diff --git a/chrome/browser/profiles/profile_impl.h b/chrome/browser/profiles/profile_impl.h index e25079e..b58ea94 100644 --- a/chrome/browser/profiles/profile_impl.h +++ b/chrome/browser/profiles/profile_impl.h @@ -111,7 +111,6 @@ class ProfileImpl : public Profile, virtual SpellCheckHost* GetSpellCheckHost(); virtual void ReinitializeSpellCheckHost(bool force); virtual WebKitContext* GetWebKitContext(); - virtual DesktopNotificationService* GetDesktopNotificationService(); virtual BackgroundContentsService* GetBackgroundContentsService() const; virtual StatusTray* GetStatusTray(); virtual void MarkAsCleanShutdown(); @@ -242,7 +241,6 @@ class ProfileImpl : public Profile, scoped_refptr<PasswordStore> password_store_; scoped_refptr<SessionService> session_service_; scoped_refptr<WebKitContext> webkit_context_; - scoped_ptr<DesktopNotificationService> desktop_notification_service_; scoped_ptr<BackgroundContentsService> background_contents_service_; scoped_ptr<BackgroundModeManager> background_mode_manager_; scoped_ptr<StatusTray> status_tray_; diff --git a/chrome/browser/profiles/profile_keyed_service_factory.cc b/chrome/browser/profiles/profile_keyed_service_factory.cc index cea66c9..755409e 100644 --- a/chrome/browser/profiles/profile_keyed_service_factory.cc +++ b/chrome/browser/profiles/profile_keyed_service_factory.cc @@ -13,7 +13,7 @@ ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( ProfileDependencyManager* manager) - : dependency_manager_(manager) { + : dependency_manager_(manager), factory_(NULL) { dependency_manager_->AddComponent(this); } @@ -35,12 +35,22 @@ ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( } } + ProfileKeyedService* service; + std::map<Profile*, ProfileKeyedService*>::iterator it = mapping_.find(profile); - if (it != mapping_.end()) - return it->second; + if (it != mapping_.end()) { + service = it->second; + if (service || !factory_) + return service; + + // service is NULL but we have a mock factory function + mapping_.erase(it); + service = factory_(profile); + } else { + service = BuildServiceInstanceFor(profile); + } - ProfileKeyedService* service = BuildServiceInstanceFor(profile); Associate(profile, service); return service; } @@ -66,7 +76,7 @@ bool ProfileKeyedServiceFactory::ServiceHasOwnInstanceInIncognito() { void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { std::map<Profile*, ProfileKeyedService*>::iterator it = mapping_.find(profile); - if (it != mapping_.end()) + if (it != mapping_.end() && it->second) it->second->Shutdown(); } diff --git a/chrome/browser/profiles/profile_keyed_service_factory.h b/chrome/browser/profiles/profile_keyed_service_factory.h index fd33b15..8126207 100644 --- a/chrome/browser/profiles/profile_keyed_service_factory.h +++ b/chrome/browser/profiles/profile_keyed_service_factory.h @@ -20,10 +20,26 @@ class ProfileKeyedService; // shutdown/destruction order. In each derived classes' constructors, the // implementors must explicitly state which services are depended on. class ProfileKeyedServiceFactory { - protected: - friend class ProfileDependencyManager; - friend class ProfileDependencyManagerUnittests; + public: + typedef ProfileKeyedService* (*FactoryFunction)(Profile* profile); + +#if defined(UNIT_TEST) + // Associate an already-created |service| with |profile| for this factory. + // The service may be a mock, or may be NULL to inhibit automatic creation of + // the service by the default function. A mock factory set with + // |set_test_factory| will be called instead if the service is NULL. + void ForceAssociationBetween(Profile* profile, ProfileKeyedService* service) { + Associate(profile, service); + } + + // Sets the factory function to use to create mock instances of this service. + // The factory function will only be called for profiles for which + // |ForceAssociationBetween| has been previously called with a NULL service + // pointer, and therefore does not affect normal non-test profiles. + void set_test_factory(FactoryFunction factory) { factory_ = factory; } +#endif + protected: // ProfileKeyedServiceFactories must communicate with a // ProfileDependencyManager. For all non-test code, write your subclass // constructors like this: @@ -74,6 +90,9 @@ class ProfileKeyedServiceFactory { virtual void ProfileDestroyed(Profile* profile); private: + friend class ProfileDependencyManager; + friend class ProfileDependencyManagerUnittests; + // The mapping between a Profile and its service. std::map<Profile*, ProfileKeyedService*> mapping_; @@ -81,6 +100,9 @@ class ProfileKeyedServiceFactory { // this will always be ProfileDependencyManager::GetInstance(), but unit // tests will want to use their own copy. ProfileDependencyManager* dependency_manager_; + + // A mock factory function to use to create the service, used by tests. + FactoryFunction factory_; }; #endif // CHROME_BROWSER_PROFILES_PROFILE_KEYED_SERVICE_FACTORY_H_ diff --git a/chrome/browser/sync/glue/theme_util_unittest.cc b/chrome/browser/sync/glue/theme_util_unittest.cc index 5c83117..d72eb4a 100644 --- a/chrome/browser/sync/glue/theme_util_unittest.cc +++ b/chrome/browser/sync/glue/theme_util_unittest.cc @@ -102,7 +102,8 @@ TEST_F(ThemeUtilTest, SetCurrentThemeDefaultTheme) { sync_pb::ThemeSpecifics theme_specifics; TestingProfile profile; MockThemeService* mock_theme_service = new MockThemeService; - ThemeServiceFactory::ForceAssociationBetween(&profile, mock_theme_service); + ThemeServiceFactory::GetInstance()->ForceAssociationBetween(&profile, + mock_theme_service); EXPECT_CALL(*mock_theme_service, UseDefaultTheme()).Times(1); @@ -115,7 +116,8 @@ TEST_F(ThemeUtilTest, SetCurrentThemeSystemTheme) { TestingProfile profile; MockThemeService* mock_theme_service = new MockThemeService; - ThemeServiceFactory::ForceAssociationBetween(&profile, mock_theme_service); + ThemeServiceFactory::GetInstance()->ForceAssociationBetween(&profile, + mock_theme_service); EXPECT_CALL(*mock_theme_service, SetNativeTheme()).Times(1); @@ -215,7 +217,8 @@ TEST_F(ThemeUtilTest, GetThemeSpecificsHelperCustomThemeDistinct) { TEST_F(ThemeUtilTest, SetCurrentThemeIfNecessaryDefaultThemeNotNecessary) { TestingProfile profile; MockThemeService* mock_theme_service = new MockThemeService; - ThemeServiceFactory::ForceAssociationBetween(&profile, mock_theme_service); + ThemeServiceFactory::GetInstance()->ForceAssociationBetween(&profile, + mock_theme_service); EXPECT_CALL(*mock_theme_service, GetThemeID()).WillRepeatedly(Return( ThemeService::kDefaultThemeID)); diff --git a/chrome/browser/themes/theme_service_factory.cc b/chrome/browser/themes/theme_service_factory.cc index 3a9e4b9..d0ba8eb 100644 --- a/chrome/browser/themes/theme_service_factory.cc +++ b/chrome/browser/themes/theme_service_factory.cc @@ -21,6 +21,7 @@ ThemeService* ThemeServiceFactory::GetForProfile(Profile* profile) { GetInstance()->GetServiceForProfile(profile)); } +// static const Extension* ThemeServiceFactory::GetThemeForProfile(Profile* profile) { std::string id = GetForProfile(profile)->GetThemeID(); if (id == ThemeService::kDefaultThemeID) @@ -29,11 +30,7 @@ const Extension* ThemeServiceFactory::GetThemeForProfile(Profile* profile) { return profile->GetExtensionService()->GetExtensionById(id, false); } -void ThemeServiceFactory::ForceAssociationBetween(Profile* profile, - ThemeService* provider) { - GetInstance()->Associate(profile, provider); -} - +// static ThemeServiceFactory* ThemeServiceFactory::GetInstance() { return Singleton<ThemeServiceFactory>::get(); } diff --git a/chrome/browser/themes/theme_service_factory.h b/chrome/browser/themes/theme_service_factory.h index a837ee8..924704b 100644 --- a/chrome/browser/themes/theme_service_factory.h +++ b/chrome/browser/themes/theme_service_factory.h @@ -31,11 +31,6 @@ class ThemeServiceFactory : public ProfileKeyedServiceFactory { // no installed theme, or the theme was cleared. static const Extension* GetThemeForProfile(Profile* profile); - // Forces an association between |profile| and |provider|. Used in unit tests - // where we need to mock ThemeService. - static void ForceAssociationBetween(Profile* profile, - ThemeService* provider); - static ThemeServiceFactory* GetInstance(); private: diff --git a/chrome/browser/ui/cocoa/notifications/balloon_controller.mm b/chrome/browser/ui/cocoa/notifications/balloon_controller.mm index 094ec95..dec0b90 100644 --- a/chrome/browser/ui/cocoa/notifications/balloon_controller.mm +++ b/chrome/browser/ui/cocoa/notifications/balloon_controller.mm @@ -11,6 +11,7 @@ #include "base/utf_string_conversions.h" #include "chrome/browser/notifications/balloon.h" #include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/notifications/notification.h" #include "chrome/browser/notifications/notification_options_menu_model.h" #include "chrome/browser/profiles/profile.h" @@ -144,7 +145,7 @@ const int kRightMargin = 2; - (IBAction)permissionRevoked:(id)sender { DesktopNotificationService* service = - balloon_->profile()->GetDesktopNotificationService(); + DesktopNotificationServiceFactory::GetForProfile(balloon_->profile()); service->DenyPermission(balloon_->notification().origin_url()); } diff --git a/chrome/browser/ui/options/options_util.cc b/chrome/browser/ui/options/options_util.cc index 590c093..43cd7dd 100644 --- a/chrome/browser/ui/options/options_util.cc +++ b/chrome/browser/ui/options/options_util.cc @@ -12,6 +12,7 @@ #include "chrome/browser/geolocation/geolocation_content_settings_map.h" #include "chrome/browser/metrics/metrics_service.h" #include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/pref_names.h" @@ -83,7 +84,8 @@ void OptionsUtil::ResetToDefaults(Profile* profile) { profile->GetHostContentSettingsMap()->ResetToDefaults(); profile->GetGeolocationContentSettingsMap()->ResetToDefault(); profile->GetHostZoomMap()->ResetToDefaults(); - profile->GetDesktopNotificationService()->ResetToDefaultContentSetting(); + DesktopNotificationServiceFactory::GetForProfile(profile)-> + ResetToDefaultContentSetting(); for (size_t i = 0; i < arraysize(kUserPrefs); ++i) prefs->ClearPref(kUserPrefs[i]); diff --git a/chrome/browser/ui/webui/options/content_settings_handler.cc b/chrome/browser/ui/webui/options/content_settings_handler.cc index 3c631d2..6bbab55 100644 --- a/chrome/browser/ui/webui/options/content_settings_handler.cc +++ b/chrome/browser/ui/webui/options/content_settings_handler.cc @@ -13,6 +13,7 @@ #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/geolocation/geolocation_content_settings_map.h" #include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser_list.h" #include "chrome/common/chrome_switches.h" @@ -375,8 +376,8 @@ std::string ContentSettingsHandler::GetSettingDefaultFromModel( default_setting = web_ui_->GetProfile()-> GetGeolocationContentSettingsMap()->GetDefaultContentSetting(); } else if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { - default_setting = web_ui_->GetProfile()-> - GetDesktopNotificationService()->GetDefaultContentSetting(); + default_setting = DesktopNotificationServiceFactory::GetForProfile( + web_ui_->GetProfile())->GetDefaultContentSetting(); } else { default_setting = GetContentSettingsMap()->GetDefaultContentSetting(type); } @@ -390,8 +391,8 @@ bool ContentSettingsHandler::GetDefaultSettingManagedFromModel( return web_ui_->GetProfile()-> GetGeolocationContentSettingsMap()->IsDefaultContentSettingManaged(); } else if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { - return web_ui_->GetProfile()-> - GetDesktopNotificationService()->IsDefaultContentSettingManaged(); + return DesktopNotificationServiceFactory::GetForProfile( + web_ui_->GetProfile())->IsDefaultContentSettingManaged(); } else { return GetContentSettingsMap()->IsDefaultContentSettingManaged(type); } @@ -491,7 +492,7 @@ void ContentSettingsHandler::UpdateGeolocationExceptionsView() { void ContentSettingsHandler::UpdateNotificationExceptionsView() { DesktopNotificationService* service = - web_ui_->GetProfile()->GetDesktopNotificationService(); + DesktopNotificationServiceFactory::GetForProfile(web_ui_->GetProfile()); std::vector<GURL> allowed(service->GetAllowedOrigins()); std::vector<GURL> blocked(service->GetBlockedOrigins()); @@ -590,7 +591,7 @@ void ContentSettingsHandler::SetContentFilter(const ListValue* args) { web_ui_->GetProfile()->GetGeolocationContentSettingsMap()-> SetDefaultContentSetting(default_setting); } else if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { - web_ui_->GetProfile()->GetDesktopNotificationService()-> + DesktopNotificationServiceFactory::GetForProfile(web_ui_->GetProfile())-> SetDefaultContentSetting(default_setting); } else { GetContentSettingsMap()-> @@ -632,11 +633,11 @@ void ContentSettingsHandler::RemoveException(const ListValue* args) { DCHECK(rv); ContentSetting content_setting = ContentSettingFromString(setting); if (content_setting == CONTENT_SETTING_ALLOW) { - web_ui_->GetProfile()->GetDesktopNotificationService()-> + DesktopNotificationServiceFactory::GetForProfile(web_ui_->GetProfile())-> ResetAllowedOrigin(GURL(origin)); } else { DCHECK_EQ(content_setting, CONTENT_SETTING_BLOCK); - web_ui_->GetProfile()->GetDesktopNotificationService()-> + DesktopNotificationServiceFactory::GetForProfile(web_ui_->GetProfile())-> ResetBlockedOrigin(GURL(origin)); } } else { diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 85215cd..1b1493d 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1391,6 +1391,8 @@ 'browser/notifications/balloon_host.h', 'browser/notifications/desktop_notification_service.cc', 'browser/notifications/desktop_notification_service.h', + 'browser/notifications/desktop_notification_service_factory.cc', + 'browser/notifications/desktop_notification_service_factory.h', 'browser/notifications/notification.cc', 'browser/notifications/notification.h', 'browser/notifications/notification_delegate.h', diff --git a/chrome/test/testing_profile.cc b/chrome/test/testing_profile.cc index 468c051..cab23da 100644 --- a/chrome/test/testing_profile.cc +++ b/chrome/test/testing_profile.cc @@ -27,6 +27,7 @@ #include "chrome/browser/net/gaia/token_service.h" #include "chrome/browser/net/pref_proxy_config_service.h" #include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/desktop_notification_service_factory.h" #include "chrome/browser/prefs/browser_prefs.h" #include "chrome/browser/prefs/testing_pref_store.h" #include "chrome/browser/prerender/prerender_manager.h" @@ -141,6 +142,10 @@ class TestExtensionURLRequestContextGetter scoped_refptr<net::URLRequestContext> context_; }; +ProfileKeyedService* CreateTestDesktopNotificationService(Profile* profile) { + return new DesktopNotificationService(profile, NULL); +} + } // namespace TestingProfile::TestingProfile() @@ -172,6 +177,12 @@ TestingProfile::TestingProfile() CHECK(temp_dir_.Set(system_tmp_dir)); } } + + // Install profile keyed service factory hooks for dummy/test services + DesktopNotificationServiceFactory::GetInstance()->set_test_factory( + &CreateTestDesktopNotificationService); + DesktopNotificationServiceFactory::GetInstance()->ForceAssociationBetween( + this, NULL); } TestingProfile::~TestingProfile() { @@ -362,11 +373,6 @@ TestingPrefService* TestingProfile::GetTestingPrefService() { return testing_prefs_; } -void TestingProfile::SetProfileDependencyManager( - ProfileDependencyManager* manager) { - profile_dependency_manager_ = manager; -} - ProfileId TestingProfile::GetRuntimeId() { return reinterpret_cast<ProfileId>(this); } @@ -693,15 +699,6 @@ NTPResourceCache* TestingProfile::GetNTPResourceCache() { return ntp_resource_cache_.get(); } -DesktopNotificationService* TestingProfile::GetDesktopNotificationService() { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - if (!desktop_notification_service_.get()) { - desktop_notification_service_.reset(new DesktopNotificationService( - this, NULL)); - } - return desktop_notification_service_.get(); -} - BackgroundContentsService* TestingProfile::GetBackgroundContentsService() const { return NULL; diff --git a/chrome/test/testing_profile.h b/chrome/test/testing_profile.h index 154294f..8b519d2 100644 --- a/chrome/test/testing_profile.h +++ b/chrome/test/testing_profile.h @@ -27,7 +27,6 @@ class SpecialStoragePolicy; class AutocompleteClassifier; class BookmarkModel; class CommandLine; -class DesktopNotificationService; class ExtensionPrefs; class ExtensionPrefStore; class ExtensionPrefValueMap; @@ -130,8 +129,6 @@ class TestingProfile : public Profile { TestingPrefService* GetTestingPrefService(); - void SetProfileDependencyManager(ProfileDependencyManager* manager); - virtual ProfileId GetRuntimeId(); virtual FilePath GetPath(); @@ -250,7 +247,6 @@ class TestingProfile : public Profile { virtual void InitRegisteredProtocolHandlers() {} virtual NTPResourceCache* GetNTPResourceCache(); - virtual DesktopNotificationService* GetDesktopNotificationService(); virtual BackgroundContentsService* GetBackgroundContentsService() const; virtual StatusTray* GetStatusTray(); virtual FilePath last_selected_directory(); @@ -371,7 +367,6 @@ class TestingProfile : public Profile { scoped_refptr<GeolocationContentSettingsMap> geolocation_content_settings_map_; scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; - scoped_ptr<DesktopNotificationService> desktop_notification_service_; // Find bar state. Created lazily by GetFindBarState(). scoped_ptr<FindBarState> find_bar_state_; |