Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #ifndef COMPONENTS_USER_MANAGER_USER_IMAGE_USER_IMAGE_H_ | 5 #ifndef COMPONENTS_USER_MANAGER_USER_IMAGE_USER_IMAGE_H_ |
| 6 #define COMPONENTS_USER_MANAGER_USER_IMAGE_USER_IMAGE_H_ | 6 #define COMPONENTS_USER_MANAGER_USER_IMAGE_USER_IMAGE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
| 14 #include "components/user_manager/user_manager_export.h" | 14 #include "components/user_manager/user_manager_export.h" |
| 15 #include "ui/gfx/image/image_skia.h" | 15 #include "ui/gfx/image/image_skia.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 namespace user_manager { | 18 namespace user_manager { |
| 19 | 19 |
| 20 // Wrapper class storing a still image and its bytes representation for | 20 // Wrapper class storing a still image and its bytes representation for |
| 21 // WebUI in a web-compatible format such as JPEG. Could be used for storing | 21 // WebUI in a web-compatible format such as JPEG. Could be used for storing |
| 22 // profile images and user wallpapers. | 22 // profile images and user wallpapers. |
| 23 class USER_MANAGER_EXPORT UserImage { | 23 class USER_MANAGER_EXPORT UserImage { |
| 24 public: | 24 public: |
| 25 // Encodes the given bitmap to bytes representation for WebUI. Returns null | 25 // The format of the user image's bytes representation. PNG can support |
| 26 // on failure. | 26 // transparent background that's not supported with JPEG. |
| 27 static scoped_refptr<base::RefCountedBytes> Encode(const SkBitmap& bitmap); | 27 enum ImageFormat { |
| 28 FORMAT_JPEG, | |
| 29 FORMAT_PNG, | |
| 30 FORMAT_UNKNOWN, | |
| 31 }; | |
| 32 | |
| 33 // Encodes the given bitmap to bytes representation in |image_format| for | |
| 34 // WebUI. Returns nullptr on failure. | |
| 35 static scoped_refptr<base::RefCountedBytes> Encode(const SkBitmap& bitmap, | |
| 36 ImageFormat image_format); | |
| 28 | 37 |
| 29 // Creates a new instance from a given still frame and tries to encode it | 38 // Creates a new instance from a given still frame and tries to encode it |
| 30 // to bytes representation for WebUI. Always returns a non-null result. | 39 // to bytes representation in |image_format| for WebUI. Always returns a |
| 40 // non-null result. | |
| 31 // TODO(ivankr): remove eventually. | 41 // TODO(ivankr): remove eventually. |
|
achuithb
2016/11/30 00:26:48
Do you know what this TODO is about?
satorux1
2016/12/01 07:43:36
I don't know. This function is used in many places
achuithb
2016/12/01 20:20:32
Doesn't look like ivankr@ has been active in chrom
| |
| 32 static std::unique_ptr<UserImage> CreateAndEncode( | 42 static std::unique_ptr<UserImage> CreateAndEncode( |
| 33 const gfx::ImageSkia& image); | 43 const gfx::ImageSkia& image, |
| 44 ImageFormat image_format); | |
| 45 | |
| 46 // Choose the image format suitable for the given bitmap. Returns | |
| 47 // FORMAT_PNG if the bitmap contains transparent/translucent | |
| 48 // pixels. Otherwise, returns FORMAT_JPEG. | |
| 49 static ImageFormat ChooseImageFormat(const SkBitmap& bitmap); | |
| 34 | 50 |
| 35 // Create instance with an empty still frame and no bytes | 51 // Create instance with an empty still frame and no bytes |
| 36 // representation for WebUI. | 52 // representation for WebUI. |
| 37 UserImage(); | 53 UserImage(); |
| 38 | 54 |
| 39 // Creates a new instance from a given still frame without any bytes | 55 // Creates a new instance from a given still frame without any bytes |
| 40 // representation for WebUI. | 56 // representation for WebUI. |
| 41 explicit UserImage(const gfx::ImageSkia& image); | 57 explicit UserImage(const gfx::ImageSkia& image); |
| 42 | 58 |
| 43 // Creates a new instance from a given still frame and bytes | 59 // Creates a new instance from a given still frame and bytes |
| 44 // representation for WebUI. | 60 // representation for WebUI. |
| 45 UserImage(const gfx::ImageSkia& image, | 61 UserImage(const gfx::ImageSkia& image, |
| 46 scoped_refptr<base::RefCountedBytes> image_bytes); | 62 scoped_refptr<base::RefCountedBytes> image_bytes, |
| 63 ImageFormat image_format); | |
| 47 | 64 |
| 48 virtual ~UserImage(); | 65 virtual ~UserImage(); |
| 49 | 66 |
| 50 const gfx::ImageSkia& image() const { return image_; } | 67 const gfx::ImageSkia& image() const { return image_; } |
| 51 | 68 |
| 52 // Optional bytes representation of the still image for WebUI. | 69 // Optional bytes representation of the still image for WebUI. |
| 53 bool has_image_bytes() const { return image_bytes_ != nullptr; } | 70 bool has_image_bytes() const { return image_bytes_ != nullptr; } |
| 54 scoped_refptr<base::RefCountedBytes> image_bytes() const { | 71 scoped_refptr<base::RefCountedBytes> image_bytes() const { |
| 55 return image_bytes_; | 72 return image_bytes_; |
| 56 } | 73 } |
| 74 // Format of the bytes representation. | |
| 75 ImageFormat image_format() const { return image_format_; } | |
| 57 | 76 |
| 58 // URL from which this image was originally downloaded, if any. | 77 // URL from which this image was originally downloaded, if any. |
| 59 void set_url(const GURL& url) { url_ = url; } | 78 void set_url(const GURL& url) { url_ = url; } |
| 60 GURL url() const { return url_; } | 79 GURL url() const { return url_; } |
| 61 | 80 |
| 62 // Whether |image_bytes| contains data in format that is considered safe to | 81 // Whether |image_bytes| contains data in format that is considered safe to |
| 63 // decode in sensitive environment (on Login screen). | 82 // decode in sensitive environment (on Login screen). |
| 64 bool is_safe_format() const { return is_safe_format_; } | 83 bool is_safe_format() const { return is_safe_format_; } |
| 65 void MarkAsSafe(); | 84 void MarkAsSafe(); |
| 66 | 85 |
| 67 const base::FilePath& file_path() const { return file_path_; } | 86 const base::FilePath& file_path() const { return file_path_; } |
| 68 void set_file_path(const base::FilePath& file_path) { | 87 void set_file_path(const base::FilePath& file_path) { |
| 69 file_path_ = file_path; | 88 file_path_ = file_path; |
| 70 } | 89 } |
| 71 | 90 |
| 72 private: | 91 private: |
| 73 gfx::ImageSkia image_; | 92 gfx::ImageSkia image_; |
| 74 scoped_refptr<base::RefCountedBytes> image_bytes_; | 93 scoped_refptr<base::RefCountedBytes> image_bytes_; |
| 75 GURL url_; | 94 GURL url_; |
| 76 | 95 |
| 77 // If image was loaded from the local file, file path is stored here. | 96 // If image was loaded from the local file, file path is stored here. |
| 78 base::FilePath file_path_; | 97 base::FilePath file_path_; |
| 79 bool is_safe_format_ = false; | 98 bool is_safe_format_ = false; |
| 99 ImageFormat image_format_ = FORMAT_UNKNOWN; | |
| 80 | 100 |
| 81 DISALLOW_COPY_AND_ASSIGN(UserImage); | 101 DISALLOW_COPY_AND_ASSIGN(UserImage); |
| 82 }; | 102 }; |
| 83 | 103 |
| 84 } // namespace user_manager | 104 } // namespace user_manager |
| 85 | 105 |
| 86 #endif // COMPONENTS_USER_MANAGER_USER_IMAGE_USER_IMAGE_H_ | 106 #endif // COMPONENTS_USER_MANAGER_USER_IMAGE_USER_IMAGE_H_ |
| OLD | NEW |