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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 if (!pack->WriteToDisk(path)) | 72 if (!pack->WriteToDisk(path)) |
73 NOTREACHED() << "Could not write theme pack to disk"; | 73 NOTREACHED() << "Could not write theme pack to disk"; |
74 } | 74 } |
75 | 75 |
76 } // namespace | 76 } // namespace |
77 | 77 |
78 ThemeService::ThemeService() | 78 ThemeService::ThemeService() |
79 : rb_(ResourceBundle::GetSharedInstance()), | 79 : rb_(ResourceBundle::GetSharedInstance()), |
80 profile_(NULL), | 80 profile_(NULL), |
81 ready_(false), | 81 ready_(false), |
82 number_of_infobars_(0) { | 82 number_of_infobars_(0), |
| 83 weak_ptr_factory_(this) { |
83 } | 84 } |
84 | 85 |
85 ThemeService::~ThemeService() { | 86 ThemeService::~ThemeService() { |
86 FreePlatformCaches(); | 87 FreePlatformCaches(); |
87 } | 88 } |
88 | 89 |
89 void ThemeService::Init(Profile* profile) { | 90 void ThemeService::Init(Profile* profile) { |
90 DCHECK(CalledOnValidThread()); | 91 DCHECK(CalledOnValidThread()); |
91 profile_ = profile; | 92 profile_ = profile; |
92 | 93 |
93 LoadThemePrefs(); | 94 LoadThemePrefs(); |
94 | 95 |
95 if (!ready_) { | |
96 registrar_.Add(this, | |
97 chrome::NOTIFICATION_EXTENSIONS_READY, | |
98 content::Source<Profile>(profile_)); | |
99 } | |
100 | |
101 theme_syncable_service_.reset(new ThemeSyncableService(profile_, this)); | 96 theme_syncable_service_.reset(new ThemeSyncableService(profile_, this)); |
102 } | 97 } |
103 | 98 |
104 gfx::Image ThemeService::GetImageNamed(int id) const { | 99 gfx::Image ThemeService::GetImageNamed(int id) const { |
105 DCHECK(CalledOnValidThread()); | 100 DCHECK(CalledOnValidThread()); |
106 | 101 |
107 gfx::Image image; | 102 gfx::Image image; |
108 if (theme_pack_.get()) | 103 if (theme_pack_.get()) |
109 image = theme_pack_->GetImageNamed(id); | 104 image = theme_pack_->GetImageNamed(id); |
110 | 105 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 | 193 |
199 base::RefCountedMemory* data = NULL; | 194 base::RefCountedMemory* data = NULL; |
200 if (theme_pack_.get()) | 195 if (theme_pack_.get()) |
201 data = theme_pack_->GetRawData(id, scale_factor); | 196 data = theme_pack_->GetRawData(id, scale_factor); |
202 if (!data) | 197 if (!data) |
203 data = rb_.LoadDataResourceBytesForScale(id, ui::SCALE_FACTOR_100P); | 198 data = rb_.LoadDataResourceBytesForScale(id, ui::SCALE_FACTOR_100P); |
204 | 199 |
205 return data; | 200 return data; |
206 } | 201 } |
207 | 202 |
208 void ThemeService::Observe(int type, | |
209 const content::NotificationSource& source, | |
210 const content::NotificationDetails& details) { | |
211 DCHECK(type == chrome::NOTIFICATION_EXTENSIONS_READY); | |
212 registrar_.Remove(this, chrome::NOTIFICATION_EXTENSIONS_READY, | |
213 content::Source<Profile>(profile_)); | |
214 | |
215 MigrateTheme(); | |
216 set_ready(); | |
217 | |
218 // Send notification in case anyone requested data and cached it when the | |
219 // theme service was not ready yet. | |
220 NotifyThemeChanged(); | |
221 } | |
222 | |
223 void ThemeService::SetTheme(const Extension* extension) { | 203 void ThemeService::SetTheme(const Extension* extension) { |
224 // Clear our image cache. | 204 // Clear our image cache. |
225 FreePlatformCaches(); | 205 FreePlatformCaches(); |
226 | 206 |
227 DCHECK(extension); | 207 DCHECK(extension); |
228 DCHECK(extension->is_theme()); | 208 DCHECK(extension->is_theme()); |
229 if (DCHECK_IS_ON()) { | 209 if (DCHECK_IS_ON()) { |
230 ExtensionService* service = | 210 ExtensionService* service = |
231 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 211 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
232 DCHECK(service); | 212 DCHECK(service); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 theme_pack_ = BrowserThemePack::BuildFromDataPack(path, current_id); | 301 theme_pack_ = BrowserThemePack::BuildFromDataPack(path, current_id); |
322 loaded_pack = theme_pack_.get() != NULL; | 302 loaded_pack = theme_pack_.get() != NULL; |
323 } | 303 } |
324 | 304 |
325 if (loaded_pack) { | 305 if (loaded_pack) { |
326 content::RecordAction(UserMetricsAction("Themes.Loaded")); | 306 content::RecordAction(UserMetricsAction("Themes.Loaded")); |
327 set_ready(); | 307 set_ready(); |
328 } else { | 308 } else { |
329 // TODO(erg): We need to pop up a dialog informing the user that their | 309 // TODO(erg): We need to pop up a dialog informing the user that their |
330 // theme is being migrated. | 310 // theme is being migrated. |
331 ExtensionService* service = | 311 extensions::ExtensionSystem::Get(profile_)->ready() |
332 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 312 .Post(FROM_HERE, |
333 if (service && service->is_ready()) { | 313 base::Bind(&ThemeService::FinishLoadingAfterExtensionsReady, |
334 MigrateTheme(); | 314 weak_ptr_factory_.GetWeakPtr())); |
335 set_ready(); | |
336 } | |
337 } | 315 } |
338 } | 316 } |
339 | 317 |
340 void ThemeService::NotifyThemeChanged() { | 318 void ThemeService::NotifyThemeChanged() { |
341 DVLOG(1) << "Sending BROWSER_THEME_CHANGED"; | 319 DVLOG(1) << "Sending BROWSER_THEME_CHANGED"; |
342 // Redraw! | 320 // Redraw! |
343 content::NotificationService* service = | 321 content::NotificationService* service = |
344 content::NotificationService::current(); | 322 content::NotificationService::current(); |
345 service->Notify(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 323 service->Notify(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
346 content::Source<ThemeService>(this), | 324 content::Source<ThemeService>(this), |
347 content::NotificationService::NoDetails()); | 325 content::NotificationService::NoDetails()); |
348 #if defined(OS_MACOSX) | 326 #if defined(OS_MACOSX) |
349 NotifyPlatformThemeChanged(); | 327 NotifyPlatformThemeChanged(); |
350 #endif // OS_MACOSX | 328 #endif // OS_MACOSX |
351 | 329 |
352 // Notify sync that theme has changed. | 330 // Notify sync that theme has changed. |
353 if (theme_syncable_service_.get()) { | 331 if (theme_syncable_service_.get()) { |
354 theme_syncable_service_->OnThemeChange(); | 332 theme_syncable_service_->OnThemeChange(); |
355 } | 333 } |
356 } | 334 } |
357 | 335 |
358 #if defined(OS_WIN) || defined(USE_AURA) | 336 #if defined(OS_WIN) || defined(USE_AURA) |
359 void ThemeService::FreePlatformCaches() { | 337 void ThemeService::FreePlatformCaches() { |
360 // Views (Skia) has no platform image cache to clear. | 338 // Views (Skia) has no platform image cache to clear. |
361 } | 339 } |
362 #endif | 340 #endif |
363 | 341 |
| 342 void ThemeService::FinishLoadingAfterExtensionsReady() { |
| 343 MigrateTheme(); |
| 344 set_ready(); |
| 345 |
| 346 // Send notification in case anyone requested data and cached it when the |
| 347 // theme service was not ready yet. |
| 348 NotifyThemeChanged(); |
| 349 } |
| 350 |
364 void ThemeService::MigrateTheme() { | 351 void ThemeService::MigrateTheme() { |
365 ExtensionService* service = | 352 ExtensionService* service = |
366 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 353 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
367 const Extension* extension = service ? | 354 const Extension* extension = service ? |
368 service->GetExtensionById(GetThemeID(), false) : NULL; | 355 service->GetExtensionById(GetThemeID(), false) : NULL; |
369 if (extension) { | 356 if (extension) { |
370 DLOG(ERROR) << "Migrating theme"; | 357 DLOG(ERROR) << "Migrating theme"; |
371 BuildFromExtension(extension); | 358 BuildFromExtension(extension); |
372 content::RecordAction(UserMetricsAction("Themes.Migrated")); | 359 content::RecordAction(UserMetricsAction("Themes.Migrated")); |
373 } else { | 360 } else { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 void ThemeService::OnInfobarDestroyed() { | 406 void ThemeService::OnInfobarDestroyed() { |
420 number_of_infobars_--; | 407 number_of_infobars_--; |
421 | 408 |
422 if (number_of_infobars_ == 0) | 409 if (number_of_infobars_ == 0) |
423 RemoveUnusedThemes(); | 410 RemoveUnusedThemes(); |
424 } | 411 } |
425 | 412 |
426 ThemeSyncableService* ThemeService::GetThemeSyncableService() const { | 413 ThemeSyncableService* ThemeService::GetThemeSyncableService() const { |
427 return theme_syncable_service_.get(); | 414 return theme_syncable_service_.get(); |
428 } | 415 } |
OLD | NEW |