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