Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: extensions/browser/image_loader.cc

Issue 334053003: Moves extension_icon_image and image_loader to extensions/browser (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: polish Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/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;
40 using extensions::ImageLoader; 27 using extensions::ImageLoader;
41 using extensions::Manifest; 28 using extensions::Manifest;
42 29
43 namespace { 30 namespace {
44 31
45 bool ShouldResizeImageRepresentation( 32 bool ShouldResizeImageRepresentation(
46 ImageLoader::ImageRepresentation::ResizeCondition resize_method, 33 ImageLoader::ImageRepresentation::ResizeCondition resize_method,
47 const gfx::Size& decoded_size, 34 const gfx::Size& decoded_size,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 reinterpret_cast<const unsigned char*>(file_contents.data()); 85 reinterpret_cast<const unsigned char*>(file_contents.data());
99 // Note: This class only decodes bitmaps from extension resources. Chrome 86 // Note: This class only decodes bitmaps from extension resources. Chrome
100 // doesn't (for security reasons) directly load extension resources provided 87 // doesn't (for security reasons) directly load extension resources provided
101 // by the extension author, but instead decodes them in a separate 88 // by the extension author, but instead decodes them in a separate
102 // locked-down utility process. Only if the decoding succeeds is the image 89 // 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. 90 // saved from memory to disk and subsequently used in the Chrome UI.
104 // Chrome is therefore decoding bitmaps here that were generated by Chrome. 91 // Chrome is therefore decoding bitmaps here that were generated by Chrome.
105 gfx::PNGCodec::Decode(data, file_contents.length(), bitmap); 92 gfx::PNGCodec::Decode(data, file_contents.length(), bitmap);
106 } 93 }
107 94
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( 95 std::vector<SkBitmap> LoadResourceBitmaps(
126 const Extension* extension, 96 const Extension* extension,
127 const std::vector<ImageLoader::ImageRepresentation>& info_list) { 97 const std::vector<ImageLoader::ImageRepresentation>& info_list) {
128 // Loading resources has to happen on the UI thread. So do this first, and 98 // 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. 99 // pass the rest of the work off as a blocking pool task.
130 std::vector<SkBitmap> bitmaps; 100 std::vector<SkBitmap> bitmaps;
131 bitmaps.resize(info_list.size()); 101 bitmaps.resize(info_list.size());
132 102
133 int i = 0; 103 int i = 0;
134 for (std::vector<ImageLoader::ImageRepresentation>::const_iterator 104 for (std::vector<ImageLoader::ImageRepresentation>::const_iterator
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } 211 }
242 212
243 ImageLoader::~ImageLoader() { 213 ImageLoader::~ImageLoader() {
244 } 214 }
245 215
246 // static 216 // static
247 ImageLoader* ImageLoader::Get(content::BrowserContext* context) { 217 ImageLoader* ImageLoader::Get(content::BrowserContext* context) {
248 return ImageLoaderFactory::GetForBrowserContext(context); 218 return ImageLoaderFactory::GetForBrowserContext(context);
249 } 219 }
250 220
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 221 // static
257 bool ImageLoader::IsComponentExtensionResource( 222 bool ImageLoader::IsComponentExtensionResource(
258 const base::FilePath& extension_path, 223 const base::FilePath& extension_path,
259 const base::FilePath& resource_path, 224 const base::FilePath& resource_path,
260 int* resource_id) { 225 int* resource_id) {
261 static const GritResourceMap kExtraComponentExtensionResources[] = { 226 return ComponentExtensionResourceManager::HasInstance() &&
262 {"web_store/webstore_icon_128.png", IDR_WEBSTORE_ICON}, 227 ComponentExtensionResourceManager::Get()->IsComponentExtensionResource(
263 {"web_store/webstore_icon_16.png", IDR_WEBSTORE_ICON_16}, 228 extension_path, resource_path, resource_id);
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 } 229 }
319 230
320 void ImageLoader::LoadImageAsync(const Extension* extension, 231 void ImageLoader::LoadImageAsync(const Extension* extension,
321 const ExtensionResource& resource, 232 const ExtensionResource& resource,
322 const gfx::Size& max_size, 233 const gfx::Size& max_size,
323 const ImageLoaderImageCallback& callback) { 234 const ImageLoaderImageCallback& callback) {
324 std::vector<ImageRepresentation> info_list; 235 std::vector<ImageRepresentation> info_list;
325 info_list.push_back(ImageRepresentation( 236 info_list.push_back(ImageRepresentation(
326 resource, 237 resource,
327 ImageRepresentation::RESIZE_WHEN_LARGER, 238 ImageRepresentation::RESIZE_WHEN_LARGER,
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 it != image_skia_map.end(); 326 it != image_skia_map.end();
416 ++it) { 327 ++it) {
417 it->second.MakeThreadSafe(); 328 it->second.MakeThreadSafe();
418 image_family.Add(it->second); 329 image_family.Add(it->second);
419 } 330 }
420 331
421 callback.Run(image_family); 332 callback.Run(image_family);
422 } 333 }
423 334
424 } // namespace extensions 335 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698