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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/extensions/extension_resource.h" | 11 #include "chrome/common/extensions/extension_resource.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
| 14 #include "skia/ext/image_operations.h" | 14 #include "skia/ext/image_operations.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/gfx/image/image.h" | |
| 16 #include "webkit/glue/image_decoder.h" | 17 #include "webkit/glue/image_decoder.h" |
| 17 | 18 |
| 18 using content::BrowserThread; | 19 using content::BrowserThread; |
| 19 | 20 |
| 21 //////////////////////////////////////////////////////////////////////////////// | |
| 22 // ImageLoadingTracker::Observer | |
| 23 | |
| 20 ImageLoadingTracker::Observer::~Observer() {} | 24 ImageLoadingTracker::Observer::~Observer() {} |
| 21 | 25 |
| 22 //////////////////////////////////////////////////////////////////////////////// | 26 //////////////////////////////////////////////////////////////////////////////// |
| 27 // ImageLoadingTracker::ImageInfo | |
| 28 | |
| 29 ImageLoadingTracker::ImageInfo::ImageInfo( | |
| 30 const ExtensionResource resource, gfx::Size max_size) | |
| 31 : resource(resource), max_size(max_size) { | |
| 32 } | |
| 33 | |
| 34 ImageLoadingTracker::ImageInfo::~ImageInfo() { | |
| 35 } | |
| 36 | |
| 37 //////////////////////////////////////////////////////////////////////////////// | |
| 38 // ImageLoadingTracker::PendingLoadInfo | |
| 39 | |
| 40 ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo() | |
| 41 : extension(NULL), | |
| 42 pending_count(0) { | |
| 43 } | |
| 44 | |
| 45 ImageLoadingTracker::PendingLoadInfo::~PendingLoadInfo() { | |
| 46 } | |
| 47 | |
| 48 //////////////////////////////////////////////////////////////////////////////// | |
| 23 // ImageLoadingTracker::ImageLoader | 49 // ImageLoadingTracker::ImageLoader |
| 24 | 50 |
| 25 // A RefCounted class for loading images on the File thread and reporting back | 51 // A RefCounted class for loading images on the File thread and reporting back |
| 26 // on the UI thread. | 52 // on the UI thread. |
| 27 class ImageLoadingTracker::ImageLoader | 53 class ImageLoadingTracker::ImageLoader |
| 28 : public base::RefCountedThreadSafe<ImageLoader> { | 54 : public base::RefCountedThreadSafe<ImageLoader> { |
| 29 public: | 55 public: |
| 30 explicit ImageLoader(ImageLoadingTracker* tracker) | 56 explicit ImageLoader(ImageLoadingTracker* tracker) |
| 31 : tracker_(tracker) { | 57 : tracker_(tracker) { |
| 32 CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_)); | 58 CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_)); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 // The loader is created lazily and is NULL if the tracker is destroyed before | 158 // The loader is created lazily and is NULL if the tracker is destroyed before |
| 133 // any valid image load tasks have been posted. | 159 // any valid image load tasks have been posted. |
| 134 if (loader_) | 160 if (loader_) |
| 135 loader_->StopTracking(); | 161 loader_->StopTracking(); |
| 136 } | 162 } |
| 137 | 163 |
| 138 void ImageLoadingTracker::LoadImage(const Extension* extension, | 164 void ImageLoadingTracker::LoadImage(const Extension* extension, |
| 139 const ExtensionResource& resource, | 165 const ExtensionResource& resource, |
| 140 const gfx::Size& max_size, | 166 const gfx::Size& max_size, |
| 141 CacheParam cache) { | 167 CacheParam cache) { |
| 142 // If we don't have a path we don't need to do any further work, just respond | 168 std::vector<ImageInfo> info_list; |
| 143 // back. | 169 info_list.push_back(ImageInfo(resource, max_size)); |
| 170 LoadImages(extension, info_list, cache); | |
| 171 } | |
| 172 | |
| 173 void ImageLoadingTracker::LoadImages(const Extension* extension, | |
| 174 const std::vector<ImageInfo>& info_list, | |
| 175 CacheParam cache) { | |
| 176 PendingLoadInfo load_info; | |
| 177 load_info.extension = extension; | |
| 178 load_info.cache = cache; | |
| 179 load_info.extension_id = extension->id(); | |
| 180 load_info.pending_count = info_list.size(); | |
| 144 int id = next_id_++; | 181 int id = next_id_++; |
| 145 if (resource.relative_path().empty()) { | 182 load_map_[id] = load_info; |
| 146 OnImageLoaded(NULL, resource, max_size, id); | 183 |
| 147 return; | 184 for (std::vector<ImageInfo>::const_iterator it = info_list.begin(); |
| 185 it != info_list.end(); ++it) { | |
| 186 // If we don't have a path we don't need to do any further work, just | |
| 187 // respond back. | |
| 188 if (it->resource.relative_path().empty()) { | |
| 189 OnImageLoaded(NULL, it->resource, it->max_size, id); | |
| 190 continue; | |
| 191 } | |
| 192 | |
| 193 DCHECK(extension->path() == it->resource.extension_root()); | |
| 194 | |
| 195 // See if the extension has the image already. | |
| 196 if (extension->HasCachedImage(it->resource, it->max_size)) { | |
| 197 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size); | |
| 198 OnImageLoaded(&image, it->resource, it->max_size, id); | |
| 199 continue; | |
| 200 } | |
| 201 | |
| 202 // Instruct the ImageLoader to load this on the File thread. LoadImage does | |
| 203 // not block. | |
| 204 if (!loader_) | |
| 205 loader_ = new ImageLoader(this); | |
| 206 loader_->LoadImage(it->resource, it->max_size, id); | |
| 148 } | 207 } |
| 149 | |
| 150 DCHECK(extension->path() == resource.extension_root()); | |
| 151 | |
| 152 // See if the extension has the image already. | |
| 153 if (extension->HasCachedImage(resource, max_size)) { | |
| 154 SkBitmap image = extension->GetCachedImage(resource, max_size); | |
| 155 OnImageLoaded(&image, resource, max_size, id); | |
| 156 return; | |
| 157 } | |
| 158 | |
| 159 if (cache == CACHE) | |
| 160 load_map_[id] = extension; | |
| 161 | |
| 162 // Instruct the ImageLoader to load this on the File thread. LoadImage does | |
| 163 // not block. | |
| 164 if (!loader_) | |
| 165 loader_ = new ImageLoader(this); | |
| 166 loader_->LoadImage(resource, max_size, id); | |
| 167 } | 208 } |
| 168 | 209 |
| 169 void ImageLoadingTracker::OnImageLoaded( | 210 void ImageLoadingTracker::OnImageLoaded( |
| 170 SkBitmap* image, | 211 SkBitmap* image, |
| 171 const ExtensionResource& resource, | 212 const ExtensionResource& resource, |
| 172 const gfx::Size& original_size, | 213 const gfx::Size& original_size, |
| 173 int id) { | 214 int id) { |
| 174 LoadMap::iterator i = load_map_.find(id); | 215 LoadMap::iterator it = load_map_.find(id); |
| 175 if (i != load_map_.end()) { | 216 DCHECK(it != load_map_.end()); |
|
Robert Sesek
2012/03/05 15:18:32
Can't remember if it works for stl iterators, but
| |
| 176 i->second->SetCachedImage(resource, image ? *image : SkBitmap(), | 217 |
| 177 original_size); | 218 PendingLoadInfo* info = &it->second; |
| 178 load_map_.erase(i); | 219 |
| 220 // Save the pending results. | |
| 221 DCHECK(info->pending_count > 0); | |
|
Robert Sesek
2012/03/05 15:18:32
DCHECK_GT
| |
| 222 info->pending_count--; | |
| 223 if (image) | |
| 224 info->bitmaps.push_back(*image); | |
| 225 | |
| 226 // Add to the extension's image cache if requested. | |
| 227 DCHECK(info->cache != CACHE || info->extension); | |
| 228 if (info->cache == CACHE && | |
| 229 !info->extension->HasCachedImage(resource, original_size)) { | |
| 230 info->extension->SetCachedImage(resource, image ? *image : SkBitmap(), | |
| 231 original_size); | |
| 179 } | 232 } |
| 180 | 233 |
| 181 observer_->OnImageLoaded(image, resource, id); | 234 // If all pending images are done then report back. |
| 235 if (info->pending_count == 0) { | |
| 236 if (info->bitmaps.size() > 0) { | |
| 237 std::vector<const SkBitmap*> bitmaps; | |
| 238 for (std::vector<SkBitmap>::const_iterator it = info->bitmaps.begin(); | |
| 239 it != info->bitmaps.end(); ++it) { | |
| 240 // gfx::Image takes ownership of this bitmap. | |
| 241 bitmaps.push_back(new SkBitmap(*it)); | |
| 242 } | |
| 243 gfx::Image gfx_image(bitmaps); | |
| 244 observer_->OnImageLoaded(gfx_image, info->extension_id, id); | |
| 245 } else { | |
| 246 observer_->OnImageLoaded(gfx::Image(), info->extension_id, id); | |
| 247 } | |
| 248 load_map_.erase(it); | |
| 249 } | |
| 182 } | 250 } |
| 183 | 251 |
| 184 void ImageLoadingTracker::Observe(int type, | 252 void ImageLoadingTracker::Observe(int type, |
| 185 const content::NotificationSource& source, | 253 const content::NotificationSource& source, |
| 186 const content::NotificationDetails& details) { | 254 const content::NotificationDetails& details) { |
| 187 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); | 255 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 188 | 256 |
| 189 const Extension* extension = | 257 const Extension* extension = |
| 190 content::Details<UnloadedExtensionInfo>(details)->extension; | 258 content::Details<UnloadedExtensionInfo>(details)->extension; |
| 191 | 259 |
| 192 // Remove all entries in the load_map_ referencing the extension. This ensures | 260 // Remove reference to this extension from all pending load entries. This |
| 193 // we don't attempt to cache the image when the load completes. | 261 // ensures we don't attempt to cache the image when the load completes. |
| 194 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end();) { | 262 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| 195 if (i->second == extension) | 263 PendingLoadInfo* info = &i->second; |
| 196 load_map_.erase(i++); | 264 if (info->extension == extension) { |
| 197 else | 265 info->extension = NULL; |
| 198 ++i; | 266 info->cache = DONT_CACHE; |
| 267 } | |
| 199 } | 268 } |
| 200 } | 269 } |
| OLD | NEW |