| 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 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 217 |
| 241 // The thread that we need to call back on to report that we are done. | 218 // The thread that we need to call back on to report that we are done. |
| 242 BrowserThread::ID callback_thread_id_; | 219 BrowserThread::ID callback_thread_id_; |
| 243 | 220 |
| 244 DISALLOW_COPY_AND_ASSIGN(ImageLoader); | 221 DISALLOW_COPY_AND_ASSIGN(ImageLoader); |
| 245 }; | 222 }; |
| 246 | 223 |
| 247 //////////////////////////////////////////////////////////////////////////////// | 224 //////////////////////////////////////////////////////////////////////////////// |
| 248 // ImageLoadingTracker | 225 // ImageLoadingTracker |
| 249 | 226 |
| 250 // static | |
| 251 bool ImageLoadingTracker::IsSpecialBundledExtensionId( | |
| 252 const std::string& extension_id) { | |
| 253 int resource_id = -1; | |
| 254 return FindSpecialExtensionResourceId(extension_id, &resource_id); | |
| 255 } | |
| 256 | |
| 257 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) | 227 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) |
| 258 : observer_(observer), | 228 : observer_(observer), |
| 259 next_id_(0) { | 229 next_id_(0) { |
| 260 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 230 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 261 content::NotificationService::AllSources()); | 231 content::NotificationService::AllSources()); |
| 262 } | 232 } |
| 263 | 233 |
| 264 ImageLoadingTracker::~ImageLoadingTracker() { | 234 ImageLoadingTracker::~ImageLoadingTracker() { |
| 265 // The loader is created lazily and is NULL if the tracker is destroyed before | 235 // The loader is created lazily and is NULL if the tracker is destroyed before |
| 266 // any valid image load tasks have been posted. | 236 // any valid image load tasks have been posted. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 288 PendingLoadInfo load_info; | 258 PendingLoadInfo load_info; |
| 289 load_info.extension = extension; | 259 load_info.extension = extension; |
| 290 load_info.cache = cache; | 260 load_info.cache = cache; |
| 291 load_info.extension_id = extension->id(); | 261 load_info.extension_id = extension->id(); |
| 292 load_info.pending_count = info_list.size(); | 262 load_info.pending_count = info_list.size(); |
| 293 int id = next_id_++; | 263 int id = next_id_++; |
| 294 load_map_[id] = load_info; | 264 load_map_[id] = load_info; |
| 295 | 265 |
| 296 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); | 266 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); |
| 297 it != info_list.end(); ++it) { | 267 it != info_list.end(); ++it) { |
| 298 int resource_id = -1; | |
| 299 | |
| 300 // Load resources for special component extensions. | |
| 301 if (FindSpecialExtensionResourceId(load_info.extension_id, &resource_id)) { | |
| 302 if (!loader_) | |
| 303 loader_ = new ImageLoader(this); | |
| 304 loader_->LoadResource(*it, id, resource_id); | |
| 305 continue; | |
| 306 } | |
| 307 | |
| 308 // 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 |
| 309 // respond back. | 269 // respond back. |
| 310 if (it->resource.relative_path().empty()) { | 270 if (it->resource.relative_path().empty()) { |
| 311 OnBitmapLoaded(NULL, *it, it->desired_size, id, false); | 271 OnBitmapLoaded(NULL, *it, it->desired_size, id, false); |
| 312 continue; | 272 continue; |
| 313 } | 273 } |
| 314 | 274 |
| 315 DCHECK(extension->path() == it->resource.extension_root()); | 275 DCHECK(extension->path() == it->resource.extension_root()); |
| 316 | 276 |
| 317 // See if the extension has the bitmap already. | 277 // See if the extension has the bitmap already. |
| 318 if (extension->HasCachedImage(it->resource, it->desired_size)) { | 278 if (extension->HasCachedImage(it->resource, it->desired_size)) { |
| 319 SkBitmap bitmap = extension->GetCachedImage(it->resource, | 279 SkBitmap bitmap = extension->GetCachedImage(it->resource, |
| 320 it->desired_size); | 280 it->desired_size); |
| 321 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); | 281 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); |
| 322 continue; | 282 continue; |
| 323 } | 283 } |
| 324 | 284 |
| 325 // Instruct the ImageLoader to load this on the File thread. LoadImage and | 285 // Instruct the ImageLoader to load this on the File thread. LoadImage and |
| 326 // LoadResource do not block. | 286 // LoadResource do not block. |
| 327 if (!loader_) | 287 if (!loader_) |
| 328 loader_ = new ImageLoader(this); | 288 loader_ = new ImageLoader(this); |
| 329 | 289 |
| 330 if (IsComponentExtensionResource(extension, it->resource, resource_id)) | 290 int resource_id = -1; |
| 291 if (IsComponentExtensionResource(extension, it->resource.relative_path(), |
| 292 &resource_id)) |
| 331 loader_->LoadResource(*it, id, resource_id); | 293 loader_->LoadResource(*it, id, resource_id); |
| 332 else | 294 else |
| 333 loader_->LoadImage(*it, id); | 295 loader_->LoadImage(*it, id); |
| 334 } | 296 } |
| 335 } | 297 } |
| 336 | 298 |
| 337 bool ImageLoadingTracker::IsComponentExtensionResource( | 299 bool ImageLoadingTracker::IsComponentExtensionResource( |
| 338 const Extension* extension, | 300 const Extension* extension, |
| 339 const ExtensionResource& resource, | 301 const FilePath& resource_path, |
| 340 int& resource_id) const { | 302 int* resource_id) { |
| 303 static const GritResourceMap kExtraComponentExtensionResources[] = { |
| 304 {"web_store/webstore_icon_128.png", IDR_WEBSTORE_ICON}, |
| 305 {"web_store/webstore_icon_16.png", IDR_WEBSTORE_ICON_16}, |
| 306 {"chrome_app/product_logo_128.png", IDR_PRODUCT_LOGO_128}, |
| 307 {"chrome_app/product_logo_16.png", IDR_PRODUCT_LOGO_16}, |
| 308 }; |
| 309 static const size_t kExtraComponentExtensionResourcesSize = |
| 310 arraysize(kExtraComponentExtensionResources); |
| 311 |
| 341 if (extension->location() != Extension::COMPONENT) | 312 if (extension->location() != Extension::COMPONENT) |
| 342 return false; | 313 return false; |
| 343 | 314 |
| 344 FilePath directory_path = extension->path(); | 315 FilePath directory_path = extension->path(); |
| 345 FilePath relative_path = directory_path.BaseName().Append( | 316 FilePath relative_path = directory_path.BaseName().Append(resource_path); |
| 346 resource.relative_path()); | 317 relative_path = relative_path.NormalizePathSeparators(); |
| 347 | 318 |
| 319 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to |
| 320 // covert to FilePaths all the time. This will be more useful as we add |
| 321 // more resources. |
| 348 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | 322 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { |
| 349 FilePath resource_path = | 323 FilePath resource_path = |
| 350 FilePath().AppendASCII(kComponentExtensionResources[i].name); | 324 FilePath().AppendASCII(kComponentExtensionResources[i].name); |
| 351 resource_path = resource_path.NormalizePathSeparators(); | 325 resource_path = resource_path.NormalizePathSeparators(); |
| 352 | 326 |
| 353 if (relative_path == resource_path) { | 327 if (relative_path == resource_path) { |
| 354 resource_id = kComponentExtensionResources[i].value; | 328 *resource_id = kComponentExtensionResources[i].value; |
| 355 return true; | 329 return true; |
| 356 } | 330 } |
| 357 } | 331 } |
| 332 for (size_t i = 0; i < kExtraComponentExtensionResourcesSize; ++i) { |
| 333 FilePath resource_path = |
| 334 FilePath().AppendASCII(kExtraComponentExtensionResources[i].name); |
| 335 resource_path = resource_path.NormalizePathSeparators(); |
| 336 |
| 337 if (relative_path == resource_path) { |
| 338 *resource_id = kExtraComponentExtensionResources[i].value; |
| 339 return true; |
| 340 } |
| 341 } |
| 358 return false; | 342 return false; |
| 359 } | 343 } |
| 360 | 344 |
| 361 void ImageLoadingTracker::OnBitmapLoaded( | 345 void ImageLoadingTracker::OnBitmapLoaded( |
| 362 const SkBitmap* bitmap, | 346 const SkBitmap* bitmap, |
| 363 const ImageRepresentation& image_info, | 347 const ImageRepresentation& image_info, |
| 364 const gfx::Size& original_size, | 348 const gfx::Size& original_size, |
| 365 int id, | 349 int id, |
| 366 bool should_cache) { | 350 bool should_cache) { |
| 367 LoadMap::iterator load_map_it = load_map_.find(id); | 351 LoadMap::iterator load_map_it = load_map_.find(id); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 // Remove reference to this extension from all pending load entries. This | 398 // Remove reference to this extension from all pending load entries. This |
| 415 // ensures we don't attempt to cache the bitmap when the load completes. | 399 // ensures we don't attempt to cache the bitmap when the load completes. |
| 416 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 400 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| 417 PendingLoadInfo* info = &i->second; | 401 PendingLoadInfo* info = &i->second; |
| 418 if (info->extension == extension) { | 402 if (info->extension == extension) { |
| 419 info->extension = NULL; | 403 info->extension = NULL; |
| 420 info->cache = DONT_CACHE; | 404 info->cache = DONT_CACHE; |
| 421 } | 405 } |
| 422 } | 406 } |
| 423 } | 407 } |
| OLD | NEW |