| 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::ResizeCondition 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 ResizeCondition 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, | 130 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| 81 int id) { | |
| 82 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 if (ShouldResizeImageRepresentation(image_info.resize_method, |
| 121 if (decoded->width() > max_size.width() || | 166 original_size, |
| 122 decoded->height() > max_size.height()) { | 167 image_info.desired_size)) { |
| 123 // The bitmap is too big, re-sample. | |
| 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, |
| 174 true /* delete bitmap */); |
| 130 } | 175 } |
| 131 | 176 |
| 132 // Instructs the loader to load a resource on the File thread. | 177 // Instructs the loader to load a resource on the File thread. |
| 133 void LoadResource(const ExtensionResource& resource, | 178 void LoadResource(const ImageRepresentation& image_info, |
| 134 const gfx::Size& max_size, | |
| 135 int id, | 179 int id, |
| 136 int resource_id) { | 180 int resource_id) { |
| 137 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 181 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| 138 BrowserThread::PostTask( | 182 BrowserThread::PostTask( |
| 139 BrowserThread::FILE, FROM_HERE, | 183 BrowserThread::FILE, FROM_HERE, |
| 140 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, resource, | 184 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, image_info, |
| 141 max_size, id, resource_id)); | 185 id, resource_id)); |
| 142 } | 186 } |
| 143 | 187 |
| 144 void LoadResourceOnFileThread(const ExtensionResource& resource, | 188 void LoadResourceOnFileThread(const ImageRepresentation& image_info, |
| 145 const gfx::Size& max_size, | |
| 146 int id, | 189 int id, |
| 147 int resource_id) { | 190 int resource_id) { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 149 SkBitmap* image = ExtensionIconSource::LoadImageByResourceId( | 192 const SkBitmap* bitmap = ResourceBundle::GetSharedInstance().GetImageNamed( |
| 150 resource_id); | 193 resource_id).ToSkBitmap(); |
| 151 ReportBack(image, resource, max_size, id); | 194 ReportBack(bitmap, image_info, image_info.desired_size, id, |
| 195 false /* don't delete bitmap */); |
| 152 } | 196 } |
| 153 | 197 |
| 154 void ReportBack(SkBitmap* image, const ExtensionResource& resource, | 198 void ReportBack(const SkBitmap* bitmap, const ImageRepresentation& image_info, |
| 155 const gfx::Size& original_size, int id) { | 199 const gfx::Size& original_size, int id, bool delete_bitmap) { |
| 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 157 | 201 |
| 158 BrowserThread::PostTask( | 202 BrowserThread::PostTask( |
| 159 callback_thread_id_, FROM_HERE, | 203 callback_thread_id_, FROM_HERE, |
| 160 base::Bind(&ImageLoader::ReportOnUIThread, this, | 204 base::Bind(&ImageLoader::ReportOnCallingThread, this, |
| 161 image, resource, original_size, id)); | 205 bitmap, image_info, original_size, id, delete_bitmap)); |
| 162 } | 206 } |
| 163 | 207 |
| 164 void ReportOnUIThread(SkBitmap* image, const ExtensionResource& resource, | 208 void ReportOnCallingThread(const SkBitmap* bitmap, |
| 165 const gfx::Size& original_size, int id) { | 209 const ImageRepresentation& image_info, |
| 166 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 210 const gfx::Size& original_size, |
| 211 int id, |
| 212 bool delete_bitmap) { |
| 213 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| 167 | 214 |
| 168 if (tracker_) | 215 if (tracker_) |
| 169 tracker_->OnImageLoaded(image, resource, original_size, id, true); | 216 tracker_->OnBitmapLoaded(bitmap, image_info, original_size, id, true); |
| 170 | 217 |
| 171 delete image; | 218 if (bitmap && delete_bitmap) |
| 219 delete bitmap; |
| 172 } | 220 } |
| 173 | 221 |
| 174 private: | 222 private: |
| 175 friend class base::RefCountedThreadSafe<ImageLoader>; | 223 friend class base::RefCountedThreadSafe<ImageLoader>; |
| 176 ~ImageLoader() {} | 224 ~ImageLoader() {} |
| 177 | 225 |
| 178 // The tracker we are loading the image for. If NULL, it means the tracker is | 226 // The tracker we are loading the bitmap for. If NULL, it means the tracker is |
| 179 // no longer interested in the reply. | 227 // no longer interested in the reply. |
| 180 ImageLoadingTracker* tracker_; | 228 ImageLoadingTracker* tracker_; |
| 181 | 229 |
| 182 // The thread that we need to call back on to report that we are done. | 230 // The thread that we need to call back on to report that we are done. |
| 183 BrowserThread::ID callback_thread_id_; | 231 BrowserThread::ID callback_thread_id_; |
| 184 | 232 |
| 185 DISALLOW_COPY_AND_ASSIGN(ImageLoader); | 233 DISALLOW_COPY_AND_ASSIGN(ImageLoader); |
| 186 }; | 234 }; |
| 187 | 235 |
| 188 //////////////////////////////////////////////////////////////////////////////// | 236 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 10 matching lines...) Expand all Loading... |
| 199 // The loader is created lazily and is NULL if the tracker is destroyed before | 247 // The loader is created lazily and is NULL if the tracker is destroyed before |
| 200 // any valid image load tasks have been posted. | 248 // any valid image load tasks have been posted. |
| 201 if (loader_) | 249 if (loader_) |
| 202 loader_->StopTracking(); | 250 loader_->StopTracking(); |
| 203 } | 251 } |
| 204 | 252 |
| 205 void ImageLoadingTracker::LoadImage(const Extension* extension, | 253 void ImageLoadingTracker::LoadImage(const Extension* extension, |
| 206 const ExtensionResource& resource, | 254 const ExtensionResource& resource, |
| 207 const gfx::Size& max_size, | 255 const gfx::Size& max_size, |
| 208 CacheParam cache) { | 256 CacheParam cache) { |
| 209 std::vector<ImageInfo> info_list; | 257 std::vector<ImageRepresentation> info_list; |
| 210 info_list.push_back(ImageInfo(resource, max_size)); | 258 info_list.push_back(ImageRepresentation( |
| 259 resource, |
| 260 ImageRepresentation::RESIZE_WHEN_LARGER, |
| 261 max_size, |
| 262 ui::SCALE_FACTOR_100P)); |
| 211 LoadImages(extension, info_list, cache); | 263 LoadImages(extension, info_list, cache); |
| 212 } | 264 } |
| 213 | 265 |
| 214 void ImageLoadingTracker::LoadImages(const Extension* extension, | 266 void ImageLoadingTracker::LoadImages( |
| 215 const std::vector<ImageInfo>& info_list, | 267 const Extension* extension, |
| 216 CacheParam cache) { | 268 const std::vector<ImageRepresentation>& info_list, |
| 269 CacheParam cache) { |
| 217 PendingLoadInfo load_info; | 270 PendingLoadInfo load_info; |
| 218 load_info.extension = extension; | 271 load_info.extension = extension; |
| 219 load_info.cache = cache; | 272 load_info.cache = cache; |
| 220 load_info.extension_id = extension->id(); | 273 load_info.extension_id = extension->id(); |
| 221 load_info.pending_count = info_list.size(); | 274 load_info.pending_count = info_list.size(); |
| 222 int id = next_id_++; | 275 int id = next_id_++; |
| 223 load_map_[id] = load_info; | 276 load_map_[id] = load_info; |
| 224 | 277 |
| 225 for (std::vector<ImageInfo>::const_iterator it = info_list.begin(); | 278 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); |
| 226 it != info_list.end(); ++it) { | 279 it != info_list.end(); ++it) { |
| 280 int resource_id = -1; |
| 281 |
| 227 // Load resources for special component extensions. | 282 // Load resources for special component extensions. |
| 228 if (load_info.extension_id == extension_misc::kWebStoreAppId) { | 283 if (FindSpecialExtensionResourceId(load_info.extension_id, &resource_id)) { |
| 229 if (!loader_) | 284 if (!loader_) |
| 230 loader_ = new ImageLoader(this); | 285 loader_ = new ImageLoader(this); |
| 231 loader_->LoadResource(it->resource, it->max_size, id, IDR_WEBSTORE_ICON); | 286 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; | 287 continue; |
| 241 } | 288 } |
| 242 | 289 |
| 243 // If we don't have a path we don't need to do any further work, just | 290 // If we don't have a path we don't need to do any further work, just |
| 244 // respond back. | 291 // respond back. |
| 245 if (it->resource.relative_path().empty()) { | 292 if (it->resource.relative_path().empty()) { |
| 246 OnImageLoaded(NULL, it->resource, it->max_size, id, false); | 293 OnBitmapLoaded(NULL, *it, it->desired_size, id, false); |
| 247 continue; | 294 continue; |
| 248 } | 295 } |
| 249 | 296 |
| 250 DCHECK(extension->path() == it->resource.extension_root()); | 297 DCHECK(extension->path() == it->resource.extension_root()); |
| 251 | 298 |
| 252 // See if the extension has the image already. | 299 // See if the extension has the bitmap already. |
| 253 if (extension->HasCachedImage(it->resource, it->max_size)) { | 300 if (extension->HasCachedImage(it->resource, it->desired_size)) { |
| 254 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size); | 301 SkBitmap bitmap = extension->GetCachedImage(it->resource, |
| 255 OnImageLoaded(&image, it->resource, it->max_size, id, false); | 302 it->desired_size); |
| 303 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); |
| 256 continue; | 304 continue; |
| 257 } | 305 } |
| 258 | 306 |
| 259 // Instruct the ImageLoader to load this on the File thread. LoadImage and | 307 // Instruct the ImageLoader to load this on the File thread. LoadImage and |
| 260 // LoadResource do not block. | 308 // LoadResource do not block. |
| 261 if (!loader_) | 309 if (!loader_) |
| 262 loader_ = new ImageLoader(this); | 310 loader_ = new ImageLoader(this); |
| 263 | 311 |
| 264 int resource_id; | |
| 265 if (IsComponentExtensionResource(extension, it->resource, resource_id)) | 312 if (IsComponentExtensionResource(extension, it->resource, resource_id)) |
| 266 loader_->LoadResource(it->resource, it->max_size, id, resource_id); | 313 loader_->LoadResource(*it, id, resource_id); |
| 267 else | 314 else |
| 268 loader_->LoadImage(it->resource, it->max_size, id); | 315 loader_->LoadImage(*it, id); |
| 269 } | 316 } |
| 270 } | 317 } |
| 271 | 318 |
| 272 bool ImageLoadingTracker::IsComponentExtensionResource( | 319 bool ImageLoadingTracker::IsComponentExtensionResource( |
| 273 const Extension* extension, | 320 const Extension* extension, |
| 274 const ExtensionResource& resource, | 321 const ExtensionResource& resource, |
| 275 int& resource_id) const { | 322 int& resource_id) const { |
| 276 if (extension->location() != Extension::COMPONENT) | 323 if (extension->location() != Extension::COMPONENT) |
| 277 return false; | 324 return false; |
| 278 | 325 |
| 279 FilePath directory_path = extension->path(); | 326 FilePath directory_path = extension->path(); |
| 280 FilePath relative_path = directory_path.BaseName().Append( | 327 FilePath relative_path = directory_path.BaseName().Append( |
| 281 resource.relative_path()); | 328 resource.relative_path()); |
| 282 | 329 |
| 283 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | 330 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { |
| 284 FilePath resource_path = | 331 FilePath resource_path = |
| 285 FilePath().AppendASCII(kComponentExtensionResources[i].name); | 332 FilePath().AppendASCII(kComponentExtensionResources[i].name); |
| 286 resource_path = resource_path.NormalizePathSeparators(); | 333 resource_path = resource_path.NormalizePathSeparators(); |
| 287 | 334 |
| 288 if (relative_path == resource_path) { | 335 if (relative_path == resource_path) { |
| 289 resource_id = kComponentExtensionResources[i].value; | 336 resource_id = kComponentExtensionResources[i].value; |
| 290 return true; | 337 return true; |
| 291 } | 338 } |
| 292 } | 339 } |
| 293 return false; | 340 return false; |
| 294 } | 341 } |
| 295 | 342 |
| 296 void ImageLoadingTracker::OnImageLoaded( | 343 void ImageLoadingTracker::OnBitmapLoaded( |
| 297 SkBitmap* image, | 344 const SkBitmap* bitmap, |
| 298 const ExtensionResource& resource, | 345 const ImageRepresentation& image_info, |
| 299 const gfx::Size& original_size, | 346 const gfx::Size& original_size, |
| 300 int id, | 347 int id, |
| 301 bool should_cache) { | 348 bool should_cache) { |
| 302 LoadMap::iterator load_map_it = load_map_.find(id); | 349 LoadMap::iterator load_map_it = load_map_.find(id); |
| 303 DCHECK(load_map_it != load_map_.end()); | 350 DCHECK(load_map_it != load_map_.end()); |
| 304 PendingLoadInfo* info = &load_map_it->second; | 351 PendingLoadInfo* info = &load_map_it->second; |
| 305 | 352 |
| 306 // Save the pending results. | 353 // Save the pending results. |
| 307 DCHECK(info->pending_count > 0); | 354 DCHECK_GT(info->pending_count, 0u); |
| 308 info->pending_count--; | 355 info->pending_count--; |
| 309 if (image) | 356 if (bitmap) { |
| 310 info->bitmaps.push_back(*image); | 357 info->image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap, |
| 358 image_info.scale_factor)); |
| 359 } |
| 311 | 360 |
| 312 // Add to the extension's image cache if requested. | 361 // Add to the extension's bitmap cache if requested. |
| 313 DCHECK(info->cache != CACHE || info->extension); | 362 DCHECK(info->cache != CACHE || info->extension); |
| 314 if (should_cache && info->cache == CACHE && !resource.empty() && | 363 if (should_cache && info->cache == CACHE && !image_info.resource.empty() && |
| 315 !info->extension->HasCachedImage(resource, original_size)) { | 364 !info->extension->HasCachedImage(image_info.resource, original_size)) { |
| 316 info->extension->SetCachedImage(resource, image ? *image : SkBitmap(), | 365 info->extension->SetCachedImage(image_info.resource, |
| 366 bitmap ? *bitmap : SkBitmap(), |
| 317 original_size); | 367 original_size); |
| 318 } | 368 } |
| 319 | 369 |
| 320 // If all pending images are done then report back. | 370 // If all pending bitmaps are done then report back. |
| 321 if (info->pending_count == 0) { | 371 if (info->pending_count == 0) { |
| 322 gfx::Image image; | 372 gfx::Image image; |
| 323 std::string extension_id = info->extension_id; | 373 std::string extension_id = info->extension_id; |
| 324 | 374 |
| 325 if (info->bitmaps.size() > 0) { | 375 if (!info->image_skia.empty()) |
| 326 gfx::ImageSkia image_skia; | 376 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 | 377 |
| 336 load_map_.erase(load_map_it); | 378 load_map_.erase(load_map_it); |
| 337 | 379 |
| 338 // ImageLoadingTracker might be deleted after the callback so don't | 380 // ImageLoadingTracker might be deleted after the callback so don't do |
| 339 // anything after this statement. | 381 // anything after this statement. |
| 340 observer_->OnImageLoaded(image, extension_id, id); | 382 observer_->OnImageLoaded(image, extension_id, id); |
| 341 } | 383 } |
| 342 } | 384 } |
| 343 | 385 |
| 344 void ImageLoadingTracker::Observe(int type, | 386 void ImageLoadingTracker::Observe(int type, |
| 345 const content::NotificationSource& source, | 387 const content::NotificationSource& source, |
| 346 const content::NotificationDetails& details) { | 388 const content::NotificationDetails& details) { |
| 347 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); | 389 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 348 | 390 |
| 349 const Extension* extension = | 391 const Extension* extension = |
| 350 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 392 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 351 | 393 |
| 352 // Remove reference to this extension from all pending load entries. This | 394 // 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. | 395 // 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) { | 396 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| 355 PendingLoadInfo* info = &i->second; | 397 PendingLoadInfo* info = &i->second; |
| 356 if (info->extension == extension) { | 398 if (info->extension == extension) { |
| 357 info->extension = NULL; | 399 info->extension = NULL; |
| 358 info->cache = DONT_CACHE; | 400 info->cache = DONT_CACHE; |
| 359 } | 401 } |
| 360 } | 402 } |
| 361 } | 403 } |
| OLD | NEW |