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

Side by Side Diff: chrome/browser/extensions/extension_app_icon_loader.cc

Issue 2819413003: Refactor extension app icon. (Closed)
Patch Set: nits Created 3 years, 7 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/extension_app_icon_loader.h"
6
7 #include "base/stl_util.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/extensions/extension_constants.h"
12 #include "extensions/browser/extension_system.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/manifest_handlers/icons_handler.h"
15 #include "ui/gfx/color_utils.h"
16 #include "ui/gfx/image/image_skia_operations.h"
17
18 #if defined(OS_CHROMEOS)
19 #include "chrome/browser/chromeos/extensions/gfx_utils.h"
20 #endif
21
22 namespace {
23
24 const extensions::Extension* GetExtensionByID(Profile* profile,
25 const std::string& id) {
26 ExtensionService* service =
27 extensions::ExtensionSystem::Get(profile)->extension_service();
28 if (!service)
29 return nullptr;
30 return service->GetInstalledExtension(id);
31 }
32
33 } // namespace
34
35 namespace extensions {
36
37 ExtensionAppIconLoader::ExtensionAppIconLoader(
38 Profile* profile,
39 int icon_size,
40 AppIconLoaderDelegate* delegate)
41 : AppIconLoader(profile, icon_size, delegate) {
42 }
43
44 ExtensionAppIconLoader::~ExtensionAppIconLoader() {
45 }
46
47 bool ExtensionAppIconLoader::CanLoadImageForApp(const std::string& id) {
48 if (map_.find(id) != map_.end())
49 return true;
50 return GetExtensionByID(profile(), id) != nullptr;
51 }
52
53 void ExtensionAppIconLoader::FetchImage(const std::string& id) {
54 if (map_.find(id) != map_.end())
55 return; // Already loading the image.
56
57 const extensions::Extension* extension = GetExtensionByID(profile(), id);
58 if (!extension)
59 return;
60
61 std::unique_ptr<extensions::IconImage> image(new extensions::IconImage(
62 profile(), extension, extensions::IconsInfo::GetIcons(extension),
63 icon_size(), extensions::util::GetDefaultAppIcon(), this));
64
65 // Triggers image loading now instead of depending on paint message. This
66 // makes the temp blank image be shown for shorter time and improves user
67 // experience. See http://crbug.com/146114.
68 image->image_skia().EnsureRepsForSupportedScales();
69 map_[id] = std::move(image);
70 }
71
72 void ExtensionAppIconLoader::ClearImage(const std::string& id) {
73 map_.erase(id);
74 }
75
76 void ExtensionAppIconLoader::UpdateImage(const std::string& id) {
77 ExtensionIDToImageMap::iterator it = map_.find(id);
78 if (it == map_.end())
79 return;
80
81 BuildImage(it->first, it->second->image_skia());
82 }
83
84 void ExtensionAppIconLoader::OnExtensionIconImageChanged(
85 extensions::IconImage* image) {
86 for (const auto& pair : map_) {
87 if (pair.second.get() == image) {
88 BuildImage(pair.first, pair.second->image_skia());
89 return;
90 }
91 }
92 // The image has been removed, do nothing.
93 }
94
95 void ExtensionAppIconLoader::BuildImage(const std::string& id,
96 const gfx::ImageSkia& icon) {
97 gfx::ImageSkia image = icon;
98
99 #if defined(OS_CHROMEOS)
100 util::MaybeApplyChromeBadge(profile(), id, &image);
101 #endif
102
103 if (!util::IsAppLaunchable(id, profile())) {
104 const color_utils::HSL shift = {-1, 0, 0.6};
105 image = gfx::ImageSkiaOperations::CreateHSLShiftedImage(image, shift);
106 }
107
108 delegate()->OnAppImageUpdated(id, image);
109 }
110
111 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698