summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/image_loading_tracker.cc
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-30 20:40:18 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-30 20:40:18 +0000
commitd9ad80fce90880db9b63f59206ba106d203a4976 (patch)
tree57107f54ee4db32ed51ea5116f4faef575c209ae /chrome/browser/extensions/image_loading_tracker.cc
parent94c682ee504e8ea9e74a051e9b172468f62b1526 (diff)
downloadchromium_src-d9ad80fce90880db9b63f59206ba106d203a4976.zip
chromium_src-d9ad80fce90880db9b63f59206ba106d203a4976.tar.gz
chromium_src-d9ad80fce90880db9b63f59206ba106d203a4976.tar.bz2
Attempt 2 at landing this. Patch is exactly same as last time around.
Adds ability for ImageLoadingTracker to cache images. BUG=none TEST=none TBR=aa@chromium.org Review URL: http://codereview.chromium.org/1534006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43130 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/image_loading_tracker.cc')
-rw-r--r--chrome/browser/extensions/image_loading_tracker.cc90
1 files changed, 71 insertions, 19 deletions
diff --git a/chrome/browser/extensions/image_loading_tracker.cc b/chrome/browser/extensions/image_loading_tracker.cc
index a1f4ad2..2f7ef4d 100644
--- a/chrome/browser/extensions/image_loading_tracker.cc
+++ b/chrome/browser/extensions/image_loading_tracker.cc
@@ -7,6 +7,8 @@
#include "base/file_util.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/common/extensions/extension_resource.h"
+#include "chrome/common/notification_service.h"
+#include "chrome/common/notification_type.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "webkit/glue/image_decoder.h"
@@ -33,23 +35,25 @@ class ImageLoadingTracker::ImageLoader
// Instructs the loader to load a task on the File thread.
void LoadImage(const ExtensionResource& resource,
- const gfx::Size& max_size) {
+ const gfx::Size& max_size,
+ int id) {
DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::FILE));
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this, &ImageLoader::LoadOnFileThread, resource,
- max_size));
+ max_size, id));
}
void LoadOnFileThread(ExtensionResource resource,
- const gfx::Size& max_size) {
+ const gfx::Size& max_size,
+ int id) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
// Read the file from disk.
std::string file_contents;
FilePath path = resource.GetFilePath();
if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) {
- ReportBack(NULL, resource);
+ ReportBack(NULL, resource, id);
return;
}
@@ -60,7 +64,7 @@ class ImageLoadingTracker::ImageLoader
scoped_ptr<SkBitmap> decoded(new SkBitmap());
*decoded = decoder.Decode(data, file_contents.length());
if (decoded->empty()) {
- ReportBack(NULL, resource);
+ ReportBack(NULL, resource, id);
return; // Unable to decode.
}
@@ -72,26 +76,27 @@ class ImageLoadingTracker::ImageLoader
max_size.width(), max_size.height());
}
- ReportBack(decoded.release(), resource);
+ ReportBack(decoded.release(), resource, id);
}
- void ReportBack(SkBitmap* image, const ExtensionResource& resource) {
+ void ReportBack(SkBitmap* image, const ExtensionResource& resource,
+ int id) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
ChromeThread::PostTask(
callback_thread_id_, FROM_HERE,
NewRunnableMethod(this, &ImageLoader::ReportOnUIThread,
- image, resource));
+ image, resource, id));
}
- void ReportOnUIThread(SkBitmap* image, ExtensionResource resource) {
+ void ReportOnUIThread(SkBitmap* image, ExtensionResource resource,
+ int id) {
DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::FILE));
if (tracker_)
- tracker_->OnImageLoaded(image, resource);
+ tracker_->OnImageLoaded(image, resource, id);
- if (image)
- delete image;
+ delete image;
}
private:
@@ -110,7 +115,11 @@ class ImageLoadingTracker::ImageLoader
ImageLoadingTracker::ImageLoadingTracker(Observer* observer)
: observer_(observer),
- responses_(0) {
+ next_id_(0) {
+ registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
+ NotificationService::AllSources());
+ registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
+ NotificationService::AllSources());
}
ImageLoadingTracker::~ImageLoadingTracker() {
@@ -120,23 +129,66 @@ ImageLoadingTracker::~ImageLoadingTracker() {
loader_->StopTracking();
}
-void ImageLoadingTracker::LoadImage(const ExtensionResource& resource,
- gfx::Size max_size) {
+void ImageLoadingTracker::LoadImage(Extension* extension,
+ const ExtensionResource& resource,
+ const gfx::Size& max_size,
+ CacheParam cache) {
+ DCHECK(extension->path() == resource.extension_root());
+
// If we don't have a path we don't need to do any further work, just respond
// back.
+ int id = next_id_++;
if (resource.relative_path().empty()) {
- OnImageLoaded(NULL, resource);
+ OnImageLoaded(NULL, resource, id);
+ return;
+ }
+
+ // See if the extension has the image already.
+ if (extension->HasCachedImage(resource)) {
+ SkBitmap image = extension->GetCachedImage(resource);
+ OnImageLoaded(&image, resource, id);
return;
}
+ if (cache == CACHE) {
+ load_map_[id] = extension;
+ }
+
// Instruct the ImageLoader to load this on the File thread. LoadImage does
// not block.
if (!loader_)
loader_ = new ImageLoader(this);
- loader_->LoadImage(resource, max_size);
+ loader_->LoadImage(resource, max_size, id);
}
void ImageLoadingTracker::OnImageLoaded(
- SkBitmap* image, const ExtensionResource& resource) {
- observer_->OnImageLoaded(image, resource, responses_++);
+ SkBitmap* image,
+ const ExtensionResource& resource,
+ int id) {
+ LoadMap::iterator i = load_map_.find(id);
+ if (i != load_map_.end()) {
+ i->second->SetCachedImage(resource, image ? *image : SkBitmap());
+ load_map_.erase(i);
+ }
+
+ observer_->OnImageLoaded(image, resource, id);
+}
+
+void ImageLoadingTracker::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ DCHECK(type == NotificationType::EXTENSION_UNLOADED ||
+ type == NotificationType::EXTENSION_UNLOADED_DISABLED);
+
+ Extension* extension = Details<Extension>(details).ptr();
+
+ // Remove all entries in the load_map_ referencing the extension. This ensures
+ // we don't attempt to cache the image when the load completes.
+ for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end();) {
+ if (i->second == extension) {
+ load_map_.erase(i++);
+ } else {
+ ++i;
+ }
+ }
}