1 // Copyright 2014 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 "extensions/browser/extension_icon_image.h"
10 #include "content/public/browser/notification_service.h"
11 #include "extensions/browser/image_loader.h"
12 #include "extensions/browser/notification_types.h"
13 #include "extensions/common/extension.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/gfx/geometry/size_conversions.h"
17 #include "ui/gfx/image/canvas_image_source.h"
18 #include "ui/gfx/image/image.h"
19 #include "ui/gfx/image/image_skia_operations.h"
20 #include "ui/gfx/image/image_skia_source.h"
22 // The ImageSkia provided by extensions::IconImage contains ImageSkiaReps that
23 // are computed and updated using the following algorithm (if no default icon
24 // was supplied, transparent icon is considered the default):
25 // - |LoadImageForScaleFactors()| searches the extension for an icon of an
26 // appropriate size. If the extension doesn't have a icon resource needed for
27 // the image representation, the default icon's representation for the
28 // requested scale factor is returned by ImageSkiaSource.
29 // - If the extension has the resource, IconImage tries to load it using
31 // - |ImageLoader| is asynchronous.
32 // - ImageSkiaSource will initially return transparent image resource of the
34 // - The image will be updated with an appropriate image representation when
35 // the |ImageLoader| finishes. The image representation is chosen the same
36 // way as in the synchronous case. The observer is notified of the image
37 // change, unless the added image representation is transparent (in which
38 // case the image had already contained the appropriate image
43 extensions::ExtensionResource
GetExtensionIconResource(
44 const extensions::Extension
* extension
,
45 const ExtensionIconSet
& icons
,
47 ExtensionIconSet::MatchType match_type
) {
48 const std::string
& path
= icons
.Get(size
, match_type
);
49 return path
.empty() ? extensions::ExtensionResource()
50 : extension
->GetResource(path
);
53 class BlankImageSource
: public gfx::CanvasImageSource
{
55 explicit BlankImageSource(const gfx::Size
& size_in_dip
)
56 : CanvasImageSource(size_in_dip
, /*is_opaque =*/ false) {
58 ~BlankImageSource() override
{}
61 // gfx::CanvasImageSource overrides:
62 void Draw(gfx::Canvas
* canvas
) override
{
63 canvas
->DrawColor(SkColorSetARGB(0, 0, 0, 0));
66 DISALLOW_COPY_AND_ASSIGN(BlankImageSource
);
71 namespace extensions
{
73 ////////////////////////////////////////////////////////////////////////////////
76 class IconImage::Source
: public gfx::ImageSkiaSource
{
78 Source(IconImage
* host
, const gfx::Size
& size_in_dip
);
84 // gfx::ImageSkiaSource overrides:
85 gfx::ImageSkiaRep
GetImageForScale(float scale
) override
;
87 // Used to load images, possibly asynchronously. NULLed out when the IconImage
91 // Image whose representations will be used until |host_| loads the real
92 // representations for the image.
93 gfx::ImageSkia blank_image_
;
95 DISALLOW_COPY_AND_ASSIGN(Source
);
98 IconImage::Source::Source(IconImage
* host
, const gfx::Size
& size_in_dip
)
100 blank_image_(new BlankImageSource(size_in_dip
), size_in_dip
) {
103 IconImage::Source::~Source() {
106 void IconImage::Source::ResetHost() {
110 gfx::ImageSkiaRep
IconImage::Source::GetImageForScale(float scale
) {
111 gfx::ImageSkiaRep representation
;
114 host_
->LoadImageForScaleFactor(ui::GetSupportedScaleFactor(scale
));
117 if (!representation
.is_null())
118 return representation
;
120 return blank_image_
.GetRepresentation(scale
);
123 ////////////////////////////////////////////////////////////////////////////////
126 IconImage::IconImage(
127 content::BrowserContext
* context
,
128 const Extension
* extension
,
129 const ExtensionIconSet
& icon_set
,
130 int resource_size_in_dip
,
131 const gfx::ImageSkia
& default_icon
,
133 : browser_context_(context
),
134 extension_(extension
),
136 resource_size_in_dip_(resource_size_in_dip
),
139 default_icon_(gfx::ImageSkiaOperations::CreateResizedImage(
141 skia::ImageOperations::RESIZE_BEST
,
142 gfx::Size(resource_size_in_dip
, resource_size_in_dip
))),
143 weak_ptr_factory_(this) {
144 gfx::Size
resource_size(resource_size_in_dip
, resource_size_in_dip
);
145 source_
= new Source(this, resource_size
);
146 image_skia_
= gfx::ImageSkia(source_
, resource_size
);
149 extensions::NOTIFICATION_EXTENSION_REMOVED
,
150 content::NotificationService::AllSources());
153 IconImage::~IconImage() {
154 source_
->ResetHost();
157 gfx::ImageSkiaRep
IconImage::LoadImageForScaleFactor(
158 ui::ScaleFactor scale_factor
) {
159 // Do nothing if extension is unloaded.
161 return gfx::ImageSkiaRep();
163 const float scale
= ui::GetScaleForScaleFactor(scale_factor
);
164 const int resource_size_in_pixel
=
165 static_cast<int>(resource_size_in_dip_
* scale
);
167 extensions::ExtensionResource resource
;
169 // Find extension resource for non bundled component extensions.
170 resource
= GetExtensionIconResource(extension_
,
172 resource_size_in_pixel
,
173 ExtensionIconSet::MATCH_BIGGER
);
175 // If resource is not found by now, try matching smaller one.
176 if (resource
.empty()) {
177 resource
= GetExtensionIconResource(extension_
, icon_set_
,
178 resource_size_in_pixel
, ExtensionIconSet::MATCH_SMALLER
);
181 // If there is no resource found, return default icon.
182 if (resource
.empty())
183 return default_icon_
.GetRepresentation(scale
);
185 std::vector
<ImageLoader::ImageRepresentation
> info_list
;
186 info_list
.push_back(ImageLoader::ImageRepresentation(
188 ImageLoader::ImageRepresentation::ALWAYS_RESIZE
,
189 gfx::ToFlooredSize(gfx::ScaleSize(
190 gfx::Size(resource_size_in_dip_
, resource_size_in_dip_
), scale
)),
193 extensions::ImageLoader
* loader
=
194 extensions::ImageLoader::Get(browser_context_
);
195 loader
->LoadImagesAsync(extension_
, info_list
,
196 base::Bind(&IconImage::OnImageLoaded
,
197 weak_ptr_factory_
.GetWeakPtr(),
200 return gfx::ImageSkiaRep();
203 void IconImage::OnImageLoaded(float scale
, const gfx::Image
& image_in
) {
204 const gfx::ImageSkia
* image
=
205 image_in
.IsEmpty() ? &default_icon_
: image_in
.ToImageSkia();
207 // Maybe default icon was not set.
211 gfx::ImageSkiaRep rep
= image
->GetRepresentation(scale
);
212 DCHECK(!rep
.is_null());
213 DCHECK_EQ(scale
, rep
.scale());
215 // Remove old representation if there is one.
216 image_skia_
.RemoveRepresentation(scale
);
217 image_skia_
.AddRepresentation(rep
);
220 observer_
->OnExtensionIconImageChanged(this);
223 void IconImage::Observe(int type
,
224 const content::NotificationSource
& source
,
225 const content::NotificationDetails
& details
) {
226 DCHECK_EQ(type
, extensions::NOTIFICATION_EXTENSION_REMOVED
);
228 const Extension
* extension
= content::Details
<const Extension
>(details
).ptr();
230 if (extension_
== extension
)
234 } // namespace extensions