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