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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | |
10 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
11 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
12 #include "chrome/common/extensions/extension_constants.h" | |
13 #include "chrome/common/extensions/extension_resource.h" | 11 #include "chrome/common/extensions/extension_resource.h" |
14 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
16 #include "grit/component_extension_resources_map.h" | |
17 #include "grit/theme_resources.h" | |
18 #include "skia/ext/image_operations.h" | 14 #include "skia/ext/image_operations.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
20 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
21 #include "webkit/glue/image_decoder.h" | 17 #include "webkit/glue/image_decoder.h" |
22 | 18 |
23 using content::BrowserThread; | 19 using content::BrowserThread; |
24 | 20 |
25 //////////////////////////////////////////////////////////////////////////////// | 21 //////////////////////////////////////////////////////////////////////////////// |
26 // ImageLoadingTracker::Observer | 22 // ImageLoadingTracker::Observer |
27 | 23 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 decoded->height() > max_size.height()) { | 107 decoded->height() > max_size.height()) { |
112 // The bitmap is too big, re-sample. | 108 // The bitmap is too big, re-sample. |
113 *decoded = skia::ImageOperations::Resize( | 109 *decoded = skia::ImageOperations::Resize( |
114 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, | 110 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, |
115 max_size.width(), max_size.height()); | 111 max_size.width(), max_size.height()); |
116 } | 112 } |
117 | 113 |
118 ReportBack(decoded.release(), resource, original_size, id); | 114 ReportBack(decoded.release(), resource, original_size, id); |
119 } | 115 } |
120 | 116 |
121 // Instructs the loader to load a resource on the File thread. | |
122 void LoadResource(const ExtensionResource& resource, | |
123 const gfx::Size& max_size, | |
124 int id, | |
125 int resource_id) { | |
126 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
127 BrowserThread::PostTask( | |
128 BrowserThread::FILE, FROM_HERE, | |
129 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, resource, | |
130 max_size, id, resource_id)); | |
131 } | |
132 | |
133 void LoadResourceOnFileThread(const ExtensionResource& resource, | |
134 const gfx::Size& max_size, | |
135 int id, | |
136 int resource_id) { | |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
138 SkBitmap* image = ExtensionIconSource::LoadImageByResourceId( | |
139 resource_id); | |
140 ReportBack(image, resource, max_size, id); | |
141 } | |
142 | |
143 void ReportBack(SkBitmap* image, const ExtensionResource& resource, | 117 void ReportBack(SkBitmap* image, const ExtensionResource& resource, |
144 const gfx::Size& original_size, int id) { | 118 const gfx::Size& original_size, int id) { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
146 | 120 |
147 BrowserThread::PostTask( | 121 BrowserThread::PostTask( |
148 callback_thread_id_, FROM_HERE, | 122 callback_thread_id_, FROM_HERE, |
149 base::Bind(&ImageLoader::ReportOnUIThread, this, | 123 base::Bind(&ImageLoader::ReportOnUIThread, this, |
150 image, resource, original_size, id)); | 124 image, resource, original_size, id)); |
151 } | 125 } |
152 | 126 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 193 |
220 DCHECK(extension->path() == it->resource.extension_root()); | 194 DCHECK(extension->path() == it->resource.extension_root()); |
221 | 195 |
222 // See if the extension has the image already. | 196 // See if the extension has the image already. |
223 if (extension->HasCachedImage(it->resource, it->max_size)) { | 197 if (extension->HasCachedImage(it->resource, it->max_size)) { |
224 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size); | 198 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size); |
225 OnImageLoaded(&image, it->resource, it->max_size, id, false); | 199 OnImageLoaded(&image, it->resource, it->max_size, id, false); |
226 continue; | 200 continue; |
227 } | 201 } |
228 | 202 |
229 // Instruct the ImageLoader to load this on the File thread. LoadImage and | 203 // Instruct the ImageLoader to load this on the File thread. LoadImage does |
230 // LoadResource do not block. | 204 // not block. |
231 if (!loader_) | 205 if (!loader_) |
232 loader_ = new ImageLoader(this); | 206 loader_ = new ImageLoader(this); |
233 | 207 loader_->LoadImage(it->resource, it->max_size, id); |
234 // Load resources for WebStore component extension. | |
235 if (load_info.extension_id == extension_misc::kWebStoreAppId) { | |
236 loader_->LoadResource(it->resource, it->max_size, id, IDR_WEBSTORE_ICON); | |
237 continue; | |
238 } | |
239 | |
240 int resource_id; | |
241 if (IsComponentExtensionResource(extension, it->resource, resource_id)) | |
242 loader_->LoadResource(it->resource, it->max_size, id, resource_id); | |
243 else | |
244 loader_->LoadImage(it->resource, it->max_size, id); | |
245 } | 208 } |
246 } | 209 } |
247 | 210 |
248 bool ImageLoadingTracker::IsComponentExtensionResource( | |
249 const Extension* extension, | |
250 const ExtensionResource& resource, | |
251 int& resource_id) const { | |
252 if (extension->location() != Extension::COMPONENT) | |
253 return false; | |
254 | |
255 FilePath directory_path = extension->path(); | |
256 FilePath relative_path = directory_path.BaseName().Append( | |
257 resource.relative_path()); | |
258 | |
259 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | |
260 FilePath resource_path = | |
261 FilePath().AppendASCII(kComponentExtensionResources[i].name); | |
262 resource_path = resource_path.NormalizePathSeparators(); | |
263 | |
264 if (relative_path == resource_path) { | |
265 resource_id = kComponentExtensionResources[i].value; | |
266 return true; | |
267 } | |
268 } | |
269 return false; | |
270 } | |
271 | |
272 void ImageLoadingTracker::OnImageLoaded( | 211 void ImageLoadingTracker::OnImageLoaded( |
273 SkBitmap* image, | 212 SkBitmap* image, |
274 const ExtensionResource& resource, | 213 const ExtensionResource& resource, |
275 const gfx::Size& original_size, | 214 const gfx::Size& original_size, |
276 int id, | 215 int id, |
277 bool should_cache) { | 216 bool should_cache) { |
278 LoadMap::iterator load_map_it = load_map_.find(id); | 217 LoadMap::iterator load_map_it = load_map_.find(id); |
279 DCHECK(load_map_it != load_map_.end()); | 218 DCHECK(load_map_it != load_map_.end()); |
280 PendingLoadInfo* info = &load_map_it->second; | 219 PendingLoadInfo* info = &load_map_it->second; |
281 | 220 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 // Remove reference to this extension from all pending load entries. This | 266 // Remove reference to this extension from all pending load entries. This |
328 // ensures we don't attempt to cache the image when the load completes. | 267 // ensures we don't attempt to cache the image when the load completes. |
329 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 268 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
330 PendingLoadInfo* info = &i->second; | 269 PendingLoadInfo* info = &i->second; |
331 if (info->extension == extension) { | 270 if (info->extension == extension) { |
332 info->extension = NULL; | 271 info->extension = NULL; |
333 info->cache = DONT_CACHE; | 272 info->cache = DONT_CACHE; |
334 } | 273 } |
335 } | 274 } |
336 } | 275 } |
OLD | NEW |