OLD | NEW |
---|---|
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/image_loading_tracker.h" | 5 #include "chrome/browser/extensions/image_loading_tracker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | |
13 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
14 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
15 #include "chrome/common/extensions/extension_constants.h" | 14 #include "chrome/common/extensions/extension_constants.h" |
16 #include "chrome/common/extensions/extension_resource.h" | 15 #include "chrome/common/extensions/extension_resource.h" |
17 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
19 #include "grit/component_extension_resources_map.h" | 18 #include "grit/component_extension_resources_map.h" |
20 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
21 #include "skia/ext/image_operations.h" | 20 #include "skia/ext/image_operations.h" |
22 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
22 #include "ui/base/resource/resource_bundle.h" | |
23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
24 #include "ui/gfx/image/image_skia.h" | |
25 #include "ui/gfx/image/image_skia_rep.h" | 24 #include "ui/gfx/image/image_skia_rep.h" |
26 #include "webkit/glue/image_decoder.h" | 25 #include "webkit/glue/image_decoder.h" |
27 | 26 |
28 using content::BrowserThread; | 27 using content::BrowserThread; |
29 using extensions::Extension; | 28 using extensions::Extension; |
30 | 29 |
30 namespace { | |
31 | |
32 struct ComponentExtensionResource { | |
33 const char* extension_id; | |
34 const int resource_id; | |
35 }; | |
36 | |
37 const ComponentExtensionResource kSpecialComponentExtensionResources[] = { | |
38 { extension_misc::kWebStoreAppId, IDR_WEBSTORE_ICON }, | |
39 { extension_misc::kChromeAppId, IDR_PRODUCT_LOGO_128 }, | |
40 }; | |
41 | |
42 // Finds special component extension resource id for given extension id. | |
43 bool FindSpecialExtensionResourceId(const std::string& extension_id, | |
44 int* out_resource_id) { | |
45 for (size_t i = 0; i < arraysize(kSpecialComponentExtensionResources); ++i) { | |
46 if (extension_id == kSpecialComponentExtensionResources[i].extension_id) { | |
47 if (out_resource_id) | |
48 *out_resource_id = kSpecialComponentExtensionResources[i].resource_id; | |
49 return true; | |
50 } | |
51 } | |
52 | |
53 return false; | |
54 } | |
55 | |
56 bool ShouldResizeImageRepresentation( | |
57 ImageLoadingTracker::ImageRepresentation::ResizeMethod resize_method, | |
58 const gfx::Size& decoded_size, | |
59 const gfx::Size& desired_size) { | |
60 switch (resize_method) { | |
61 case ImageLoadingTracker::ImageRepresentation::ALWAYS_RESIZE: | |
62 return decoded_size != desired_size; | |
63 case ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER: | |
64 return decoded_size.width() > desired_size.width() || | |
65 decoded_size.height() > desired_size.height(); | |
66 default: | |
67 NOTREACHED(); | |
68 return false; | |
69 } | |
70 } | |
71 | |
72 } // namespace | |
73 | |
31 //////////////////////////////////////////////////////////////////////////////// | 74 //////////////////////////////////////////////////////////////////////////////// |
32 // ImageLoadingTracker::Observer | 75 // ImageLoadingTracker::Observer |
33 | 76 |
34 ImageLoadingTracker::Observer::~Observer() {} | 77 ImageLoadingTracker::Observer::~Observer() {} |
35 | 78 |
36 //////////////////////////////////////////////////////////////////////////////// | 79 //////////////////////////////////////////////////////////////////////////////// |
37 // ImageLoadingTracker::ImageInfo | 80 // ImageLoadingTracker::ImageRepresentation |
38 | 81 |
39 ImageLoadingTracker::ImageInfo::ImageInfo( | 82 ImageLoadingTracker::ImageRepresentation::ImageRepresentation( |
40 const ExtensionResource& resource, gfx::Size max_size) | 83 const ExtensionResource& resource, |
41 : resource(resource), max_size(max_size) { | 84 ResizeMethod resize_method, |
85 const gfx::Size& desired_size, | |
86 ui::ScaleFactor scale_factor) | |
87 : resource(resource), | |
88 resize_method(resize_method), | |
89 desired_size(desired_size), | |
90 scale_factor(scale_factor) { | |
42 } | 91 } |
43 | 92 |
44 ImageLoadingTracker::ImageInfo::~ImageInfo() { | 93 ImageLoadingTracker::ImageRepresentation::~ImageRepresentation() { |
45 } | 94 } |
46 | 95 |
47 //////////////////////////////////////////////////////////////////////////////// | 96 //////////////////////////////////////////////////////////////////////////////// |
48 // ImageLoadingTracker::PendingLoadInfo | 97 // ImageLoadingTracker::PendingLoadInfo |
49 | 98 |
50 ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo() | 99 ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo() |
51 : extension(NULL), | 100 : extension(NULL), |
52 cache(CACHE), | 101 cache(CACHE), |
53 pending_count(0) { | 102 pending_count(0) { |
54 } | 103 } |
55 | 104 |
56 ImageLoadingTracker::PendingLoadInfo::~PendingLoadInfo() {} | 105 ImageLoadingTracker::PendingLoadInfo::~PendingLoadInfo() {} |
57 | 106 |
58 //////////////////////////////////////////////////////////////////////////////// | 107 //////////////////////////////////////////////////////////////////////////////// |
59 // ImageLoadingTracker::ImageLoader | 108 // ImageLoadingTracker::ImageLoader |
60 | 109 |
61 // A RefCounted class for loading images on the File thread and reporting back | 110 // A RefCounted class for loading bitmaps/image reps on the File thread and |
62 // on the UI thread. | 111 // reporting back on the UI thread. |
63 class ImageLoadingTracker::ImageLoader | 112 class ImageLoadingTracker::ImageLoader |
64 : public base::RefCountedThreadSafe<ImageLoader> { | 113 : public base::RefCountedThreadSafe<ImageLoader> { |
65 public: | 114 public: |
66 explicit ImageLoader(ImageLoadingTracker* tracker) | 115 explicit ImageLoader(ImageLoadingTracker* tracker) |
67 : tracker_(tracker) { | 116 : tracker_(tracker) { |
68 CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_)); | 117 CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_)); |
69 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 118 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
70 } | 119 } |
71 | 120 |
72 // Lets this class know that the tracker is no longer interested in the | 121 // Lets this class know that the tracker is no longer interested in the |
73 // results. | 122 // results. |
74 void StopTracking() { | 123 void StopTracking() { |
75 tracker_ = NULL; | 124 tracker_ = NULL; |
76 } | 125 } |
77 | 126 |
78 // Instructs the loader to load a task on the File thread. | 127 // Instructs the loader to load a task on the File thread. |
79 void LoadImage(const ExtensionResource& resource, | 128 void LoadImage(const ImageRepresentation& image_info, int id) { |
80 const gfx::Size& max_size, | |
81 int id) { | |
82 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 129 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
83 BrowserThread::PostTask( | 130 BrowserThread::PostTask( |
84 BrowserThread::FILE, FROM_HERE, | 131 BrowserThread::FILE, FROM_HERE, |
85 base::Bind(&ImageLoader::LoadOnFileThread, this, resource, | 132 base::Bind(&ImageLoader::LoadOnFileThread, this, image_info, id)); |
86 max_size, id)); | |
87 } | 133 } |
88 | 134 |
89 void LoadOnFileThread(const ExtensionResource& resource, | 135 void LoadOnFileThread(const ImageRepresentation& image_info, int id) { |
90 const gfx::Size& max_size, | |
91 int id) { | |
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
93 | 137 |
94 // Read the file from disk. | 138 // Read the file from disk. |
95 std::string file_contents; | 139 std::string file_contents; |
96 FilePath path = resource.GetFilePath(); | 140 FilePath path = image_info.resource.GetFilePath(); |
97 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | 141 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
98 ReportBack(NULL, resource, gfx::Size(), id); | 142 ReportBack(NULL, image_info, gfx::Size(), id); |
99 return; | 143 return; |
100 } | 144 } |
101 | 145 |
102 // Decode the image using WebKit's image decoder. | 146 // Decode the bitmap using WebKit's image decoder. |
103 const unsigned char* data = | 147 const unsigned char* data = |
104 reinterpret_cast<const unsigned char*>(file_contents.data()); | 148 reinterpret_cast<const unsigned char*>(file_contents.data()); |
105 webkit_glue::ImageDecoder decoder; | 149 webkit_glue::ImageDecoder decoder; |
106 scoped_ptr<SkBitmap> decoded(new SkBitmap()); | 150 scoped_ptr<SkBitmap> decoded(new SkBitmap()); |
107 // Note: This class only decodes images from extension resources. Chrome | 151 // Note: This class only decodes bitmaps from extension resources. Chrome |
108 // doesn't (for security reasons) directly load extension resources provided | 152 // doesn't (for security reasons) directly load extension resources provided |
109 // by the extension author, but instead decodes them in a separate | 153 // by the extension author, but instead decodes them in a separate |
110 // locked-down utility process. Only if the decoding succeeds is the image | 154 // locked-down utility process. Only if the decoding succeeds is the image |
111 // saved from memory to disk and subsequently used in the Chrome UI. | 155 // saved from memory to disk and subsequently used in the Chrome UI. |
112 // Chrome is therefore decoding images here that were generated by Chrome. | 156 // Chrome is therefore decoding bitmaps here that were generated by Chrome. |
113 *decoded = decoder.Decode(data, file_contents.length()); | 157 *decoded = decoder.Decode(data, file_contents.length()); |
114 if (decoded->empty()) { | 158 if (decoded->empty()) { |
115 ReportBack(NULL, resource, gfx::Size(), id); | 159 ReportBack(NULL, image_info, gfx::Size(), id); |
116 return; // Unable to decode. | 160 return; // Unable to decode. |
117 } | 161 } |
118 | 162 |
119 gfx::Size original_size(decoded->width(), decoded->height()); | 163 gfx::Size original_size(decoded->width(), decoded->height()); |
120 | 164 |
121 if (decoded->width() > max_size.width() || | 165 if (ShouldResizeImageRepresentation(image_info.resize_method, |
122 decoded->height() > max_size.height()) { | 166 original_size, |
123 // The bitmap is too big, re-sample. | 167 image_info.desired_size)) { |
124 *decoded = skia::ImageOperations::Resize( | 168 *decoded = skia::ImageOperations::Resize( |
125 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, | 169 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, |
126 max_size.width(), max_size.height()); | 170 image_info.desired_size.width(), image_info.desired_size.height()); |
127 } | 171 } |
128 | 172 |
129 ReportBack(decoded.release(), resource, original_size, id); | 173 ReportBack(decoded.release(), image_info, original_size, id); |
130 } | 174 } |
131 | 175 |
132 // Instructs the loader to load a resource on the File thread. | 176 // Instructs the loader to load a resource on the File thread. |
133 void LoadResource(const ExtensionResource& resource, | 177 void LoadResource(const ImageRepresentation& image_info, |
134 const gfx::Size& max_size, | |
135 int id, | 178 int id, |
136 int resource_id) { | 179 int resource_id) { |
137 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 180 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
138 BrowserThread::PostTask( | 181 BrowserThread::PostTask( |
139 BrowserThread::FILE, FROM_HERE, | 182 BrowserThread::FILE, FROM_HERE, |
140 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, resource, | 183 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, image_info, |
141 max_size, id, resource_id)); | 184 id, resource_id)); |
142 } | 185 } |
143 | 186 |
144 void LoadResourceOnFileThread(const ExtensionResource& resource, | 187 void LoadResourceOnFileThread(const ImageRepresentation& image_info, |
145 const gfx::Size& max_size, | |
146 int id, | 188 int id, |
147 int resource_id) { | 189 int resource_id) { |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
149 SkBitmap* image = ExtensionIconSource::LoadImageByResourceId( | 191 SkBitmap* bitmap = |
150 resource_id); | 192 ResourceBundle::GetSharedInstance().GetBitmapNamed(resource_id); |
tbarzic
2012/08/06 18:37:43
This is causing a crash, so I'll back off on this
tbarzic
2012/08/06 21:37:12
nevermind.. just had to remove delete in ReportOnU
| |
151 ReportBack(image, resource, max_size, id); | 193 ReportBack(bitmap, image_info, image_info.desired_size, id); |
152 } | 194 } |
153 | 195 |
154 void ReportBack(SkBitmap* image, const ExtensionResource& resource, | 196 void ReportBack(SkBitmap* bitmap, const ImageRepresentation& image_info, |
155 const gfx::Size& original_size, int id) { | 197 const gfx::Size& original_size, int id) { |
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
157 | 199 |
158 BrowserThread::PostTask( | 200 BrowserThread::PostTask( |
159 callback_thread_id_, FROM_HERE, | 201 callback_thread_id_, FROM_HERE, |
160 base::Bind(&ImageLoader::ReportOnUIThread, this, | 202 base::Bind(&ImageLoader::ReportOnUIThread, this, |
161 image, resource, original_size, id)); | 203 bitmap, image_info, original_size, id)); |
162 } | 204 } |
163 | 205 |
164 void ReportOnUIThread(SkBitmap* image, const ExtensionResource& resource, | 206 void ReportOnUIThread(SkBitmap* bitmap, const ImageRepresentation& image_info, |
165 const gfx::Size& original_size, int id) { | 207 const gfx::Size& original_size, int id) { |
166 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 208 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
167 | 209 |
168 if (tracker_) | 210 if (tracker_) |
169 tracker_->OnImageLoaded(image, resource, original_size, id, true); | 211 tracker_->OnBitmapLoaded(bitmap, image_info, original_size, id, true); |
170 | 212 |
171 delete image; | 213 delete bitmap; |
172 } | 214 } |
173 | 215 |
174 private: | 216 private: |
175 friend class base::RefCountedThreadSafe<ImageLoader>; | 217 friend class base::RefCountedThreadSafe<ImageLoader>; |
176 ~ImageLoader() {} | 218 ~ImageLoader() {} |
177 | 219 |
178 // The tracker we are loading the image for. If NULL, it means the tracker is | 220 // The tracker we are loading the bitmap for. If NULL, it means the tracker is |
179 // no longer interested in the reply. | 221 // no longer interested in the reply. |
180 ImageLoadingTracker* tracker_; | 222 ImageLoadingTracker* tracker_; |
181 | 223 |
182 // The thread that we need to call back on to report that we are done. | 224 // The thread that we need to call back on to report that we are done. |
183 BrowserThread::ID callback_thread_id_; | 225 BrowserThread::ID callback_thread_id_; |
184 | 226 |
185 DISALLOW_COPY_AND_ASSIGN(ImageLoader); | 227 DISALLOW_COPY_AND_ASSIGN(ImageLoader); |
186 }; | 228 }; |
187 | 229 |
188 //////////////////////////////////////////////////////////////////////////////// | 230 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 10 matching lines...) Expand all Loading... | |
199 // The loader is created lazily and is NULL if the tracker is destroyed before | 241 // The loader is created lazily and is NULL if the tracker is destroyed before |
200 // any valid image load tasks have been posted. | 242 // any valid image load tasks have been posted. |
201 if (loader_) | 243 if (loader_) |
202 loader_->StopTracking(); | 244 loader_->StopTracking(); |
203 } | 245 } |
204 | 246 |
205 void ImageLoadingTracker::LoadImage(const Extension* extension, | 247 void ImageLoadingTracker::LoadImage(const Extension* extension, |
206 const ExtensionResource& resource, | 248 const ExtensionResource& resource, |
207 const gfx::Size& max_size, | 249 const gfx::Size& max_size, |
208 CacheParam cache) { | 250 CacheParam cache) { |
209 std::vector<ImageInfo> info_list; | 251 std::vector<ImageRepresentation> info_list; |
210 info_list.push_back(ImageInfo(resource, max_size)); | 252 info_list.push_back(ImageRepresentation( |
253 resource, | |
254 ImageRepresentation::RESIZE_WHEN_LARGER, | |
255 max_size, | |
256 ui::SCALE_FACTOR_100P)); | |
211 LoadImages(extension, info_list, cache); | 257 LoadImages(extension, info_list, cache); |
212 } | 258 } |
213 | 259 |
214 void ImageLoadingTracker::LoadImages(const Extension* extension, | 260 void ImageLoadingTracker::LoadImages( |
215 const std::vector<ImageInfo>& info_list, | 261 const Extension* extension, |
216 CacheParam cache) { | 262 const std::vector<ImageRepresentation>& info_list, |
263 CacheParam cache) { | |
217 PendingLoadInfo load_info; | 264 PendingLoadInfo load_info; |
218 load_info.extension = extension; | 265 load_info.extension = extension; |
219 load_info.cache = cache; | 266 load_info.cache = cache; |
220 load_info.extension_id = extension->id(); | 267 load_info.extension_id = extension->id(); |
221 load_info.pending_count = info_list.size(); | 268 load_info.pending_count = info_list.size(); |
222 int id = next_id_++; | 269 int id = next_id_++; |
223 load_map_[id] = load_info; | 270 load_map_[id] = load_info; |
224 | 271 |
225 for (std::vector<ImageInfo>::const_iterator it = info_list.begin(); | 272 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); |
226 it != info_list.end(); ++it) { | 273 it != info_list.end(); ++it) { |
274 int resource_id = -1; | |
275 | |
227 // Load resources for special component extensions. | 276 // Load resources for special component extensions. |
228 if (load_info.extension_id == extension_misc::kWebStoreAppId) { | 277 if (FindSpecialExtensionResourceId(load_info.extension_id, &resource_id)) { |
229 if (!loader_) | 278 if (!loader_) |
230 loader_ = new ImageLoader(this); | 279 loader_ = new ImageLoader(this); |
231 loader_->LoadResource(it->resource, it->max_size, id, IDR_WEBSTORE_ICON); | 280 loader_->LoadResource(*it, id, resource_id); |
232 continue; | |
233 } else if (load_info.extension_id == extension_misc::kChromeAppId) { | |
234 if (!loader_) | |
235 loader_ = new ImageLoader(this); | |
236 loader_->LoadResource(it->resource, | |
237 it->max_size, | |
238 id, | |
239 IDR_PRODUCT_LOGO_128); | |
240 continue; | 281 continue; |
241 } | 282 } |
242 | 283 |
243 // If we don't have a path we don't need to do any further work, just | 284 // If we don't have a path we don't need to do any further work, just |
244 // respond back. | 285 // respond back. |
245 if (it->resource.relative_path().empty()) { | 286 if (it->resource.relative_path().empty()) { |
246 OnImageLoaded(NULL, it->resource, it->max_size, id, false); | 287 OnBitmapLoaded(NULL, *it, it->desired_size, id, false); |
247 continue; | 288 continue; |
248 } | 289 } |
249 | 290 |
250 DCHECK(extension->path() == it->resource.extension_root()); | 291 DCHECK(extension->path() == it->resource.extension_root()); |
251 | 292 |
252 // See if the extension has the image already. | 293 // See if the extension has the bitmap already. |
253 if (extension->HasCachedImage(it->resource, it->max_size)) { | 294 if (extension->HasCachedImage(it->resource, it->desired_size)) { |
254 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size); | 295 SkBitmap bitmap = extension->GetCachedImage(it->resource, |
255 OnImageLoaded(&image, it->resource, it->max_size, id, false); | 296 it->desired_size); |
297 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); | |
256 continue; | 298 continue; |
257 } | 299 } |
258 | 300 |
259 // Instruct the ImageLoader to load this on the File thread. LoadImage and | 301 // Instruct the ImageLoader to load this on the File thread. LoadImage and |
260 // LoadResource do not block. | 302 // LoadResource do not block. |
261 if (!loader_) | 303 if (!loader_) |
262 loader_ = new ImageLoader(this); | 304 loader_ = new ImageLoader(this); |
263 | 305 |
264 int resource_id; | |
265 if (IsComponentExtensionResource(extension, it->resource, resource_id)) | 306 if (IsComponentExtensionResource(extension, it->resource, resource_id)) |
266 loader_->LoadResource(it->resource, it->max_size, id, resource_id); | 307 loader_->LoadResource(*it, id, resource_id); |
267 else | 308 else |
268 loader_->LoadImage(it->resource, it->max_size, id); | 309 loader_->LoadImage(*it, id); |
269 } | 310 } |
270 } | 311 } |
271 | 312 |
272 bool ImageLoadingTracker::IsComponentExtensionResource( | 313 bool ImageLoadingTracker::IsComponentExtensionResource( |
273 const Extension* extension, | 314 const Extension* extension, |
274 const ExtensionResource& resource, | 315 const ExtensionResource& resource, |
275 int& resource_id) const { | 316 int& resource_id) const { |
276 if (extension->location() != Extension::COMPONENT) | 317 if (extension->location() != Extension::COMPONENT) |
277 return false; | 318 return false; |
278 | 319 |
279 FilePath directory_path = extension->path(); | 320 FilePath directory_path = extension->path(); |
280 FilePath relative_path = directory_path.BaseName().Append( | 321 FilePath relative_path = directory_path.BaseName().Append( |
281 resource.relative_path()); | 322 resource.relative_path()); |
282 | 323 |
283 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | 324 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { |
284 FilePath resource_path = | 325 FilePath resource_path = |
285 FilePath().AppendASCII(kComponentExtensionResources[i].name); | 326 FilePath().AppendASCII(kComponentExtensionResources[i].name); |
286 resource_path = resource_path.NormalizePathSeparators(); | 327 resource_path = resource_path.NormalizePathSeparators(); |
287 | 328 |
288 if (relative_path == resource_path) { | 329 if (relative_path == resource_path) { |
289 resource_id = kComponentExtensionResources[i].value; | 330 resource_id = kComponentExtensionResources[i].value; |
290 return true; | 331 return true; |
291 } | 332 } |
292 } | 333 } |
293 return false; | 334 return false; |
294 } | 335 } |
295 | 336 |
296 void ImageLoadingTracker::OnImageLoaded( | 337 void ImageLoadingTracker::OnBitmapLoaded( |
297 SkBitmap* image, | 338 SkBitmap* bitmap, |
298 const ExtensionResource& resource, | 339 const ImageRepresentation& image_info, |
299 const gfx::Size& original_size, | 340 const gfx::Size& original_size, |
300 int id, | 341 int id, |
301 bool should_cache) { | 342 bool should_cache) { |
302 LoadMap::iterator load_map_it = load_map_.find(id); | 343 LoadMap::iterator load_map_it = load_map_.find(id); |
303 DCHECK(load_map_it != load_map_.end()); | 344 DCHECK(load_map_it != load_map_.end()); |
304 PendingLoadInfo* info = &load_map_it->second; | 345 PendingLoadInfo* info = &load_map_it->second; |
305 | 346 |
306 // Save the pending results. | 347 // Save the pending results. |
307 DCHECK(info->pending_count > 0); | 348 DCHECK_GT(info->pending_count, 0u); |
308 info->pending_count--; | 349 info->pending_count--; |
309 if (image) | 350 if (bitmap) { |
310 info->bitmaps.push_back(*image); | 351 info->image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap, |
352 image_info.scale_factor)); | |
353 } | |
311 | 354 |
312 // Add to the extension's image cache if requested. | 355 // Add to the extension's bitmap cache if requested. |
313 DCHECK(info->cache != CACHE || info->extension); | 356 DCHECK(info->cache != CACHE || info->extension); |
314 if (should_cache && info->cache == CACHE && | 357 if (should_cache && info->cache == CACHE && |
315 !info->extension->HasCachedImage(resource, original_size)) { | 358 !info->extension->HasCachedImage(image_info.resource, original_size)) { |
316 info->extension->SetCachedImage(resource, image ? *image : SkBitmap(), | 359 info->extension->SetCachedImage(image_info.resource, |
360 bitmap ? *bitmap : SkBitmap(), | |
317 original_size); | 361 original_size); |
318 } | 362 } |
319 | 363 |
320 // If all pending images are done then report back. | 364 // If all pending bitmaps are done then report back. |
321 if (info->pending_count == 0) { | 365 if (info->pending_count == 0) { |
322 gfx::Image image; | 366 gfx::Image image; |
323 std::string extension_id = info->extension_id; | 367 std::string extension_id = info->extension_id; |
324 | 368 |
325 if (info->bitmaps.size() > 0) { | 369 if (!info->image_skia.empty()) |
326 gfx::ImageSkia image_skia; | 370 image = gfx::Image(info->image_skia); |
327 for (std::vector<SkBitmap>::const_iterator it = info->bitmaps.begin(); | |
328 it != info->bitmaps.end(); ++it) { | |
329 // TODO(pkotwicz): Do something better but ONLY when DIP is enabled. | |
330 image_skia.AddRepresentation( | |
331 gfx::ImageSkiaRep(*it, ui::SCALE_FACTOR_100P)); | |
332 } | |
333 image = gfx::Image(image_skia); | |
334 } | |
335 | 371 |
336 load_map_.erase(load_map_it); | 372 load_map_.erase(load_map_it); |
337 | 373 |
338 // ImageLoadingTracker might be deleted after the callback so don't | 374 // ImageLoadingTracker might be deleted after the callback so don't do |
339 // anything after this statement. | 375 // anything after this statement. |
340 observer_->OnImageLoaded(image, extension_id, id); | 376 observer_->OnImageLoaded(image, extension_id, id); |
341 } | 377 } |
342 } | 378 } |
343 | 379 |
344 void ImageLoadingTracker::Observe(int type, | 380 void ImageLoadingTracker::Observe(int type, |
345 const content::NotificationSource& source, | 381 const content::NotificationSource& source, |
346 const content::NotificationDetails& details) { | 382 const content::NotificationDetails& details) { |
347 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); | 383 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); |
348 | 384 |
349 const Extension* extension = | 385 const Extension* extension = |
350 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 386 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
351 | 387 |
352 // Remove reference to this extension from all pending load entries. This | 388 // Remove reference to this extension from all pending load entries. This |
353 // ensures we don't attempt to cache the image when the load completes. | 389 // ensures we don't attempt to cache the bitmap when the load completes. |
354 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 390 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
355 PendingLoadInfo* info = &i->second; | 391 PendingLoadInfo* info = &i->second; |
356 if (info->extension == extension) { | 392 if (info->extension == extension) { |
357 info->extension = NULL; | 393 info->extension = NULL; |
358 info->cache = DONT_CACHE; | 394 info->cache = DONT_CACHE; |
359 } | 395 } |
360 } | 396 } |
361 } | 397 } |
OLD | NEW |