Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.h" | 5 #include "chrome/browser/themes/theme_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 #include "extensions/common/extension_set.h" | 42 #include "extensions/common/extension_set.h" |
| 43 #include "extensions/features/features.h" | 43 #include "extensions/features/features.h" |
| 44 #include "ui/base/layout.h" | 44 #include "ui/base/layout.h" |
| 45 #include "ui/base/resource/resource_bundle.h" | 45 #include "ui/base/resource/resource_bundle.h" |
| 46 #include "ui/gfx/color_palette.h" | 46 #include "ui/gfx/color_palette.h" |
| 47 #include "ui/gfx/image/image_skia.h" | 47 #include "ui/gfx/image/image_skia.h" |
| 48 #include "ui/native_theme/common_theme.h" | 48 #include "ui/native_theme/common_theme.h" |
| 49 #include "ui/native_theme/native_theme.h" | 49 #include "ui/native_theme/native_theme.h" |
| 50 | 50 |
| 51 #if BUILDFLAG(ENABLE_EXTENSIONS) | 51 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 52 #include "base/scoped_observer.h" | |
| 52 #include "extensions/browser/extension_registry_observer.h" | 53 #include "extensions/browser/extension_registry_observer.h" |
| 53 #endif | 54 #endif |
| 54 | 55 |
| 55 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) | 56 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
| 56 #include "chrome/browser/supervised_user/supervised_user_theme.h" | 57 #include "chrome/browser/supervised_user/supervised_user_theme.h" |
| 57 #endif | 58 #endif |
| 58 | 59 |
| 59 using base::UserMetricsAction; | 60 using base::UserMetricsAction; |
| 60 using content::BrowserThread; | 61 using content::BrowserThread; |
| 61 using extensions::Extension; | 62 using extensions::Extension; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 return theme_service_.GetRawData(id, scale_factor); | 154 return theme_service_.GetRawData(id, scale_factor); |
| 154 } | 155 } |
| 155 | 156 |
| 156 | 157 |
| 157 // ThemeService::ThemeObserver ------------------------------------------------ | 158 // ThemeService::ThemeObserver ------------------------------------------------ |
| 158 | 159 |
| 159 #if BUILDFLAG(ENABLE_EXTENSIONS) | 160 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 160 class ThemeService::ThemeObserver | 161 class ThemeService::ThemeObserver |
| 161 : public extensions::ExtensionRegistryObserver { | 162 : public extensions::ExtensionRegistryObserver { |
| 162 public: | 163 public: |
| 163 explicit ThemeObserver(ThemeService* service) : theme_service_(service) { | 164 explicit ThemeObserver(ThemeService* service) |
| 164 extensions::ExtensionRegistry::Get(theme_service_->profile_) | 165 : theme_service_(service), extension_registry_observer_(this) { |
| 165 ->AddObserver(this); | 166 extension_registry_observer_.Add( |
| 167 extensions::ExtensionRegistry::Get(theme_service_->profile_)); | |
| 166 } | 168 } |
| 167 | 169 |
| 168 ~ThemeObserver() override { | 170 ~ThemeObserver() override { |
| 169 extensions::ExtensionRegistry::Get(theme_service_->profile_) | |
| 170 ->RemoveObserver(this); | |
| 171 } | 171 } |
| 172 | 172 |
| 173 private: | 173 private: |
| 174 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, | 174 // extensions::ExtensionRegistryObserver:: |
| 175 const extensions::Extension* extension, | |
| 176 bool is_update, | |
| 177 const std::string& old_name) override { | |
| 178 if (extension->is_theme()) { | |
| 179 // The theme may be initially disabled. Wait till it is loaded (if ever). | |
| 180 theme_service_->installed_pending_load_id_ = extension->id(); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 void OnExtensionLoaded(content::BrowserContext* browser_context, | 175 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 185 const extensions::Extension* extension) override { | 176 const extensions::Extension* extension) override { |
|
pkotwicz
2017/05/25 20:52:25
On startup, OnExtensionLoaded() is not guaranteed
lazyboy
2017/05/30 23:38:35
I've added the exception to bail out if extension-
| |
| 186 if (extension->is_theme() && | 177 // Set the new theme during extension load. |
| 187 theme_service_->installed_pending_load_id_ != kDefaultThemeID && | 178 // Even if the theme was an update of a disabled extension, it shouldn't be |
| 188 theme_service_->installed_pending_load_id_ == extension->id()) { | 179 // loaded. |
| 180 if (extension->is_theme()) | |
| 189 theme_service_->SetTheme(extension); | 181 theme_service_->SetTheme(extension); |
| 190 } | |
| 191 theme_service_->installed_pending_load_id_ = kDefaultThemeID; | |
| 192 } | 182 } |
| 193 | 183 |
| 194 void OnExtensionUnloaded( | 184 void OnExtensionUnloaded( |
| 195 content::BrowserContext* browser_context, | 185 content::BrowserContext* browser_context, |
| 196 const extensions::Extension* extension, | 186 const extensions::Extension* extension, |
| 197 extensions::UnloadedExtensionReason reason) override { | 187 extensions::UnloadedExtensionReason reason) override { |
| 198 if (reason != extensions::UnloadedExtensionReason::UPDATE && | 188 if (reason != extensions::UnloadedExtensionReason::UPDATE && |
| 199 reason != extensions::UnloadedExtensionReason::LOCK_ALL && | 189 reason != extensions::UnloadedExtensionReason::LOCK_ALL && |
| 200 extension->is_theme() && | 190 extension->is_theme() && |
| 201 extension->id() == theme_service_->GetThemeID()) { | 191 extension->id() == theme_service_->GetThemeID()) { |
| 202 theme_service_->UseDefaultTheme(); | 192 theme_service_->UseDefaultTheme(); |
| 203 } | 193 } |
| 204 } | 194 } |
| 205 | 195 |
| 206 ThemeService* theme_service_; | 196 ThemeService* theme_service_; |
| 197 | |
| 198 ScopedObserver<extensions::ExtensionRegistry, | |
| 199 extensions::ExtensionRegistryObserver> | |
| 200 extension_registry_observer_; | |
| 201 | |
| 202 DISALLOW_COPY_AND_ASSIGN(ThemeObserver); | |
| 207 }; | 203 }; |
| 208 #endif // BUILDFLAG(ENABLE_EXTENSIONS) | 204 #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
| 209 | 205 |
| 210 | 206 |
| 211 // ThemeService --------------------------------------------------------------- | 207 // ThemeService --------------------------------------------------------------- |
| 212 | 208 |
| 213 // The default theme if we haven't installed a theme yet or if we've clicked | 209 // The default theme if we haven't installed a theme yet or if we've clicked |
| 214 // the "Use Classic" button. | 210 // the "Use Classic" button. |
| 215 const char ThemeService::kDefaultThemeID[] = ""; | 211 const char ThemeService::kDefaultThemeID[] = ""; |
| 216 | 212 |
| 217 ThemeService::ThemeService() | 213 ThemeService::ThemeService() |
| 218 : ready_(false), | 214 : ready_(false), |
| 219 rb_(ResourceBundle::GetSharedInstance()), | 215 rb_(ResourceBundle::GetSharedInstance()), |
| 220 profile_(nullptr), | 216 profile_(nullptr), |
| 221 installed_pending_load_id_(kDefaultThemeID), | |
| 222 number_of_infobars_(0), | 217 number_of_infobars_(0), |
| 223 original_theme_provider_(*this, false), | 218 original_theme_provider_(*this, false), |
| 224 incognito_theme_provider_(*this, true), | 219 incognito_theme_provider_(*this, true), |
| 225 weak_ptr_factory_(this) {} | 220 weak_ptr_factory_(this) {} |
| 226 | 221 |
| 227 ThemeService::~ThemeService() { | 222 ThemeService::~ThemeService() { |
| 228 FreePlatformCaches(); | 223 FreePlatformCaches(); |
| 229 } | 224 } |
| 230 | 225 |
| 231 void ThemeService::Init(Profile* profile) { | 226 void ThemeService::Init(Profile* profile) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 251 const content::NotificationSource& source, | 246 const content::NotificationSource& source, |
| 252 const content::NotificationDetails& details) { | 247 const content::NotificationDetails& details) { |
| 253 using content::Details; | 248 using content::Details; |
| 254 switch (type) { | 249 switch (type) { |
| 255 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED: | 250 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED: |
| 256 registrar_.Remove(this, | 251 registrar_.Remove(this, |
| 257 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, | 252 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, |
| 258 content::Source<Profile>(profile_)); | 253 content::Source<Profile>(profile_)); |
| 259 OnExtensionServiceReady(); | 254 OnExtensionServiceReady(); |
| 260 break; | 255 break; |
| 261 case extensions::NOTIFICATION_EXTENSION_ENABLED: { | |
| 262 const Extension* extension = Details<const Extension>(details).ptr(); | |
| 263 if (extension->is_theme()) | |
| 264 SetTheme(extension); | |
| 265 break; | |
| 266 } | |
| 267 default: | 256 default: |
| 268 NOTREACHED(); | 257 NOTREACHED(); |
| 269 } | 258 } |
| 270 } | 259 } |
| 271 | 260 |
| 272 void ThemeService::SetTheme(const Extension* extension) { | 261 void ThemeService::SetTheme(const Extension* extension) { |
| 273 DCHECK(extension->is_theme()); | 262 DCHECK(extension->is_theme()); |
| 274 ExtensionService* service = | 263 ExtensionService* service = |
| 275 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 264 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 276 if (!service->IsExtensionEnabled(extension->id())) { | 265 if (!service->IsExtensionEnabled(extension->id())) { |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 784 // be recreated from the extension. | 773 // be recreated from the extension. |
| 785 MigrateTheme(); | 774 MigrateTheme(); |
| 786 set_ready(); | 775 set_ready(); |
| 787 | 776 |
| 788 // Send notification in case anyone requested data and cached it when the | 777 // Send notification in case anyone requested data and cached it when the |
| 789 // theme service was not ready yet. | 778 // theme service was not ready yet. |
| 790 NotifyThemeChanged(); | 779 NotifyThemeChanged(); |
| 791 } | 780 } |
| 792 | 781 |
| 793 #if BUILDFLAG(ENABLE_EXTENSIONS) | 782 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 794 theme_observer_.reset(new ThemeObserver(this)); | 783 theme_observer_ = base::MakeUnique<ThemeObserver>(this); |
| 795 #endif | 784 #endif |
| 796 | 785 |
| 797 registrar_.Add(this, | |
| 798 extensions::NOTIFICATION_EXTENSION_ENABLED, | |
| 799 content::Source<Profile>(profile_)); | |
| 800 | |
| 801 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 786 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 802 FROM_HERE, base::Bind(&ThemeService::RemoveUnusedThemes, | 787 FROM_HERE, base::Bind(&ThemeService::RemoveUnusedThemes, |
| 803 weak_ptr_factory_.GetWeakPtr(), false), | 788 weak_ptr_factory_.GetWeakPtr(), false), |
| 804 base::TimeDelta::FromSeconds(kRemoveUnusedThemesStartupDelay)); | 789 base::TimeDelta::FromSeconds(kRemoveUnusedThemesStartupDelay)); |
| 805 } | 790 } |
| 806 | 791 |
| 807 void ThemeService::MigrateTheme() { | 792 void ThemeService::MigrateTheme() { |
| 808 // TODO(erg): We need to pop up a dialog informing the user that their | 793 // TODO(erg): We need to pop up a dialog informing the user that their |
| 809 // theme is being migrated. | 794 // theme is being migrated. |
| 810 ExtensionService* service = | 795 ExtensionService* service = |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 868 | 853 |
| 869 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) | 854 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
| 870 bool ThemeService::IsSupervisedUser() const { | 855 bool ThemeService::IsSupervisedUser() const { |
| 871 return profile_->IsSupervised(); | 856 return profile_->IsSupervised(); |
| 872 } | 857 } |
| 873 | 858 |
| 874 void ThemeService::SetSupervisedUserTheme() { | 859 void ThemeService::SetSupervisedUserTheme() { |
| 875 SetCustomDefaultTheme(new SupervisedUserTheme); | 860 SetCustomDefaultTheme(new SupervisedUserTheme); |
| 876 } | 861 } |
| 877 #endif | 862 #endif |
| OLD | NEW |