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