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