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

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, 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 "base/message_loop_proxy.h"
9 #include "chrome/common/chrome_notification_types.h" 10 #include "chrome/common/chrome_notification_types.h"
10 #include "chrome/common/extensions/extension.h" 11 #include "chrome/common/extensions/extension.h"
12 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/notification_service.h" 13 #include "content/public/browser/notification_service.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 using content::BrowserThread;
19
16 namespace { 20 namespace {
17 21
18 const int kMatchBiggerTreshold = 32; 22 const int kMatchBiggerTreshold = 32;
19 23
20 ExtensionResource GetExtensionIconResource( 24 ExtensionResource GetExtensionIconResource(
21 const extensions::Extension* extension, 25 const extensions::Extension* extension,
22 const ExtensionIconSet& icons, 26 const ExtensionIconSet& icons,
23 int size, 27 int size,
24 ExtensionIconSet::MatchType match_type) { 28 ExtensionIconSet::MatchType match_type) {
25 std::string path = icons.Get(size, match_type); 29 std::string path = icons.Get(size, match_type);
26 if (path.empty()) 30 if (path.empty())
27 return ExtensionResource(); 31 return ExtensionResource();
28 32
29 return extension->GetResource(path); 33 return extension->GetResource(path);
30 } 34 }
31 35
32 } // namespace 36 } // namespace
33 37
34 namespace extensions { 38 namespace extensions {
35 39
36 //////////////////////////////////////////////////////////////////////////////// 40 ////////////////////////////////////////////////////////////////////////////////
37 // ExtensionIconImage::Source 41 // ExtensionIconImage::Source
38 42
39 class IconImage::Source : public gfx::ImageSkiaSource { 43 class IconImage::Source : public gfx::ImageSkiaSource {
40 public: 44 public:
41 explicit Source(IconImage* host); 45 explicit Source(IconImage* host, const gfx::ImageSkia& default_image);
42 virtual ~Source(); 46 virtual ~Source();
43 47
44 void ResetHost(); 48 void ResetHost();
45 49
46 private: 50 private:
47 // gfx::ImageSkiaSource overrides: 51 // gfx::ImageSkiaSource overrides:
48 virtual gfx::ImageSkiaRep GetImageForScale( 52 virtual gfx::ImageSkiaRep GetImageForScale(
49 ui::ScaleFactor scale_factor) OVERRIDE; 53 ui::ScaleFactor scale_factor) OVERRIDE;
50 54
51 IconImage* host_; 55 IconImage* host_;
52 56
57 // Image whose representation GetImageForScale will return if |host_| supplies
58 // a null representation.
59 gfx::ImageSkia default_image_;
60
53 DISALLOW_COPY_AND_ASSIGN(Source); 61 DISALLOW_COPY_AND_ASSIGN(Source);
54 }; 62 };
55 63
56 IconImage::Source::Source(IconImage* host) : host_(host) { 64 IconImage::Source::Source(IconImage* host, const gfx::ImageSkia& default_image)
65 : host_(host), default_image_(default_image) {
57 } 66 }
58 67
59 IconImage::Source::~Source() { 68 IconImage::Source::~Source() {
60 } 69 }
61 70
62 void IconImage::Source::ResetHost() { 71 void IconImage::Source::ResetHost() {
63 host_ = NULL; 72 host_ = NULL;
64 } 73 }
65 74
66 gfx::ImageSkiaRep IconImage::Source::GetImageForScale( 75 gfx::ImageSkiaRep IconImage::Source::GetImageForScale(
67 ui::ScaleFactor scale_factor) { 76 ui::ScaleFactor scale_factor) {
77 gfx::ImageSkiaRep representation;
68 if (host_) 78 if (host_)
69 host_->LoadImageForScaleFactor(scale_factor); 79 representation = host_->LoadImageForScaleFactor(scale_factor);
70 return gfx::ImageSkiaRep(); 80
81 if (!representation.is_null())
82 return representation;
83
84 representation = default_image_.GetRepresentation(scale_factor);
85 return gfx::ImageSkiaRep(representation.sk_bitmap(), scale_factor);
tbarzic 2012/08/22 02:39:10 pkotwicz: is it safe to assume that e.g. ui::Resou
pkotwicz 2012/08/22 23:17:55 If the default icon comes from the resource bundle
71 } 86 }
72 87
73 //////////////////////////////////////////////////////////////////////////////// 88 ////////////////////////////////////////////////////////////////////////////////
74 // ExtensionIconImage 89 // ExtensionIconImage
75 90
76 IconImage::IconImage( 91 IconImage::IconImage(
77 const Extension* extension, 92 const Extension* extension,
78 const ExtensionIconSet& icon_set, 93 const ExtensionIconSet& icon_set,
79 int resource_size_in_dip, 94 int resource_size_in_dip,
95 const gfx::ImageSkia& default_icon,
80 Observer* observer) 96 Observer* observer)
81 : extension_(extension), 97 : extension_(extension),
82 icon_set_(icon_set), 98 icon_set_(icon_set),
83 resource_size_in_dip_(resource_size_in_dip), 99 resource_size_in_dip_(resource_size_in_dip),
84 desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip), 100 desired_size_in_dip_(resource_size_in_dip, resource_size_in_dip),
85 observer_(observer), 101 observer_(observer),
86 source_(NULL), 102 source_(NULL),
87 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { 103 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)),
88 source_ = new Source(this); 104 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
105 source_ = new Source(this, default_icon);
89 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_); 106 image_skia_ = gfx::ImageSkia(source_, desired_size_in_dip_);
90 107
91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 108 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
92 content::NotificationService::AllSources()); 109 content::NotificationService::AllSources());
93 } 110 }
94 111
95 IconImage::~IconImage() { 112 IconImage::~IconImage() {
96 // |source_| could be NULL if resource does not exist. 113 // |source_| could be NULL if resource does not exist.
97 if (source_) 114 if (source_)
98 source_->ResetHost(); 115 source_->ResetHost();
99 } 116 }
100 117
101 void IconImage::LoadImageForScaleFactor(ui::ScaleFactor scale_factor) { 118 gfx::ImageSkiaRep IconImage::LoadImageForScaleFactor(
119 ui::ScaleFactor scale_factor) {
102 // Do nothing if extension is unloaded. 120 // Do nothing if extension is unloaded.
103 if (!extension_) 121 if (!extension_)
104 return; 122 return gfx::ImageSkiaRep();
105 123
106 const float scale = ui::GetScaleFactorScale(scale_factor); 124 const float scale = ui::GetScaleFactorScale(scale_factor);
107 const int resource_size_in_pixel = 125 const int resource_size_in_pixel =
108 static_cast<int>(resource_size_in_dip_ * scale); 126 static_cast<int>(resource_size_in_dip_ * scale);
109 127
110 ExtensionResource resource; 128 ExtensionResource resource;
111 // We try loading bigger image only if resource size is >= 32. 129 // We try loading bigger image only if resource size is >= 32.
112 if (resource_size_in_pixel >= kMatchBiggerTreshold) { 130 if (resource_size_in_pixel >= kMatchBiggerTreshold) {
113 resource = GetExtensionIconResource(extension_, icon_set_, 131 resource = GetExtensionIconResource(extension_, icon_set_,
114 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER); 132 resource_size_in_pixel, ExtensionIconSet::MATCH_BIGGER);
115 } 133 }
116 134
117 // If resource is not found by now, try matching smaller one. 135 // If resource is not found by now, try matching smaller one.
118 if (resource.empty()) { 136 if (resource.empty()) {
119 resource = GetExtensionIconResource(extension_, icon_set_, 137 resource = GetExtensionIconResource(extension_, icon_set_,
120 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER); 138 resource_size_in_pixel, ExtensionIconSet::MATCH_SMALLER);
121 } 139 }
122 140
123 // If there is no resource found, bail out and notify observer of failure. 141 // If there is no resource found, bail out and notify observer of failure.
124 if (resource.empty()) { 142 if (resource.empty()) {
125 if (observer_) 143 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
126 observer_->OnIconImageLoadFailed(this, scale_factor); 144 base::Bind(&IconImage::NotifyFail, weak_ptr_factory_.GetWeakPtr(),
127 return; 145 scale_factor));
146 return gfx::ImageSkiaRep();
128 } 147 }
129 148
130 int id = tracker_.next_id(); 149 int id = tracker_.next_id();
131 load_map_[id] = scale_factor; 150 load_map_[id].scale_factor = scale_factor;
151 load_map_[id].is_async = false;
132 152
133 std::vector<ImageLoadingTracker::ImageRepresentation> info_list; 153 std::vector<ImageLoadingTracker::ImageRepresentation> info_list;
134 info_list.push_back(ImageLoadingTracker::ImageRepresentation( 154 info_list.push_back(ImageLoadingTracker::ImageRepresentation(
135 resource, 155 resource,
136 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER, 156 ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER,
137 desired_size_in_dip_.Scale(scale), 157 desired_size_in_dip_.Scale(scale),
138 scale_factor)); 158 scale_factor));
139 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE); 159 tracker_.LoadImages(extension_, info_list, ImageLoadingTracker::DONT_CACHE);
160
161 // If requested image has been cached, we may already have the wanted
162 // representation.
pkotwicz 2012/08/22 23:17:55 Nit: return representation if we have cached it a
tbarzic 2012/08/23 00:06:09 are you sure, we could paint two different images
pkotwicz 2012/08/23 13:59:09 I should have been clearer. My nit was about chang
pkotwicz 2012/08/23 14:09:45 Ok, I see what you are doing. Sorry about that. Ch
tbarzic 2012/08/23 17:41:53 Done.
163 if (image_skia_.HasRepresentation(scale_factor))
164 return image_skia_.GetRepresentation(scale_factor);
165
166 // If we have not received |OnImageLoaded|, note that iamge load request is
167 // asynchronous.
pkotwicz 2012/08/22 23:17:55 Nit: I think it might be a bit more understandable
tbarzic 2012/08/23 00:06:09 Done.
168 if (load_map_.find(id) != load_map_.end())
169 load_map_[id].is_async = true;
170
171 return gfx::ImageSkiaRep();
140 } 172 }
141 173
142 void IconImage::OnImageLoaded(const gfx::Image& image, 174 void IconImage::OnImageLoaded(const gfx::Image& image,
143 const std::string& extension_id, 175 const std::string& extension_id,
144 int index) { 176 int index) {
145 LoadMap::iterator load_map_it = load_map_.find(index); 177 LoadMap::iterator load_map_it = load_map_.find(index);
146 DCHECK(load_map_it != load_map_.end()); 178 DCHECK(load_map_it != load_map_.end());
147 179
148 ui::ScaleFactor scale_factor = load_map_it->second; 180 ui::ScaleFactor scale_factor = load_map_it->second.scale_factor;
181 bool is_async = load_map_it->second.is_async;
149 182
150 load_map_.erase(load_map_it); 183 load_map_.erase(load_map_it);
151
152 if (image.IsEmpty()) { 184 if (image.IsEmpty()) {
153 // There waas an error loading the image. 185 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
154 if (observer_) 186 base::Bind(&IconImage::NotifyFail, weak_ptr_factory_.GetWeakPtr(),
155 observer_->OnIconImageLoadFailed(this, scale_factor); 187 scale_factor));
156 return; 188 return;
157 } 189 }
158 190
159 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor)); 191 DCHECK(image.ToImageSkia()->HasRepresentation(scale_factor));
160 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor); 192 gfx::ImageSkiaRep rep = image.ToImageSkia()->GetRepresentation(scale_factor);
161 DCHECK(!rep.is_null()); 193 DCHECK(!rep.is_null());
194
195 // Remove old representation if there is one.
196 image_skia_.RemoveRepresentation(rep.scale_factor());
162 image_skia_.AddRepresentation(rep); 197 image_skia_.AddRepresentation(rep);
163 198
199 // If |tracker_| called us synchronously the image did not really change from
200 // the observers perspective, since we still haven't returned the initial
201 // image representation.
202 if (is_async && observer_)
203 observer_->OnExtensionIconImageChanged(this);
204 }
205
206 void IconImage::NotifyFail(ui::ScaleFactor scale_factor) {
164 if (observer_) 207 if (observer_)
165 observer_->OnExtensionIconImageChanged(this); 208 observer_->OnIconImageLoadFailed(this, scale_factor);
166 } 209 }
167 210
168 void IconImage::Observe(int type, 211 void IconImage::Observe(int type,
169 const content::NotificationSource& source, 212 const content::NotificationSource& source,
170 const content::NotificationDetails& details) { 213 const content::NotificationDetails& details) {
171 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); 214 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED);
172 215
173 const Extension* extension = 216 const Extension* extension =
174 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; 217 content::Details<extensions::UnloadedExtensionInfo>(details)->extension;
175 218
176 if (extension_ == extension) 219 if (extension_ == extension)
177 extension_ = NULL; 220 extension_ = NULL;
178 } 221 }
179 222
180 } // namespace extensions 223 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698