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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/app_icon_loader_impl.h"
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"
20 const extensions::Extension
* GetExtensionByID(Profile
* profile
,
21 const std::string
& id
) {
22 ExtensionService
* service
=
23 extensions::ExtensionSystem::Get(profile
)->extension_service();
26 return service
->GetInstalledExtension(id
);
31 namespace extensions
{
33 AppIconLoaderImpl::AppIconLoaderImpl(
36 AppIconLoader::Delegate
* delegate
)
39 icon_size_(icon_size
) {
43 AppIconLoaderImpl::~AppIconLoaderImpl() {
44 STLDeleteContainerPairFirstPointers(map_
.begin(), map_
.end());
47 void AppIconLoaderImpl::FetchImage(const std::string
& id
) {
48 for (ImageToExtensionIDMap::const_iterator i
= map_
.begin();
49 i
!= map_
.end(); ++i
) {
51 return; // Already loading the image.
54 const extensions::Extension
* extension
= GetExtensionByID(profile_
, id
);
58 extensions::IconImage
* image
= new extensions::IconImage(
61 extensions::IconsInfo::GetIcons(extension
),
63 extensions::util::GetDefaultAppIcon(),
65 // |map_| takes ownership of |image|.
68 // Triggers image loading now instead of depending on paint message. This
69 // makes the temp blank image be shown for shorter time and improves user
70 // experience. See http://crbug.com/146114.
71 image
->image_skia().EnsureRepsForSupportedScales();
74 void AppIconLoaderImpl::ClearImage(const std::string
& id
) {
75 for (ImageToExtensionIDMap::iterator i
= map_
.begin();
76 i
!= map_
.end(); ++i
) {
77 if (i
->second
== id
) {
85 void AppIconLoaderImpl::UpdateImage(const std::string
& id
) {
86 for (ImageToExtensionIDMap::iterator i
= map_
.begin();
87 i
!= map_
.end(); ++i
) {
88 if (i
->second
== id
) {
89 BuildImage(i
->second
, i
->first
->image_skia());
95 void AppIconLoaderImpl::OnExtensionIconImageChanged(
96 extensions::IconImage
* image
) {
97 ImageToExtensionIDMap::iterator i
= map_
.find(image
);
99 return; // The image has been removed, do nothing.
101 BuildImage(i
->second
, i
->first
->image_skia());
104 void AppIconLoaderImpl::BuildImage(const std::string
& id
,
105 const gfx::ImageSkia
& icon
) {
106 gfx::ImageSkia image
= icon
;
108 if (!util::IsAppLaunchable(id
, profile_
)) {
109 const color_utils::HSL shift
= {-1, 0, 0.6};
110 image
= gfx::ImageSkiaOperations::CreateHSLShiftedImage(image
, shift
);
113 delegate_
->SetAppImage(id
, image
);
116 } // namespace extensions