| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/gfx/size.h" | 8 #include "base/gfx/size.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "base/task.h" | 12 #include "base/task.h" |
| 13 #include "base/thread.h" | 13 #include "base/thread.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/chrome_thread.h" |
| 15 #include "chrome/common/extensions/extension_resource.h" | 15 #include "chrome/common/extensions/extension_resource.h" |
| 16 #include "skia/ext/image_operations.h" | 16 #include "skia/ext/image_operations.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "webkit/glue/image_decoder.h" | 18 #include "webkit/glue/image_decoder.h" |
| 19 | 19 |
| 20 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
| 21 // ImageLoadingTracker::LoadImageTask | 21 // ImageLoadingTracker::LoadImageTask |
| 22 | 22 |
| 23 // The LoadImageTask is for asynchronously loading the image on the file thread. | 23 // The LoadImageTask is for asynchronously loading the image on the file thread. |
| 24 // If the image is successfully loaded and decoded it will report back on the | 24 // If the image is successfully loaded and decoded it will report back on the |
| 25 // |callback_loop| to let the caller know the image is done loading. | 25 // calling thread to let the caller know the image is done loading. |
| 26 class ImageLoadingTracker::LoadImageTask : public Task { | 26 class ImageLoadingTracker::LoadImageTask : public Task { |
| 27 public: | 27 public: |
| 28 // Constructor for the LoadImageTask class. |tracker| is the object that | 28 // Constructor for the LoadImageTask class. |tracker| is the object that |
| 29 // we use to communicate back to the entity that wants the image after we | 29 // we use to communicate back to the entity that wants the image after we |
| 30 // decode it. |path| is the path to load the image from. |max_size| is the | 30 // decode it. |path| is the path to load the image from. |max_size| is the |
| 31 // maximum size for the loaded image. It will be resized to fit this if | 31 // maximum size for the loaded image. It will be resized to fit this if |
| 32 // larger. |index| is an identifier for the image that we pass back to the | 32 // larger. |index| is an identifier for the image that we pass back to the |
| 33 // caller. | 33 // caller. |
| 34 LoadImageTask(ImageLoadingTracker* tracker, | 34 LoadImageTask(ImageLoadingTracker* tracker, |
| 35 const ExtensionResource& resource, | 35 const ExtensionResource& resource, |
| 36 const gfx::Size& max_size, | 36 const gfx::Size& max_size, |
| 37 size_t index) | 37 size_t index) |
| 38 : callback_loop_(MessageLoop::current()), | 38 : tracker_(tracker), |
| 39 tracker_(tracker), | |
| 40 resource_(resource), | 39 resource_(resource), |
| 41 max_size_(max_size), | 40 max_size_(max_size), |
| 42 index_(index) {} | 41 index_(index) { |
| 42 CHECK(ChromeThread::GetCurrentThreadIdentifier(&callback_thread_id_)); |
| 43 } |
| 43 | 44 |
| 44 void ReportBack(SkBitmap* image) { | 45 void ReportBack(SkBitmap* image) { |
| 45 callback_loop_->PostTask(FROM_HERE, NewRunnableMethod(tracker_, | 46 ChromeThread::PostTask( |
| 46 &ImageLoadingTracker::OnImageLoaded, | 47 callback_thread_id_, FROM_HERE, |
| 47 image, | 48 NewRunnableMethod( |
| 48 index_)); | 49 tracker_, &ImageLoadingTracker::OnImageLoaded, image, index_)); |
| 49 } | 50 } |
| 50 | 51 |
| 51 virtual void Run() { | 52 virtual void Run() { |
| 52 // Read the file from disk. | 53 // Read the file from disk. |
| 53 std::string file_contents; | 54 std::string file_contents; |
| 54 FilePath path = resource_.GetFilePath(); | 55 FilePath path = resource_.GetFilePath(); |
| 55 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | 56 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
| 56 ReportBack(NULL); | 57 ReportBack(NULL); |
| 57 return; | 58 return; |
| 58 } | 59 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 73 // The bitmap is too big, re-sample. | 74 // The bitmap is too big, re-sample. |
| 74 *decoded = skia::ImageOperations::Resize( | 75 *decoded = skia::ImageOperations::Resize( |
| 75 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, | 76 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, |
| 76 max_size_.width(), max_size_.height()); | 77 max_size_.width(), max_size_.height()); |
| 77 } | 78 } |
| 78 | 79 |
| 79 ReportBack(decoded.release()); | 80 ReportBack(decoded.release()); |
| 80 } | 81 } |
| 81 | 82 |
| 82 private: | 83 private: |
| 83 // The message loop that we need to call back on to report that we are done. | 84 // The thread that we need to call back on to report that we are done. |
| 84 MessageLoop* callback_loop_; | 85 ChromeThread::ID callback_thread_id_; |
| 85 | 86 |
| 86 // The object that is waiting for us to respond back. | 87 // The object that is waiting for us to respond back. |
| 87 ImageLoadingTracker* tracker_; | 88 ImageLoadingTracker* tracker_; |
| 88 | 89 |
| 89 // The image resource to load asynchronously. | 90 // The image resource to load asynchronously. |
| 90 ExtensionResource resource_; | 91 ExtensionResource resource_; |
| 91 | 92 |
| 92 // The max size for the loaded image. | 93 // The max size for the loaded image. |
| 93 gfx::Size max_size_; | 94 gfx::Size max_size_; |
| 94 | 95 |
| 95 // The index of the icon being loaded. | 96 // The index of the icon being loaded. |
| 96 size_t index_; | 97 size_t index_; |
| 97 }; | 98 }; |
| 98 | 99 |
| 99 //////////////////////////////////////////////////////////////////////////////// | 100 //////////////////////////////////////////////////////////////////////////////// |
| 100 // ImageLoadingTracker | 101 // ImageLoadingTracker |
| 101 | 102 |
| 102 void ImageLoadingTracker::PostLoadImageTask(const ExtensionResource& resource, | 103 void ImageLoadingTracker::PostLoadImageTask(const ExtensionResource& resource, |
| 103 const gfx::Size& max_size) { | 104 const gfx::Size& max_size) { |
| 104 MessageLoop* file_loop = g_browser_process->file_thread()->message_loop(); | 105 ChromeThread::PostTask( |
| 105 file_loop->PostTask(FROM_HERE, new LoadImageTask(this, resource, max_size, | 106 ChromeThread::FILE, FROM_HERE, |
| 106 posted_count_++)); | 107 new LoadImageTask(this, resource, max_size, posted_count_++)); |
| 107 } | 108 } |
| 108 | 109 |
| 109 void ImageLoadingTracker::OnImageLoaded(SkBitmap* image, size_t index) { | 110 void ImageLoadingTracker::OnImageLoaded(SkBitmap* image, size_t index) { |
| 110 if (observer_) | 111 if (observer_) |
| 111 observer_->OnImageLoaded(image, index); | 112 observer_->OnImageLoaded(image, index); |
| 112 | 113 |
| 113 if (image) | 114 if (image) |
| 114 delete image; | 115 delete image; |
| 115 | 116 |
| 116 if (--image_count_ == 0) | 117 if (--image_count_ == 0) |
| 117 Release(); // We are no longer needed. | 118 Release(); // We are no longer needed. |
| 118 } | 119 } |
| OLD | NEW |