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/component_extension_resource_manager.h" |
| 17 #include "extensions/browser/extensions_browser_client.h" |
| 18 #include "extensions/browser/image_loader_factory.h" |
20 #include "extensions/common/extension.h" | 19 #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" | 20 #include "skia/ext/image_operations.h" |
25 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
26 #include "ui/gfx/codec/png_codec.h" | 22 #include "ui/gfx/codec/png_codec.h" |
27 #include "ui/gfx/image/image_family.h" | 23 #include "ui/gfx/image/image_family.h" |
28 #include "ui/gfx/image/image_skia.h" | 24 #include "ui/gfx/image/image_skia.h" |
29 | 25 |
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; | 26 using content::BrowserThread; |
39 using extensions::Extension; | 27 using extensions::Extension; |
| 28 using extensions::ExtensionsBrowserClient; |
40 using extensions::ImageLoader; | 29 using extensions::ImageLoader; |
41 using extensions::Manifest; | 30 using extensions::Manifest; |
42 | 31 |
43 namespace { | 32 namespace { |
44 | 33 |
45 bool ShouldResizeImageRepresentation( | 34 bool ShouldResizeImageRepresentation( |
46 ImageLoader::ImageRepresentation::ResizeCondition resize_method, | 35 ImageLoader::ImageRepresentation::ResizeCondition resize_method, |
47 const gfx::Size& decoded_size, | 36 const gfx::Size& decoded_size, |
48 const gfx::Size& desired_size) { | 37 const gfx::Size& desired_size) { |
49 switch (resize_method) { | 38 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()); | 87 reinterpret_cast<const unsigned char*>(file_contents.data()); |
99 // Note: This class only decodes bitmaps from extension resources. Chrome | 88 // Note: This class only decodes bitmaps from extension resources. Chrome |
100 // doesn't (for security reasons) directly load extension resources provided | 89 // doesn't (for security reasons) directly load extension resources provided |
101 // by the extension author, but instead decodes them in a separate | 90 // by the extension author, but instead decodes them in a separate |
102 // locked-down utility process. Only if the decoding succeeds is the image | 91 // 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. | 92 // saved from memory to disk and subsequently used in the Chrome UI. |
104 // Chrome is therefore decoding bitmaps here that were generated by Chrome. | 93 // Chrome is therefore decoding bitmaps here that were generated by Chrome. |
105 gfx::PNGCodec::Decode(data, file_contents.length(), bitmap); | 94 gfx::PNGCodec::Decode(data, file_contents.length(), bitmap); |
106 } | 95 } |
107 | 96 |
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( | 97 std::vector<SkBitmap> LoadResourceBitmaps( |
126 const Extension* extension, | 98 const Extension* extension, |
127 const std::vector<ImageLoader::ImageRepresentation>& info_list) { | 99 const std::vector<ImageLoader::ImageRepresentation>& info_list) { |
128 // Loading resources has to happen on the UI thread. So do this first, and | 100 // 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. | 101 // pass the rest of the work off as a blocking pool task. |
130 std::vector<SkBitmap> bitmaps; | 102 std::vector<SkBitmap> bitmaps; |
131 bitmaps.resize(info_list.size()); | 103 bitmaps.resize(info_list.size()); |
132 | 104 |
133 int i = 0; | 105 int i = 0; |
134 for (std::vector<ImageLoader::ImageRepresentation>::const_iterator | 106 for (std::vector<ImageLoader::ImageRepresentation>::const_iterator |
135 it = info_list.begin(); | 107 it = info_list.begin(); |
136 it != info_list.end(); | 108 it != info_list.end(); |
137 ++it, ++i) { | 109 ++it, ++i) { |
138 DCHECK(it->resource.relative_path().empty() || | 110 DCHECK(it->resource.relative_path().empty() || |
139 extension->path() == it->resource.extension_root()); | 111 extension->path() == it->resource.extension_root()); |
140 | 112 |
141 int resource_id; | 113 int resource_id; |
142 if (extension->location() == Manifest::COMPONENT && | 114 if (extension->location() == Manifest::COMPONENT) { |
143 ImageLoader::IsComponentExtensionResource( | 115 extensions::ComponentExtensionResourceManager* manager = |
144 extension->path(), it->resource.relative_path(), &resource_id)) { | 116 extensions::ExtensionsBrowserClient::Get()-> |
145 LoadResourceOnUIThread(resource_id, &bitmaps[i]); | 117 GetComponentExtensionResourceManager(); |
| 118 if (manager && manager->IsComponentExtensionResource( |
| 119 extension->path(), it->resource.relative_path(), &resource_id)) { |
| 120 LoadResourceOnUIThread(resource_id, &bitmaps[i]); |
| 121 } |
146 } | 122 } |
147 } | 123 } |
148 return bitmaps; | 124 return bitmaps; |
149 } | 125 } |
150 | 126 |
151 } // namespace | 127 } // namespace |
152 | 128 |
153 namespace extensions { | 129 namespace extensions { |
154 | 130 |
155 //////////////////////////////////////////////////////////////////////////////// | 131 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 217 } |
242 | 218 |
243 ImageLoader::~ImageLoader() { | 219 ImageLoader::~ImageLoader() { |
244 } | 220 } |
245 | 221 |
246 // static | 222 // static |
247 ImageLoader* ImageLoader::Get(content::BrowserContext* context) { | 223 ImageLoader* ImageLoader::Get(content::BrowserContext* context) { |
248 return ImageLoaderFactory::GetForBrowserContext(context); | 224 return ImageLoaderFactory::GetForBrowserContext(context); |
249 } | 225 } |
250 | 226 |
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, | 227 void ImageLoader::LoadImageAsync(const Extension* extension, |
321 const ExtensionResource& resource, | 228 const ExtensionResource& resource, |
322 const gfx::Size& max_size, | 229 const gfx::Size& max_size, |
323 const ImageLoaderImageCallback& callback) { | 230 const ImageLoaderImageCallback& callback) { |
324 std::vector<ImageRepresentation> info_list; | 231 std::vector<ImageRepresentation> info_list; |
325 info_list.push_back(ImageRepresentation( | 232 info_list.push_back(ImageRepresentation( |
326 resource, | 233 resource, |
327 ImageRepresentation::RESIZE_WHEN_LARGER, | 234 ImageRepresentation::RESIZE_WHEN_LARGER, |
328 max_size, | 235 max_size, |
329 ui::SCALE_FACTOR_100P)); | 236 ui::SCALE_FACTOR_100P)); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 it != image_skia_map.end(); | 322 it != image_skia_map.end(); |
416 ++it) { | 323 ++it) { |
417 it->second.MakeThreadSafe(); | 324 it->second.MakeThreadSafe(); |
418 image_family.Add(it->second); | 325 image_family.Add(it->second); |
419 } | 326 } |
420 | 327 |
421 callback.Run(image_family); | 328 callback.Run(image_family); |
422 } | 329 } |
423 | 330 |
424 } // namespace extensions | 331 } // namespace extensions |
OLD | NEW |