OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/image_loader.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/file_util.h" | |
9 #include "base/path_service.h" | |
10 #include "base/string_number_conversions.h" | |
11 #include "base/threading/sequenced_worker_pool.h" | |
12 #include "chrome/common/chrome_paths.h" | |
13 #include "chrome/common/extensions/extension.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "grit/component_extension_resources_map.h" | |
16 #include "grit/theme_resources.h" | |
17 #include "skia/ext/image_operations.h" | |
18 #include "ui/base/resource/resource_bundle.h" | |
19 #include "ui/gfx/image/image_skia.h" | |
20 #include "webkit/glue/image_decoder.h" | |
21 | |
22 using content::BrowserThread; | |
23 using extensions::Extension; | |
24 using extensions::ImageLoader; | |
25 | |
26 namespace { | |
27 | |
28 std::string SizeToString(const gfx::Size& max_size) { | |
xiyuan
2012/10/26 01:11:57
nit: seems no longer needed.
Marijn Kruisselbrink
2012/10/30 23:11:07
Done.
| |
29 return base::IntToString(max_size.width()) + "x" + | |
30 base::IntToString(max_size.height()); | |
31 } | |
32 | |
33 bool ShouldResizeImageRepresentation( | |
34 ImageLoader::ImageRepresentation::ResizeCondition resize_method, | |
35 const gfx::Size& decoded_size, | |
36 const gfx::Size& desired_size) { | |
37 switch (resize_method) { | |
38 case ImageLoader::ImageRepresentation::ALWAYS_RESIZE: | |
39 return decoded_size != desired_size; | |
40 case ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER: | |
41 return decoded_size.width() > desired_size.width() || | |
42 decoded_size.height() > desired_size.height(); | |
43 default: | |
44 NOTREACHED(); | |
45 return false; | |
46 } | |
47 } | |
48 | |
49 SkBitmap ResizeIfNeeded(const SkBitmap& bitmap, | |
50 const ImageLoader::ImageRepresentation& image_info) { | |
51 gfx::Size original_size(bitmap.width(), bitmap.height()); | |
52 if (ShouldResizeImageRepresentation(image_info.resize_condition, | |
53 original_size, | |
54 image_info.desired_size)) { | |
55 return skia::ImageOperations::Resize( | |
56 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | |
57 image_info.desired_size.width(), image_info.desired_size.height()); | |
58 } | |
59 | |
60 return bitmap; | |
61 } | |
62 | |
63 void LoadResourceOnUIThread(int resource_id, SkBitmap* bitmap) { | |
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
65 | |
66 gfx::ImageSkia image( | |
67 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); | |
68 image.MakeThreadSafe(); | |
69 *bitmap = *image.bitmap(); | |
70 } | |
71 | |
72 void LoadImageOnBlockingPool(const ImageLoader::ImageRepresentation& image_info, | |
73 SkBitmap* bitmap) { | |
74 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
75 | |
76 // Read the file from disk. | |
77 std::string file_contents; | |
78 FilePath path = image_info.resource.GetFilePath(); | |
79 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | |
80 return; | |
81 } | |
82 | |
83 // Decode the bitmap using WebKit's image decoder. | |
84 const unsigned char* data = | |
85 reinterpret_cast<const unsigned char*>(file_contents.data()); | |
86 webkit_glue::ImageDecoder decoder; | |
87 // Note: This class only decodes bitmaps from extension resources. Chrome | |
88 // doesn't (for security reasons) directly load extension resources provided | |
89 // by the extension author, but instead decodes them in a separate | |
90 // locked-down utility process. Only if the decoding succeeds is the image | |
91 // saved from memory to disk and subsequently used in the Chrome UI. | |
92 // Chrome is therefore decoding bitmaps here that were generated by Chrome. | |
93 *bitmap = decoder.Decode(data, file_contents.length()); | |
94 } | |
95 | |
96 } // namespace | |
97 | |
98 namespace extensions { | |
99 | |
100 //////////////////////////////////////////////////////////////////////////////// | |
101 // ImageLoader::ImageRepresentation | |
102 | |
103 ImageLoader::ImageRepresentation::ImageRepresentation( | |
104 const ExtensionResource& resource, | |
105 ResizeCondition resize_condition, | |
106 const gfx::Size& desired_size, | |
107 ui::ScaleFactor scale_factor) | |
108 : resource(resource), | |
109 resize_condition(resize_condition), | |
110 desired_size(desired_size), | |
111 scale_factor(scale_factor) { | |
112 } | |
113 | |
114 ImageLoader::ImageRepresentation::~ImageRepresentation() { | |
115 } | |
116 | |
117 //////////////////////////////////////////////////////////////////////////////// | |
118 // ImageLoader::LoadResult | |
119 | |
120 struct ImageLoader::LoadResult { | |
121 LoadResult(const SkBitmap& bitmap, | |
122 const gfx::Size& original_size, | |
123 const ImageRepresentation& image_representation); | |
124 ~LoadResult(); | |
125 | |
126 SkBitmap bitmap; | |
127 gfx::Size original_size; | |
128 ImageRepresentation image_representation; | |
129 }; | |
130 | |
131 ImageLoader::LoadResult::LoadResult( | |
132 const SkBitmap& bitmap, | |
133 const gfx::Size& original_size, | |
134 const ImageLoader::ImageRepresentation& image_representation) | |
135 : bitmap(bitmap), | |
136 original_size(original_size), | |
137 image_representation(image_representation) { | |
138 } | |
139 | |
140 ImageLoader::LoadResult::~LoadResult() { | |
141 } | |
142 | |
143 //////////////////////////////////////////////////////////////////////////////// | |
144 // ImageLoader | |
145 | |
146 ImageLoader::ImageLoader() { | |
147 } | |
148 | |
149 ImageLoader::~ImageLoader() { | |
150 } | |
151 | |
152 // static | |
153 bool ImageLoader::IsComponentExtensionResource(const Extension* extension, | |
154 const FilePath& resource_path, | |
155 int* resource_id) { | |
156 static const GritResourceMap kExtraComponentExtensionResources[] = { | |
157 {"web_store/webstore_icon_128.png", IDR_WEBSTORE_ICON}, | |
158 {"web_store/webstore_icon_16.png", IDR_WEBSTORE_ICON_16}, | |
159 {"chrome_app/product_logo_128.png", IDR_PRODUCT_LOGO_128}, | |
160 {"chrome_app/product_logo_16.png", IDR_PRODUCT_LOGO_16}, | |
161 {"settings_app/settings_app_icon_128.png", IDR_SETTINGS_APP_ICON_128}, | |
162 {"settings_app/settings_app_icon_16.png", IDR_SETTINGS_APP_ICON_16}, | |
163 }; | |
164 static const size_t kExtraComponentExtensionResourcesSize = | |
165 arraysize(kExtraComponentExtensionResources); | |
166 | |
167 if (extension->location() != Extension::COMPONENT) | |
168 return false; | |
169 | |
170 FilePath directory_path = extension->path(); | |
171 FilePath resources_dir; | |
172 FilePath relative_path; | |
173 if (!PathService::Get(chrome::DIR_RESOURCES, &resources_dir) || | |
174 !resources_dir.AppendRelativePath(directory_path, &relative_path)) { | |
175 return false; | |
176 } | |
177 relative_path = relative_path.Append(resource_path); | |
178 relative_path = relative_path.NormalizePathSeparators(); | |
179 | |
180 // TODO(tc): Make a map of FilePath -> resource ids so we don't have to | |
181 // covert to FilePaths all the time. This will be more useful as we add | |
182 // more resources. | |
183 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | |
184 FilePath resource_path = | |
185 FilePath().AppendASCII(kComponentExtensionResources[i].name); | |
186 resource_path = resource_path.NormalizePathSeparators(); | |
187 | |
188 if (relative_path == resource_path) { | |
189 *resource_id = kComponentExtensionResources[i].value; | |
190 return true; | |
191 } | |
192 } | |
193 for (size_t i = 0; i < kExtraComponentExtensionResourcesSize; ++i) { | |
194 FilePath resource_path = | |
195 FilePath().AppendASCII(kExtraComponentExtensionResources[i].name); | |
196 resource_path = resource_path.NormalizePathSeparators(); | |
197 | |
198 if (relative_path == resource_path) { | |
199 *resource_id = kExtraComponentExtensionResources[i].value; | |
200 return true; | |
201 } | |
202 } | |
203 return false; | |
204 } | |
205 | |
206 void ImageLoader::LoadImageAsync( | |
207 const Extension* extension, | |
208 const ExtensionResource& resource, | |
209 const gfx::Size& max_size, | |
210 const base::Callback<void(const gfx::Image&)>& callback) { | |
211 std::vector<ImageRepresentation> info_list; | |
212 info_list.push_back(ImageRepresentation( | |
213 resource, | |
214 ImageRepresentation::RESIZE_WHEN_LARGER, | |
215 max_size, | |
216 ui::SCALE_FACTOR_100P)); | |
217 LoadImagesAsync(extension, info_list, callback); | |
218 } | |
219 | |
220 void ImageLoader::LoadImagesAsync( | |
221 const Extension* extension, | |
222 const std::vector<ImageRepresentation>& info_list, | |
223 const base::Callback<void(const gfx::Image&)>& callback) { | |
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
225 | |
226 // Loading an image from the cache and loading resources have to happen | |
227 // on the UI thread. So do those two things first, and pass the rest of the | |
228 // work of as a blocking pool task. | |
229 | |
230 std::vector<SkBitmap> bitmaps; | |
231 bitmaps.resize(info_list.size()); | |
232 | |
233 int i = 0; | |
234 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); | |
235 it != info_list.end(); ++it, ++i) { | |
236 DCHECK(it->resource.relative_path().empty() || | |
237 extension->path() == it->resource.extension_root()); | |
238 | |
239 int resource_id; | |
240 if (IsComponentExtensionResource(extension, it->resource.relative_path(), | |
241 &resource_id)) { | |
242 LoadResourceOnUIThread(resource_id, &bitmaps[i]); | |
243 } | |
244 } | |
245 | |
246 DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
247 BrowserThread::PostBlockingPoolTask( | |
248 FROM_HERE, | |
249 base::Bind(&ImageLoader::LoadImagesOnBlockingPool, base::Unretained(this), | |
250 info_list, bitmaps, callback)); | |
251 } | |
252 | |
253 void ImageLoader::LoadImagesOnBlockingPool( | |
254 const std::vector<ImageRepresentation>& info_list, | |
255 const std::vector<SkBitmap>& bitmaps, | |
256 const base::Callback<void(const gfx::Image&)>& callback) { | |
257 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
258 | |
259 gfx::ImageSkia image_skia; | |
260 | |
261 std::vector<LoadResult> load_result; | |
262 | |
263 int i = 0; | |
264 for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); | |
265 it != info_list.end(); ++it, ++i) { | |
266 // If we don't have a path there isn't anything we can do, just skip it. | |
267 if (it->resource.relative_path().empty()) | |
268 continue; | |
269 | |
270 SkBitmap bitmap; | |
271 if (!bitmaps[i].isNull()) { | |
272 bitmap = bitmaps[i]; | |
273 } else { | |
274 LoadImageOnBlockingPool(*it, &bitmap); | |
275 } | |
276 | |
277 // If the image failed to load, skip it. | |
278 if (bitmap.isNull() || bitmap.empty()) | |
279 continue; | |
280 | |
281 gfx::Size original_size(bitmap.width(), bitmap.height()); | |
282 bitmap = ResizeIfNeeded(bitmap, *it); | |
283 | |
284 load_result.push_back(LoadResult(bitmap, original_size, *it)); | |
285 } | |
286 | |
287 gfx::Image image; | |
288 | |
289 if (!image_skia.isNull()) { | |
290 image_skia.MakeThreadSafe(); | |
291 image = gfx::Image(image_skia); | |
292 } | |
293 | |
294 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
295 base::Bind(&ImageLoader::ReplyBack, | |
296 base::Unretained(this), load_result, | |
297 callback)); | |
298 } | |
299 | |
300 void ImageLoader::ReplyBack( | |
301 const std::vector<LoadResult>& load_result, | |
302 const base::Callback<void(const gfx::Image&)>& callback) { | |
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
304 | |
305 gfx::ImageSkia image_skia; | |
306 | |
307 for (std::vector<LoadResult>::const_iterator it = load_result.begin(); | |
308 it != load_result.end(); ++it) { | |
309 const SkBitmap& bitmap = it->bitmap; | |
310 const ImageRepresentation& image_rep = it->image_representation; | |
311 | |
312 image_skia.AddRepresentation(gfx::ImageSkiaRep( | |
313 bitmap, image_rep.scale_factor)); | |
314 } | |
315 | |
316 gfx::Image image; | |
317 if (!image_skia.isNull()) { | |
318 image_skia.MakeThreadSafe(); | |
319 image = gfx::Image(image_skia); | |
320 } | |
321 | |
322 callback.Run(image); | |
323 } | |
324 | |
325 } // namespace extensions | |
OLD | NEW |