| 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 "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 13 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" | 16 #include "chrome/common/extensions/extension_constants.h" |
| 17 #include "chrome/common/extensions/extension_file_util.h" |
| 17 #include "chrome/common/extensions/extension_resource.h" | 18 #include "chrome/common/extensions/extension_resource.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 20 #include "grit/component_extension_resources_map.h" | 21 #include "grit/component_extension_resources_map.h" |
| 21 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
| 22 #include "skia/ext/image_operations.h" | 23 #include "skia/ext/image_operations.h" |
| 23 #include "third_party/skia/include/core/SkBitmap.h" | 24 #include "third_party/skia/include/core/SkBitmap.h" |
| 24 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 25 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 26 #include "ui/gfx/image/image_skia_rep.h" | 27 #include "ui/gfx/image/image_skia_rep.h" |
| 27 #include "webkit/glue/image_decoder.h" | 28 #include "webkit/glue/image_decoder.h" |
| 28 | 29 |
| 29 using content::BrowserThread; | 30 using content::BrowserThread; |
| 30 using extensions::Extension; | 31 using extensions::Extension; |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 struct ComponentExtensionResource { | |
| 35 const char* extension_id; | |
| 36 const int resource_id; | |
| 37 }; | |
| 38 | |
| 39 const ComponentExtensionResource kSpecialComponentExtensionResources[] = { | |
| 40 { extension_misc::kWebStoreAppId, IDR_WEBSTORE_ICON }, | |
| 41 { extension_misc::kChromeAppId, IDR_PRODUCT_LOGO_128 }, | |
| 42 }; | |
| 43 | |
| 44 // Finds special component extension resource id for given extension id. | |
| 45 bool FindSpecialExtensionResourceId(const std::string& extension_id, | |
| 46 int* out_resource_id) { | |
| 47 for (size_t i = 0; i < arraysize(kSpecialComponentExtensionResources); ++i) { | |
| 48 if (extension_id == kSpecialComponentExtensionResources[i].extension_id) { | |
| 49 if (out_resource_id) | |
| 50 *out_resource_id = kSpecialComponentExtensionResources[i].resource_id; | |
| 51 return true; | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 bool ShouldResizeImageRepresentation( | 35 bool ShouldResizeImageRepresentation( |
| 59 ImageLoadingTracker::ImageRepresentation::ResizeCondition resize_method, | 36 ImageLoadingTracker::ImageRepresentation::ResizeCondition resize_method, |
| 60 const gfx::Size& decoded_size, | 37 const gfx::Size& decoded_size, |
| 61 const gfx::Size& desired_size) { | 38 const gfx::Size& desired_size) { |
| 62 switch (resize_method) { | 39 switch (resize_method) { |
| 63 case ImageLoadingTracker::ImageRepresentation::ALWAYS_RESIZE: | 40 case ImageLoadingTracker::ImageRepresentation::ALWAYS_RESIZE: |
| 64 return decoded_size != desired_size; | 41 return decoded_size != desired_size; |
| 65 case ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER: | 42 case ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER: |
| 66 return decoded_size.width() > desired_size.width() || | 43 return decoded_size.width() > desired_size.width() || |
| 67 decoded_size.height() > desired_size.height(); | 44 decoded_size.height() > desired_size.height(); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 | 216 |
| 240 // The thread that we need to call back on to report that we are done. | 217 // The thread that we need to call back on to report that we are done. |
| 241 BrowserThread::ID callback_thread_id_; | 218 BrowserThread::ID callback_thread_id_; |
| 242 | 219 |
| 243 DISALLOW_COPY_AND_ASSIGN(ImageLoader); | 220 DISALLOW_COPY_AND_ASSIGN(ImageLoader); |
| 244 }; | 221 }; |
| 245 | 222 |
| 246 //////////////////////////////////////////////////////////////////////////////// | 223 //////////////////////////////////////////////////////////////////////////////// |
| 247 // ImageLoadingTracker | 224 // ImageLoadingTracker |
| 248 | 225 |
| 249 // static | |
| 250 bool ImageLoadingTracker::IsSpecialBundledExtensionId( | |
| 251 const std::string& extension_id) { | |
| 252 int resource_id = -1; | |
| 253 return FindSpecialExtensionResourceId(extension_id, &resource_id); | |
| 254 } | |
| 255 | |
| 256 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) | 226 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) |
| 257 : observer_(observer), | 227 : observer_(observer), |
| 258 next_id_(0) { | 228 next_id_(0) { |
| 259 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 229 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 260 content::NotificationService::AllSources()); | 230 content::NotificationService::AllSources()); |
| 261 } | 231 } |
| 262 | 232 |
| 263 ImageLoadingTracker::~ImageLoadingTracker() { | 233 ImageLoadingTracker::~ImageLoadingTracker() { |
| 264 // The loader is created lazily and is NULL if the tracker is destroyed before | 234 // The loader is created lazily and is NULL if the tracker is destroyed before |
| 265 // any valid image load tasks have been posted. | 235 // any valid image load tasks have been posted. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 287 PendingLoadInfo load_info; | 257 PendingLoadInfo load_info; |
| 288 load_info.extension = extension; | 258 load_info.extension = extension; |
| 289 load_info.cache = cache; | 259 load_info.cache = cache; |
| 290 load_info.extension_id = extension->id(); | 260 load_info.extension_id = extension->id(); |
| 291 load_info.pending_count = info_list.size(); | 261 load_info.pending_count = info_list.size(); |
| 292 int id = next_id_++; | 262 int id = next_id_++; |
| 293 load_map_[id] = load_info; | 263 load_map_[id] = load_info; |
| 294 | 264 |
| 295 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); | 265 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); |
| 296 it != info_list.end(); ++it) { | 266 it != info_list.end(); ++it) { |
| 297 int resource_id = -1; | |
| 298 | |
| 299 // Load resources for special component extensions. | |
| 300 if (FindSpecialExtensionResourceId(load_info.extension_id, &resource_id)) { | |
| 301 if (!loader_) | |
| 302 loader_ = new ImageLoader(this); | |
| 303 loader_->LoadResource(*it, id, resource_id); | |
| 304 continue; | |
| 305 } | |
| 306 | |
| 307 // If we don't have a path we don't need to do any further work, just | 267 // If we don't have a path we don't need to do any further work, just |
| 308 // respond back. | 268 // respond back. |
| 309 if (it->resource.relative_path().empty()) { | 269 if (it->resource.relative_path().empty()) { |
| 310 OnBitmapLoaded(NULL, *it, it->desired_size, id, false); | 270 OnBitmapLoaded(NULL, *it, it->desired_size, id, false); |
| 311 continue; | 271 continue; |
| 312 } | 272 } |
| 313 | 273 |
| 314 DCHECK(extension->path() == it->resource.extension_root()); | 274 DCHECK(extension->path() == it->resource.extension_root()); |
| 315 | 275 |
| 316 // See if the extension has the bitmap already. | 276 // See if the extension has the bitmap already. |
| 317 if (extension->HasCachedImage(it->resource, it->desired_size)) { | 277 if (extension->HasCachedImage(it->resource, it->desired_size)) { |
| 318 SkBitmap bitmap = extension->GetCachedImage(it->resource, | 278 SkBitmap bitmap = extension->GetCachedImage(it->resource, |
| 319 it->desired_size); | 279 it->desired_size); |
| 320 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); | 280 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); |
| 321 continue; | 281 continue; |
| 322 } | 282 } |
| 323 | 283 |
| 324 // Instruct the ImageLoader to load this on the File thread. LoadImage and | 284 // Instruct the ImageLoader to load this on the File thread. LoadImage and |
| 325 // LoadResource do not block. | 285 // LoadResource do not block. |
| 326 if (!loader_) | 286 if (!loader_) |
| 327 loader_ = new ImageLoader(this); | 287 loader_ = new ImageLoader(this); |
| 328 | 288 |
| 329 if (IsComponentExtensionResource(extension, it->resource, resource_id)) | 289 int resource_id = -1; |
| 290 if (IsComponentExtensionResource(extension, it->resource, &resource_id)) |
| 330 loader_->LoadResource(*it, id, resource_id); | 291 loader_->LoadResource(*it, id, resource_id); |
| 331 else | 292 else |
| 332 loader_->LoadImage(*it, id); | 293 loader_->LoadImage(*it, id); |
| 333 } | 294 } |
| 334 } | 295 } |
| 335 | 296 |
| 336 bool ImageLoadingTracker::IsComponentExtensionResource( | 297 bool ImageLoadingTracker::IsComponentExtensionResource( |
| 337 const Extension* extension, | 298 const Extension* extension, |
| 338 const ExtensionResource& resource, | 299 const ExtensionResource& resource, |
| 339 int& resource_id) const { | 300 int* resource_id) const { |
| 340 if (extension->location() != Extension::COMPONENT) | 301 return extension_file_util::IsComponentExtensionResource(extension, |
| 341 return false; | 302 resource.relative_path(), resource_id); |
| 342 | |
| 343 FilePath directory_path = extension->path(); | |
| 344 FilePath relative_path = directory_path.BaseName().Append( | |
| 345 resource.relative_path()); | |
| 346 | |
| 347 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | |
| 348 FilePath resource_path = | |
| 349 FilePath().AppendASCII(kComponentExtensionResources[i].name); | |
| 350 resource_path = resource_path.NormalizePathSeparators(); | |
| 351 | |
| 352 if (relative_path == resource_path) { | |
| 353 resource_id = kComponentExtensionResources[i].value; | |
| 354 return true; | |
| 355 } | |
| 356 } | |
| 357 return false; | |
| 358 } | 303 } |
| 359 | 304 |
| 360 void ImageLoadingTracker::OnBitmapLoaded( | 305 void ImageLoadingTracker::OnBitmapLoaded( |
| 361 const SkBitmap* bitmap, | 306 const SkBitmap* bitmap, |
| 362 const ImageRepresentation& image_info, | 307 const ImageRepresentation& image_info, |
| 363 const gfx::Size& original_size, | 308 const gfx::Size& original_size, |
| 364 int id, | 309 int id, |
| 365 bool should_cache) { | 310 bool should_cache) { |
| 366 LoadMap::iterator load_map_it = load_map_.find(id); | 311 LoadMap::iterator load_map_it = load_map_.find(id); |
| 367 DCHECK(load_map_it != load_map_.end()); | 312 DCHECK(load_map_it != load_map_.end()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 // Remove reference to this extension from all pending load entries. This | 358 // Remove reference to this extension from all pending load entries. This |
| 414 // ensures we don't attempt to cache the bitmap when the load completes. | 359 // ensures we don't attempt to cache the bitmap when the load completes. |
| 415 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 360 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| 416 PendingLoadInfo* info = &i->second; | 361 PendingLoadInfo* info = &i->second; |
| 417 if (info->extension == extension) { | 362 if (info->extension == extension) { |
| 418 info->extension = NULL; | 363 info->extension = NULL; |
| 419 info->cache = DONT_CACHE; | 364 info->cache = DONT_CACHE; |
| 420 } | 365 } |
| 421 } | 366 } |
| 422 } | 367 } |
| OLD | NEW |