| 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 // TODO(xiyuan): Clean up to use SkBitmap here and in LoadOnFileThread. |
| 193 resource_id).ToSkBitmap(); | 186 scoped_ptr<SkBitmap> bitmap(new SkBitmap); |
| 194 ReportBack(bitmap, image_info, image_info.desired_size, id, | 187 *bitmap = ResourceBundle::GetSharedInstance().GetImageNamed( |
| 195 false /* don't delete bitmap */); | 188 resource_id).AsBitmap(); |
| 189 |
| 190 *bitmap = ResizeIfNeeded(*bitmap, image_info); |
| 191 ReportBack(bitmap.release(), image_info, image_info.desired_size, id); |
| 196 } | 192 } |
| 197 | 193 |
| 198 void ReportBack(const SkBitmap* bitmap, const ImageRepresentation& image_info, | 194 void ReportBack(const SkBitmap* bitmap, const ImageRepresentation& image_info, |
| 199 const gfx::Size& original_size, int id, bool delete_bitmap) { | 195 const gfx::Size& original_size, int id) { |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 201 | 197 |
| 202 BrowserThread::PostTask( | 198 BrowserThread::PostTask( |
| 203 callback_thread_id_, FROM_HERE, | 199 callback_thread_id_, FROM_HERE, |
| 204 base::Bind(&ImageLoader::ReportOnCallingThread, this, | 200 base::Bind(&ImageLoader::ReportOnCallingThread, this, |
| 205 bitmap, image_info, original_size, id, delete_bitmap)); | 201 bitmap, image_info, original_size, id)); |
| 206 } | 202 } |
| 207 | 203 |
| 208 void ReportOnCallingThread(const SkBitmap* bitmap, | 204 void ReportOnCallingThread(const SkBitmap* bitmap, |
| 209 const ImageRepresentation& image_info, | 205 const ImageRepresentation& image_info, |
| 210 const gfx::Size& original_size, | 206 const gfx::Size& original_size, |
| 211 int id, | 207 int id) { |
| 212 bool delete_bitmap) { | |
| 213 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); | 208 DCHECK(BrowserThread::CurrentlyOn(callback_thread_id_)); |
| 214 | 209 |
| 215 if (tracker_) | 210 if (tracker_) |
| 216 tracker_->OnBitmapLoaded(bitmap, image_info, original_size, id, true); | 211 tracker_->OnBitmapLoaded(bitmap, image_info, original_size, id, true); |
| 217 | 212 |
| 218 if (bitmap && delete_bitmap) | 213 if (bitmap) |
| 219 delete bitmap; | 214 delete bitmap; |
| 220 } | 215 } |
| 221 | 216 |
| 222 private: | 217 private: |
| 223 friend class base::RefCountedThreadSafe<ImageLoader>; | 218 friend class base::RefCountedThreadSafe<ImageLoader>; |
| 224 ~ImageLoader() {} | 219 ~ImageLoader() {} |
| 225 | 220 |
| 221 SkBitmap ResizeIfNeeded(const SkBitmap& bitmap, |
| 222 const ImageRepresentation& image_info) { |
| 223 gfx::Size original_size(bitmap.width(), bitmap.height()); |
| 224 if (ShouldResizeImageRepresentation(image_info.resize_method, |
| 225 original_size, |
| 226 image_info.desired_size)) { |
| 227 return skia::ImageOperations::Resize( |
| 228 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, |
| 229 image_info.desired_size.width(), image_info.desired_size.height()); |
| 230 } |
| 231 |
| 232 return bitmap; |
| 233 } |
| 234 |
| 226 // The tracker we are loading the bitmap for. If NULL, it means the tracker is | 235 // The tracker we are loading the bitmap for. If NULL, it means the tracker is |
| 227 // no longer interested in the reply. | 236 // no longer interested in the reply. |
| 228 ImageLoadingTracker* tracker_; | 237 ImageLoadingTracker* tracker_; |
| 229 | 238 |
| 230 // The thread that we need to call back on to report that we are done. | 239 // The thread that we need to call back on to report that we are done. |
| 231 BrowserThread::ID callback_thread_id_; | 240 BrowserThread::ID callback_thread_id_; |
| 232 | 241 |
| 233 DISALLOW_COPY_AND_ASSIGN(ImageLoader); | 242 DISALLOW_COPY_AND_ASSIGN(ImageLoader); |
| 234 }; | 243 }; |
| 235 | 244 |
| 236 //////////////////////////////////////////////////////////////////////////////// | 245 //////////////////////////////////////////////////////////////////////////////// |
| 237 // ImageLoadingTracker | 246 // ImageLoadingTracker |
| 238 | 247 |
| 248 // static |
| 249 bool ImageLoadingTracker::IsSpecialBundledExtensionId( |
| 250 const std::string& extension_id) { |
| 251 int resource_id = -1; |
| 252 return FindSpecialExtensionResourceId(extension_id, &resource_id); |
| 253 } |
| 254 |
| 239 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) | 255 ImageLoadingTracker::ImageLoadingTracker(Observer* observer) |
| 240 : observer_(observer), | 256 : observer_(observer), |
| 241 next_id_(0) { | 257 next_id_(0) { |
| 242 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 258 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 243 content::NotificationService::AllSources()); | 259 content::NotificationService::AllSources()); |
| 244 } | 260 } |
| 245 | 261 |
| 246 ImageLoadingTracker::~ImageLoadingTracker() { | 262 ImageLoadingTracker::~ImageLoadingTracker() { |
| 247 // The loader is created lazily and is NULL if the tracker is destroyed before | 263 // The loader is created lazily and is NULL if the tracker is destroyed before |
| 248 // any valid image load tasks have been posted. | 264 // any valid image load tasks have been posted. |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 // Remove reference to this extension from all pending load entries. This | 412 // Remove reference to this extension from all pending load entries. This |
| 397 // ensures we don't attempt to cache the bitmap when the load completes. | 413 // ensures we don't attempt to cache the bitmap when the load completes. |
| 398 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { | 414 for (LoadMap::iterator i = load_map_.begin(); i != load_map_.end(); ++i) { |
| 399 PendingLoadInfo* info = &i->second; | 415 PendingLoadInfo* info = &i->second; |
| 400 if (info->extension == extension) { | 416 if (info->extension == extension) { |
| 401 info->extension = NULL; | 417 info->extension = NULL; |
| 402 info->cache = DONT_CACHE; | 418 info->cache = DONT_CACHE; |
| 403 } | 419 } |
| 404 } | 420 } |
| 405 } | 421 } |
| OLD | NEW |