| 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/image_loading_tracker.h" | 5 #include "chrome/browser/extensions/image_loading_tracker.h" |
| 6 | 6 |
| 7 #include "app/gfx/favicon_size.h" | 7 #include "app/gfx/favicon_size.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.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/browser_process.h" |
| 15 #include "chrome/common/extensions/extension_resource.h" |
| 15 #include "skia/ext/image_operations.h" | 16 #include "skia/ext/image_operations.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "webkit/glue/image_decoder.h" | 18 #include "webkit/glue/image_decoder.h" |
| 18 | 19 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
| 20 // ImageLoadingTracker::LoadImageTask | 21 // ImageLoadingTracker::LoadImageTask |
| 21 | 22 |
| 22 // 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. |
| 23 // 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 |
| 24 // |callback_loop| to let the caller know the image is done loading. | 25 // |callback_loop| to let the caller know the image is done loading. |
| 25 class ImageLoadingTracker::LoadImageTask : public Task { | 26 class ImageLoadingTracker::LoadImageTask : public Task { |
| 26 public: | 27 public: |
| 27 // Constructor for the LoadImageTask class. |tracker| is the object that | 28 // Constructor for the LoadImageTask class. |tracker| is the object that |
| 28 // 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 |
| 29 // decode it. |path| is the path to load the image from. |index| is an | 30 // decode it. |path| is the path to load the image from. |index| is an |
| 30 // identifier for the image that we pass back to the caller. | 31 // identifier for the image that we pass back to the caller. |
| 31 LoadImageTask(ImageLoadingTracker* tracker, | 32 LoadImageTask(ImageLoadingTracker* tracker, |
| 32 const FilePath& path, | 33 const ExtensionResource& resource, |
| 33 size_t index) | 34 size_t index) |
| 34 : callback_loop_(MessageLoop::current()), | 35 : callback_loop_(MessageLoop::current()), |
| 35 tracker_(tracker), | 36 tracker_(tracker), |
| 36 path_(path), | 37 resource_(resource), |
| 37 index_(index) {} | 38 index_(index) {} |
| 38 | 39 |
| 39 void ReportBack(SkBitmap* image) { | 40 void ReportBack(SkBitmap* image) { |
| 40 DCHECK(image); | 41 DCHECK(image); |
| 41 callback_loop_->PostTask(FROM_HERE, NewRunnableMethod(tracker_, | 42 callback_loop_->PostTask(FROM_HERE, NewRunnableMethod(tracker_, |
| 42 &ImageLoadingTracker::OnImageLoaded, | 43 &ImageLoadingTracker::OnImageLoaded, |
| 43 image, | 44 image, |
| 44 index_)); | 45 index_)); |
| 45 } | 46 } |
| 46 | 47 |
| 47 virtual void Run() { | 48 virtual void Run() { |
| 48 // Read the file from disk. | 49 // Read the file from disk. |
| 49 std::string file_contents; | 50 std::string file_contents; |
| 50 if (!file_util::PathExists(path_) || | 51 FilePath path = resource_.GetFilePath(); |
| 51 !file_util::ReadFileToString(path_, &file_contents)) { | 52 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
| 52 ReportBack(NULL); | 53 ReportBack(NULL); |
| 53 return; | 54 return; |
| 54 } | 55 } |
| 55 | 56 |
| 56 // Decode the image using WebKit's image decoder. | 57 // Decode the image using WebKit's image decoder. |
| 57 const unsigned char* data = | 58 const unsigned char* data = |
| 58 reinterpret_cast<const unsigned char*>(file_contents.data()); | 59 reinterpret_cast<const unsigned char*>(file_contents.data()); |
| 59 webkit_glue::ImageDecoder decoder(gfx::Size(kFavIconSize, kFavIconSize)); | 60 webkit_glue::ImageDecoder decoder(gfx::Size(kFavIconSize, kFavIconSize)); |
| 60 scoped_ptr<SkBitmap> decoded(new SkBitmap()); | 61 scoped_ptr<SkBitmap> decoded(new SkBitmap()); |
| 61 *decoded = decoder.Decode(data, file_contents.length()); | 62 *decoded = decoder.Decode(data, file_contents.length()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 78 ReportBack(decoded.release()); | 79 ReportBack(decoded.release()); |
| 79 } | 80 } |
| 80 | 81 |
| 81 private: | 82 private: |
| 82 // The message loop that we need to call back on to report that we are done. | 83 // The message loop that we need to call back on to report that we are done. |
| 83 MessageLoop* callback_loop_; | 84 MessageLoop* callback_loop_; |
| 84 | 85 |
| 85 // The object that is waiting for us to respond back. | 86 // The object that is waiting for us to respond back. |
| 86 ImageLoadingTracker* tracker_; | 87 ImageLoadingTracker* tracker_; |
| 87 | 88 |
| 88 // The path to the image to load asynchronously. | 89 // The image resource to load asynchronously. |
| 89 FilePath path_; | 90 ExtensionResource resource_; |
| 90 | 91 |
| 91 // The index of the icon being loaded. | 92 // The index of the icon being loaded. |
| 92 size_t index_; | 93 size_t index_; |
| 93 }; | 94 }; |
| 94 | 95 |
| 95 //////////////////////////////////////////////////////////////////////////////// | 96 //////////////////////////////////////////////////////////////////////////////// |
| 96 // ImageLoadingTracker | 97 // ImageLoadingTracker |
| 97 | 98 |
| 98 void ImageLoadingTracker::PostLoadImageTask(FilePath path) { | 99 void ImageLoadingTracker::PostLoadImageTask(const ExtensionResource& resource) { |
| 99 MessageLoop* file_loop = g_browser_process->file_thread()->message_loop(); | 100 MessageLoop* file_loop = g_browser_process->file_thread()->message_loop(); |
| 100 file_loop->PostTask(FROM_HERE, new LoadImageTask(this, path, | 101 file_loop->PostTask(FROM_HERE, new LoadImageTask(this, resource, |
| 101 posted_count_++)); | 102 posted_count_++)); |
| 102 } | 103 } |
| 103 | 104 |
| 104 void ImageLoadingTracker::OnImageLoaded(SkBitmap* image, size_t index) { | 105 void ImageLoadingTracker::OnImageLoaded(SkBitmap* image, size_t index) { |
| 105 if (image == NULL) { | 106 if (image == NULL) { |
| 106 NOTREACHED() << "Image failed to decode."; | 107 NOTREACHED() << "Image failed to decode."; |
| 107 image = new SkBitmap(); | 108 image = new SkBitmap(); |
| 108 } | 109 } |
| 109 if (observer_) | 110 if (observer_) |
| 110 observer_->OnImageLoaded(image, index); | 111 observer_->OnImageLoaded(image, index); |
| 111 delete image; | 112 delete image; |
| 112 if (--image_count_ == 0) | 113 if (--image_count_ == 0) |
| 113 Release(); // We are no longer needed. | 114 Release(); // We are no longer needed. |
| 114 } | 115 } |
| OLD | NEW |