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

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

Issue 965233002: Remove deprecated extension notification from theme_service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: guard ENABLE_EXTENSIONS Created 5 years, 9 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
OLDNEW
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 <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/ref_counted_memory.h" 10 #include "base/memory/ref_counted_memory.h"
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 const int kChannelTolerance = 9; 89 const int kChannelTolerance = 9;
90 int r = SkColorGetR(color); 90 int r = SkColorGetR(color);
91 int g = SkColorGetG(color); 91 int g = SkColorGetG(color);
92 int b = SkColorGetB(color); 92 int b = SkColorGetB(color);
93 int range = std::max(r, std::max(g, b)) - std::min(r, std::min(g, b)); 93 int range = std::max(r, std::max(g, b)) - std::min(r, std::min(g, b));
94 return range < kChannelTolerance; 94 return range < kChannelTolerance;
95 } 95 }
96 96
97 } // namespace 97 } // namespace
98 98
99 #if defined(ENABLE_EXTENSIONS)
100 class ThemeService::ExtensionObserver
101 : public extensions::ExtensionRegistryObserver {
102 public:
103 explicit ExtensionObserver(ThemeService& service) : theme_service_(service) {}
104 ~ExtensionObserver() override {}
105 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
106 const extensions::Extension* extension,
107 bool is_update,
108 bool from_ephemeral,
109 const std::string& old_name) override {
110 if (extension->is_theme()) {
111 // The theme may be initially disabled. Wait till it is loaded (if ever).
112 theme_service_.installed_pending_load_id_ = extension->id();
113 }
114 }
115 void OnExtensionLoaded(content::BrowserContext* browser_context,
116 const extensions::Extension* extension) override {
117 if (extension->is_theme() &&
118 theme_service_.installed_pending_load_id_ != kDefaultThemeID &&
119 theme_service_.installed_pending_load_id_ == extension->id()) {
120 theme_service_.SetTheme(extension);
121 }
122 theme_service_.installed_pending_load_id_ = kDefaultThemeID;
123 }
124 void OnExtensionUnloaded(
125 content::BrowserContext* browser_context,
126 const extensions::Extension* extension,
127 extensions::UnloadedExtensionInfo::Reason reason) override {
128 if (reason != extensions::UnloadedExtensionInfo::REASON_UPDATE &&
129 reason != extensions::UnloadedExtensionInfo::REASON_LOCK_ALL &&
130 extension->is_theme() &&
131 extension->id() == theme_service_.GetThemeID()) {
132 theme_service_.UseDefaultTheme();
133 }
134 }
135
136 private:
137 ThemeService& theme_service_;
not at google - send to devlin 2015/03/12 17:08:33 nit: usually you would hold onto a pointer, not a
limasdf 2015/03/13 03:01:54 Done.
138 };
139 #endif // defined(ENABLE_EXTENSIONS)
140
99 ThemeService::ThemeService() 141 ThemeService::ThemeService()
100 : ready_(false), 142 : ready_(false),
101 rb_(ResourceBundle::GetSharedInstance()), 143 rb_(ResourceBundle::GetSharedInstance()),
102 profile_(NULL), 144 profile_(NULL),
103 installed_pending_load_id_(kDefaultThemeID), 145 installed_pending_load_id_(kDefaultThemeID),
104 number_of_infobars_(0), 146 number_of_infobars_(0),
147 #if defined(ENABLE_EXTENSIONS)
148 extension_registry_(NULL),
not at google - send to devlin 2015/03/12 17:08:33 nullptr
limasdf 2015/03/13 03:01:54 Done. including other place as well for the consis
149 #endif
105 weak_ptr_factory_(this) { 150 weak_ptr_factory_(this) {
106 } 151 }
107 152
108 ThemeService::~ThemeService() { 153 ThemeService::~ThemeService() {
109 FreePlatformCaches(); 154 FreePlatformCaches();
110 } 155 }
111 156
112 void ThemeService::Init(Profile* profile) { 157 void ThemeService::Init(Profile* profile) {
113 DCHECK(CalledOnValidThread()); 158 DCHECK(CalledOnValidThread());
114 profile_ = profile; 159 profile_ = profile;
115 160
116 LoadThemePrefs(); 161 LoadThemePrefs();
162 #if defined(ENABLE_EXTENSIONS)
163 extension_registry_ = extensions::ExtensionRegistry::Get(profile_);
164 #endif
117 165
118 registrar_.Add(this, 166 registrar_.Add(this,
119 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, 167 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
120 content::Source<Profile>(profile_)); 168 content::Source<Profile>(profile_));
121 169
122 theme_syncable_service_.reset(new ThemeSyncableService(profile_, this)); 170 theme_syncable_service_.reset(new ThemeSyncableService(profile_, this));
123 } 171 }
124 172
125 gfx::Image ThemeService::GetImageNamed(int id) const { 173 gfx::Image ThemeService::GetImageNamed(int id) const {
126 DCHECK(CalledOnValidThread()); 174 DCHECK(CalledOnValidThread());
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 298
251 base::RefCountedMemory* data = NULL; 299 base::RefCountedMemory* data = NULL;
252 if (theme_supplier_.get()) 300 if (theme_supplier_.get())
253 data = theme_supplier_->GetRawData(id, scale_factor); 301 data = theme_supplier_->GetRawData(id, scale_factor);
254 if (!data) 302 if (!data)
255 data = rb_.LoadDataResourceBytesForScale(id, ui::SCALE_FACTOR_100P); 303 data = rb_.LoadDataResourceBytesForScale(id, ui::SCALE_FACTOR_100P);
256 304
257 return data; 305 return data;
258 } 306 }
259 307
308 void ThemeService::Shutdown() {
309 #if defined(ENABLE_EXTENSIONS)
310 extension_registry_->RemoveObserver(extension_observer_.get());
not at google - send to devlin 2015/03/12 17:08:33 (see below - you might as well destroy the observe
limasdf 2015/03/13 03:01:54 Done.
311 #endif
312 }
313
260 void ThemeService::Observe(int type, 314 void ThemeService::Observe(int type,
261 const content::NotificationSource& source, 315 const content::NotificationSource& source,
262 const content::NotificationDetails& details) { 316 const content::NotificationDetails& details) {
263 using content::Details; 317 using content::Details;
264 switch (type) { 318 switch (type) {
265 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED: 319 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED:
266 registrar_.Remove(this, 320 registrar_.Remove(this,
267 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, 321 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
268 content::Source<Profile>(profile_)); 322 content::Source<Profile>(profile_));
269 OnExtensionServiceReady(); 323 OnExtensionServiceReady();
270 break; 324 break;
271 case extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED: {
272 // The theme may be initially disabled. Wait till it is loaded (if ever).
273 Details<const extensions::InstalledExtensionInfo> installed_details(
274 details);
275 if (installed_details->extension->is_theme())
276 installed_pending_load_id_ = installed_details->extension->id();
277 break;
278 }
279 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: {
280 const Extension* extension = Details<const Extension>(details).ptr();
281 if (extension->is_theme() &&
282 installed_pending_load_id_ != kDefaultThemeID &&
283 installed_pending_load_id_ == extension->id()) {
284 SetTheme(extension);
285 }
286 installed_pending_load_id_ = kDefaultThemeID;
287 break;
288 }
289 case extensions::NOTIFICATION_EXTENSION_ENABLED: { 325 case extensions::NOTIFICATION_EXTENSION_ENABLED: {
290 const Extension* extension = Details<const Extension>(details).ptr(); 326 const Extension* extension = Details<const Extension>(details).ptr();
291 if (extension->is_theme()) 327 if (extension->is_theme())
292 SetTheme(extension); 328 SetTheme(extension);
293 break; 329 break;
294 } 330 }
295 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { 331 default:
296 Details<const UnloadedExtensionInfo> unloaded_details(details); 332 NOTREACHED();
297 if (unloaded_details->reason != UnloadedExtensionInfo::REASON_UPDATE &&
298 unloaded_details->reason != UnloadedExtensionInfo::REASON_LOCK_ALL &&
299 unloaded_details->extension->is_theme() &&
300 unloaded_details->extension->id() == GetThemeID()) {
301 UseDefaultTheme();
302 }
303 break;
304 }
305 } 333 }
306 } 334 }
307 335
308 void ThemeService::SetTheme(const Extension* extension) { 336 void ThemeService::SetTheme(const Extension* extension) {
309 DCHECK(extension->is_theme()); 337 DCHECK(extension->is_theme());
310 ExtensionService* service = 338 ExtensionService* service =
311 extensions::ExtensionSystem::Get(profile_)->extension_service(); 339 extensions::ExtensionSystem::Get(profile_)->extension_service();
312 if (!service->IsExtensionEnabled(extension->id())) { 340 if (!service->IsExtensionEnabled(extension->id())) {
313 // |extension| is disabled when reverting to the previous theme via an 341 // |extension| is disabled when reverting to the previous theme via an
314 // infobar. 342 // infobar.
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 // If the ThemeService is not ready yet, the custom theme data pack needs to 550 // If the ThemeService is not ready yet, the custom theme data pack needs to
523 // be recreated from the extension. 551 // be recreated from the extension.
524 MigrateTheme(); 552 MigrateTheme();
525 set_ready(); 553 set_ready();
526 554
527 // Send notification in case anyone requested data and cached it when the 555 // Send notification in case anyone requested data and cached it when the
528 // theme service was not ready yet. 556 // theme service was not ready yet.
529 NotifyThemeChanged(); 557 NotifyThemeChanged();
530 } 558 }
531 559
532 registrar_.Add( 560 #if defined(ENABLE_EXTENSIONS)
533 this, 561 extension_observer_.reset(new ExtensionObserver(*this));
534 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED, 562 extension_registry_->AddObserver(extension_observer_.get());
not at google - send to devlin 2015/03/12 17:08:33 If you make ExtensionObserver add/remove itself as
limasdf 2015/03/13 03:01:54 Done. Yes. Exactly.
535 content::Source<Profile>(profile_)); 563 #endif
536 registrar_.Add(this, 564
537 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
538 content::Source<Profile>(profile_));
539 registrar_.Add(this, 565 registrar_.Add(this,
540 extensions::NOTIFICATION_EXTENSION_ENABLED, 566 extensions::NOTIFICATION_EXTENSION_ENABLED,
541 content::Source<Profile>(profile_)); 567 content::Source<Profile>(profile_));
542 registrar_.Add(this,
543 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
544 content::Source<Profile>(profile_));
545 568
546 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, 569 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
547 base::Bind(&ThemeService::RemoveUnusedThemes, 570 base::Bind(&ThemeService::RemoveUnusedThemes,
548 weak_ptr_factory_.GetWeakPtr(), 571 weak_ptr_factory_.GetWeakPtr(),
549 false), 572 false),
550 base::TimeDelta::FromSeconds(kRemoveUnusedThemesStartupDelay)); 573 base::TimeDelta::FromSeconds(kRemoveUnusedThemesStartupDelay));
551 } 574 }
552 575
553 void ThemeService::MigrateTheme() { 576 void ThemeService::MigrateTheme() {
554 // TODO(erg): We need to pop up a dialog informing the user that their 577 // TODO(erg): We need to pop up a dialog informing the user that their
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 void ThemeService::OnInfobarDestroyed() { 652 void ThemeService::OnInfobarDestroyed() {
630 number_of_infobars_--; 653 number_of_infobars_--;
631 654
632 if (number_of_infobars_ == 0) 655 if (number_of_infobars_ == 0)
633 RemoveUnusedThemes(false); 656 RemoveUnusedThemes(false);
634 } 657 }
635 658
636 ThemeSyncableService* ThemeService::GetThemeSyncableService() const { 659 ThemeSyncableService* ThemeService::GetThemeSyncableService() const {
637 return theme_syncable_service_.get(); 660 return theme_syncable_service_.get();
638 } 661 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698