| 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_loader.h" | 5 #include "extensions/browser/image_loader.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/lazy_instance.h" | |
| 14 #include "base/path_service.h" | |
| 15 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "chrome/browser/extensions/image_loader_factory.h" | |
| 18 #include "chrome/common/chrome_paths.h" | |
| 19 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "extensions/browser/extensions_browser_client.h" |
| 17 #include "extensions/browser/image_loader_factory.h" |
| 20 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
| 21 #include "grit/chrome_unscaled_resources.h" | |
| 22 #include "grit/component_extension_resources_map.h" | |
| 23 #include "grit/theme_resources.h" | |
| 24 #include "skia/ext/image_operations.h" | 19 #include "skia/ext/image_operations.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/gfx/codec/png_codec.h" | 21 #include "ui/gfx/codec/png_codec.h" |
| 27 #include "ui/gfx/image/image_family.h" | 22 #include "ui/gfx/image/image_family.h" |
| 28 #include "ui/gfx/image/image_skia.h" | 23 #include "ui/gfx/image/image_skia.h" |
| 29 | 24 |
| 30 #if defined(OS_CHROMEOS) | |
| 31 #include "ui/file_manager/file_manager_resource_util.h" | |
| 32 #endif | |
| 33 | |
| 34 #if defined(USE_AURA) | |
| 35 #include "ui/keyboard/keyboard_util.h" | |
| 36 #endif | |
| 37 | |
| 38 using content::BrowserThread; | 25 using content::BrowserThread; |
| 39 using extensions::Extension; | 26 using extensions::Extension; |
| 27 using extensions::ExtensionsBrowserClient; |
| 40 using extensions::ImageLoader; | 28 using extensions::ImageLoader; |
| 41 using extensions::Manifest; | 29 using extensions::Manifest; |
| 42 | 30 |
| 43 namespace { | 31 namespace { |
| 44 | 32 |
| 45 bool ShouldResizeImageRepresentation( | 33 bool ShouldResizeImageRepresentation( |
| 46 ImageLoader::ImageRepresentation::ResizeCondition resize_method, | 34 ImageLoader::ImageRepresentation::ResizeCondition resize_method, |
| 47 const gfx::Size& decoded_size, | 35 const gfx::Size& decoded_size, |
| 48 const gfx::Size& desired_size) { | 36 const gfx::Size& desired_size) { |
| 49 switch (resize_method) { | 37 switch (resize_method) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 reinterpret_cast<const unsigned char*>(file_contents.data()); | 86 reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 99 // Note: This class only decodes bitmaps from extension resources. Chrome | 87 // Note: This class only decodes bitmaps from extension resources. Chrome |
| 100 // doesn't (for security reasons) directly load extension resources provided | 88 // doesn't (for security reasons) directly load extension resources provided |
| 101 // by the extension author, but instead decodes them in a separate | 89 // by the extension author, but instead decodes them in a separate |
| 102 // locked-down utility process. Only if the decoding succeeds is the image | 90 // locked-down utility process. Only if the decoding succeeds is the image |
| 103 // saved from memory to disk and subsequently used in the Chrome UI. | 91 // saved from memory to disk and subsequently used in the Chrome UI. |
| 104 // Chrome is therefore decoding bitmaps here that were generated by Chrome. | 92 // Chrome is therefore decoding bitmaps here that were generated by Chrome. |
| 105 gfx::PNGCodec::Decode(data, file_contents.length(), bitmap); | 93 gfx::PNGCodec::Decode(data, file_contents.length(), bitmap); |
| 106 } | 94 } |
| 107 | 95 |
| 108 // Add the resources from |entries| (there are |size| of them) to | |
| 109 // |path_to_resource_id| after normalizing separators. | |
| 110 void AddComponentResourceEntries( | |
| 111 std::map<base::FilePath, int>* path_to_resource_id, | |
| 112 const GritResourceMap* entries, | |
| 113 size_t size) { | |
| 114 for (size_t i = 0; i < size; ++i) { | |
| 115 base::FilePath resource_path = base::FilePath().AppendASCII( | |
| 116 entries[i].name); | |
| 117 resource_path = resource_path.NormalizePathSeparators(); | |
| 118 | |
| 119 DCHECK(path_to_resource_id->find(resource_path) == | |
| 120 path_to_resource_id->end()); | |
| 121 (*path_to_resource_id)[resource_path] = entries[i].value; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 std::vector<SkBitmap> LoadResourceBitmaps( | 96 std::vector<SkBitmap> LoadResourceBitmaps( |
| 126 const Extension* extension, | 97 const Extension* extension, |
| 127 const std::vector<ImageLoader::ImageRepresentation>& info_list) { | 98 const std::vector<ImageLoader::ImageRepresentation>& info_list) { |
| 128 // Loading resources has to happen on the UI thread. So do this first, and | 99 // Loading resources has to happen on the UI thread. So do this first, and |
| 129 // pass the rest of the work off as a blocking pool task. | 100 // pass the rest of the work off as a blocking pool task. |
| 130 std::vector<SkBitmap> bitmaps; | 101 std::vector<SkBitmap> bitmaps; |
| 131 bitmaps.resize(info_list.size()); | 102 bitmaps.resize(info_list.size()); |
| 132 | 103 |
| 133 int i = 0; | 104 int i = 0; |
| 134 for (std::vector<ImageLoader::ImageRepresentation>::const_iterator | 105 for (std::vector<ImageLoader::ImageRepresentation>::const_iterator |
| 135 it = info_list.begin(); | 106 it = info_list.begin(); |
| 136 it != info_list.end(); | 107 it != info_list.end(); |
| 137 ++it, ++i) { | 108 ++it, ++i) { |
| 138 DCHECK(it->resource.relative_path().empty() || | 109 DCHECK(it->resource.relative_path().empty() || |
| 139 extension->path() == it->resource.extension_root()); | 110 extension->path() == it->resource.extension_root()); |
| 140 | 111 |
| 141 int resource_id; | 112 int resource_id; |
| 142 if (extension->location() == Manifest::COMPONENT && | 113 if (extension->location() == Manifest::COMPONENT && |
| 143 ImageLoader::IsComponentExtensionResource( | 114 ExtensionsBrowserClient::Get()->IsComponentExtensionResource( |
| 144 extension->path(), it->resource.relative_path(), &resource_id)) { | 115 extension->path(), it->resource.relative_path(), &resource_id)) { |
| 145 LoadResourceOnUIThread(resource_id, &bitmaps[i]); | 116 LoadResourceOnUIThread(resource_id, &bitmaps[i]); |
| 146 } | 117 } |
| 147 } | 118 } |
| 148 return bitmaps; | 119 return bitmaps; |
| 149 } | 120 } |
| 150 | 121 |
| 151 } // namespace | 122 } // namespace |
| 152 | 123 |
| 153 namespace extensions { | 124 namespace extensions { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 } | 212 } |
| 242 | 213 |
| 243 ImageLoader::~ImageLoader() { | 214 ImageLoader::~ImageLoader() { |
| 244 } | 215 } |
| 245 | 216 |
| 246 // static | 217 // static |
| 247 ImageLoader* ImageLoader::Get(content::BrowserContext* context) { | 218 ImageLoader* ImageLoader::Get(content::BrowserContext* context) { |
| 248 return ImageLoaderFactory::GetForBrowserContext(context); | 219 return ImageLoaderFactory::GetForBrowserContext(context); |
| 249 } | 220 } |
| 250 | 221 |
| 251 // A map from a resource path to the resource ID. Used only by | |
| 252 // IsComponentExtensionResource below. | |
| 253 static base::LazyInstance<std::map<base::FilePath, int> > path_to_resource_id = | |
| 254 LAZY_INSTANCE_INITIALIZER; | |
| 255 | |
| 256 // static | |
| 257 bool ImageLoader::IsComponentExtensionResource( | |
| 258 const base::FilePath& extension_path, | |
| 259 const base::FilePath& resource_path, | |
| 260 int* resource_id) { | |
| 261 static const GritResourceMap kExtraComponentExtensionResources[] = { | |
| 262 {"web_store/webstore_icon_128.png", IDR_WEBSTORE_ICON}, | |
| 263 {"web_store/webstore_icon_16.png", IDR_WEBSTORE_ICON_16}, | |
| 264 {"chrome_app/product_logo_128.png", IDR_PRODUCT_LOGO_128}, | |
| 265 {"chrome_app/product_logo_16.png", IDR_PRODUCT_LOGO_16}, | |
| 266 #if defined(ENABLE_SETTINGS_APP) | |
| 267 {"settings_app/settings_app_icon_128.png", IDR_SETTINGS_APP_ICON_128}, | |
| 268 {"settings_app/settings_app_icon_16.png", IDR_SETTINGS_APP_ICON_16}, | |
| 269 {"settings_app/settings_app_icon_32.png", IDR_SETTINGS_APP_ICON_32}, | |
| 270 {"settings_app/settings_app_icon_48.png", IDR_SETTINGS_APP_ICON_48}, | |
| 271 #endif | |
| 272 }; | |
| 273 | |
| 274 if (path_to_resource_id.Get().empty()) { | |
| 275 AddComponentResourceEntries( | |
| 276 path_to_resource_id.Pointer(), | |
| 277 kComponentExtensionResources, | |
| 278 kComponentExtensionResourcesSize); | |
| 279 AddComponentResourceEntries( | |
| 280 path_to_resource_id.Pointer(), | |
| 281 kExtraComponentExtensionResources, | |
| 282 arraysize(kExtraComponentExtensionResources)); | |
| 283 #if defined(OS_CHROMEOS) | |
| 284 size_t file_manager_resource_size; | |
| 285 const GritResourceMap* file_manager_resources = | |
| 286 file_manager::GetFileManagerResources(&file_manager_resource_size); | |
| 287 AddComponentResourceEntries( | |
| 288 path_to_resource_id.Pointer(), | |
| 289 file_manager_resources, | |
| 290 file_manager_resource_size); | |
| 291 | |
| 292 size_t keyboard_resource_size; | |
| 293 const GritResourceMap* keyboard_resources = | |
| 294 keyboard::GetKeyboardExtensionResources(&keyboard_resource_size); | |
| 295 AddComponentResourceEntries( | |
| 296 path_to_resource_id.Pointer(), | |
| 297 keyboard_resources, | |
| 298 keyboard_resource_size); | |
| 299 #endif | |
| 300 } | |
| 301 | |
| 302 base::FilePath directory_path = extension_path; | |
| 303 base::FilePath resources_dir; | |
| 304 base::FilePath relative_path; | |
| 305 if (!PathService::Get(chrome::DIR_RESOURCES, &resources_dir) || | |
| 306 !resources_dir.AppendRelativePath(directory_path, &relative_path)) { | |
| 307 return false; | |
| 308 } | |
| 309 relative_path = relative_path.Append(resource_path); | |
| 310 relative_path = relative_path.NormalizePathSeparators(); | |
| 311 | |
| 312 std::map<base::FilePath, int>::const_iterator entry = | |
| 313 path_to_resource_id.Get().find(relative_path); | |
| 314 if (entry != path_to_resource_id.Get().end()) | |
| 315 *resource_id = entry->second; | |
| 316 | |
| 317 return entry != path_to_resource_id.Get().end(); | |
| 318 } | |
| 319 | |
| 320 void ImageLoader::LoadImageAsync(const Extension* extension, | 222 void ImageLoader::LoadImageAsync(const Extension* extension, |
| 321 const ExtensionResource& resource, | 223 const ExtensionResource& resource, |
| 322 const gfx::Size& max_size, | 224 const gfx::Size& max_size, |
| 323 const ImageLoaderImageCallback& callback) { | 225 const ImageLoaderImageCallback& callback) { |
| 324 std::vector<ImageRepresentation> info_list; | 226 std::vector<ImageRepresentation> info_list; |
| 325 info_list.push_back(ImageRepresentation( | 227 info_list.push_back(ImageRepresentation( |
| 326 resource, | 228 resource, |
| 327 ImageRepresentation::RESIZE_WHEN_LARGER, | 229 ImageRepresentation::RESIZE_WHEN_LARGER, |
| 328 max_size, | 230 max_size, |
| 329 ui::SCALE_FACTOR_100P)); | 231 ui::SCALE_FACTOR_100P)); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 it != image_skia_map.end(); | 317 it != image_skia_map.end(); |
| 416 ++it) { | 318 ++it) { |
| 417 it->second.MakeThreadSafe(); | 319 it->second.MakeThreadSafe(); |
| 418 image_family.Add(it->second); | 320 image_family.Add(it->second); |
| 419 } | 321 } |
| 420 | 322 |
| 421 callback.Run(image_family); | 323 callback.Run(image_family); |
| 422 } | 324 } |
| 423 | 325 |
| 424 } // namespace extensions | 326 } // namespace extensions |
| OLD | NEW |