| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file_util.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 20 #include "chrome/browser/chrome_notification_types.h" | 21 #include "chrome/browser/chrome_notification_types.h" |
| 21 #include "chrome/browser/extensions/extension_service.h" | 22 #include "chrome/browser/extensions/extension_service.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 32 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
| 33 #include "content/public/browser/notification_service.h" | 34 #include "content/public/browser/notification_service.h" |
| 34 #include "content/public/browser/user_metrics.h" | 35 #include "content/public/browser/user_metrics.h" |
| 35 #include "extensions/browser/extension_prefs.h" | 36 #include "extensions/browser/extension_prefs.h" |
| 36 #include "extensions/browser/extension_registry.h" | 37 #include "extensions/browser/extension_registry.h" |
| 37 #include "extensions/browser/extension_system.h" | 38 #include "extensions/browser/extension_system.h" |
| 38 #include "extensions/browser/uninstall_reason.h" | 39 #include "extensions/browser/uninstall_reason.h" |
| 39 #include "extensions/common/extension.h" | 40 #include "extensions/common/extension.h" |
| 40 #include "extensions/common/extension_set.h" | 41 #include "extensions/common/extension_set.h" |
| 41 #include "ui/base/layout.h" | 42 #include "ui/base/layout.h" |
| 42 #include "ui/base/material_design/material_design_controller.h" | |
| 43 #include "ui/base/resource/resource_bundle.h" | 43 #include "ui/base/resource/resource_bundle.h" |
| 44 #include "ui/gfx/color_palette.h" | 44 #include "ui/gfx/color_palette.h" |
| 45 #include "ui/gfx/image/image_skia.h" | 45 #include "ui/gfx/image/image_skia.h" |
| 46 #include "ui/native_theme/common_theme.h" | 46 #include "ui/native_theme/common_theme.h" |
| 47 #include "ui/native_theme/native_theme.h" | 47 #include "ui/native_theme/native_theme.h" |
| 48 | 48 |
| 49 #if defined(ENABLE_EXTENSIONS) | 49 #if defined(ENABLE_EXTENSIONS) |
| 50 #include "extensions/browser/extension_registry_observer.h" | 50 #include "extensions/browser/extension_registry_observer.h" |
| 51 #endif | 51 #endif |
| 52 | 52 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 79 | 79 |
| 80 SkColor IncreaseLightness(SkColor color, double percent) { | 80 SkColor IncreaseLightness(SkColor color, double percent) { |
| 81 color_utils::HSL result; | 81 color_utils::HSL result; |
| 82 color_utils::SkColorToHSL(color, &result); | 82 color_utils::SkColorToHSL(color, &result); |
| 83 result.l += (1 - result.l) * percent; | 83 result.l += (1 - result.l) * percent; |
| 84 return color_utils::HSLToSkColor(result, SkColorGetA(color)); | 84 return color_utils::HSLToSkColor(result, SkColorGetA(color)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Writes the theme pack to disk on a separate thread. | 87 // Writes the theme pack to disk on a separate thread. |
| 88 void WritePackToDiskCallback(BrowserThemePack* pack, | 88 void WritePackToDiskCallback(BrowserThemePack* pack, |
| 89 const base::FilePath& path) { | 89 const base::FilePath& directory) { |
| 90 base::FilePath path = directory.Append(chrome::kThemePackFilename); |
| 90 if (!pack->WriteToDisk(path)) | 91 if (!pack->WriteToDisk(path)) |
| 91 NOTREACHED() << "Could not write theme pack to disk"; | 92 NOTREACHED() << "Could not write theme pack to disk"; |
| 93 |
| 94 // Clean up any theme .pak that was generated during the Material Design |
| 95 // transitional period. |
| 96 // TODO(estade): remove this line in Q2 2017. |
| 97 base::DeleteFile(directory.Append("Cached Theme Material Design.pak"), false); |
| 92 } | 98 } |
| 93 | 99 |
| 94 // Heuristic to determine if color is grayscale. This is used to decide whether | 100 // Heuristic to determine if color is grayscale. This is used to decide whether |
| 95 // to use the colorful or white logo, if a theme fails to specify which. | 101 // to use the colorful or white logo, if a theme fails to specify which. |
| 96 bool IsColorGrayscale(SkColor color) { | 102 bool IsColorGrayscale(SkColor color) { |
| 97 const int kChannelTolerance = 9; | 103 const int kChannelTolerance = 9; |
| 98 int r = SkColorGetR(color); | 104 int r = SkColorGetR(color); |
| 99 int g = SkColorGetG(color); | 105 int g = SkColorGetG(color); |
| 100 int b = SkColorGetB(color); | 106 int b = SkColorGetB(color); |
| 101 int range = std::max(r, std::max(g, b)) - std::min(r, std::min(g, b)); | 107 int range = std::max(r, std::max(g, b)) - std::min(r, std::min(g, b)); |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 if (ShouldInitWithSystemTheme()) | 572 if (ShouldInitWithSystemTheme()) |
| 567 UseSystemTheme(); | 573 UseSystemTheme(); |
| 568 else | 574 else |
| 569 UseDefaultTheme(); | 575 UseDefaultTheme(); |
| 570 set_ready(); | 576 set_ready(); |
| 571 return; | 577 return; |
| 572 } | 578 } |
| 573 | 579 |
| 574 bool loaded_pack = false; | 580 bool loaded_pack = false; |
| 575 | 581 |
| 576 // If we don't have a file pack, we're updating from an old version, or the | 582 // If we don't have a file pack, we're updating from an old version. |
| 577 // pack was created for an alternative MaterialDesignController::Mode. | |
| 578 base::FilePath path = prefs->GetFilePath(prefs::kCurrentThemePackFilename); | 583 base::FilePath path = prefs->GetFilePath(prefs::kCurrentThemePackFilename); |
| 579 if (path != base::FilePath()) { | 584 if (path != base::FilePath()) { |
| 580 path = path.Append(ui::MaterialDesignController::IsModeMaterial() | 585 path = path.Append(chrome::kThemePackFilename); |
| 581 ? chrome::kThemePackMaterialDesignFilename | |
| 582 : chrome::kThemePackFilename); | |
| 583 SwapThemeSupplier(BrowserThemePack::BuildFromDataPack(path, current_id)); | 586 SwapThemeSupplier(BrowserThemePack::BuildFromDataPack(path, current_id)); |
| 584 if (theme_supplier_) | 587 if (theme_supplier_) |
| 585 loaded_pack = true; | 588 loaded_pack = true; |
| 586 } | 589 } |
| 587 | 590 |
| 588 if (loaded_pack) { | 591 if (loaded_pack) { |
| 589 content::RecordAction(UserMetricsAction("Themes.Loaded")); | 592 content::RecordAction(UserMetricsAction("Themes.Loaded")); |
| 590 set_ready(); | 593 set_ready(); |
| 591 } | 594 } |
| 592 // Else: wait for the extension service to be ready so that the theme pack | 595 // Else: wait for the extension service to be ready so that the theme pack |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 850 LOG(ERROR) << "Could not load theme."; | 853 LOG(ERROR) << "Could not load theme."; |
| 851 return; | 854 return; |
| 852 } | 855 } |
| 853 | 856 |
| 854 ExtensionService* service = | 857 ExtensionService* service = |
| 855 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 858 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 856 if (!service) | 859 if (!service) |
| 857 return; | 860 return; |
| 858 | 861 |
| 859 // Write the packed file to disk. | 862 // Write the packed file to disk. |
| 860 base::FilePath pack_path = | |
| 861 extension->path().Append(ui::MaterialDesignController::IsModeMaterial() | |
| 862 ? chrome::kThemePackMaterialDesignFilename | |
| 863 : chrome::kThemePackFilename); | |
| 864 service->GetFileTaskRunner()->PostTask( | 863 service->GetFileTaskRunner()->PostTask( |
| 865 FROM_HERE, | 864 FROM_HERE, base::Bind(&WritePackToDiskCallback, base::RetainedRef(pack), |
| 866 base::Bind(&WritePackToDiskCallback, base::RetainedRef(pack), pack_path)); | 865 extension->path())); |
| 867 | 866 |
| 868 // Save only the extension path. The packed file which matches the | 867 // Save only the extension path. The packed file will be loaded via |
| 869 // MaterialDesignController::Mode will be loaded via LoadThemePrefs(). | 868 // LoadThemePrefs(). |
| 870 SavePackName(extension->path()); | 869 SavePackName(extension->path()); |
| 871 SwapThemeSupplier(pack); | 870 SwapThemeSupplier(pack); |
| 872 } | 871 } |
| 873 | 872 |
| 874 #if defined(ENABLE_SUPERVISED_USERS) | 873 #if defined(ENABLE_SUPERVISED_USERS) |
| 875 bool ThemeService::IsSupervisedUser() const { | 874 bool ThemeService::IsSupervisedUser() const { |
| 876 return profile_->IsSupervised(); | 875 return profile_->IsSupervised(); |
| 877 } | 876 } |
| 878 | 877 |
| 879 void ThemeService::SetSupervisedUserTheme() { | 878 void ThemeService::SetSupervisedUserTheme() { |
| 880 SetCustomDefaultTheme(new SupervisedUserTheme); | 879 SetCustomDefaultTheme(new SupervisedUserTheme); |
| 881 } | 880 } |
| 882 #endif | 881 #endif |
| OLD | NEW |