Chromium Code Reviews| 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 12 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_constants.h" | 15 #include "chrome/common/extensions/extension_constants.h" |
| 16 #include "chrome/common/extensions/extension_icon_set.h" | |
| 16 #include "chrome/common/extensions/extension_resource.h" | 17 #include "chrome/common/extensions/extension_resource.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 19 #include "grit/component_extension_resources_map.h" | 20 #include "grit/component_extension_resources_map.h" |
| 20 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
| 21 #include "skia/ext/image_operations.h" | 22 #include "skia/ext/image_operations.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
| 24 #include "ui/gfx/image/image_skia.h" | |
| 25 #include "ui/gfx/image/image_skia_rep.h" | 25 #include "ui/gfx/image/image_skia_rep.h" |
| 26 #include "ui/gfx/screen.h" | |
| 26 #include "webkit/glue/image_decoder.h" | 27 #include "webkit/glue/image_decoder.h" |
| 27 | 28 |
| 28 using content::BrowserThread; | 29 using content::BrowserThread; |
| 29 using extensions::Extension; | 30 using extensions::Extension; |
| 30 | 31 |
| 31 //////////////////////////////////////////////////////////////////////////////// | 32 //////////////////////////////////////////////////////////////////////////////// |
| 32 // ImageLoadingTracker::Observer | 33 // ImageLoadingTracker::Observer |
| 33 | 34 |
| 34 ImageLoadingTracker::Observer::~Observer() {} | 35 ImageLoadingTracker::Observer::~Observer() {} |
| 35 | 36 |
| 36 //////////////////////////////////////////////////////////////////////////////// | 37 //////////////////////////////////////////////////////////////////////////////// |
| 37 // ImageLoadingTracker::ImageInfo | 38 // ImageLoadingTracker::ImageInfo |
| 38 | 39 |
| 39 ImageLoadingTracker::ImageInfo::ImageInfo( | 40 ImageLoadingTracker::ImageInfo::ImageInfo( |
| 40 const ExtensionResource& resource, gfx::Size max_size) | 41 const ExtensionResource& resource, |
| 41 : resource(resource), max_size(max_size) { | 42 const gfx::Size& max_size, |
| 43 ui::ScaleFactor scale_factor) | |
| 44 : resource(resource), | |
| 45 max_size(max_size), | |
| 46 scale_factor(scale_factor) { | |
| 42 } | 47 } |
| 43 | 48 |
| 44 ImageLoadingTracker::ImageInfo::~ImageInfo() { | 49 ImageLoadingTracker::ImageInfo::~ImageInfo() { |
| 45 } | 50 } |
| 46 | 51 |
| 47 //////////////////////////////////////////////////////////////////////////////// | 52 //////////////////////////////////////////////////////////////////////////////// |
| 48 // ImageLoadingTracker::PendingLoadInfo | 53 // ImageLoadingTracker::PendingLoadInfo |
| 49 | 54 |
| 50 ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo() | 55 ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo() |
| 51 : extension(NULL), | 56 : extension(NULL), |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 69 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 74 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 70 } | 75 } |
| 71 | 76 |
| 72 // Lets this class know that the tracker is no longer interested in the | 77 // Lets this class know that the tracker is no longer interested in the |
| 73 // results. | 78 // results. |
| 74 void StopTracking() { | 79 void StopTracking() { |
| 75 tracker_ = NULL; | 80 tracker_ = NULL; |
| 76 } | 81 } |
| 77 | 82 |
| 78 // Instructs the loader to load a task on the File thread. | 83 // Instructs the loader to load a task on the File thread. |
| 79 void LoadImage(const ExtensionResource& resource, | 84 void LoadImage(const ImageInfo& image_info, int id) { |
| 80 const gfx::Size& max_size, | |
| 81 int id) { | |
| 82 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 85 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 83 BrowserThread::PostTask( | 86 BrowserThread::PostTask( |
| 84 BrowserThread::FILE, FROM_HERE, | 87 BrowserThread::FILE, FROM_HERE, |
| 85 base::Bind(&ImageLoader::LoadOnFileThread, this, resource, | 88 base::Bind(&ImageLoader::LoadOnFileThread, this, image_info, id)); |
| 86 max_size, id)); | |
| 87 } | 89 } |
| 88 | 90 |
| 89 void LoadOnFileThread(const ExtensionResource& resource, | 91 void LoadOnFileThread(const ImageInfo& image_info, int id) { |
| 90 const gfx::Size& max_size, | |
| 91 int id) { | |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 93 | 93 |
| 94 // Read the file from disk. | 94 // Read the file from disk. |
| 95 std::string file_contents; | 95 std::string file_contents; |
| 96 FilePath path = resource.GetFilePath(); | 96 FilePath path = image_info.resource.GetFilePath(); |
| 97 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | 97 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
| 98 ReportBack(NULL, resource, gfx::Size(), id); | 98 ReportBack(NULL, image_info, gfx::Size(), id); |
| 99 return; | 99 return; |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Decode the image using WebKit's image decoder. | 102 // Decode the image using WebKit's image decoder. |
| 103 const unsigned char* data = | 103 const unsigned char* data = |
| 104 reinterpret_cast<const unsigned char*>(file_contents.data()); | 104 reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 105 webkit_glue::ImageDecoder decoder; | 105 webkit_glue::ImageDecoder decoder; |
| 106 scoped_ptr<SkBitmap> decoded(new SkBitmap()); | 106 scoped_ptr<SkBitmap> decoded(new SkBitmap()); |
| 107 // Note: This class only decodes images from extension resources. Chrome | 107 // Note: This class only decodes images from extension resources. Chrome |
| 108 // doesn't (for security reasons) directly load extension resources provided | 108 // doesn't (for security reasons) directly load extension resources provided |
| 109 // by the extension author, but instead decodes them in a separate | 109 // by the extension author, but instead decodes them in a separate |
| 110 // locked-down utility process. Only if the decoding succeeds is the image | 110 // locked-down utility process. Only if the decoding succeeds is the image |
| 111 // saved from memory to disk and subsequently used in the Chrome UI. | 111 // saved from memory to disk and subsequently used in the Chrome UI. |
| 112 // Chrome is therefore decoding images here that were generated by Chrome. | 112 // Chrome is therefore decoding images here that were generated by Chrome. |
| 113 *decoded = decoder.Decode(data, file_contents.length()); | 113 *decoded = decoder.Decode(data, file_contents.length()); |
| 114 if (decoded->empty()) { | 114 if (decoded->empty()) { |
| 115 ReportBack(NULL, resource, gfx::Size(), id); | 115 ReportBack(NULL, image_info, gfx::Size(), id); |
| 116 return; // Unable to decode. | 116 return; // Unable to decode. |
| 117 } | 117 } |
| 118 | 118 |
| 119 gfx::Size original_size(decoded->width(), decoded->height()); | 119 gfx::Size original_size(decoded->width(), decoded->height()); |
| 120 | 120 |
| 121 if (decoded->width() > max_size.width() || | 121 if (decoded->width() > image_info.max_size.width() || |
| 122 decoded->height() > max_size.height()) { | 122 decoded->height() > image_info.max_size.height()) { |
| 123 // The bitmap is too big, re-sample. | 123 // The bitmap is too big, re-sample. |
| 124 *decoded = skia::ImageOperations::Resize( | 124 *decoded = skia::ImageOperations::Resize( |
| 125 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, | 125 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, |
| 126 max_size.width(), max_size.height()); | 126 image_info.max_size.width(), image_info.max_size.height()); |
| 127 } | 127 } |
| 128 | 128 |
| 129 ReportBack(decoded.release(), resource, original_size, id); | 129 ReportBack(decoded.release(), image_info, original_size, id); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Instructs the loader to load a resource on the File thread. | 132 // Instructs the loader to load a resource on the File thread. |
| 133 void LoadResource(const ExtensionResource& resource, | 133 void LoadResource(const ImageInfo& image_info, int id, int resource_id) { |
| 134 const gfx::Size& max_size, | |
| 135 int id, | |
| 136 int resource_id) { | |
| 137 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 134 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 138 BrowserThread::PostTask( | 135 BrowserThread::PostTask( |
| 139 BrowserThread::FILE, FROM_HERE, | 136 BrowserThread::FILE, FROM_HERE, |
| 140 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, resource, | 137 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, image_info, |
| 141 max_size, id, resource_id)); | 138 id, resource_id)); |
| 142 } | 139 } |
| 143 | 140 |
| 144 void LoadResourceOnFileThread(const ExtensionResource& resource, | 141 void LoadResourceOnFileThread(const ImageInfo& image_info, |
| 145 const gfx::Size& max_size, | |
| 146 int id, | 142 int id, |
| 147 int resource_id) { | 143 int resource_id) { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 149 SkBitmap* image = ExtensionIconSource::LoadImageByResourceId( | 145 SkBitmap* image = ExtensionIconSource::LoadImageByResourceId( |
| 150 resource_id); | 146 resource_id); |
| 151 ReportBack(image, resource, max_size, id); | 147 ReportBack(image, image_info, image_info.max_size, id); |
| 152 } | 148 } |
| 153 | 149 |
| 154 void ReportBack(SkBitmap* image, const ExtensionResource& resource, | 150 void ReportBack(SkBitmap* image, const ImageInfo& image_info, |
| 155 const gfx::Size& original_size, int id) { | 151 const gfx::Size& original_size, int id) { |
| 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 157 | 153 |
| 158 BrowserThread::PostTask( | 154 BrowserThread::PostTask( |
| 159 callback_thread_id_, FROM_HERE, | 155 callback_thread_id_, FROM_HERE, |
| 160 base::Bind(&ImageLoader::ReportOnUIThread, this, | 156 base::Bind(&ImageLoader::ReportOnUIThread, this, |
| 161 image, resource, original_size, id)); | 157 image, image_info, original_size, id)); |
| 162 } | 158 } |
| 163 | 159 |
| 164 void ReportOnUIThread(SkBitmap* image, const ExtensionResource& resource, | 160 void ReportOnUIThread(SkBitmap* image, const ImageInfo& image_info, |
| 165 const gfx::Size& original_size, int id) { | 161 const gfx::Size& original_size, int id) { |
| 166 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 162 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 167 | 163 |
| 168 if (tracker_) | 164 if (tracker_) |
| 169 tracker_->OnImageLoaded(image, resource, original_size, id, true); | 165 tracker_->OnImageLoaded(image, image_info, original_size, id, true); |
| 170 | 166 |
| 171 delete image; | 167 delete image; |
| 172 } | 168 } |
| 173 | 169 |
| 174 private: | 170 private: |
| 175 friend class base::RefCountedThreadSafe<ImageLoader>; | 171 friend class base::RefCountedThreadSafe<ImageLoader>; |
| 176 ~ImageLoader() {} | 172 ~ImageLoader() {} |
| 177 | 173 |
| 178 // The tracker we are loading the image for. If NULL, it means the tracker is | 174 // The tracker we are loading the image for. If NULL, it means the tracker is |
| 179 // no longer interested in the reply. | 175 // no longer interested in the reply. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 200 // any valid image load tasks have been posted. | 196 // any valid image load tasks have been posted. |
| 201 if (loader_) | 197 if (loader_) |
| 202 loader_->StopTracking(); | 198 loader_->StopTracking(); |
| 203 } | 199 } |
| 204 | 200 |
| 205 void ImageLoadingTracker::LoadImage(const Extension* extension, | 201 void ImageLoadingTracker::LoadImage(const Extension* extension, |
| 206 const ExtensionResource& resource, | 202 const ExtensionResource& resource, |
| 207 const gfx::Size& max_size, | 203 const gfx::Size& max_size, |
| 208 CacheParam cache) { | 204 CacheParam cache) { |
| 209 std::vector<ImageInfo> info_list; | 205 std::vector<ImageInfo> info_list; |
| 210 info_list.push_back(ImageInfo(resource, max_size)); | 206 info_list.push_back(ImageInfo(resource, max_size, ui::SCALE_FACTOR_NONE)); |
| 211 LoadImages(extension, info_list, cache); | 207 LoadImages(extension, info_list, cache); |
| 212 } | 208 } |
| 213 | 209 |
| 210 void ImageLoadingTracker::LoadDIPImage( | |
| 211 const extensions::Extension* extension, | |
| 212 int preferred_dip_size, | |
| 213 gfx::Size max_dip_size, | |
| 214 CacheParam cache) { | |
| 215 if (!gfx::Screen::IsDIPEnabled()) { | |
| 216 LoadImage(extension, | |
| 217 extension->GetIconResource(preferred_dip_size, | |
| 218 ExtensionIconSet::MATCH_BIGGER), | |
| 219 max_dip_size, | |
| 220 cache); | |
| 221 return; | |
|
oshima
2012/07/10 23:51:06
is there an issue using ui::SCALE_FACTOR_100P when
xiyuan
2012/07/11 17:39:13
I assumed ui::SCALE_FACTOR_100P is the default whe
oshima
2012/07/11 18:08:41
I probably wasn't clear enough. I was asking if th
xiyuan
2012/07/11 21:40:40
I see your point. Let's follow ImageLoadingTracker
| |
| 222 } | |
| 223 | |
| 224 // Always use a hi-res resource when DIP is enabled so that it is safe to | |
| 225 // complete hand off the image to the caller. | |
| 226 const ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_200P; | |
| 227 const float scale = ui::GetScaleFactorScale(scale_factor); | |
| 228 std::vector<ImageInfo> info_list; | |
| 229 info_list.push_back(ImageInfo( | |
| 230 extension->GetIconResource(static_cast<int>(preferred_dip_size * scale), | |
| 231 ExtensionIconSet::MATCH_BIGGER), | |
| 232 max_dip_size.Scale(scale), | |
| 233 scale_factor)); | |
| 234 LoadImages(extension, info_list, cache); | |
| 235 } | |
| 236 | |
| 237 void ImageLoadingTracker::LoadDIPImage( | |
| 238 const extensions::Extension* extension, | |
| 239 int preferred_dip_size, | |
| 240 gfx::Size max_dip_size, | |
| 241 CacheParam cache) { | |
| 242 if (!gfx::Screen::IsDIPEnabled()) { | |
| 243 LoadImage(extension, | |
| 244 extension->GetIconResource(preferred_dip_size, | |
| 245 ExtensionIconSet::MATCH_BIGGER), | |
| 246 max_dip_size, | |
| 247 cache); | |
| 248 return; | |
| 249 } | |
| 250 | |
| 251 float scale = ui::GetScaleFactorScale(ui::SCALE_FACTOR_200P); | |
| 252 std::vector<ImageInfo> info_list; | |
| 253 info_list.push_back(ImageInfo( | |
| 254 extension->GetIconResource(static_cast<int>(preferred_dip_size * scale), | |
| 255 ExtensionIconSet::MATCH_BIGGER), | |
| 256 gfx::Size(static_cast<int>(max_dip_size.width() * scale), | |
| 257 static_cast<int>(max_dip_size.height() * scale)), | |
|
oshima
2012/07/10 23:51:06
max_dip_size.Scale(scale)
xiyuan
2012/07/11 17:39:13
Bad merge as I already have a LoadDIPImage just ab
| |
| 258 scale)); | |
| 259 LoadImages(extension, info_list, cache); | |
| 260 } | |
| 261 | |
| 214 void ImageLoadingTracker::LoadImages(const Extension* extension, | 262 void ImageLoadingTracker::LoadImages(const Extension* extension, |
| 215 const std::vector<ImageInfo>& info_list, | 263 const std::vector<ImageInfo>& info_list, |
| 216 CacheParam cache) { | 264 CacheParam cache) { |
| 217 PendingLoadInfo load_info; | 265 PendingLoadInfo load_info; |
| 218 load_info.extension = extension; | 266 load_info.extension = extension; |
| 219 load_info.cache = cache; | 267 load_info.cache = cache; |
| 220 load_info.extension_id = extension->id(); | 268 load_info.extension_id = extension->id(); |
| 221 load_info.pending_count = info_list.size(); | 269 load_info.pending_count = info_list.size(); |
| 222 int id = next_id_++; | 270 int id = next_id_++; |
| 223 load_map_[id] = load_info; | 271 load_map_[id] = load_info; |
| 224 | 272 |
| 225 for (std::vector<ImageInfo>::const_iterator it = info_list.begin(); | 273 for (std::vector<ImageInfo>::const_iterator it = info_list.begin(); |
| 226 it != info_list.end(); ++it) { | 274 it != info_list.end(); ++it) { |
| 227 // Load resources for special component extensions. | 275 // Load resources for special component extensions. |
| 228 if (load_info.extension_id == extension_misc::kWebStoreAppId) { | 276 if (load_info.extension_id == extension_misc::kWebStoreAppId) { |
| 229 if (!loader_) | 277 if (!loader_) |
| 230 loader_ = new ImageLoader(this); | 278 loader_ = new ImageLoader(this); |
| 231 loader_->LoadResource(it->resource, it->max_size, id, IDR_WEBSTORE_ICON); | 279 loader_->LoadResource(*it, id, IDR_WEBSTORE_ICON); |
| 232 continue; | 280 continue; |
| 233 } else if (load_info.extension_id == extension_misc::kChromeAppId) { | 281 } else if (load_info.extension_id == extension_misc::kChromeAppId) { |
| 234 if (!loader_) | 282 if (!loader_) |
| 235 loader_ = new ImageLoader(this); | 283 loader_ = new ImageLoader(this); |
| 236 loader_->LoadResource(it->resource, | 284 loader_->LoadResource(*it, id, IDR_PRODUCT_LOGO_128); |
| 237 it->max_size, | |
| 238 id, | |
| 239 IDR_PRODUCT_LOGO_128); | |
| 240 continue; | 285 continue; |
| 241 } | 286 } |
| 242 | 287 |
| 243 // If we don't have a path we don't need to do any further work, just | 288 // If we don't have a path we don't need to do any further work, just |
| 244 // respond back. | 289 // respond back. |
| 245 if (it->resource.relative_path().empty()) { | 290 if (it->resource.relative_path().empty()) { |
| 246 OnImageLoaded(NULL, it->resource, it->max_size, id, false); | 291 OnImageLoaded(NULL, *it, it->max_size, id, false); |
| 247 continue; | 292 continue; |
| 248 } | 293 } |
| 249 | 294 |
| 250 DCHECK(extension->path() == it->resource.extension_root()); | 295 DCHECK(extension->path() == it->resource.extension_root()); |
| 251 | 296 |
| 252 // See if the extension has the image already. | 297 // See if the extension has the image already. |
| 253 if (extension->HasCachedImage(it->resource, it->max_size)) { | 298 if (extension->HasCachedImage(it->resource, it->max_size)) { |
| 254 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size); | 299 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size); |
| 255 OnImageLoaded(&image, it->resource, it->max_size, id, false); | 300 OnImageLoaded(&image, *it, it->max_size, id, false); |
| 256 continue; | 301 continue; |
| 257 } | 302 } |
| 258 | 303 |
| 259 // Instruct the ImageLoader to load this on the File thread. LoadImage and | 304 // Instruct the ImageLoader to load this on the File thread. LoadImage and |
| 260 // LoadResource do not block. | 305 // LoadResource do not block. |
| 261 if (!loader_) | 306 if (!loader_) |
| 262 loader_ = new ImageLoader(this); | 307 loader_ = new ImageLoader(this); |
| 263 | 308 |
| 264 int resource_id; | 309 int resource_id; |
| 265 if (IsComponentExtensionResource(extension, it->resource, resource_id)) | 310 if (IsComponentExtensionResource(extension, it->resource, resource_id)) |
| 266 loader_->LoadResource(it->resource, it->max_size, id, resource_id); | 311 loader_->LoadResource(*it, id, resource_id); |
| 267 else | 312 else |
| 268 loader_->LoadImage(it->resource, it->max_size, id); | 313 loader_->LoadImage(*it, id); |
| 269 } | 314 } |
| 270 } | 315 } |
| 271 | 316 |
| 272 bool ImageLoadingTracker::IsComponentExtensionResource( | 317 bool ImageLoadingTracker::IsComponentExtensionResource( |
| 273 const Extension* extension, | 318 const Extension* extension, |
| 274 const ExtensionResource& resource, | 319 const ExtensionResource& resource, |
| 275 int& resource_id) const { | 320 int& resource_id) const { |
| 276 if (extension->location() != Extension::COMPONENT) | 321 if (extension->location() != Extension::COMPONENT) |
| 277 return false; | 322 return false; |
| 278 | 323 |
| 279 FilePath directory_path = extension->path(); | 324 FilePath directory_path = extension->path(); |
| 280 FilePath relative_path = directory_path.BaseName().Append( | 325 FilePath relative_path = directory_path.BaseName().Append( |
| 281 resource.relative_path()); | 326 resource.relative_path()); |
| 282 | 327 |
| 283 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { | 328 for (size_t i = 0; i < kComponentExtensionResourcesSize; ++i) { |
| 284 FilePath resource_path = | 329 FilePath resource_path = |
| 285 FilePath().AppendASCII(kComponentExtensionResources[i].name); | 330 FilePath().AppendASCII(kComponentExtensionResources[i].name); |
| 286 resource_path = resource_path.NormalizePathSeparators(); | 331 resource_path = resource_path.NormalizePathSeparators(); |
| 287 | 332 |
| 288 if (relative_path == resource_path) { | 333 if (relative_path == resource_path) { |
| 289 resource_id = kComponentExtensionResources[i].value; | 334 resource_id = kComponentExtensionResources[i].value; |
| 290 return true; | 335 return true; |
| 291 } | 336 } |
| 292 } | 337 } |
| 293 return false; | 338 return false; |
| 294 } | 339 } |
| 295 | 340 |
| 296 void ImageLoadingTracker::OnImageLoaded( | 341 void ImageLoadingTracker::OnImageLoaded( |
| 297 SkBitmap* image, | 342 SkBitmap* image, |
| 298 const ExtensionResource& resource, | 343 const ImageInfo& image_info, |
| 299 const gfx::Size& original_size, | 344 const gfx::Size& original_size, |
| 300 int id, | 345 int id, |
| 301 bool should_cache) { | 346 bool should_cache) { |
| 302 LoadMap::iterator load_map_it = load_map_.find(id); | 347 LoadMap::iterator load_map_it = load_map_.find(id); |
| 303 DCHECK(load_map_it != load_map_.end()); | 348 DCHECK(load_map_it != load_map_.end()); |
| 304 PendingLoadInfo* info = &load_map_it->second; | 349 PendingLoadInfo* info = &load_map_it->second; |
| 305 | 350 |
| 306 // Save the pending results. | 351 // Save the pending results. |
| 307 DCHECK(info->pending_count > 0); | 352 DCHECK(info->pending_count > 0); |
| 308 info->pending_count--; | 353 info->pending_count--; |
| 309 if (image) | 354 if (image) { |
| 310 info->bitmaps.push_back(*image); | 355 info->images.AddRepresentation(gfx::ImageSkiaRep(*image, |
| 356 image_info.scale_factor)); | |
| 357 } | |
| 311 | 358 |
| 312 // Add to the extension's image cache if requested. | 359 // Add to the extension's image cache if requested. |
| 313 DCHECK(info->cache != CACHE || info->extension); | 360 DCHECK(info->cache != CACHE || info->extension); |
| 314 if (should_cache && info->cache == CACHE && | 361 if (should_cache && info->cache == CACHE && |
| 315 !info->extension->HasCachedImage(resource, original_size)) { | 362 !info->extension->HasCachedImage(image_info.resource, original_size)) { |
| 316 info->extension->SetCachedImage(resource, image ? *image : SkBitmap(), | 363 info->extension->SetCachedImage(image_info.resource, |
| 364 image ? *image : SkBitmap(), | |
| 317 original_size); | 365 original_size); |
| 318 } | 366 } |
| 319 | 367 |
| 320 // If all pending images are done then report back. | 368 // If all pending images are done then report back. |
| 321 if (info->pending_count == 0) { | 369 if (info->pending_count == 0) { |
| 322 gfx::Image image; | 370 gfx::Image image(info->images); |
| 323 std::string extension_id = info->extension_id; | 371 std::string extension_id = info->extension_id; |
| 324 | 372 |
| 325 if (info->bitmaps.size() > 0) { | |
| 326 gfx::ImageSkia image_skia; | |
| 327 for (std::vector<SkBitmap>::const_iterator it = info->bitmaps.begin(); | |
| 328 it != info->bitmaps.end(); ++it) { | |
| 329 // TODO(pkotwicz): Do something better but ONLY when DIP is enabled. | |
| 330 image_skia.AddRepresentation( | |
| 331 gfx::ImageSkiaRep(*it, ui::SCALE_FACTOR_100P)); | |
| 332 } | |
| 333 image = gfx::Image(image_skia); | |
| 334 } | |
| 335 | |
| 336 load_map_.erase(load_map_it); | 373 load_map_.erase(load_map_it); |
| 337 | 374 |
| 338 // ImageLoadingTracker might be deleted after the callback so don't | 375 // ImageLoadingTracker might be deleted after the callback so don't |
| 339 // anything after this statement. | 376 // anything after this statement. |
| 340 observer_->OnImageLoaded(image, extension_id, id); | 377 observer_->OnImageLoaded(image, extension_id, id); |
| 341 } | 378 } |
| 342 } | 379 } |
| 343 | 380 |
| 344 void ImageLoadingTracker::Observe(int type, | 381 void ImageLoadingTracker::Observe(int type, |
| 345 const content::NotificationSource& source, | 382 const content::NotificationSource& source, |
| 346 const content::NotificationDetails& details) { | 383 const content::NotificationDetails& details) { |
| 347 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); | 384 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 348 | 385 |
| 349 const Extension* extension = | 386 const Extension* extension = |
| 350 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 387 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| 351 | 388 |
| 352 // Remove reference to this extension from all pending load entries. This | 389 // Remove reference to this extension from all pending load entries. This |
| 353 // ensures we don't attempt to cache the image when the load completes. | 390 // ensures we don't attempt to cache the image when the load completes. |
| 354 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 391 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| 355 PendingLoadInfo* info = &i->second; | 392 PendingLoadInfo* info = &i->second; |
| 356 if (info->extension == extension) { | 393 if (info->extension == extension) { |
| 357 info->extension = NULL; | 394 info->extension = NULL; |
| 358 info->cache = DONT_CACHE; | 395 info->cache = DONT_CACHE; |
| 359 } | 396 } |
| 360 } | 397 } |
| 361 } | 398 } |
| OLD | NEW |