Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Side by Side Diff: chrome/browser/chromeos/login/users/avatar/user_image_loader.cc

Issue 1870793002: Convert //chrome/browser/chromeos from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/users/avatar/user_image_loader.h" 5 #include "chrome/browser/chromeos/login/users/avatar/user_image_loader.h"
6 6
7 #include <memory>
7 #include <utility> 8 #include <utility>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
11 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
12 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/sequenced_task_runner.h" 15 #include "base/sequenced_task_runner.h"
15 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
16 #include "base/task_runner_util.h" 17 #include "base/task_runner_util.h"
17 #include "base/thread_task_runner_handle.h" 18 #include "base/thread_task_runner_handle.h"
18 #include "chrome/browser/chromeos/login/helper.h" 19 #include "chrome/browser/chromeos/login/helper.h"
19 #include "components/user_manager/user_image/user_image.h" 20 #include "components/user_manager/user_image/user_image.h"
20 #include "skia/ext/image_operations.h" 21 #include "skia/ext/image_operations.h"
21 #include "third_party/skia/include/core/SkBitmap.h" 22 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "ui/gfx/codec/png_codec.h" 23 #include "ui/gfx/codec/png_codec.h"
23 #include "ui/gfx/skbitmap_operations.h" 24 #include "ui/gfx/skbitmap_operations.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 if (pixels_per_side > target_size) { 64 if (pixels_per_side > target_size) {
64 // Also downsize the image to save space and memory. 65 // Also downsize the image to save space and memory.
65 final_image = skia::ImageOperations::Resize( 66 final_image = skia::ImageOperations::Resize(
66 cropped_image, skia::ImageOperations::RESIZE_LANCZOS3, target_size, 67 cropped_image, skia::ImageOperations::RESIZE_LANCZOS3, target_size,
67 target_size); 68 target_size);
68 } else { 69 } else {
69 final_image = cropped_image; 70 final_image = cropped_image;
70 } 71 }
71 72
72 // Encode the cropped image to web-compatible bytes representation 73 // Encode the cropped image to web-compatible bytes representation
73 scoped_ptr<user_manager::UserImage::Bytes> encoded = 74 std::unique_ptr<user_manager::UserImage::Bytes> encoded =
74 user_manager::UserImage::Encode(final_image); 75 user_manager::UserImage::Encode(final_image);
75 if (!encoded) 76 if (!encoded)
76 return false; 77 return false;
77 78
78 bitmap->swap(final_image); 79 bitmap->swap(final_image);
79 bytes->swap(*encoded); 80 bytes->swap(*encoded);
80 return true; 81 return true;
81 } 82 }
82 83
83 // Handles the decoded image returned from ImageDecoder through the 84 // Handles the decoded image returned from ImageDecoder through the
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 const SkBitmap& image, 154 const SkBitmap& image,
154 const user_manager::UserImage::Bytes& image_bytes, 155 const user_manager::UserImage::Bytes& image_bytes,
155 bool image_bytes_regenerated) { 156 bool image_bytes_regenerated) {
156 SkBitmap final_image = image; 157 SkBitmap final_image = image;
157 // Make the SkBitmap immutable as we won't modify it. This is important 158 // Make the SkBitmap immutable as we won't modify it. This is important
158 // because otherwise it gets duplicated during painting, wasting memory. 159 // because otherwise it gets duplicated during painting, wasting memory.
159 final_image.setImmutable(); 160 final_image.setImmutable();
160 gfx::ImageSkia final_image_skia = 161 gfx::ImageSkia final_image_skia =
161 gfx::ImageSkia::CreateFrom1xBitmap(final_image); 162 gfx::ImageSkia::CreateFrom1xBitmap(final_image);
162 final_image_skia.MakeThreadSafe(); 163 final_image_skia.MakeThreadSafe();
163 scoped_ptr<user_manager::UserImage> user_image( 164 std::unique_ptr<user_manager::UserImage> user_image(
164 new user_manager::UserImage(final_image_skia, image_bytes)); 165 new user_manager::UserImage(final_image_skia, image_bytes));
165 user_image->set_file_path(image_info_.file_path); 166 user_image->set_file_path(image_info_.file_path);
166 if (image_info_.image_codec == ImageDecoder::ROBUST_JPEG_CODEC || 167 if (image_info_.image_codec == ImageDecoder::ROBUST_JPEG_CODEC ||
167 image_bytes_regenerated) 168 image_bytes_regenerated)
168 user_image->MarkAsSafe(); 169 user_image->MarkAsSafe();
169 image_info_.loaded_cb.Run(std::move(user_image)); 170 image_info_.loaded_cb.Run(std::move(user_image));
170 delete this; 171 delete this;
171 } 172 }
172 173
173 void UserImageRequest::OnDecodeImageFailed() { 174 void UserImageRequest::OnDecodeImageFailed() {
174 image_info_.loaded_cb.Run(make_scoped_ptr(new user_manager::UserImage)); 175 image_info_.loaded_cb.Run(base::WrapUnique(new user_manager::UserImage));
175 delete this; 176 delete this;
176 } 177 }
177 178
178 // Starts decoding the image with ImageDecoder for the image |data| if 179 // Starts decoding the image with ImageDecoder for the image |data| if
179 // |data_is_ready| is true. 180 // |data_is_ready| is true.
180 void DecodeImage( 181 void DecodeImage(
181 const ImageInfo& image_info, 182 const ImageInfo& image_info,
182 scoped_refptr<base::SequencedTaskRunner> background_task_runner, 183 scoped_refptr<base::SequencedTaskRunner> background_task_runner,
183 const std::string* data, 184 const std::string* data,
184 bool data_is_ready) { 185 bool data_is_ready) {
185 if (!data_is_ready) { 186 if (!data_is_ready) {
186 base::ThreadTaskRunnerHandle::Get()->PostTask( 187 base::ThreadTaskRunnerHandle::Get()->PostTask(
187 FROM_HERE, 188 FROM_HERE,
188 base::Bind(image_info.loaded_cb, 189 base::Bind(image_info.loaded_cb, base::Passed(base::WrapUnique(
189 base::Passed(make_scoped_ptr(new user_manager::UserImage)))); 190 new user_manager::UserImage))));
190 return; 191 return;
191 } 192 }
192 193
193 UserImageRequest* image_request = 194 UserImageRequest* image_request =
194 new UserImageRequest(image_info, *data, background_task_runner); 195 new UserImageRequest(image_info, *data, background_task_runner);
195 ImageDecoder::StartWithOptions(image_request, *data, image_info.image_codec, 196 ImageDecoder::StartWithOptions(image_request, *data, image_info.image_codec,
196 false); 197 false);
197 } 198 }
198 199
199 } // namespace 200 } // namespace
200 201
201 void StartWithFilePath( 202 void StartWithFilePath(
202 scoped_refptr<base::SequencedTaskRunner> background_task_runner, 203 scoped_refptr<base::SequencedTaskRunner> background_task_runner,
203 const base::FilePath& file_path, 204 const base::FilePath& file_path,
204 ImageDecoder::ImageCodec image_codec, 205 ImageDecoder::ImageCodec image_codec,
205 int pixels_per_side, 206 int pixels_per_side,
206 const LoadedCallback& loaded_cb) { 207 const LoadedCallback& loaded_cb) {
207 std::string* data = new std::string; 208 std::string* data = new std::string;
208 base::PostTaskAndReplyWithResult( 209 base::PostTaskAndReplyWithResult(
209 background_task_runner.get(), FROM_HERE, 210 background_task_runner.get(), FROM_HERE,
210 base::Bind(&base::ReadFileToString, file_path, data), 211 base::Bind(&base::ReadFileToString, file_path, data),
211 base::Bind(&DecodeImage, 212 base::Bind(&DecodeImage,
212 ImageInfo(file_path, pixels_per_side, image_codec, loaded_cb), 213 ImageInfo(file_path, pixels_per_side, image_codec, loaded_cb),
213 background_task_runner, base::Owned(data))); 214 background_task_runner, base::Owned(data)));
214 } 215 }
215 216
216 void StartWithData( 217 void StartWithData(
217 scoped_refptr<base::SequencedTaskRunner> background_task_runner, 218 scoped_refptr<base::SequencedTaskRunner> background_task_runner,
218 scoped_ptr<std::string> data, 219 std::unique_ptr<std::string> data,
219 ImageDecoder::ImageCodec image_codec, 220 ImageDecoder::ImageCodec image_codec,
220 int pixels_per_side, 221 int pixels_per_side,
221 const LoadedCallback& loaded_cb) { 222 const LoadedCallback& loaded_cb) {
222 DecodeImage( 223 DecodeImage(
223 ImageInfo(base::FilePath(), pixels_per_side, image_codec, loaded_cb), 224 ImageInfo(base::FilePath(), pixels_per_side, image_codec, loaded_cb),
224 background_task_runner, data.get(), true /* data_is_ready */); 225 background_task_runner, data.get(), true /* data_is_ready */);
225 } 226 }
226 227
227 } // namespace user_image_loader 228 } // namespace user_image_loader
228 } // namespace chromeos 229 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698