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 "app/gfx/favicon_size.h" | |
8 #include "base/file_util.h" | 7 #include "base/file_util.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/browser_process.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 // |callback_loop| 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. |index| is an | 30 // decode it. |path| is the path to load the image from. |max_size| is the |
31 // identifier for the image that we pass back to the caller. | 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 |
| 33 // caller. |
32 LoadImageTask(ImageLoadingTracker* tracker, | 34 LoadImageTask(ImageLoadingTracker* tracker, |
33 const ExtensionResource& resource, | 35 const ExtensionResource& resource, |
| 36 const gfx::Size& max_size, |
34 size_t index) | 37 size_t index) |
35 : callback_loop_(MessageLoop::current()), | 38 : callback_loop_(MessageLoop::current()), |
36 tracker_(tracker), | 39 tracker_(tracker), |
37 resource_(resource), | 40 resource_(resource), |
| 41 max_size_(max_size), |
38 index_(index) {} | 42 index_(index) {} |
39 | 43 |
40 void ReportBack(SkBitmap* image) { | 44 void ReportBack(SkBitmap* image) { |
41 callback_loop_->PostTask(FROM_HERE, NewRunnableMethod(tracker_, | 45 callback_loop_->PostTask(FROM_HERE, NewRunnableMethod(tracker_, |
42 &ImageLoadingTracker::OnImageLoaded, | 46 &ImageLoadingTracker::OnImageLoaded, |
43 image, | 47 image, |
44 index_)); | 48 index_)); |
45 } | 49 } |
46 | 50 |
47 virtual void Run() { | 51 virtual void Run() { |
48 // Read the file from disk. | 52 // Read the file from disk. |
49 std::string file_contents; | 53 std::string file_contents; |
50 FilePath path = resource_.GetFilePath(); | 54 FilePath path = resource_.GetFilePath(); |
51 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { | 55 if (path.empty() || !file_util::ReadFileToString(path, &file_contents)) { |
52 ReportBack(NULL); | 56 ReportBack(NULL); |
53 return; | 57 return; |
54 } | 58 } |
55 | 59 |
56 // Decode the image using WebKit's image decoder. | 60 // Decode the image using WebKit's image decoder. |
57 const unsigned char* data = | 61 const unsigned char* data = |
58 reinterpret_cast<const unsigned char*>(file_contents.data()); | 62 reinterpret_cast<const unsigned char*>(file_contents.data()); |
59 webkit_glue::ImageDecoder decoder(gfx::Size(kFavIconSize, kFavIconSize)); | 63 webkit_glue::ImageDecoder decoder; |
60 scoped_ptr<SkBitmap> decoded(new SkBitmap()); | 64 scoped_ptr<SkBitmap> decoded(new SkBitmap()); |
61 *decoded = decoder.Decode(data, file_contents.length()); | 65 *decoded = decoder.Decode(data, file_contents.length()); |
62 if (decoded->empty()) { | 66 if (decoded->empty()) { |
63 ReportBack(NULL); | 67 ReportBack(NULL); |
64 return; // Unable to decode. | 68 return; // Unable to decode. |
65 } | 69 } |
66 | 70 |
67 if (decoded->width() != kFavIconSize || decoded->height() != kFavIconSize) { | 71 if (decoded->width() > max_size_.width() || |
68 // The bitmap is not the correct size, re-sample. | 72 decoded->height() > max_size_.height()) { |
69 int new_width = decoded->width(); | 73 // The bitmap is too big, re-sample. |
70 int new_height = decoded->height(); | |
71 // Calculate what dimensions to use within the constraints (16x16 max). | |
72 calc_favicon_target_size(&new_width, &new_height); | |
73 *decoded = skia::ImageOperations::Resize( | 74 *decoded = skia::ImageOperations::Resize( |
74 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, | 75 *decoded, skia::ImageOperations::RESIZE_LANCZOS3, |
75 new_width, new_height); | 76 max_size_.width(), max_size_.height()); |
76 } | 77 } |
77 | 78 |
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 image resource to load asynchronously. | 89 // The image resource to load asynchronously. |
89 ExtensionResource resource_; | 90 ExtensionResource resource_; |
90 | 91 |
| 92 // The max size for the loaded image. |
| 93 gfx::Size max_size_; |
| 94 |
91 // The index of the icon being loaded. | 95 // The index of the icon being loaded. |
92 size_t index_; | 96 size_t index_; |
93 }; | 97 }; |
94 | 98 |
95 //////////////////////////////////////////////////////////////////////////////// | 99 //////////////////////////////////////////////////////////////////////////////// |
96 // ImageLoadingTracker | 100 // ImageLoadingTracker |
97 | 101 |
98 void ImageLoadingTracker::PostLoadImageTask(const ExtensionResource& resource) { | 102 void ImageLoadingTracker::PostLoadImageTask(const ExtensionResource& resource, |
| 103 const gfx::Size& max_size) { |
99 MessageLoop* file_loop = g_browser_process->file_thread()->message_loop(); | 104 MessageLoop* file_loop = g_browser_process->file_thread()->message_loop(); |
100 file_loop->PostTask(FROM_HERE, new LoadImageTask(this, resource, | 105 file_loop->PostTask(FROM_HERE, new LoadImageTask(this, resource, max_size, |
101 posted_count_++)); | 106 posted_count_++)); |
102 } | 107 } |
103 | 108 |
104 void ImageLoadingTracker::OnImageLoaded(SkBitmap* image, size_t index) { | 109 void ImageLoadingTracker::OnImageLoaded(SkBitmap* image, size_t index) { |
105 if (observer_) | 110 if (observer_) |
106 observer_->OnImageLoaded(image, index); | 111 observer_->OnImageLoaded(image, index); |
107 | 112 |
108 if (image) | 113 if (image) |
109 delete image; | 114 delete image; |
110 | 115 |
111 if (--image_count_ == 0) | 116 if (--image_count_ == 0) |
112 Release(); // We are no longer needed. | 117 Release(); // We are no longer needed. |
113 } | 118 } |
OLD | NEW |