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" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 base::Bind(&ImageLoader::LoadOnFileThread, this, image_info, id)); | 133 base::Bind(&ImageLoader::LoadOnFileThread, this, image_info, id)); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void LoadOnFileThread(const ImageRepresentation& image_info, int id) { | 136 void LoadOnFileThread(const ImageRepresentation& image_info, int id) { |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 138 | 138 |
| 139 // Read the file from disk. | 139 // Read the file from disk. |
| 140 std::string file_contents; | 140 std::string file_contents; |
| 141 FilePath path = image_info.resource.GetFilePath(); | 141 FilePath path = image_info.resource.GetFilePath(); |
| 142 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | 142 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
| 143 ReportBack(NULL, image_info, gfx::Size(), id, false); | 143 ReportBack(NULL, image_info, gfx::Size(), id); |
| 144 return; | 144 return; |
| 145 } | 145 } |
| 146 | 146 |
| 147 // Decode the bitmap using WebKit's image decoder. | 147 // Decode the bitmap using WebKit's image decoder. |
| 148 const unsigned char* data = | 148 const unsigned char* data = |
| 149 reinterpret_cast<const unsigned char*>(file_contents.data()); | 149 reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 150 webkit_glue::ImageDecoder decoder; | 150 webkit_glue::ImageDecoder decoder; |
| 151 scoped_ptr<SkBitmap> decoded(new SkBitmap()); | 151 scoped_ptr<SkBitmap> decoded(new SkBitmap()); |
| 152 // Note: This class only decodes bitmaps from extension resources. Chrome | 152 // Note: This class only decodes bitmaps from extension resources. Chrome |
| 153 // doesn't (for security reasons) directly load extension resources provided | 153 // doesn't (for security reasons) directly load extension resources provided |
| 154 // by the extension author, but instead decodes them in a separate | 154 // by the extension author, but instead decodes them in a separate |
| 155 // locked-down utility process. Only if the decoding succeeds is the image | 155 // locked-down utility process. Only if the decoding succeeds is the image |
| 156 // saved from memory to disk and subsequently used in the Chrome UI. | 156 // saved from memory to disk and subsequently used in the Chrome UI. |
| 157 // Chrome is therefore decoding bitmaps here that were generated by Chrome. | 157 // Chrome is therefore decoding bitmaps here that were generated by Chrome. |
| 158 *decoded = decoder.Decode(data, file_contents.length()); | 158 *decoded = decoder.Decode(data, file_contents.length()); |
| 159 if (decoded->empty()) { | 159 if (decoded->empty()) { |
| 160 ReportBack(NULL, image_info, gfx::Size(), id, false); | 160 ReportBack(NULL, image_info, gfx::Size(), id); |
| 161 return; // Unable to decode. | 161 return; // Unable to decode. |
| 162 } | 162 } |
| 163 | 163 |
| 164 gfx::Size original_size(decoded->width(), decoded->height()); | 164 gfx::Size original_size(decoded->width(), decoded->height()); |
| 165 if (ShouldResizeImageRepresentation(image_info.resize_method, | 165 *decoded = ResizeIfNeeded(*decoded, image_info); |
| 166 original_size, | |
| 167 image_info.desired_size)) { | |
| 168 *decoded = skia::ImageOperations::Resize( | |
| 169 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, | |
| 170 image_info.desired_size.width(), image_info.desired_size.height()); | |
| 171 } | |
| 172 | 166 |
| 173 ReportBack(decoded.release(), image_info, original_size, id, | 167 ReportBack(decoded.release(), image_info, original_size, id); |
| 174 true /* delete bitmap */); | |
| 175 } | 168 } |
| 176 | 169 |
| 177 // Instructs the loader to load a resource on the File thread. | 170 // Instructs the loader to load a resource on the File thread. |
| 178 void LoadResource(const ImageRepresentation& image_info, | 171 void LoadResource(const ImageRepresentation& image_info, |
| 179 int id, | 172 int id, |
| 180 int resource_id) { | 173 int resource_id) { |
| 181 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); | 174 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| 182 BrowserThread::PostTask( | 175 BrowserThread::PostTask( |
| 183 BrowserThread::FILE, FROM_HERE, | 176 BrowserThread::FILE, FROM_HERE, |
| 184 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, image_info, | 177 base::Bind(&ImageLoader::LoadResourceOnFileThread, this, image_info, |
| 185 id, resource_id)); | 178 id, resource_id)); |
| 186 } | 179 } |
| 187 | 180 |
| 188 void LoadResourceOnFileThread(const ImageRepresentation& image_info, | 181 void LoadResourceOnFileThread(const ImageRepresentation& image_info, |
| 189 int id, | 182 int id, |
| 190 int resource_id) { | 183 int resource_id) { |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 192 const SkBitmap* bitmap = ResourceBundle::GetSharedInstance().GetImageNamed( | 185 scoped_ptr<SkBitmap> bitmap(new SkBitmap); |
|
tbarzic
2012/08/25 01:46:39
now I wonder why I didn't do this in the first pla
| |
| 186 *bitmap = *ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 193 resource_id).ToSkBitmap(); | 187 resource_id).ToSkBitmap(); |
| 194 ReportBack(bitmap, image_info, image_info.desired_size, id, | 188 |
| 195 false /* don't delete bitmap */); | 189 *bitmap = ResizeIfNeeded(*bitmap, image_info); |
| 190 ReportBack(bitmap.release(), image_info, image_info.desired_size, id); | |
| 196 } | 191 } |
| 197 | 192 |
| 198 void ReportBack(const SkBitmap* bitmap, const ImageRepresentation& image_info, | 193 void ReportBack(const SkBitmap* bitmap, const ImageRepresentation& image_info, |
| 199 const gfx::Size& original_size, int id, bool delete_bitmap) { | 194 const gfx::Size& original_size, int id) { |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 201 | 196 |
| 202 BrowserThread::PostTask( | 197 BrowserThread::PostTask( |
| 203 callback_thread_id_, FROM_HERE, | 198 callback_thread_id_, FROM_HERE, |
| 204 base::Bind(&ImageLoader::ReportOnCallingThread, this, | 199 base::Bind(&ImageLoader::ReportOnCallingThread, this, |
| 205 bitmap, image_info, original_size, id, delete_bitmap)); | 200 bitmap, image_info, original_size, id)); |
| 206 } | 201 } |
| 207 | 202 |
| 208 void ReportOnCallingThread(const SkBitmap* bitmap, | 203 void ReportOnCallingThread(const SkBitmap* bitmap, |
| 209 const ImageRepresentation& image_info, | 204 const ImageRepresentation& image_info, |
| 210 const gfx::Size& original_size, | 205 const gfx::Size& original_size, |
| 211 int id, | 206 int id) { |
| 212 bool delete_bitmap) { | |
| 213 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); | 207 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| 214 | 208 |
| 215 if (tracker_) | 209 if (tracker_) |
| 216 tracker_->OnBitmapLoaded(bitmap, image_info, original_size, id, true); | 210 tracker_->OnBitmapLoaded(bitmap, image_info, original_size, id, true); |
| 217 | 211 |
| 218 if (bitmap && delete_bitmap) | 212 if (bitmap) |
| 219 delete bitmap; | 213 delete bitmap; |
| 220 } | 214 } |
| 221 | 215 |
| 222 private: | 216 private: |
| 223 friend class base::RefCountedThreadSafe<ImageLoader>; | 217 friend class base::RefCountedThreadSafe<ImageLoader>; |
| 224 ~ImageLoader() {} | 218 ~ImageLoader() {} |
| 225 | 219 |
| 220 SkBitmap ResizeIfNeeded(const SkBitmap& bitmap, | |
| 221 const ImageRepresentation& image_info) { | |
| 222 gfx::Size original_size(bitmap.width(), bitmap.height()); | |
| 223 if (ShouldResizeImageRepresentation(image_info.resize_method, | |
| 224 original_size, | |
| 225 image_info.desired_size)) { | |
| 226 return skia::ImageOperations::Resize( | |
| 227 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, | |
| 228 image_info.desired_size.width(), image_info.desired_size.height()); | |
| 229 } | |
| 230 | |
| 231 return bitmap; | |
| 232 } | |
| 233 | |
| 226 // The tracker we are loading the bitmap for. If NULL, it means the tracker is | 234 // The tracker we are loading the bitmap for. If NULL, it means the tracker is |
| 227 // no longer interested in the reply. | 235 // no longer interested in the reply. |
| 228 ImageLoadingTracker* tracker_; | 236 ImageLoadingTracker* tracker_; |
| 229 | 237 |
| 230 // The thread that we need to call back on to report that we are done. | 238 // The thread that we need to call back on to report that we are done. |
| 231 BrowserThread::ID callback_thread_id_; | 239 BrowserThread::ID callback_thread_id_; |
| 232 | 240 |
| 233 DISALLOW_COPY_AND_ASSIGN(ImageLoader); | 241 DISALLOW_COPY_AND_ASSIGN(ImageLoader); |
| 234 }; | 242 }; |
| 235 | 243 |
| 236 //////////////////////////////////////////////////////////////////////////////// | 244 //////////////////////////////////////////////////////////////////////////////// |
| 237 // ImageLoadingTracker | 245 // ImageLoadingTracker |
| 238 | 246 |
| 247 // static | |
| 248 bool ImageLoadingTracker::IsSpecialBundledExtensionId( | |
| 249 const std::string& extension_id) { | |
| 250 int resource_id = -1; | |
| 251 return FindSpecialExtensionResourceId(extension_id, &resource_id); | |
| 252 } | |
| 253 | |
| 239 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) | 254 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) |
| 240 : observer_(observer), | 255 : observer_(observer), |
| 241 next_id_(0) { | 256 next_id_(0) { |
| 242 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 257 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 243 content::NotificationService::AllSources()); | 258 content::NotificationService::AllSources()); |
| 244 } | 259 } |
| 245 | 260 |
| 246 ImageLoadingTracker::~ImageLoadingTracker() { | 261 ImageLoadingTracker::~ImageLoadingTracker() { |
| 247 // The loader is created lazily and is NULL if the tracker is destroyed before | 262 // The loader is created lazily and is NULL if the tracker is destroyed before |
| 248 // any valid image load tasks have been posted. | 263 // any valid image load tasks have been posted. |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 // Remove reference to this extension from all pending load entries. This | 409 // Remove reference to this extension from all pending load entries. This |
| 395 // ensures we don't attempt to cache the bitmap when the load completes. | 410 // ensures we don't attempt to cache the bitmap when the load completes. |
| 396 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 411 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| 397 PendingLoadInfo* info = &i->second; | 412 PendingLoadInfo* info = &i->second; |
| 398 if (info->extension == extension) { | 413 if (info->extension == extension) { |
| 399 info->extension = NULL; | 414 info->extension = NULL; |
| 400 info->cache = DONT_CACHE; | 415 info->cache = DONT_CACHE; |
| 401 } | 416 } |
| 402 } | 417 } |
| 403 } | 418 } |
| OLD | NEW |