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

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

Issue 10861034: Supply default icon to extension icon image. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
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/extensions/extension_icon_image.h" 5 #include "chrome/browser/extensions/extension_icon_image.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "chrome/common/chrome_notification_types.h" 9 #include "chrome/common/chrome_notification_types.h"
10 #include "chrome/common/extensions/extension.h" 10 #include "chrome/common/extensions/extension.h"
11 #include "content/public/browser/notification_service.h" 11 #include "content/public/browser/notification_service.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/image/canvas_image_source.h"
12 #include "ui/gfx/image/image.h" 14 #include "ui/gfx/image/image.h"
13 #include "ui/gfx/image/image_skia_source.h" 15 #include "ui/gfx/image/image_skia_source.h"
14 #include "ui/gfx/size.h" 16 #include "ui/gfx/size.h"
15 17
18 /**
19 * The file contains extensions::IconImage implementation, which provides
Jeffrey Yasskin 2012/09/01 01:10:27 Shorter comments are better, all else equal, to pr
tbarzic 2012/09/04 21:07:09 Done.
20 * extension icons to be displayed in the UI.
21 * The icon provided by IconImage is ImageSkia icon defined with custom
22 * ImageSkiaSource. When a new image representation is requesetd from the image
Jeffrey Yasskin 2012/09/01 01:10:27 sp: requesetd
tbarzic 2012/09/04 21:07:09 Done.
23 * source the following happens:
24 * - If the extension doesn't have a icon resource needed for the image
Jeffrey Yasskin 2012/09/01 01:10:27 There's some complex behavior around which sizes a
tbarzic 2012/09/04 21:07:09 Done.
25 * representation, default icon's representation for the requested scale
26 * factor is returned by ImageSkiaSource. If the default icon is empty,
Jeffrey Yasskin 2012/09/01 01:10:27 You wind up saying the empty-default fallback 3 ti
tbarzic 2012/09/04 21:07:09 Done.
27 * transparent image representation is returned.
28 * - If the extension has the resource, IconImage tries to load it using
29 * ImageLoadingTracker.
30 * - |ImageLoaderTracker| may return both syncronously and asyncronously.
Jeffrey Yasskin 2012/09/01 01:10:27 sp: syncronously 2x
tbarzic 2012/09/04 21:07:09 Done.
31 * IconImage behaviour depends on this.
32 * 1. |ImageLoaderTracker| is synchronous.
33 * - If image representation resurce is successfully loaded, the
Jeffrey Yasskin 2012/09/01 01:10:27 sp: resurce
tbarzic 2012/09/04 21:07:09 Done.
34 * representation returned by ImageSkiaSource is created from the loaded
35 * bitmap.
36 * - If resource loading fails, ImageSkiaSource returns either default icon's
37 * representation, or a blank image representation; depending on whether the
38 * supplied default icon was non empty.
39 * 2. |ImageLoadingTracker| is asyncronous.
40 * - ImageSkiaSource will initially return transparent image resource of the
41 * desired size.
42 * - The image will be manually updated when the |ImageLoadingTracker|
Jeffrey Yasskin 2012/09/01 01:10:27 Nothing's manual in code. I think you mean that we
tbarzic 2012/09/04 21:07:09 Done.
43 * finishes.
44 * - If image resource is successfully loaded, the image is updated with the
45 * representation created from the loaded bitmap (the old representation is
46 * removed from the image). Also, the observer is notified the image has
47 * changed.
48 * - If resource load failed and non empty default icon was supplied, image
49 * is updated by the corresponding representatin from the default icon and
Jeffrey Yasskin 2012/09/01 01:10:27 sp: representatin
tbarzic 2012/09/04 21:07:09 Done.
50 * the obesrver is notifeid image has changed.
51 * - If resource load failed and an empty default icon was supplied, nothing
52 * is done. The image will keep initial, transparent representation.
53 **/
16 namespace { 54 namespace {
17 55
18 const int kMatchBiggerTreshold = 32; 56 const int kMatchBiggerTreshold = 32;
19 57
20 ExtensionResource GetExtensionIconResource( 58 ExtensionResource GetExtensionIconResource(
21 const extensions::Extension* extension, 59 const extensions::Extension* extension,
22 const ExtensionIconSet& icons, 60 const ExtensionIconSet& icons,
23 int size, 61 int size,
24 ExtensionIconSet::MatchType match_type) { 62 ExtensionIconSet::MatchType match_type) {
25 std::string path = icons.Get(size, match_type); 63 std::string path = icons.Get(size, match_type);
26 if (path.empty()) 64 if (path.empty())
27 return ExtensionResource(); 65 return ExtensionResource();
28 66
29 return extension->GetResource(path); 67 return extension->GetResource(path);
30 } 68 }
31 69
70 class BlankImageSource : public gfx::CanvasImageSource {
71 public:
72 explicit BlankImageSource(const gfx::Size& size_in_dip)
73 : CanvasImageSource(size_in_dip, false) {
Jeffrey Yasskin 2012/09/01 01:10:27 I prefer writing something like /*is_opaque=*/fals
tbarzic 2012/09/04 21:07:09 yeah, I've started doing this, but I still occasio
74 }
75 virtual ~BlankImageSource() {}
76
77 private:
78 // gfx::CanvasImageSource overrides:
79 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
80 canvas->DrawColor(SkColorSetARGB(0, 0, 0, 0));
81 }
82
83 DISALLOW_COPY_AND_ASSIGN(BlankImageSource);
84 };
85
32 } // namespace 86 } // namespace
33 87
34 namespace extensions { 88 namespace extensions {
35 89
36 //////////////////////////////////////////////////////////////////////////////// 90 ////////////////////////////////////////////////////////////////////////////////
37 // ExtensionIconImage::Source 91 // ExtensionIconImage::Source
38 92
39 class IconImage::Source : public gfx::ImageSkiaSource { 93 class IconImage::Source : public gfx::ImageSkiaSource {
40 public: 94 public:
41 explicit Source(IconImage* host); 95 Source(IconImage* host, const gfx::Size& size_in_dip);
42 virtual ~Source(); 96 virtual ~Source();
43 97
44 void ResetHost(); 98 void ResetHost();
45 99
46 private: 100 private:
47 // gfx::ImageSkiaSource overrides: 101 // gfx::ImageSkiaSource overrides:
48 virtual gfx::ImageSkiaRep GetImageForScale( 102 virtual gfx::ImageSkiaRep GetImageForScale(
49 ui::ScaleFactor scale_factor) OVERRIDE; 103 ui::ScaleFactor scale_factor) OVERRIDE;
50 104
105 // IconImage object that is hosting this image source. It is used to load
Jeffrey Yasskin 2012/09/01 01:10:27 Shorter: "Used to load images, possibly asynchrono
tbarzic 2012/09/04 21:07:09 Done.
106 // image representation for the extension icon.
107 // |host_| will be reset once the referenced icon image dies. Since |this|
108 // can outlive the IconImage, NULL check should be made on |host_| before
109 // using it.
51 IconImage* host_; 110 IconImage* host_;
52 111
112 // Image whose representations will be used until |host_| load the real
113 // representations for the image.
114 gfx::ImageSkia blank_image_;
115
53 DISALLOW_COPY_AND_ASSIGN(Source); 116 DISALLOW_COPY_AND_ASSIGN(Source);
54 }; 117 };
55 118
56 IconImage::Source::Source(IconImage* host) : host_(host) { 119 IconImage::Source::Source(IconImage* host, const gfx::Size& size_in_dip)
120 : host_(host),
121 blank_image_(new BlankImageSource(size_in_dip), size_in_dip) {
57 } 122 }
58 123
59 IconImage::Source::~Source() { 124 IconImage::Source::~Source() {
60 } 125 }
61 126
62 void IconImage::Source::ResetHost() { 127 void IconImage::Source::ResetHost() {
63 host_ = NULL; 128 host_ = NULL;
64 } 129 }
65 130
66 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( 131 gfx::ImageSkiaRep IconImage::Source::GetImageForScale(
67 ui::ScaleFactor scale_factor) { 132 ui::ScaleFactor scale_factor) {
133 gfx::ImageSkiaRep representation;
68 if (host_) 134 if (host_)
69 host_->LoadImageForScaleFactor(scale_factor); 135 representation = host_->LoadImageForScaleFactor(scale_factor);
70 return gfx::ImageSkiaRep(); 136
137 if (!representation.is_null())
138 return representation;
139
140 return blank_image_.GetRepresentation(scale_factor);
71 } 141 }
72 142
73 //////////////////////////////////////////////////////////////////////////////// 143 ////////////////////////////////////////////////////////////////////////////////
74 // ExtensionIconImage 144 // ExtensionIconImage
75 145
76 IconImage::IconImage( 146 IconImage::IconImage(
77 const Extension* extension, 147 const Extension* extension,
78 const ExtensionIconSet& icon_set, 148 const ExtensionIconSet& icon_set,
79 int resource_size_in_dip, 149 int resource_size_in_dip,
150 const gfx::ImageSkia& default_icon,
80 Observer* observer) 151 Observer* observer)
81 : extension_(extension), 152 : extension_(extension),
82 icon_set_(icon_set), 153 icon_set_(icon_set),
83 resource_size_in_dip_(resource_size_in_dip), 154 resource_size_in_dip_(resource_size_in_dip),
84 desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip),
85 observer_(observer), 155 observer_(observer),
86 source_(NULL), 156 source_(NULL),
157 default_icon_(default_icon),
87 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { 158 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) {
88 source_ = new Source(this); 159 gfx::Size resource_size(resource_size_in_dip, resource_size_in_dip);
89 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); 160 source_ = new Source(this, resource_size);
161 image_skia_ = gfx::ImageSkia(source_, resource_size);
90 162
91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 163 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
92 content::NotificationService::AllSources()); 164 content::NotificationService::AllSources());
93 } 165 }
94 166
95 IconImage::~IconImage() { 167 IconImage::~IconImage() {
Jeffrey Yasskin 2012/09/01 01:10:27 This still needs to call ResetHost(), right? If th
tbarzic 2012/09/04 21:07:09 jeez, I'm sometimes amazed with things I do on Fri
96 // |source_| could be NULL if resource does not exist.
97 if (source_)
98 source_->ResetHost();
99 } 168 }
100 169
101 void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { 170 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor(
171 ui::ScaleFactor scale_factor) {
102 // Do nothing if extension is unloaded. 172 // Do nothing if extension is unloaded.
103 if (!extension_) 173 if (!extension_)
104 return; 174 return gfx::ImageSkiaRep();
105 175
106 const float scale = ui::GetScaleFactorScale(scale_factor); 176 const float scale = ui::GetScaleFactorScale(scale_factor);
107 const int resource_size_in_pixel = 177 const int resource_size_in_pixel =
108 static_cast<int>(resource_size_in_dip_ * scale); 178 static_cast<int>(resource_size_in_dip_ * scale);
109 179
110 ExtensionResource resource; 180 ExtensionResource resource;
111 // We try loading bigger image only if resource size is >= 32. 181 // We try loading bigger image only if resource size is >= 32.
112 if (resource_size_in_pixel >= kMatchBiggerTreshold) { 182 if (resource_size_in_pixel >= kMatchBiggerTreshold) {
113 resource = GetExtensionIconResource(extension_, icon_set_, 183 resource = GetExtensionIconResource(extension_, icon_set_,
114 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); 184 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER);
115 } 185 }
116 186
117 // If resource is not found by now, try matching smaller one. 187 // If resource is not found by now, try matching smaller one.
118 if (resource.empty()) { 188 if (resource.empty()) {
119 resource = GetExtensionIconResource(extension_, icon_set_, 189 resource = GetExtensionIconResource(extension_, icon_set_,
120 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); 190 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER);
121 } 191 }
122 192
123 // If there is no resource found, bail out and notify observer of failure. 193 // If there is no resource found, return default icon.
124 if (resource.empty()) { 194 if (resource.empty())
125 if (observer_) 195 return default_icon_.GetRepresentation(scale_factor);
126 observer_->OnIconImageLoadFailed(this, scale_factor);
127 return;
128 }
129 196
130 int id = tracker_.next_id(); 197 int id = tracker_.next_id();
131 load_map_[id] = scale_factor; 198 load_map_[id].scale_factor = scale_factor;
199 load_map_[id].is_async = false;
132 200
133 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; 201 std::vector<ImageLoadingTracker::ImageRepresentation> info_list;
134 info_list.push_back(ImageLoadingTracker::ImageRepresentation( 202 info_list.push_back(ImageLoadingTracker::ImageRepresentation(
135 resource, 203 resource,
136 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, 204 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER,
137 desired_size_in_dip_.Scale(scale), 205 gfx::Size(resource_size_in_dip_, resource_size_in_dip_).Scale(scale),
138 scale_factor)); 206 scale_factor));
139 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); 207 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE);
208
209 // If we have not received |OnImageLoaded|, image load request is
210 // asynchronous.
211 if (load_map_.find(id) != load_map_.end())
212 load_map_[id].is_async = true;
213
214 // If LoadImages returned synchronously and the requested image rep is cached
215 // in the extension, return the cached image rep.
216 if (image_skia_.HasRepresentation(scale_factor))
217 return image_skia_.GetRepresentation(scale_factor);
218
219 return gfx::ImageSkiaRep();
140 } 220 }
141 221
142 void IconImage::OnImageLoaded(const gfx::Image& image, 222 void IconImage::OnImageLoaded(const gfx::Image& image_in,
143 const std::string& extension_id, 223 const std::string& extension_id,
144 int index) { 224 int index) {
145 LoadMap::iterator load_map_it = load_map_.find(index); 225 LoadMap::iterator load_map_it = load_map_.find(index);
146 DCHECK(load_map_it != load_map_.end()); 226 DCHECK(load_map_it != load_map_.end());
147 227
148 ui::ScaleFactor scale_factor = load_map_it->second; 228 ui::ScaleFactor scale_factor = load_map_it->second.scale_factor;
229 bool is_async = load_map_it->second.is_async;
149 230
150 load_map_.erase(load_map_it); 231 load_map_.erase(load_map_it);
151 232
152 if (image.IsEmpty()) { 233 const gfx::ImageSkia* image =
153 // There waas an error loading the image. 234 image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia();
154 if (observer_) 235
155 observer_->OnIconImageLoadFailed(this, scale_factor); 236 // If the image resource has been loaded successfully, the returned image
237 // should have the representation for the requested scale factor.
238 DCHECK(image_in.IsEmpty() || image->HasRepresentation(scale_factor));
Jeffrey Yasskin 2012/09/01 01:10:27 Your comment says GetRepresentation should work, n
tbarzic 2012/09/04 21:07:09 Done.
239
240 // Maybe default icon was not set.
241 if (image->isNull())
156 return; 242 return;
157 }
158 243
159 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); 244 gfx::ImageSkiaRep rep = image->GetRepresentation(scale_factor);
160 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor);
161 DCHECK(!rep.is_null()); 245 DCHECK(!rep.is_null());
246
247 // Remove old representation if there is one.
248 image_skia_.RemoveRepresentation(rep.scale_factor());
162 image_skia_.AddRepresentation(rep); 249 image_skia_.AddRepresentation(rep);
163 250
164 if (observer_) 251 // If |tracker_| called us synchronously the image did not really change from
252 // the observers perspective, since the initial image representation is
253 // returned synchronously.
254 if (is_async && observer_)
165 observer_->OnExtensionIconImageChanged(this); 255 observer_->OnExtensionIconImageChanged(this);
166 } 256 }
167 257
168 void IconImage::Observe(int type, 258 void IconImage::Observe(int type,
169 const content::NotificationSource& source, 259 const content::NotificationSource& source,
170 const content::NotificationDetails& details) { 260 const content::NotificationDetails& details) {
171 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); 261 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED);
172 262
173 const Extension* extension = 263 const Extension* extension =
174 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; 264 content::Details<extensions::UnloadedExtensionInfo>(details)->extension;
175 265
176 if (extension_ == extension) 266 if (extension_ == extension)
177 extension_ = NULL; 267 extension_ = NULL;
178 } 268 }
179 269
180 } // namespace extensions 270 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698