| 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/path_service.h" | |
| 13 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "chrome/browser/extensions/image_loader.h" |
| 14 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 14 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/chrome_paths.h" | |
| 17 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_constants.h" | 17 #include "chrome/common/extensions/extension_constants.h" |
| 19 #include "chrome/common/extensions/extension_file_util.h" | 18 #include "chrome/common/extensions/extension_file_util.h" |
| 20 #include "chrome/common/extensions/extension_resource.h" | 19 #include "chrome/common/extensions/extension_resource.h" |
| 21 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 23 #include "grit/component_extension_resources_map.h" | |
| 24 #include "grit/theme_resources.h" | |
| 25 #include "skia/ext/image_operations.h" | 22 #include "skia/ext/image_operations.h" |
| 26 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
| 27 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
| 28 #include "ui/gfx/image/image.h" | 25 #include "ui/gfx/image/image.h" |
| 29 #include "ui/gfx/image/image_skia_rep.h" | 26 #include "ui/gfx/image/image_skia_rep.h" |
| 30 #include "webkit/glue/image_decoder.h" | 27 #include "webkit/glue/image_decoder.h" |
| 31 | 28 |
| 32 using content::BrowserThread; | 29 using content::BrowserThread; |
| 33 using extensions::Extension; | 30 using extensions::Extension; |
| 34 | 31 |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); | 298 OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); |
| 302 continue; | 299 continue; |
| 303 } | 300 } |
| 304 | 301 |
| 305 // Instruct the ImageLoader to load this on the File thread. LoadImage and | 302 // Instruct the ImageLoader to load this on the File thread. LoadImage and |
| 306 // LoadResource do not block. | 303 // LoadResource do not block. |
| 307 if (!loader_) | 304 if (!loader_) |
| 308 loader_ = new ImageLoader(this); | 305 loader_ = new ImageLoader(this); |
| 309 | 306 |
| 310 int resource_id = -1; | 307 int resource_id = -1; |
| 311 if (IsComponentExtensionResource(extension, it->resource.relative_path(), | 308 if (extensions::ImageLoader::IsComponentExtensionResource( |
| 312 &resource_id)) | 309 extension, it->resource.relative_path(), &resource_id)) |
| 313 loader_->LoadResource(*it, id, resource_id); | 310 loader_->LoadResource(*it, id, resource_id); |
| 314 else | 311 else |
| 315 loader_->LoadImage(*it, id); | 312 loader_->LoadImage(*it, id); |
| 316 } | 313 } |
| 317 } | 314 } |
| 318 | 315 |
| 319 bool ImageLoadingTracker::IsComponentExtensionResource( | |
| 320 const Extension* extension, | |
| 321 const FilePath& resource_path, | |
| 322 int* resource_id) { | |
| 323 static const GritResourceMap kExtraComponentExtensionResources[] = { | |
| 324 {"web_store/webstore_icon_128.png", IDR_WEBSTORE_ICON}, | |
| 325 {"web_store/webstore_icon_16.png", IDR_WEBSTORE_ICON_16}, | |
| 326 {"chrome_app/product_logo_128.png", IDR_PRODUCT_LOGO_128}, | |
| 327 {"chrome_app/product_logo_16.png", IDR_PRODUCT_LOGO_16}, | |
| 328 {"settings_app/settings_app_icon_128.png", IDR_SETTINGS_APP_ICON_128}, | |
| 329 {"settings_app/settings_app_icon_16.png", IDR_SETTINGS_APP_ICON_16}, | |
| 330 }; | |
| 331 static const size_t kExtraComponentExtensionResourcesSize = | |
| 332 arraysize(kExtraComponentExtensionResources); | |
| 333 | |
| 334 if (extension->location() != Extension::COMPONENT) | |
| 335 return false; | |
| 336 | |
| 337 FilePath directory_path = extension->path(); | |
| 338 FilePath resources_dir; | |
| 339 FilePath relative_path; | |
| 340 if (!PathService::Get(chrome::DIR_RESOURCES, &resources_dir) || | |
| 341 !resources_dir.AppendRelativePath(directory_path, &relative_path)) { | |
| 342 return false; | |
| 343 } | |
| 344 relative_path = relative_path.Append(resource_path); | |
| 345 relative_path = relative_path.NormalizePathSeparators(); | |
| 346 | |
| 347 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to | |
| 348 // covert to FilePaths all the time. This will be more useful as we add | |
| 349 // more resources. | |
| 350 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | |
| 351 FilePath resource_path = | |
| 352 FilePath().AppendASCII(kComponentExtensionResources[i].name); | |
| 353 resource_path = resource_path.NormalizePathSeparators(); | |
| 354 | |
| 355 if (relative_path == resource_path) { | |
| 356 *resource_id = kComponentExtensionResources[i].value; | |
| 357 return true; | |
| 358 } | |
| 359 } | |
| 360 for (size_t i = 0; i < kExtraComponentExtensionResourcesSize; ++i) { | |
| 361 FilePath resource_path = | |
| 362 FilePath().AppendASCII(kExtraComponentExtensionResources[i].name); | |
| 363 resource_path = resource_path.NormalizePathSeparators(); | |
| 364 | |
| 365 if (relative_path == resource_path) { | |
| 366 *resource_id = kExtraComponentExtensionResources[i].value; | |
| 367 return true; | |
| 368 } | |
| 369 } | |
| 370 return false; | |
| 371 } | |
| 372 | |
| 373 void ImageLoadingTracker::OnBitmapLoaded( | 316 void ImageLoadingTracker::OnBitmapLoaded( |
| 374 const SkBitmap* bitmap, | 317 const SkBitmap* bitmap, |
| 375 const ImageRepresentation& image_info, | 318 const ImageRepresentation& image_info, |
| 376 const gfx::Size& original_size, | 319 const gfx::Size& original_size, |
| 377 int id, | 320 int id, |
| 378 bool should_cache) { | 321 bool should_cache) { |
| 379 LoadMap::iterator load_map_it = load_map_.find(id); | 322 LoadMap::iterator load_map_it = load_map_.find(id); |
| 380 DCHECK(load_map_it != load_map_.end()); | 323 DCHECK(load_map_it != load_map_.end()); |
| 381 PendingLoadInfo* info = &load_map_it->second; | 324 PendingLoadInfo* info = &load_map_it->second; |
| 382 | 325 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 // Remove reference to this extension from all pending load entries. This | 369 // Remove reference to this extension from all pending load entries. This |
| 427 // ensures we don't attempt to cache the bitmap when the load completes. | 370 // ensures we don't attempt to cache the bitmap when the load completes. |
| 428 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 371 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| 429 PendingLoadInfo* info = &i->second; | 372 PendingLoadInfo* info = &i->second; |
| 430 if (info->extension == extension) { | 373 if (info->extension == extension) { |
| 431 info->extension = NULL; | 374 info->extension = NULL; |
| 432 info->cache = DONT_CACHE; | 375 info->cache = DONT_CACHE; |
| 433 } | 376 } |
| 434 } | 377 } |
| 435 } | 378 } |
| OLD | NEW |