Chromium Code Reviews| Index: chrome/browser/extensions/image_loader.cc |
| diff --git a/chrome/browser/extensions/image_loading_tracker.cc b/chrome/browser/extensions/image_loader.cc |
| similarity index 30% |
| copy from chrome/browser/extensions/image_loading_tracker.cc |
| copy to chrome/browser/extensions/image_loader.cc |
| index 582028555d1bd5e25244fff6ba235b8b4488af1f..636af8c8a93fa81d4d9882ac0cbcc77d84f942aa 100644 |
| --- a/chrome/browser/extensions/image_loading_tracker.cc |
| +++ b/chrome/browser/extensions/image_loader.cc |
| @@ -2,46 +2,45 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/extensions/image_loading_tracker.h" |
| +#include "chrome/browser/extensions/image_loader.h" |
| -#include <string> |
| -#include <vector> |
| - |
| -#include "base/bind.h" |
| +#include "base/callback.h" |
| #include "base/file_util.h" |
| #include "base/path_service.h" |
| +#include "base/memory/singleton.h" |
| +#include "base/string_number_conversions.h" |
| #include "base/threading/sequenced_worker_pool.h" |
| -#include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| #include "chrome/common/chrome_notification_types.h" |
| #include "chrome/common/chrome_paths.h" |
| #include "chrome/common/extensions/extension.h" |
| -#include "chrome/common/extensions/extension_constants.h" |
| -#include "chrome/common/extensions/extension_file_util.h" |
| -#include "chrome/common/extensions/extension_resource.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/notification_service.h" |
| #include "grit/component_extension_resources_map.h" |
| #include "grit/theme_resources.h" |
| #include "skia/ext/image_operations.h" |
| -#include "third_party/skia/include/core/SkBitmap.h" |
| #include "ui/base/resource/resource_bundle.h" |
| -#include "ui/gfx/image/image.h" |
| -#include "ui/gfx/image/image_skia_rep.h" |
| +#include "ui/gfx/image/image_skia.h" |
| #include "webkit/glue/image_decoder.h" |
| using content::BrowserThread; |
| using extensions::Extension; |
| +using extensions::ImageLoader; |
| namespace { |
| +std::string SizeToString(const gfx::Size& max_size) { |
| + return base::IntToString(max_size.width()) + "x" + |
| + base::IntToString(max_size.height()); |
| +} |
| + |
| bool ShouldResizeImageRepresentation( |
| - ImageLoadingTracker::ImageRepresentation::ResizeCondition resize_method, |
| + ImageLoader::ImageRepresentation::ResizeCondition resize_method, |
| const gfx::Size& decoded_size, |
| const gfx::Size& desired_size) { |
| switch (resize_method) { |
| - case ImageLoadingTracker::ImageRepresentation::ALWAYS_RESIZE: |
| + case ImageLoader::ImageRepresentation::ALWAYS_RESIZE: |
| return decoded_size != desired_size; |
| - case ImageLoadingTracker::ImageRepresentation::RESIZE_WHEN_LARGER: |
| + case ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER: |
| return decoded_size.width() > desired_size.width() || |
| decoded_size.height() > desired_size.height(); |
| default: |
| @@ -50,276 +49,125 @@ bool ShouldResizeImageRepresentation( |
| } |
| } |
| -} // namespace |
| +SkBitmap ResizeIfNeeded(const SkBitmap& bitmap, |
| + const ImageLoader::ImageRepresentation& image_info) { |
| + gfx::Size original_size(bitmap.width(), bitmap.height()); |
| + if (ShouldResizeImageRepresentation(image_info.resize_condition, |
| + original_size, |
| + image_info.desired_size)) { |
| + return skia::ImageOperations::Resize( |
| + bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
| + image_info.desired_size.width(), image_info.desired_size.height()); |
| + } |
| -//////////////////////////////////////////////////////////////////////////////// |
| -// ImageLoadingTracker::Observer |
| + return bitmap; |
| +} |
| + |
| +void LoadResourceOnUIThread(int resource_id, SkBitmap* bitmap) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + gfx::ImageSkia image( |
| + *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id)); |
| + image.MakeThreadSafe(); |
| + *bitmap = *image.bitmap(); |
| +} |
| -ImageLoadingTracker::Observer::~Observer() {} |
| +void LoadImageOnBlockingPool(const ImageLoader::ImageRepresentation& image_info, |
| + SkBitmap* bitmap) { |
| + DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| + |
| + // Read the file from disk. |
| + std::string file_contents; |
| + FilePath path = image_info.resource.GetFilePath(); |
| + if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
| + return; |
| + } |
| + |
| + // Decode the bitmap using WebKit's image decoder. |
| + const unsigned char* data = |
| + reinterpret_cast<const unsigned char*>(file_contents.data()); |
| + webkit_glue::ImageDecoder decoder; |
| + // Note: This class only decodes bitmaps from extension resources. Chrome |
| + // doesn't (for security reasons) directly load extension resources provided |
| + // by the extension author, but instead decodes them in a separate |
| + // locked-down utility process. Only if the decoding succeeds is the image |
| + // saved from memory to disk and subsequently used in the Chrome UI. |
| + // Chrome is therefore decoding bitmaps here that were generated by Chrome. |
| + *bitmap = decoder.Decode(data, file_contents.length()); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace extensions { |
| //////////////////////////////////////////////////////////////////////////////// |
| -// ImageLoadingTracker::ImageRepresentation |
| +// ImageLoader::ImageRepresentation |
| -ImageLoadingTracker::ImageRepresentation::ImageRepresentation( |
| +ImageLoader::ImageRepresentation::ImageRepresentation( |
| const ExtensionResource& resource, |
| - ResizeCondition resize_method, |
| + ResizeCondition resize_condition, |
| const gfx::Size& desired_size, |
| ui::ScaleFactor scale_factor) |
| : resource(resource), |
| - resize_method(resize_method), |
| + resize_condition(resize_condition), |
| desired_size(desired_size), |
| scale_factor(scale_factor) { |
| } |
| -ImageLoadingTracker::ImageRepresentation::~ImageRepresentation() { |
| +ImageLoader::ImageRepresentation::~ImageRepresentation() { |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| -// ImageLoadingTracker::PendingLoadInfo |
| +// ImageLoader::PendingLoadInfo |
| -ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo() |
| - : extension(NULL), |
| - cache(CACHE), |
| - pending_count(0) { |
| +ImageLoader::PendingLoadInfo::PendingLoadInfo() |
| + : save_to_cache(true) { |
| } |
| -ImageLoadingTracker::PendingLoadInfo::~PendingLoadInfo() {} |
| +ImageLoader::PendingLoadInfo::~PendingLoadInfo() {} |
| //////////////////////////////////////////////////////////////////////////////// |
| -// ImageLoadingTracker::ImageLoader |
| - |
| -// A RefCounted class for loading bitmaps/image reps on the File thread and |
| -// reporting back on the UI thread. |
| -class ImageLoadingTracker::ImageLoader |
| - : public base::RefCountedThreadSafe<ImageLoader> { |
| - public: |
| - explicit ImageLoader(ImageLoadingTracker* tracker) |
| - : tracker_(tracker) { |
| - CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_)); |
| - DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| - } |
| - |
| - // Lets this class know that the tracker is no longer interested in the |
| - // results. |
| - void StopTracking() { |
| - tracker_ = NULL; |
| - } |
| - |
| - // Instructs the loader to load a task on the blocking pool. |
| - void LoadImage(const ImageRepresentation& image_info, int id) { |
| - DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| - BrowserThread::PostBlockingPoolTask( |
| - FROM_HERE, |
| - base::Bind(&ImageLoader::LoadOnBlockingPool, this, image_info, id)); |
| - } |
| - |
| - void LoadOnBlockingPool(const ImageRepresentation& image_info, int id) { |
| - DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| - |
| - // Read the file from disk. |
| - std::string file_contents; |
| - FilePath path = image_info.resource.GetFilePath(); |
| - if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
| - ReportBack(NULL, image_info, gfx::Size(), id); |
| - return; |
| - } |
| - |
| - // Decode the bitmap using WebKit's image decoder. |
| - const unsigned char* data = |
| - reinterpret_cast<const unsigned char*>(file_contents.data()); |
| - webkit_glue::ImageDecoder decoder; |
| - scoped_ptr<SkBitmap> decoded(new SkBitmap()); |
| - // Note: This class only decodes bitmaps from extension resources. Chrome |
| - // doesn't (for security reasons) directly load extension resources provided |
| - // by the extension author, but instead decodes them in a separate |
| - // locked-down utility process. Only if the decoding succeeds is the image |
| - // saved from memory to disk and subsequently used in the Chrome UI. |
| - // Chrome is therefore decoding bitmaps here that were generated by Chrome. |
| - *decoded = decoder.Decode(data, file_contents.length()); |
| - if (decoded->empty()) { |
| - ReportBack(NULL, image_info, gfx::Size(), id); |
| - return; // Unable to decode. |
| - } |
| - |
| - gfx::Size original_size(decoded->width(), decoded->height()); |
| - *decoded = ResizeIfNeeded(*decoded, image_info); |
| - |
| - ReportBack(decoded.release(), image_info, original_size, id); |
| - } |
| - |
| - // Instructs the loader to load a resource on the UI thread. |
| - void LoadResource(const ImageRepresentation& image_info, |
| - int id, |
| - int resource_id) { |
| - DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| - |
| - if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| - LoadResourceOnUIThread(image_info, id, resource_id); |
| - return; |
| - } |
| - |
| - BrowserThread::PostTask( |
| - BrowserThread::UI, FROM_HERE, |
| - base::Bind(&ImageLoader::LoadResourceOnUIThread, this, image_info, |
| - id, resource_id)); |
| - } |
| - |
| - void LoadResourceOnUIThread(const ImageRepresentation& image_info, |
| - int id, |
| - int resource_id) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - |
| - // Bundled image resources is only safe to be loaded on UI thread. |
| - gfx::ImageSkia* image = |
| - ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); |
| - image->MakeThreadSafe(); |
| - |
| - BrowserThread::PostBlockingPoolTask( |
| - FROM_HERE, |
| - base::Bind(&ImageLoader::ResizeOnBlockingPool, this, image_info, |
| - id, *image)); |
| - } |
| +// ImageLoader::LoadResult |
| - void ResizeOnBlockingPool(const ImageRepresentation& image_info, |
| - int id, |
| - const gfx::ImageSkia& image) { |
| - DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| - // TODO(xiyuan): Clean up to use SkBitmap here and in LoadOnBlockingPool. |
| - scoped_ptr<SkBitmap> bitmap(new SkBitmap); |
| - *bitmap = ResizeIfNeeded(*image.bitmap(), image_info); |
| - ReportBack(bitmap.release(), image_info, image_info.desired_size, id); |
| - } |
| - |
| - void ReportBack(const SkBitmap* bitmap, const ImageRepresentation& image_info, |
| - const gfx::Size& original_size, int id) { |
| - DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| - |
| - BrowserThread::PostTask( |
| - callback_thread_id_, FROM_HERE, |
| - base::Bind(&ImageLoader::ReportOnCallingThread, this, |
| - bitmap, image_info, original_size, id)); |
| - } |
| - |
| - void ReportOnCallingThread(const SkBitmap* bitmap, |
| - const ImageRepresentation& image_info, |
| - const gfx::Size& original_size, |
| - int id) { |
| - DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| - |
| - if (tracker_) |
| - tracker_->OnBitmapLoaded(bitmap, image_info, original_size, id, true); |
| - |
| - if (bitmap) |
| - delete bitmap; |
| - } |
| - |
| - private: |
| - friend class base::RefCountedThreadSafe<ImageLoader>; |
| - ~ImageLoader() {} |
| - |
| - SkBitmap ResizeIfNeeded(const SkBitmap& bitmap, |
| - const ImageRepresentation& image_info) { |
| - gfx::Size original_size(bitmap.width(), bitmap.height()); |
| - if (ShouldResizeImageRepresentation(image_info.resize_method, |
| - original_size, |
| - image_info.desired_size)) { |
| - return skia::ImageOperations::Resize( |
| - bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
| - image_info.desired_size.width(), image_info.desired_size.height()); |
| - } |
| - |
| - return bitmap; |
| - } |
| - |
| - // The tracker we are loading the bitmap for. If NULL, it means the tracker is |
| - // no longer interested in the reply. |
| - ImageLoadingTracker* tracker_; |
| - |
| - // The thread that we need to call back on to report that we are done. |
| - BrowserThread::ID callback_thread_id_; |
| +ImageLoader::LoadResult::LoadResult( |
| + const SkBitmap& bitmap, |
| + const gfx::Size& original_size, |
| + const ImageLoader::ImageRepresentation& image_representation) |
| + : bitmap(bitmap), |
| + original_size(original_size), |
| + image_representation(image_representation) { |
| +} |
| - DISALLOW_COPY_AND_ASSIGN(ImageLoader); |
| -}; |
| +ImageLoader::LoadResult::~LoadResult() { |
| +} |
| //////////////////////////////////////////////////////////////////////////////// |
| -// ImageLoadingTracker |
| +// ImageLoader |
| -ImageLoadingTracker::ImageLoadingTracker(Observer* observer) |
| - : observer_(observer), |
| - next_id_(0) { |
| - registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| - content::NotificationService::AllSources()); |
| -} |
| - |
| -ImageLoadingTracker::~ImageLoadingTracker() { |
| - // The loader is created lazily and is NULL if the tracker is destroyed before |
| - // any valid image load tasks have been posted. |
| - if (loader_) |
| - loader_->StopTracking(); |
| +// static |
| +ImageLoader* ImageLoader::GetInstance() { |
| + return Singleton<ImageLoader>::get(); |
| } |
| -void ImageLoadingTracker::LoadImage(const Extension* extension, |
| - const ExtensionResource& resource, |
| - const gfx::Size& max_size, |
| - CacheParam cache) { |
| - std::vector<ImageRepresentation> info_list; |
| - info_list.push_back(ImageRepresentation( |
| - resource, |
| - ImageRepresentation::RESIZE_WHEN_LARGER, |
| - max_size, |
| - ui::SCALE_FACTOR_100P)); |
| - LoadImages(extension, info_list, cache); |
| +ImageLoader::ImageLoader() { |
| + registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| + content::NotificationService::AllSources()); |
| +#if defined(OS_CHROMEOS) |
| + const int kLauncherIconSizes[] = { extension_misc::EXTENSION_ICON_SMALL, |
| + extension_misc::EXTENSION_ICON_SMALL * 2 }; |
| + SetImageSizesToCache( |
| + std::set<int>(kLauncherIconSizes, |
| + kLauncherIconSizes + arraysize(kLauncherIconSizes))); |
| +#endif |
| } |
| -void ImageLoadingTracker::LoadImages( |
| - const Extension* extension, |
| - const std::vector<ImageRepresentation>& info_list, |
| - CacheParam cache) { |
| - PendingLoadInfo load_info; |
| - load_info.extension = extension; |
| - load_info.cache = cache; |
| - load_info.extension_id = extension->id(); |
| - load_info.pending_count = info_list.size(); |
| - int id = next_id_++; |
| - load_map_[id] = load_info; |
| - |
| - for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); |
| - it != info_list.end(); ++it) { |
| - // If we don't have a path we don't need to do any further work, just |
| - // respond back. |
| - if (it->resource.relative_path().empty()) { |
| - OnBitmapLoaded(NULL, *it, it->desired_size, id, false); |
| - continue; |
| - } |
| - |
| - DCHECK(extension->path() == it->resource.extension_root()); |
| - |
| - // See if the extension has the bitmap already. |
| - if (extension->HasCachedImage(it->resource, it->desired_size)) { |
| - SkBitmap bitmap = extension->GetCachedImage(it->resource, |
| - it->desired_size); |
| - OnBitmapLoaded(&bitmap, *it, it->desired_size, id, false); |
| - continue; |
| - } |
| - |
| - // Instruct the ImageLoader to load this on the File thread. LoadImage and |
| - // LoadResource do not block. |
| - if (!loader_) |
| - loader_ = new ImageLoader(this); |
| - |
| - int resource_id = -1; |
| - if (IsComponentExtensionResource(extension, it->resource.relative_path(), |
| - &resource_id)) |
| - loader_->LoadResource(*it, id, resource_id); |
| - else |
| - loader_->LoadImage(*it, id); |
| - } |
| +ImageLoader::~ImageLoader() { |
| } |
| -bool ImageLoadingTracker::IsComponentExtensionResource( |
| - const Extension* extension, |
| - const FilePath& resource_path, |
| - int* resource_id) { |
| +// static |
| +bool ImageLoader::IsComponentExtensionResource(const Extension* extension, |
| + const FilePath& resource_path, |
| + int* resource_id) { |
| static const GritResourceMap kExtraComponentExtensionResources[] = { |
| {"web_store/webstore_icon_128.png", IDR_WEBSTORE_ICON}, |
| {"web_store/webstore_icon_16.png", IDR_WEBSTORE_ICON_16}, |
| @@ -368,66 +216,257 @@ bool ImageLoadingTracker::IsComponentExtensionResource( |
| return false; |
| } |
| -void ImageLoadingTracker::OnBitmapLoaded( |
| - const SkBitmap* bitmap, |
| - const ImageRepresentation& image_info, |
| - const gfx::Size& original_size, |
| - int id, |
| - bool should_cache) { |
| - LoadMap::iterator load_map_it = load_map_.find(id); |
| - DCHECK(load_map_it != load_map_.end()); |
| - PendingLoadInfo* info = &load_map_it->second; |
| - |
| - // Save the pending results. |
| - DCHECK_GT(info->pending_count, 0u); |
| - info->pending_count--; |
| - if (bitmap) { |
| - info->image_skia.AddRepresentation(gfx::ImageSkiaRep(*bitmap, |
| - image_info.scale_factor)); |
| - } |
| +void ImageLoader::LoadImageAsync( |
| + const Extension* extension, |
| + const ExtensionResource& resource, |
| + const gfx::Size& max_size, |
| + const base::Callback<void(const gfx::Image&)>& callback) { |
| + std::vector<ImageRepresentation> info_list; |
| + info_list.push_back(ImageRepresentation( |
| + resource, |
| + ImageRepresentation::RESIZE_WHEN_LARGER, |
| + max_size, |
| + ui::SCALE_FACTOR_100P)); |
| + LoadImagesAsync(extension, info_list, callback); |
| +} |
| - // Add to the extension's bitmap cache if requested. |
| - DCHECK(info->cache != CACHE || info->extension); |
| - if (should_cache && info->cache == CACHE && !image_info.resource.empty() && |
| - !info->extension->HasCachedImage(image_info.resource, original_size)) { |
| - info->extension->SetCachedImage(image_info.resource, |
| - bitmap ? *bitmap : SkBitmap(), |
| - original_size); |
| +void ImageLoader::LoadImagesAsync( |
| + const Extension* extension, |
| + const std::vector<ImageRepresentation>& info_list, |
| + const base::Callback<void(const gfx::Image&)>& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + // Loading an image from the cache and loading resources have to happen |
| + // on the UI thread. So do those two things first, and pass the rest of the |
| + // work of as a blocking pool task. |
| + |
| + std::vector<SkBitmap*> bitmaps; |
| + bitmaps.resize(info_list.size()); |
| + |
| + bool should_cache = false; |
| + |
| + // Count how many images are loaded from the cache, so we can check if we got |
| + // all images. |
| + unsigned loadedFromCache = 0; |
| + int i = 0; |
| + for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); |
| + it != info_list.end(); ++it, ++i) { |
| + DCHECK(it->resource.relative_path().empty() || |
| + extension->path() == it->resource.extension_root()); |
| + |
| + should_cache |= ShouldCacheImage(*it); |
| + |
| + const SkBitmap* bitmap = GetCachedImage(it->resource, it->desired_size); |
| + if (bitmap) { |
| + bitmaps[i] = new SkBitmap(*bitmap); |
|
xiyuan
2012/10/10 20:30:22
Would using a SkBitmap* gives extra benefits? If n
Marijn Kruisselbrink
2012/10/10 22:33:03
I think initially I thought I needed to be able to
|
| + ++loadedFromCache; |
| + continue; |
| + } |
| + |
| + int resource_id; |
| + if (IsComponentExtensionResource(extension, it->resource.relative_path(), |
| + &resource_id)) { |
| + bitmaps[i] = new SkBitmap; |
| + LoadResourceOnUIThread(resource_id, bitmaps[i]); |
| + } |
| } |
| - // If all pending bitmaps are done then report back. |
| - if (info->pending_count == 0) { |
| + if (loadedFromCache == info_list.size()) { |
| + gfx::ImageSkia image_skia; |
| + |
| + for (unsigned i = 0; i < info_list.size(); ++i) { |
| + image_skia.AddRepresentation(gfx::ImageSkiaRep( |
| + *bitmaps[i], info_list[i].scale_factor)); |
| + delete bitmaps[i]; |
| + } |
| + |
| gfx::Image image; |
| - std::string extension_id = info->extension_id; |
| + if (!image_skia.isNull()) { |
| + image_skia.MakeThreadSafe(); |
| + image = gfx::Image(image_skia); |
| + } |
| + |
| + callback.Run(image); |
| + return; |
| + } |
| + |
| + PendingLoadInfo* load_info = NULL; |
| + if (should_cache) { |
| + load_info = new PendingLoadInfo; |
| + load_info->extension_id = extension->id(); |
| + load_info->save_to_cache = true; |
| + pending_.insert(load_info); |
| + } |
| + |
| + DCHECK(!BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| + BrowserThread::PostBlockingPoolTask( |
| + FROM_HERE, |
| + base::Bind(&ImageLoader::LoadImagesOnBlockingPool, base::Unretained(this), |
| + info_list, bitmaps, load_info, callback)); |
| + |
| +} |
| + |
| +void ImageLoader::LoadImagesOnBlockingPool( |
| + const std::vector<ImageRepresentation>& info_list, |
| + const std::vector<SkBitmap*>& bitmaps, |
| + PendingLoadInfo* load_info, |
| + const base::Callback<void(const gfx::Image&)>& callback) { |
| + DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| + |
| + gfx::ImageSkia image_skia; |
| + |
| + std::vector<LoadResult> load_result; |
| + |
| + int i = 0; |
| + for (std::vector<ImageRepresentation>::const_iterator it = info_list.begin(); |
| + it != info_list.end(); ++it, ++i) { |
| + // If we don't have a path there isn't anything we can do, just skip it. |
| + if (it->resource.relative_path().empty()) |
| + continue; |
| - if (!info->image_skia.isNull()) { |
| - info->image_skia.MakeThreadSafe(); |
| - image = gfx::Image(info->image_skia); |
| + SkBitmap bitmap; |
| + if (bitmaps[i]) { |
| + bitmap = *bitmaps[i]; |
| + delete bitmaps[i]; |
| + } else { |
| + LoadImageOnBlockingPool(*it, &bitmap); |
| } |
| - load_map_.erase(load_map_it); |
| + // If the image failed to load, skip it. |
| + if (bitmap.isNull() || bitmap.empty()) |
| + continue; |
| + |
| + gfx::Size original_size(bitmap.width(), bitmap.height()); |
| + bitmap = ResizeIfNeeded(bitmap, *it); |
| + |
| + load_result.push_back(LoadResult(bitmap, original_size, *it)); |
| + } |
| + |
| + gfx::Image image; |
| - // ImageLoadingTracker might be deleted after the callback so don't do |
| - // anything after this statement. |
| - observer_->OnImageLoaded(image, extension_id, id); |
| + if (!image_skia.isNull()) { |
| + image_skia.MakeThreadSafe(); |
| + image = gfx::Image(image_skia); |
| } |
| + |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&ImageLoader::ReplyBack, |
| + base::Unretained(this), load_result, |
| + load_info, callback)); |
| } |
| -void ImageLoadingTracker::Observe(int type, |
| - const content::NotificationSource& source, |
| - const content::NotificationDetails& details) { |
| +void ImageLoader::ReplyBack( |
| + const std::vector<LoadResult>& load_result, |
| + PendingLoadInfo* load_info, |
| + const base::Callback<void(const gfx::Image&)>& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + gfx::ImageSkia image_skia; |
| + |
| + for (std::vector<LoadResult>::const_iterator it = load_result.begin(); |
| + it != load_result.end(); ++it) { |
| + const SkBitmap& bitmap = it->bitmap; |
| + const gfx::Size& original_size = it->original_size; |
| + const ImageRepresentation& image_rep = it->image_representation; |
| + |
| + image_skia.AddRepresentation(gfx::ImageSkiaRep( |
| + bitmap, image_rep.scale_factor)); |
| + |
| + // maybe cache |
| + if (load_info && load_info->save_to_cache) { |
| + const FilePath& path = image_rep.resource.relative_path(); |
| + gfx::Size actual_size(bitmap.width(), bitmap.height()); |
| + std::string location; |
| + if (actual_size != original_size) |
| + location = SizeToString(actual_size); |
| + SetCachedImageImpl(load_info->extension_id, |
| + ImageCacheKey(path, location), bitmap); |
| + } |
| + } |
| + |
| + if (load_info) |
| + pending_.erase(load_info); |
| + |
| + delete load_info; |
| + |
| + gfx::Image image; |
| + if (!image_skia.isNull()) { |
| + image_skia.MakeThreadSafe(); |
| + image = gfx::Image(image_skia); |
| + } |
| + |
| + callback.Run(image); |
| +} |
| + |
| +void ImageLoader::Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| const Extension* extension = |
| content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
| + const std::string& extension_id = extension->id(); |
| // Remove reference to this extension from all pending load entries. This |
| // ensures we don't attempt to cache the bitmap when the load completes. |
| - for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| - PendingLoadInfo* info = &i->second; |
| - if (info->extension == extension) { |
| - info->extension = NULL; |
| - info->cache = DONT_CACHE; |
| + for (LoadSet::iterator i = pending_.begin(); i != pending_.end(); ++i) { |
| + PendingLoadInfo* info = *i; |
| + if (info->extension_id == extension_id) { |
| + info->save_to_cache = false; |
| } |
| } |
| } |
| + |
| +void ImageLoader::SetCachedImageImpl(const std::string& extension_id, |
| + const ImageCacheKey& key, |
| + const SkBitmap& image) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + image_cache_[extension_id][key] = image; |
| +} |
| + |
| +const SkBitmap* ImageLoader::GetCachedImage(const ExtensionResource& source, |
| + const gfx::Size& max_size) const { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + std::map<std::string, ImageCache>::const_iterator it = |
| + image_cache_.find(source.extension_id()); |
| + if (it == image_cache_.end()) |
| + return NULL; |
| + |
| + const ImageCache& cache = it->second; |
| + |
| + const FilePath& path = source.relative_path(); |
| + |
| + // Look for exact size match. |
| + ImageCache::const_iterator i = cache.find( |
| + ImageCacheKey(path, SizeToString(max_size))); |
| + if (i != cache.end()) |
| + return &(i->second); |
| + |
| + // If we have the original size version cached, return that if it's small |
| + // enough. |
| + i = cache.find(ImageCacheKey(path, std::string())); |
| + if (i != cache.end()) { |
| + const SkBitmap& image = i->second; |
| + if (image.width() <= max_size.width() && |
| + image.height() <= max_size.height()) { |
| + return &(i->second); |
| + } |
| + } |
| + |
| + return NULL; |
| +} |
| + |
| +bool ImageLoader::ShouldCacheImage( |
| + const ImageRepresentation& image_info) const { |
| + return image_info.desired_size.width() == image_info.desired_size.height() && |
| + image_sizes_to_cache_.find(image_info.desired_size.width()) != |
| + image_sizes_to_cache_.end(); |
| +} |
| + |
| +void ImageLoader::SetImageSizesToCache(const std::set<int> image_sizes_) { |
| + image_sizes_to_cache_ = image_sizes_; |
| +} |
| + |
| +} // namespace extensions |