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

Side by Side Diff: components/user_manager/user_image/user_image.cc

Issue 2517053004: Use base::RefCountedBytes in user_manager::UserImage (Closed)
Patch Set: Created 4 years, 1 month 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 "components/user_manager/user_image/user_image.h" 5 #include "components/user_manager/user_image/user_image.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/trace_event/trace_event.h" 8 #include "base/trace_event/trace_event.h"
9 #include "third_party/skia/include/core/SkBitmap.h" 9 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "ui/gfx/codec/jpeg_codec.h" 10 #include "ui/gfx/codec/jpeg_codec.h"
11 11
12 namespace user_manager { 12 namespace user_manager {
13 13
14 namespace { 14 namespace {
15 15
16 // Default quality for encoding user images. 16 // Default quality for encoding user images.
17 const int kDefaultEncodingQuality = 90; 17 const int kDefaultEncodingQuality = 90;
18 18
19 } // namespace 19 } // namespace
20 20
21 // static 21 // static
22 std::unique_ptr<UserImage::Bytes> UserImage::Encode(const SkBitmap& bitmap) { 22 scoped_refptr<base::RefCountedBytes> UserImage::Encode(
23 const SkBitmap& bitmap) {
23 TRACE_EVENT2("oobe", "UserImage::Encode", 24 TRACE_EVENT2("oobe", "UserImage::Encode",
24 "width", bitmap.width(), "height", bitmap.height()); 25 "width", bitmap.width(), "height", bitmap.height());
25 SkAutoLockPixels lock_bitmap(bitmap); 26 SkAutoLockPixels lock_bitmap(bitmap);
26 std::unique_ptr<Bytes> output(new Bytes); 27 std::vector<unsigned char> output;
27 if (gfx::JPEGCodec::Encode( 28 if (gfx::JPEGCodec::Encode(
28 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), 29 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
29 gfx::JPEGCodec::FORMAT_SkBitmap, 30 gfx::JPEGCodec::FORMAT_SkBitmap,
30 bitmap.width(), 31 bitmap.width(),
31 bitmap.height(), 32 bitmap.height(),
32 bitmap.width() * bitmap.bytesPerPixel(), 33 bitmap.width() * bitmap.bytesPerPixel(),
33 kDefaultEncodingQuality, output.get())) { 34 kDefaultEncodingQuality, &output)) {
34 return output; 35 return base::RefCountedBytes::TakeVector(&output);
35 } else { 36 } else {
36 return nullptr; 37 return nullptr;
37 } 38 }
38 } 39 }
39 40
40 // static 41 // static
41 std::unique_ptr<UserImage> UserImage::CreateAndEncode( 42 std::unique_ptr<UserImage> UserImage::CreateAndEncode(
42 const gfx::ImageSkia& image) { 43 const gfx::ImageSkia& image) {
43 if (image.isNull()) 44 if (image.isNull())
44 return base::WrapUnique(new UserImage); 45 return base::WrapUnique(new UserImage);
45 46
46 std::unique_ptr<Bytes> image_bytes = Encode(*image.bitmap()); 47 scoped_refptr<base::RefCountedBytes> image_bytes = Encode(*image.bitmap());
47 if (image_bytes) { 48 if (image_bytes) {
48 // TODO(crbug.com/593251): Remove the data copy via |image_bytes|. 49 std::unique_ptr<UserImage> result(new UserImage(image, image_bytes));
49 std::unique_ptr<UserImage> result(new UserImage(image, *image_bytes));
50 result->MarkAsSafe(); 50 result->MarkAsSafe();
51 return result; 51 return result;
52 } 52 }
53 return base::WrapUnique(new UserImage(image)); 53 return base::WrapUnique(new UserImage(image));
54 } 54 }
55 55
56 UserImage::UserImage() 56 UserImage::UserImage()
57 : has_image_bytes_(false), 57 : is_safe_format_(false) {
achuithb 2016/11/22 21:16:54 Move this to header since you're here?
satorux1 2016/11/24 02:13:40 Maybe it's short enough to be in .h file, but I'd
achuithb 2016/11/28 09:01:10 Maybe there's a misunderstanding - I meant the var
satorux1 2016/11/28 23:09:16 My bad. Didn't know about the c++11 guideline. Fix
58 is_safe_format_(false) {
59 } 58 }
60 59
61 UserImage::UserImage(const gfx::ImageSkia& image) 60 UserImage::UserImage(const gfx::ImageSkia& image)
62 : image_(image), 61 : image_(image),
63 has_image_bytes_(false),
64 is_safe_format_(false) { 62 is_safe_format_(false) {
65 } 63 }
66 64
67 UserImage::UserImage(const gfx::ImageSkia& image, 65 UserImage::UserImage(const gfx::ImageSkia& image,
68 const Bytes& image_bytes) 66 scoped_refptr<base::RefCountedBytes> image_bytes)
69 : image_(image), 67 : image_(image),
70 has_image_bytes_(false), 68 image_bytes_(image_bytes),
71 is_safe_format_(false) { 69 is_safe_format_(false) {
achuithb 2016/11/22 21:16:54 ditto
satorux1 2016/11/24 02:13:40 ditto
72 has_image_bytes_ = true;
73 image_bytes_ = image_bytes;
74 } 70 }
75 71
76 UserImage::~UserImage() {} 72 UserImage::~UserImage() {}
77 73
78 void UserImage::MarkAsSafe() { 74 void UserImage::MarkAsSafe() {
79 is_safe_format_ = true; 75 is_safe_format_ = true;
80 } 76 }
81 77
82 } // namespace user_manager 78 } // namespace user_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698