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

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: test Created 8 years, 4 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"
(...skipping 11 matching lines...) Expand all
22 const ExtensionIconSet& icons, 22 const ExtensionIconSet& icons,
23 int size, 23 int size,
24 ExtensionIconSet::MatchType match_type) { 24 ExtensionIconSet::MatchType match_type) {
25 std::string path = icons.Get(size, match_type); 25 std::string path = icons.Get(size, match_type);
26 if (path.empty()) 26 if (path.empty())
27 return ExtensionResource(); 27 return ExtensionResource();
28 28
29 return extension->GetResource(path); 29 return extension->GetResource(path);
30 } 30 }
31 31
32 class BlankImageSource : public gfx::ImageSkiaSource {
33 public:
34 explicit BlankImageSource(int size_in_dip) : size_in_dip_(size_in_dip) {}
35 virtual ~BlankImageSource() {}
36
37 private:
38 // gfx::ImageSkiaSource overrides:
39 virtual gfx::ImageSkiaRep GetImageForScale(
40 ui::ScaleFactor scale_factor) OVERRIDE {
41 SkBitmap bitmap;
42 const float scale = ui::GetScaleFactorScale(scale_factor);
43 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
44 static_cast<int>(size_in_dip_ * scale),
45 static_cast<int>(size_in_dip_ * scale));
46 bitmap.allocPixels();
47 bitmap.eraseColor(SkColorSetARGB(0, 0, 0, 0));
48 return gfx::ImageSkiaRep(bitmap, scale_factor);
49 }
50
51 int size_in_dip_;
52
53 DISALLOW_COPY_AND_ASSIGN(BlankImageSource);
54 };
55
32 } // namespace 56 } // namespace
33 57
34 namespace extensions { 58 namespace extensions {
35 59
36 //////////////////////////////////////////////////////////////////////////////// 60 ////////////////////////////////////////////////////////////////////////////////
37 // ExtensionIconImage::Source 61 // ExtensionIconImage::Source
38 62
39 class IconImage::Source : public gfx::ImageSkiaSource { 63 class IconImage::Source : public gfx::ImageSkiaSource {
40 public: 64 public:
41 explicit Source(IconImage* host); 65 Source(IconImage* host, int size_in_dip);
42 virtual ~Source(); 66 virtual ~Source();
43 67
44 void ResetHost(); 68 void ResetHost();
45 69
46 private: 70 private:
47 // gfx::ImageSkiaSource overrides: 71 // gfx::ImageSkiaSource overrides:
48 virtual gfx::ImageSkiaRep GetImageForScale( 72 virtual gfx::ImageSkiaRep GetImageForScale(
49 ui::ScaleFactor scale_factor) OVERRIDE; 73 ui::ScaleFactor scale_factor) OVERRIDE;
50 74
51 IconImage* host_; 75 IconImage* host_;
52 76
77 // Image whose representations will be used until |host_| load the real
78 // representations for the image.
79 gfx::ImageSkia blank_image_;
80
53 DISALLOW_COPY_AND_ASSIGN(Source); 81 DISALLOW_COPY_AND_ASSIGN(Source);
54 }; 82 };
55 83
56 IconImage::Source::Source(IconImage* host) : host_(host) { 84 IconImage::Source::Source(IconImage* host, int size_in_dip)
85 : host_(host),
86 blank_image_(new BlankImageSource(size_in_dip),
87 gfx::Size(size_in_dip, size_in_dip)) {
57 } 88 }
58 89
59 IconImage::Source::~Source() { 90 IconImage::Source::~Source() {
60 } 91 }
61 92
62 void IconImage::Source::ResetHost() { 93 void IconImage::Source::ResetHost() {
63 host_ = NULL; 94 host_ = NULL;
64 } 95 }
65 96
66 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( 97 gfx::ImageSkiaRep IconImage::Source::GetImageForScale(
67 ui::ScaleFactor scale_factor) { 98 ui::ScaleFactor scale_factor) {
99 gfx::ImageSkiaRep representation;
68 if (host_) 100 if (host_)
69 host_->LoadImageForScaleFactor(scale_factor); 101 representation = host_->LoadImageForScaleFactor(scale_factor);
70 return gfx::ImageSkiaRep(); 102
103 if (!representation.is_null())
104 return representation;
105
106 return blank_image_.GetRepresentation(scale_factor);
71 } 107 }
72 108
73 //////////////////////////////////////////////////////////////////////////////// 109 ////////////////////////////////////////////////////////////////////////////////
74 // ExtensionIconImage 110 // ExtensionIconImage
75 111
76 IconImage::IconImage( 112 IconImage::IconImage(
77 const Extension* extension, 113 const Extension* extension,
78 const ExtensionIconSet& icon_set, 114 const ExtensionIconSet& icon_set,
79 int resource_size_in_dip, 115 int resource_size_in_dip,
116 const gfx::ImageSkia& default_icon,
80 Observer* observer) 117 Observer* observer)
81 : extension_(extension), 118 : extension_(extension),
82 icon_set_(icon_set), 119 icon_set_(icon_set),
83 resource_size_in_dip_(resource_size_in_dip), 120 resource_size_in_dip_(resource_size_in_dip),
84 desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip), 121 desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip),
85 observer_(observer), 122 observer_(observer),
86 source_(NULL), 123 source_(NULL),
124 default_icon_(default_icon),
87 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { 125 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) {
88 source_ = new Source(this); 126 source_ = new Source(this, resource_size_in_dip);
89 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); 127 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_);
90 128
91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 129 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
92 content::NotificationService::AllSources()); 130 content::NotificationService::AllSources());
93 } 131 }
94 132
95 IconImage::~IconImage() { 133 IconImage::~IconImage() {
96 // |source_| could be NULL if resource does not exist. 134 // |source_| could be NULL if resource does not exist.
97 if (source_) 135 if (source_)
98 source_->ResetHost(); 136 source_->ResetHost();
99 } 137 }
100 138
101 void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { 139 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor(
140 ui::ScaleFactor scale_factor) {
102 // Do nothing if extension is unloaded. 141 // Do nothing if extension is unloaded.
103 if (!extension_) 142 if (!extension_)
104 return; 143 return gfx::ImageSkiaRep();
105 144
106 const float scale = ui::GetScaleFactorScale(scale_factor); 145 const float scale = ui::GetScaleFactorScale(scale_factor);
107 const int resource_size_in_pixel = 146 const int resource_size_in_pixel =
108 static_cast<int>(resource_size_in_dip_ * scale); 147 static_cast<int>(resource_size_in_dip_ * scale);
109 148
110 ExtensionResource resource; 149 ExtensionResource resource;
111 // We try loading bigger image only if resource size is >= 32. 150 // We try loading bigger image only if resource size is >= 32.
112 if (resource_size_in_pixel >= kMatchBiggerTreshold) { 151 if (resource_size_in_pixel >= kMatchBiggerTreshold) {
113 resource = GetExtensionIconResource(extension_, icon_set_, 152 resource = GetExtensionIconResource(extension_, icon_set_,
114 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); 153 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER);
115 } 154 }
116 155
117 // If resource is not found by now, try matching smaller one. 156 // If resource is not found by now, try matching smaller one.
118 if (resource.empty()) { 157 if (resource.empty()) {
119 resource = GetExtensionIconResource(extension_, icon_set_, 158 resource = GetExtensionIconResource(extension_, icon_set_,
120 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); 159 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER);
121 } 160 }
122 161
123 // If there is no resource found, bail out and notify observer of failure. 162 // If there is no resource found, return default icon.
124 if (resource.empty()) { 163 if (resource.empty())
125 if (observer_) 164 return default_icon_.GetRepresentation(scale_factor);
126 observer_->OnIconImageLoadFailed(this, scale_factor);
127 return;
128 }
129 165
130 int id = tracker_.next_id(); 166 int id = tracker_.next_id();
131 load_map_[id] = scale_factor; 167 load_map_[id].scale_factor = scale_factor;
168 load_map_[id].is_async = false;
132 169
133 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; 170 std::vector<ImageLoadingTracker::ImageRepresentation> info_list;
134 info_list.push_back(ImageLoadingTracker::ImageRepresentation( 171 info_list.push_back(ImageLoadingTracker::ImageRepresentation(
135 resource, 172 resource,
136 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, 173 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER,
137 desired_size_in_dip_.Scale(scale), 174 desired_size_in_dip_.Scale(scale),
138 scale_factor)); 175 scale_factor));
139 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); 176 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE);
177
178 // If we have not received |OnImageLoaded|, image load request is
179 // asynchronous.
180 if (load_map_.find(id) != load_map_.end())
181 load_map_[id].is_async = true;
182
183 // If requested image has been cached, we may already have the wanted
184 // representation.
185 if (image_skia_.HasRepresentation(scale_factor))
186 return image_skia_.GetRepresentation(scale_factor);
187
188 return gfx::ImageSkiaRep();
140 } 189 }
141 190
142 void IconImage::OnImageLoaded(const gfx::Image& image, 191 void IconImage::OnImageLoaded(const gfx::Image& image_in,
143 const std::string& extension_id, 192 const std::string& extension_id,
144 int index) { 193 int index) {
145 LoadMap::iterator load_map_it = load_map_.find(index); 194 LoadMap::iterator load_map_it = load_map_.find(index);
146 DCHECK(load_map_it != load_map_.end()); 195 DCHECK(load_map_it != load_map_.end());
147 196
148 ui::ScaleFactor scale_factor = load_map_it->second; 197 ui::ScaleFactor scale_factor = load_map_it->second.scale_factor;
198 bool is_async = load_map_it->second.is_async;
149 199
150 load_map_.erase(load_map_it); 200 load_map_.erase(load_map_it);
151 201
152 if (image.IsEmpty()) { 202 const gfx::ImageSkia* image =
153 // There waas an error loading the image. 203 image_in.IsEmpty() ? &default_icon_ : image_in.ToImageSkia();
154 if (observer_) 204
155 observer_->OnIconImageLoadFailed(this, scale_factor); 205 // Maybe default icon was not set.
206 if (image->isNull())
156 return; 207 return;
157 }
158 208
159 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); 209 // TODO(tbarzic): add DCHECK(image->HasRepresentation(scale_factor)) once
160 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); 210 // we'll be certain that icons from resource bundle have all needed scale
211 // factors.
212 gfx::ImageSkiaRep rep = image->GetRepresentation(scale_factor);
161 DCHECK(!rep.is_null()); 213 DCHECK(!rep.is_null());
214
215 // Remove old representation if there is one.
216 image_skia_.RemoveRepresentation(rep.scale_factor());
162 image_skia_.AddRepresentation(rep); 217 image_skia_.AddRepresentation(rep);
163 218
164 if (observer_) 219 // If |tracker_| called us synchronously the image did not really change from
220 // the observers perspective, since we still haven't returned the initial
xiyuan 2012/08/23 17:25:57 think you mean: ...did not really change from the
tbarzic 2012/08/23 17:41:53 Done.
221 // image representation.
222 if (is_async && observer_)
165 observer_->OnExtensionIconImageChanged(this); 223 observer_->OnExtensionIconImageChanged(this);
166 } 224 }
167 225
168 void IconImage::Observe(int type, 226 void IconImage::Observe(int type,
169 const content::NotificationSource& source, 227 const content::NotificationSource& source,
170 const content::NotificationDetails& details) { 228 const content::NotificationDetails& details) {
171 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); 229 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED);
172 230
173 const Extension* extension = 231 const Extension* extension =
174 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; 232 content::Details<extensions::UnloadedExtensionInfo>(details)->extension;
175 233
176 if (extension_ == extension) 234 if (extension_ == extension)
177 extension_ = NULL; 235 extension_ = NULL;
178 } 236 }
179 237
180 } // namespace extensions 238 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_icon_image.h ('k') | chrome/browser/extensions/extension_icon_image_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698