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 { |
| 186 if (extension->is_theme() && | 177 if (!extension->is_theme()) |
| 187 theme_service_->installed_pending_load_id_ != kDefaultThemeID && | 178 return; |
| 188 theme_service_->installed_pending_load_id_ == extension->id()) { | 179 |
| 189 theme_service_->SetTheme(extension); | 180 // If NOTIFICATION_EXTENSIONS_READY_DEPRECATED already set this theme, bail |
| 190 } | 181 // out. |
|
pkotwicz
2017/05/31 15:46:53
Remove the comment because it is incorrect. ThemeS
lazyboy
2017/05/31 21:07:41
Done.
Also, as discussed offline, I have revived
| |
| 191 theme_service_->installed_pending_load_id_ = kDefaultThemeID; | 182 if (extension->id() == theme_service_->GetThemeID()) |
| 183 return; | |
| 184 | |
| 185 // Set the new theme during extension load. | |
| 186 // Even if the theme was an update of a disabled extension, it shouldn't be | |
| 187 // loaded. | |
| 188 theme_service_->SetTheme(extension); | |
| 192 } | 189 } |
| 193 | 190 |
| 194 void OnExtensionUnloaded( | 191 void OnExtensionUnloaded( |
| 195 content::BrowserContext* browser_context, | 192 content::BrowserContext* browser_context, |
| 196 const extensions::Extension* extension, | 193 const extensions::Extension* extension, |
| 197 extensions::UnloadedExtensionReason reason) override { | 194 extensions::UnloadedExtensionReason reason) override { |
| 198 if (reason != extensions::UnloadedExtensionReason::UPDATE && | 195 if (reason != extensions::UnloadedExtensionReason::UPDATE && |
| 199 reason != extensions::UnloadedExtensionReason::LOCK_ALL && | 196 reason != extensions::UnloadedExtensionReason::LOCK_ALL && |
| 200 extension->is_theme() && | 197 extension->is_theme() && |
| 201 extension->id() == theme_service_->GetThemeID()) { | 198 extension->id() == theme_service_->GetThemeID()) { |
| 202 theme_service_->UseDefaultTheme(); | 199 theme_service_->UseDefaultTheme(); |
| 203 } | 200 } |
| 204 } | 201 } |
| 205 | 202 |
| 206 ThemeService* theme_service_; | 203 ThemeService* theme_service_; |
| 204 | |
| 205 ScopedObserver<extensions::ExtensionRegistry, | |
| 206 extensions::ExtensionRegistryObserver> | |
| 207 extension_registry_observer_; | |
| 208 | |
| 209 DISALLOW_COPY_AND_ASSIGN(ThemeObserver); | |
| 207 }; | 210 }; |
| 208 #endif // BUILDFLAG(ENABLE_EXTENSIONS) | 211 #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
| 209 | 212 |
| 210 | 213 |
| 211 // ThemeService --------------------------------------------------------------- | 214 // ThemeService --------------------------------------------------------------- |
| 212 | 215 |
| 213 // The default theme if we haven't installed a theme yet or if we've clicked | 216 // The default theme if we haven't installed a theme yet or if we've clicked |
| 214 // the "Use Classic" button. | 217 // the "Use Classic" button. |
| 215 const char ThemeService::kDefaultThemeID[] = ""; | 218 const char ThemeService::kDefaultThemeID[] = ""; |
| 216 | 219 |
| 217 ThemeService::ThemeService() | 220 ThemeService::ThemeService() |
| 218 : ready_(false), | 221 : ready_(false), |
| 219 rb_(ResourceBundle::GetSharedInstance()), | 222 rb_(ResourceBundle::GetSharedInstance()), |
| 220 profile_(nullptr), | 223 profile_(nullptr), |
| 221 installed_pending_load_id_(kDefaultThemeID), | |
| 222 number_of_infobars_(0), | 224 number_of_infobars_(0), |
| 223 original_theme_provider_(*this, false), | 225 original_theme_provider_(*this, false), |
| 224 incognito_theme_provider_(*this, true), | 226 incognito_theme_provider_(*this, true), |
| 225 weak_ptr_factory_(this) {} | 227 weak_ptr_factory_(this) {} |
| 226 | 228 |
| 227 ThemeService::~ThemeService() { | 229 ThemeService::~ThemeService() { |
| 228 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 230 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 229 FreePlatformCaches(); | 231 FreePlatformCaches(); |
| 230 } | 232 } |
| 231 | 233 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 252 const content::NotificationSource& source, | 254 const content::NotificationSource& source, |
| 253 const content::NotificationDetails& details) { | 255 const content::NotificationDetails& details) { |
| 254 using content::Details; | 256 using content::Details; |
| 255 switch (type) { | 257 switch (type) { |
| 256 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED: | 258 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED: |
| 257 registrar_.Remove(this, | 259 registrar_.Remove(this, |
| 258 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, | 260 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, |
| 259 content::Source<Profile>(profile_)); | 261 content::Source<Profile>(profile_)); |
| 260 OnExtensionServiceReady(); | 262 OnExtensionServiceReady(); |
| 261 break; | 263 break; |
| 262 case extensions::NOTIFICATION_EXTENSION_ENABLED: { | |
| 263 const Extension* extension = Details<const Extension>(details).ptr(); | |
| 264 if (extension->is_theme()) | |
| 265 SetTheme(extension); | |
| 266 break; | |
| 267 } | |
| 268 default: | 264 default: |
| 269 NOTREACHED(); | 265 NOTREACHED(); |
| 270 } | 266 } |
| 271 } | 267 } |
| 272 | 268 |
| 273 void ThemeService::SetTheme(const Extension* extension) { | 269 void ThemeService::SetTheme(const Extension* extension) { |
| 274 DCHECK(extension->is_theme()); | 270 DCHECK(extension->is_theme()); |
| 275 ExtensionService* service = | 271 ExtensionService* service = |
| 276 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 272 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 277 if (!service->IsExtensionEnabled(extension->id())) { | 273 if (!service->IsExtensionEnabled(extension->id())) { |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 785 // be recreated from the extension. | 781 // be recreated from the extension. |
| 786 MigrateTheme(); | 782 MigrateTheme(); |
| 787 set_ready(); | 783 set_ready(); |
| 788 | 784 |
| 789 // Send notification in case anyone requested data and cached it when the | 785 // Send notification in case anyone requested data and cached it when the |
| 790 // theme service was not ready yet. | 786 // theme service was not ready yet. |
| 791 NotifyThemeChanged(); | 787 NotifyThemeChanged(); |
| 792 } | 788 } |
| 793 | 789 |
| 794 #if BUILDFLAG(ENABLE_EXTENSIONS) | 790 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 795 theme_observer_.reset(new ThemeObserver(this)); | 791 theme_observer_ = base::MakeUnique<ThemeObserver>(this); |
| 796 #endif | 792 #endif |
| 797 | 793 |
| 798 registrar_.Add(this, | |
| 799 extensions::NOTIFICATION_EXTENSION_ENABLED, | |
| 800 content::Source<Profile>(profile_)); | |
| 801 | |
| 802 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 794 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 803 FROM_HERE, base::Bind(&ThemeService::RemoveUnusedThemes, | 795 FROM_HERE, base::Bind(&ThemeService::RemoveUnusedThemes, |
| 804 weak_ptr_factory_.GetWeakPtr(), false), | 796 weak_ptr_factory_.GetWeakPtr(), false), |
| 805 base::TimeDelta::FromSeconds(kRemoveUnusedThemesStartupDelay)); | 797 base::TimeDelta::FromSeconds(kRemoveUnusedThemesStartupDelay)); |
| 806 } | 798 } |
| 807 | 799 |
| 808 void ThemeService::MigrateTheme() { | 800 void ThemeService::MigrateTheme() { |
| 809 // TODO(erg): We need to pop up a dialog informing the user that their | 801 // TODO(erg): We need to pop up a dialog informing the user that their |
| 810 // theme is being migrated. | 802 // theme is being migrated. |
| 811 ExtensionService* service = | 803 ExtensionService* service = |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 869 | 861 |
| 870 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) | 862 #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
| 871 bool ThemeService::IsSupervisedUser() const { | 863 bool ThemeService::IsSupervisedUser() const { |
| 872 return profile_->IsSupervised(); | 864 return profile_->IsSupervised(); |
| 873 } | 865 } |
| 874 | 866 |
| 875 void ThemeService::SetSupervisedUserTheme() { | 867 void ThemeService::SetSupervisedUserTheme() { |
| 876 SetCustomDefaultTheme(new SupervisedUserTheme); | 868 SetCustomDefaultTheme(new SupervisedUserTheme); |
| 877 } | 869 } |
| 878 #endif | 870 #endif |
| OLD | NEW |