OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/chromeos/login/user_image_loader.h" | 5 #include "chrome/browser/chromeos/login/user_image_loader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "chrome/browser/chromeos/login/helper.h" | 14 #include "chrome/browser/chromeos/login/helper.h" |
15 #include "chrome/browser/chromeos/login/user_image.h" | 15 #include "chrome/browser/chromeos/login/user_image.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 "ui/gfx/codec/png_codec.h" | 18 #include "ui/gfx/codec/png_codec.h" |
19 #include "ui/gfx/skbitmap_operations.h" | 19 #include "ui/gfx/skbitmap_operations.h" |
20 | 20 |
21 namespace chromeos { | 21 namespace chromeos { |
22 | 22 |
23 UserImageLoader::ImageInfo::ImageInfo(int size, | 23 UserImageLoader::ImageInfo::ImageInfo(const std::string& file_path, |
| 24 int pixels_per_side, |
24 const LoadedCallback& loaded_cb) | 25 const LoadedCallback& loaded_cb) |
25 : size(size), | 26 : file_path(file_path), |
| 27 pixels_per_side(pixels_per_side), |
26 loaded_cb(loaded_cb) { | 28 loaded_cb(loaded_cb) { |
27 } | 29 } |
28 | 30 |
29 UserImageLoader::ImageInfo::~ImageInfo() { | 31 UserImageLoader::ImageInfo::~ImageInfo() { |
30 } | 32 } |
31 | 33 |
32 UserImageLoader::UserImageLoader( | 34 UserImageLoader::UserImageLoader( |
33 ImageDecoder::ImageCodec image_codec, | 35 ImageDecoder::ImageCodec image_codec, |
34 scoped_refptr<base::SequencedTaskRunner> background_task_runner) | 36 scoped_refptr<base::SequencedTaskRunner> background_task_runner) |
35 : foreground_task_runner_(base::MessageLoopProxy::current()), | 37 : foreground_task_runner_(base::MessageLoopProxy::current()), |
36 background_task_runner_(background_task_runner), | 38 background_task_runner_(background_task_runner), |
37 image_codec_(image_codec) { | 39 image_codec_(image_codec) { |
38 } | 40 } |
39 | 41 |
40 UserImageLoader::~UserImageLoader() { | 42 UserImageLoader::~UserImageLoader() { |
41 } | 43 } |
42 | 44 |
43 void UserImageLoader::Start(const std::string& filepath, | 45 void UserImageLoader::Start(const std::string& filepath, |
44 int size, | 46 int pixels_per_side, |
45 const LoadedCallback& loaded_cb) { | 47 const LoadedCallback& loaded_cb) { |
46 background_task_runner_->PostTask( | 48 background_task_runner_->PostTask( |
47 FROM_HERE, | 49 FROM_HERE, |
48 base::Bind(&UserImageLoader::ReadAndDecodeImage, | 50 base::Bind(&UserImageLoader::ReadAndDecodeImage, |
49 this, | 51 this, |
50 filepath, | 52 ImageInfo(filepath, pixels_per_side, loaded_cb))); |
51 ImageInfo(size, loaded_cb))); | |
52 } | 53 } |
53 | 54 |
54 void UserImageLoader::Start(scoped_ptr<std::string> data, | 55 void UserImageLoader::Start(scoped_ptr<std::string> data, |
55 int size, | 56 int pixels_per_side, |
56 const LoadedCallback& loaded_cb) { | 57 const LoadedCallback& loaded_cb) { |
57 background_task_runner_->PostTask(FROM_HERE, | 58 background_task_runner_->PostTask( |
58 base::Bind(&UserImageLoader::DecodeImage, | 59 FROM_HERE, |
59 this, | 60 base::Bind(&UserImageLoader::DecodeImage, |
60 base::Passed(&data), | 61 this, |
61 ImageInfo(size, loaded_cb))); | 62 base::Passed(&data), |
| 63 ImageInfo(std::string(), pixels_per_side, loaded_cb))); |
62 } | 64 } |
63 | 65 |
64 void UserImageLoader::ReadAndDecodeImage(const std::string& filepath, | 66 void UserImageLoader::ReadAndDecodeImage(const ImageInfo& image_info) { |
65 const ImageInfo& image_info) { | |
66 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 67 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
67 | 68 |
68 scoped_ptr<std::string> data(new std::string); | 69 scoped_ptr<std::string> data(new std::string); |
69 const bool success = | 70 const bool success = |
70 base::ReadFileToString(base::FilePath(filepath), data.get()); | 71 base::ReadFileToString(base::FilePath(image_info.file_path), data.get()); |
71 DCHECK(success); | 72 DCHECK(success); |
72 | 73 |
73 DecodeImage(data.Pass(), image_info); | 74 DecodeImage(data.Pass(), image_info); |
74 } | 75 } |
75 | 76 |
76 void UserImageLoader::DecodeImage(const scoped_ptr<std::string> data, | 77 void UserImageLoader::DecodeImage(const scoped_ptr<std::string> data, |
77 const ImageInfo& image_info) { | 78 const ImageInfo& image_info) { |
78 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 79 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
79 | 80 |
80 scoped_refptr<ImageDecoder> image_decoder = | 81 scoped_refptr<ImageDecoder> image_decoder = |
81 new ImageDecoder(this, *data, image_codec_); | 82 new ImageDecoder(this, *data, image_codec_); |
82 image_info_map_.insert(std::make_pair(image_decoder.get(), image_info)); | 83 image_info_map_.insert(std::make_pair(image_decoder.get(), image_info)); |
83 image_decoder->Start(background_task_runner_); | 84 image_decoder->Start(background_task_runner_); |
84 } | 85 } |
85 | 86 |
86 void UserImageLoader::OnImageDecoded(const ImageDecoder* decoder, | 87 void UserImageLoader::OnImageDecoded(const ImageDecoder* decoder, |
87 const SkBitmap& decoded_image) { | 88 const SkBitmap& decoded_image) { |
88 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 89 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
89 | 90 |
90 ImageInfoMap::iterator it = image_info_map_.find(decoder); | 91 ImageInfoMap::iterator it = image_info_map_.find(decoder); |
91 if (it == image_info_map_.end()) { | 92 if (it == image_info_map_.end()) { |
92 NOTREACHED(); | 93 NOTREACHED(); |
93 return; | 94 return; |
94 } | 95 } |
95 const int target_size = it->second.size; | 96 const std::string file_path = it->second.file_path; |
| 97 const int target_size = it->second.pixels_per_side; |
96 const LoadedCallback loaded_cb = it->second.loaded_cb; | 98 const LoadedCallback loaded_cb = it->second.loaded_cb; |
97 image_info_map_.erase(it); | 99 image_info_map_.erase(it); |
98 | 100 |
99 SkBitmap final_image = decoded_image; | 101 SkBitmap final_image = decoded_image; |
100 | 102 |
101 if (target_size > 0) { | 103 if (target_size > 0) { |
102 // Auto crop the image, taking the largest square in the center. | 104 // Auto crop the image, taking the largest square in the center. |
103 int size = std::min(decoded_image.width(), decoded_image.height()); | 105 int pixels_per_side = |
104 int x = (decoded_image.width() - size) / 2; | 106 std::min(decoded_image.width(), decoded_image.height()); |
105 int y = (decoded_image.height() - size) / 2; | 107 int x = (decoded_image.width() - pixels_per_side) / 2; |
106 SkBitmap cropped_image = | 108 int y = (decoded_image.height() - pixels_per_side) / 2; |
107 SkBitmapOperations::CreateTiledBitmap(decoded_image, x, y, size, size); | 109 SkBitmap cropped_image = SkBitmapOperations::CreateTiledBitmap( |
108 if (size > target_size) { | 110 decoded_image, x, y, pixels_per_side, pixels_per_side); |
| 111 if (pixels_per_side > target_size) { |
109 // Also downsize the image to save space and memory. | 112 // Also downsize the image to save space and memory. |
110 final_image = | 113 final_image = |
111 skia::ImageOperations::Resize(cropped_image, | 114 skia::ImageOperations::Resize(cropped_image, |
112 skia::ImageOperations::RESIZE_LANCZOS3, | 115 skia::ImageOperations::RESIZE_LANCZOS3, |
113 target_size, | 116 target_size, |
114 target_size); | 117 target_size); |
115 } else { | 118 } else { |
116 final_image = cropped_image; | 119 final_image = cropped_image; |
117 } | 120 } |
118 } | 121 } |
119 // Make the SkBitmap immutable as we won't modify it. This is important | 122 // Make the SkBitmap immutable as we won't modify it. This is important |
120 // because otherwise it gets duplicated during painting, wasting memory. | 123 // because otherwise it gets duplicated during painting, wasting memory. |
121 final_image.setImmutable(); | 124 final_image.setImmutable(); |
122 gfx::ImageSkia final_image_skia = | 125 gfx::ImageSkia final_image_skia = |
123 gfx::ImageSkia::CreateFrom1xBitmap(final_image); | 126 gfx::ImageSkia::CreateFrom1xBitmap(final_image); |
124 final_image_skia.MakeThreadSafe(); | 127 final_image_skia.MakeThreadSafe(); |
125 UserImage user_image(final_image_skia, decoder->get_image_data()); | 128 UserImage user_image(final_image_skia, decoder->get_image_data()); |
| 129 user_image.set_file_path(file_path); |
126 if (image_codec_ == ImageDecoder::ROBUST_JPEG_CODEC) | 130 if (image_codec_ == ImageDecoder::ROBUST_JPEG_CODEC) |
127 user_image.MarkAsSafe(); | 131 user_image.MarkAsSafe(); |
128 foreground_task_runner_->PostTask(FROM_HERE, | 132 foreground_task_runner_->PostTask(FROM_HERE, |
129 base::Bind(loaded_cb, user_image)); | 133 base::Bind(loaded_cb, user_image)); |
130 } | 134 } |
131 | 135 |
132 void UserImageLoader::OnDecodeImageFailed(const ImageDecoder* decoder) { | 136 void UserImageLoader::OnDecodeImageFailed(const ImageDecoder* decoder) { |
133 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 137 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
134 | 138 |
135 ImageInfoMap::iterator it = image_info_map_.find(decoder); | 139 ImageInfoMap::iterator it = image_info_map_.find(decoder); |
136 if (it == image_info_map_.end()) { | 140 if (it == image_info_map_.end()) { |
137 NOTREACHED(); | 141 NOTREACHED(); |
138 return; | 142 return; |
139 } | 143 } |
140 const LoadedCallback loaded_cb = it->second.loaded_cb; | 144 const LoadedCallback loaded_cb = it->second.loaded_cb; |
141 image_info_map_.erase(it); | 145 image_info_map_.erase(it); |
142 | 146 |
143 foreground_task_runner_->PostTask(FROM_HERE, | 147 foreground_task_runner_->PostTask(FROM_HERE, |
144 base::Bind(loaded_cb, UserImage())); | 148 base::Bind(loaded_cb, UserImage())); |
145 } | 149 } |
146 | 150 |
147 } // namespace chromeos | 151 } // namespace chromeos |
OLD | NEW |