Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: chrome/browser/extensions/image_loading_tracker.cc

Issue 9428025: Add support for multiple icon sizes for Mac platform apps (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/common/chrome_notification_types.h" 9 #include "chrome/common/chrome_notification_types.h"
10 #include "chrome/common/extensions/extension.h" 10 #include "chrome/common/extensions/extension.h"
11 #include "chrome/common/extensions/extension_resource.h" 11 #include "chrome/common/extensions/extension_resource.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/notification_service.h" 13 #include "content/public/browser/notification_service.h"
14 #include "skia/ext/image_operations.h" 14 #include "skia/ext/image_operations.h"
15 #include "third_party/skia/include/core/SkBitmap.h" 15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/image/image.h"
16 #include "webkit/glue/image_decoder.h" 17 #include "webkit/glue/image_decoder.h"
17 18
18 using content::BrowserThread; 19 using content::BrowserThread;
19 20
20 ImageLoadingTracker::Observer::~Observer() {} 21 ImageLoadingTracker::Observer::~Observer() {}
21 22
23 ImageLoadingTracker::ImageInfo::ImageInfo(
24 const ExtensionResource resource, gfx::Size max_size)
25 : resource(resource), max_size(max_size) {
26 }
27
28 ImageLoadingTracker::ImageInfo::~ImageInfo() {
29 }
30
31 ImageLoadingTracker::PendingLoadInfo::PendingLoadInfo()
32 : extension(NULL),
33 pending_count(0) {
34 }
35
36 ImageLoadingTracker::PendingLoadInfo::~PendingLoadInfo() {
37 }
38
22 //////////////////////////////////////////////////////////////////////////////// 39 ////////////////////////////////////////////////////////////////////////////////
23 // ImageLoadingTracker::ImageLoader 40 // ImageLoadingTracker::ImageLoader
Finnur 2012/02/28 10:26:51 nit: Maybe add a similar heading for ImageInfo abo
sail 2012/02/28 23:25:36 Done.
24 41
25 // A RefCounted class for loading images on the File thread and reporting back 42 // A RefCounted class for loading images on the File thread and reporting back
26 // on the UI thread. 43 // on the UI thread.
27 class ImageLoadingTracker::ImageLoader 44 class ImageLoadingTracker::ImageLoader
28 : public base::RefCountedThreadSafe<ImageLoader> { 45 : public base::RefCountedThreadSafe<ImageLoader> {
29 public: 46 public:
30 explicit ImageLoader(ImageLoadingTracker* tracker) 47 explicit ImageLoader(ImageLoadingTracker* tracker)
31 : tracker_(tracker) { 48 : tracker_(tracker) {
32 CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_)); 49 CHECK(BrowserThread::GetCurrentThreadIdentifier(&callback_thread_id_));
33 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE)); 50 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::FILE));
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 // The loader is created lazily and is NULL if the tracker is destroyed before 149 // The loader is created lazily and is NULL if the tracker is destroyed before
133 // any valid image load tasks have been posted. 150 // any valid image load tasks have been posted.
134 if (loader_) 151 if (loader_)
135 loader_->StopTracking(); 152 loader_->StopTracking();
136 } 153 }
137 154
138 void ImageLoadingTracker::LoadImage(const Extension* extension, 155 void ImageLoadingTracker::LoadImage(const Extension* extension,
139 const ExtensionResource& resource, 156 const ExtensionResource& resource,
140 const gfx::Size& max_size, 157 const gfx::Size& max_size,
141 CacheParam cache) { 158 CacheParam cache) {
142 // If we don't have a path we don't need to do any further work, just respond 159 std::vector<ImageInfo> info_list;
143 // back. 160 info_list.push_back(ImageInfo(resource, max_size));
161 LoadImages(extension, info_list, cache);
162 }
163
164 void ImageLoadingTracker::LoadImages(const Extension* extension,
165 const std::vector<ImageInfo>& info_list,
166 CacheParam cache) {
167 PendingLoadInfo load_info;
168 load_info.extension = extension;
169 load_info.cache = cache;
170 load_info.extension_id = extension->id();
171 load_info.pending_count = info_list.size();
144 int id = next_id_++; 172 int id = next_id_++;
145 if (resource.relative_path().empty()) { 173 load_map_[id] = load_info;
146 OnImageLoaded(NULL, resource, max_size, id); 174
147 return; 175 for (std::vector<ImageInfo>::const_iterator it = info_list.begin();
176 it != info_list.end(); ++it) {
177 // If we don't have a path we don't need to do any further work, just
178 // respond back.
179 if (it->resource.relative_path().empty()) {
180 OnImageLoaded(NULL, it->resource, it->max_size, id);
181 continue;
182 }
183
184 DCHECK(extension->path() == it->resource.extension_root());
185
186 // See if the extension has the image already.
187 if (extension->HasCachedImage(it->resource, it->max_size)) {
188 SkBitmap image = extension->GetCachedImage(it->resource, it->max_size);
189 OnImageLoaded(&image, it->resource, it->max_size, id);
190 continue;
191 }
192
193 // Instruct the ImageLoader to load this on the File thread. LoadImage does
194 // not block.
195 if (!loader_)
196 loader_ = new ImageLoader(this);
197 loader_->LoadImage(it->resource, it->max_size, id);
148 } 198 }
149
150 DCHECK(extension->path() == resource.extension_root());
151
152 // See if the extension has the image already.
153 if (extension->HasCachedImage(resource, max_size)) {
154 SkBitmap image = extension->GetCachedImage(resource, max_size);
155 OnImageLoaded(&image, resource, max_size, id);
156 return;
157 }
158
159 if (cache == CACHE)
160 load_map_[id] = extension;
161
162 // Instruct the ImageLoader to load this on the File thread. LoadImage does
163 // not block.
164 if (!loader_)
165 loader_ = new ImageLoader(this);
166 loader_->LoadImage(resource, max_size, id);
167 } 199 }
168 200
169 void ImageLoadingTracker::OnImageLoaded( 201 void ImageLoadingTracker::OnImageLoaded(
170 SkBitmap* image, 202 SkBitmap* image,
171 const ExtensionResource& resource, 203 const ExtensionResource& resource,
172 const gfx::Size& original_size, 204 const gfx::Size& original_size,
173 int id) { 205 int id) {
174 LoadMap::iterator i = load_map_.find(id); 206 LoadMap::iterator it = load_map_.find(id);
175 if (i != load_map_.end()) { 207 DCHECK(it != load_map_.end());
176 i->second->SetCachedImage(resource, image ? *image : SkBitmap(), 208
177 original_size); 209 PendingLoadInfo* info = &it->second;
178 load_map_.erase(i); 210
211 // Save the pending results.
212 DCHECK(info->pending_count > 0);
213 info->pending_count--;
214 if (image)
215 info->bitmaps.push_back(*image);
216
217 // Add to the extension's image cache if requested.
218 DCHECK(info->cache != CACHE || info->extension);
219 if (info->cache == CACHE &&
220 !info->extension->HasCachedImage(resource, original_size)) {
221 info->extension->SetCachedImage(resource, image ? *image : SkBitmap(),
222 original_size);
179 } 223 }
180 224
181 observer_->OnImageLoaded(image, resource, id); 225 // If all pending images are done then report back.
226 if (info->pending_count == 0) {
227 if (info->bitmaps.size() > 0) {
228 std::vector<const SkBitmap*> bitmaps;
229 for (std::vector<SkBitmap>::const_iterator it = info->bitmaps.begin();
230 it != info->bitmaps.end(); ++it) {
231 // gfx::Image takes ownership of this bitmap.
232 bitmaps.push_back(new SkBitmap(*it));
233 }
234 gfx::Image gfx_image(bitmaps);
235 observer_->OnImageLoaded(gfx_image, info->extension_id, id);
236 } else {
237 observer_->OnImageLoaded(gfx::Image(), info->extension_id, id);
238 }
239 load_map_.erase(it);
240 }
182 } 241 }
183 242
184 void ImageLoadingTracker::Observe(int type, 243 void ImageLoadingTracker::Observe(int type,
185 const content::NotificationSource& source, 244 const content::NotificationSource& source,
186 const content::NotificationDetails& details) { 245 const content::NotificationDetails& details) {
187 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED); 246 DCHECK(type == chrome::NOTIFICATION_EXTENSION_UNLOADED);
188 247
189 const Extension* extension = 248 const Extension* extension =
190 content::Details<UnloadedExtensionInfo>(details)->extension; 249 content::Details<UnloadedExtensionInfo>(details)->extension;
191 250
192 // Remove all entries in the load_map_ referencing the extension. This ensures 251 // Remove reference to this extension from all pending load entries. This
193 // we don't attempt to cache the image when the load completes. 252 // ensures we don't attempt to cache the image when the load completes.
194 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end();) { 253 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) {
195 if (i->second == extension) 254 PendingLoadInfo* info = &i->second;
196 load_map_.erase(i++); 255 if (info->extension == extension) {
197 else 256 info->extension = NULL;
198 ++i; 257 info->cache = DONT_CACHE;
258 }
199 } 259 }
200 } 260 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698