| OLD | NEW |
| 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 return Singleton<ThemeServiceFactory>::get(); |
| 60 } | 39 } |
| 61 | 40 |
| 62 ThemeServiceFactory::ThemeServiceFactory() {} | 41 ThemeServiceFactory::ThemeServiceFactory() |
| 42 : ProfileKeyedServiceFactory( |
| 43 ProfileDependencyManager::GetInstance()) |
| 44 {} |
| 63 | 45 |
| 64 ThemeServiceFactory::~ThemeServiceFactory() { | 46 ThemeServiceFactory::~ThemeServiceFactory() {} |
| 65 DCHECK(mapping_.empty()); | 47 |
| 48 ProfileKeyedService* ThemeServiceFactory::BuildServiceInstanceFor( |
| 49 Profile* profile) const { |
| 50 ThemeService* provider = NULL; |
| 51 #if defined(TOOLKIT_USES_GTK) |
| 52 provider = new GtkThemeService; |
| 53 #else |
| 54 provider = new ThemeService; |
| 55 #endif |
| 56 provider->Init(profile); |
| 57 |
| 58 return provider; |
| 66 } | 59 } |
| 67 | 60 |
| 68 void ThemeServiceFactory::Associate(Profile* profile, | 61 bool ThemeServiceFactory::ServiceRedirectedInIncognito() { |
| 69 ThemeService* provider) { | 62 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 } | 63 } |
| 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 } | |
| OLD | NEW |