Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: chrome/browser/themes/theme_service_factory.cc

Issue 6766004: Create a ProfileDependencyManager to order ProfileKeyedService destruction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/themes/theme_service_factory.h" 5 #include "chrome/browser/themes/theme_service_factory.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/extensions/extension_service.h" 8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/profiles/profile_dependency_manager.h"
10 #include "chrome/browser/themes/theme_service.h" 11 #include "chrome/browser/themes/theme_service.h"
11 #include "content/common/notification_service.h" 12 #include "content/common/notification_service.h"
12 13
13 #if defined(TOOLKIT_USES_GTK) 14 #if defined(TOOLKIT_USES_GTK)
14 #include "chrome/browser/ui/gtk/gtk_theme_service.h" 15 #include "chrome/browser/ui/gtk/gtk_theme_service.h"
15 #endif 16 #endif
16 17
17 // static 18 // static
18 ThemeService* ThemeServiceFactory::GetForProfile(Profile* top_profile) { 19 ThemeService* ThemeServiceFactory::GetForProfile(Profile* profile) {
19 // We may be asked for the Theme of an incognito profile. Make sure we're 20 return static_cast<ThemeService*>(
20 // operating on the real profile. 21 GetInstance()->GetServiceForProfile(profile));
21 Profile* profile = top_profile->GetOriginalProfile();
22
23 ThemeServiceFactory* service = GetInstance();
24
25 std::map<Profile*, ThemeService*>::const_iterator it =
26 service->mapping_.find(profile);
27 if (it != service->mapping_.end())
28 return it->second;
29
30 ThemeService* provider = NULL;
31 #if defined(TOOLKIT_USES_GTK)
32 provider = new GtkThemeService;
33 #else
34 provider = new ThemeService;
35 #endif
36 provider->Init(profile);
37
38 service->Associate(profile, provider);
39 return provider;
40 } 22 }
41 23
42 const Extension* ThemeServiceFactory::GetThemeForProfile(Profile* profile) { 24 const Extension* ThemeServiceFactory::GetThemeForProfile(Profile* profile) {
43 std::string id = GetForProfile(profile)->GetThemeID(); 25 std::string id = GetForProfile(profile)->GetThemeID();
44 if (id == ThemeService::kDefaultThemeID) 26 if (id == ThemeService::kDefaultThemeID)
45 return NULL; 27 return NULL;
46 28
47 return profile->GetExtensionService()->GetExtensionById(id, false); 29 return profile->GetExtensionService()->GetExtensionById(id, false);
48 } 30 }
49 31
50 void ThemeServiceFactory::ForceAssociationBetween(Profile* top_profile, 32 void ThemeServiceFactory::ForceAssociationBetween(Profile* profile,
51 ThemeService* provider) { 33 ThemeService* provider) {
52 ThemeServiceFactory* service = GetInstance(); 34 GetInstance()->Associate(profile, provider);
53 Profile* profile = top_profile->GetOriginalProfile();
54
55 service->Associate(profile, provider);
56 } 35 }
57 36
58 ThemeServiceFactory* ThemeServiceFactory::GetInstance() { 37 ThemeServiceFactory* ThemeServiceFactory::GetInstance() {
59 return Singleton<ThemeServiceFactory>::get(); 38 static ThemeServiceFactory* factory = Singleton<ThemeServiceFactory>::get();
39 return factory;
60 } 40 }
61 41
62 ThemeServiceFactory::ThemeServiceFactory() {} 42 ThemeServiceFactory::ThemeServiceFactory()
43 : ProfileKeyedServiceFactory(
44 ProfileDependencyManager::GetInstance())
45 {}
63 46
64 ThemeServiceFactory::~ThemeServiceFactory() { 47 ThemeServiceFactory::~ThemeServiceFactory() {}
65 DCHECK(mapping_.empty()); 48
49 ProfileKeyedService* ThemeServiceFactory::BuildServiceInstanceFor(
50 Profile* profile) const {
51 ThemeService* provider = NULL;
52 #if defined(TOOLKIT_USES_GTK)
53 provider = new GtkThemeService;
54 #else
55 provider = new ThemeService;
56 #endif
57 provider->Init(profile);
58
59 return provider;
66 } 60 }
67 61
68 void ThemeServiceFactory::Associate(Profile* profile, 62 bool ThemeServiceFactory::ServiceRedirectedInIncognito() {
69 ThemeService* provider) { 63 return true;
70 DCHECK(mapping_.find(profile) == mapping_.end());
71 mapping_.insert(std::make_pair(profile, provider));
72
73 registrar_.Add(this,
74 NotificationType::PROFILE_DESTROYED,
75 Source<Profile>(profile));
76 registrar_.Add(this,
77 NotificationType::THEME_INSTALLED,
78 Source<Profile>(profile));
79 } 64 }
80
81 void ThemeServiceFactory::Observe(NotificationType type,
82 const NotificationSource& source,
83 const NotificationDetails& details) {
84 std::map<Profile*, ThemeService*>::iterator it =
85 mapping_.find(Source<Profile>(source).ptr());
86 DCHECK(it != mapping_.end());
87
88 if (NotificationType::PROFILE_DESTROYED == type) {
89 delete it->second;
90 mapping_.erase(it);
91
92 // Remove ourselves from listening to all notifications because the source
93 // profile has been deleted. We have to do this because several unit tests
94 // are set up so a Profile is on the same place on the stack multiple
95 // times, so while they are different instances, they have the same
96 // addresses.
97 registrar_.Remove(this,
98 NotificationType::PROFILE_DESTROYED,
99 source);
100 registrar_.Remove(this,
101 NotificationType::THEME_INSTALLED,
102 source);
103 } else if (NotificationType::THEME_INSTALLED == type) {
104 const Extension* extension = Details<const Extension>(details).ptr();
105 it->second->SetTheme(extension);
106 } else {
107 NOTREACHED();
108 }
109 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698