| 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 CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 #include <string> | 8 #include <string> |
| 10 | 9 |
| 11 #include "base/callback.h" | 10 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 13 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 14 #include "chrome/browser/image_decoder.h" | 13 #include "chrome/browser/image_decoder.h" |
| 15 | 14 |
| 16 class SkBitmap; | 15 class SkBitmap; |
| 17 | 16 |
| 18 namespace base { | 17 namespace base { |
| 19 class SequencedTaskRunner; | 18 class SequencedTaskRunner; |
| 20 } | 19 } |
| 21 | 20 |
| 22 namespace user_manager { | 21 namespace user_manager { |
| 23 class UserImage; | 22 class UserImage; |
| 24 } | 23 } |
| 25 | 24 |
| 26 namespace chromeos { | 25 namespace chromeos { |
| 27 | 26 |
| 28 // Helper that reads, decodes and optionally resizes an image on a background | 27 // Helper that reads, decodes and optionally resizes an image on a background |
| 29 // thread. Returns the image in the form of an SkBitmap. | 28 // thread. Returns the image in the form of an SkBitmap. |
| 30 class UserImageLoader : public base::RefCountedThreadSafe<UserImageLoader>, | 29 class UserImageLoader : public base::RefCountedThreadSafe<UserImageLoader> { |
| 31 public ImageDecoder::Delegate { | |
| 32 public: | 30 public: |
| 33 // Callback used to return the result of an image load operation. | 31 // Callback used to return the result of an image load operation. |
| 34 typedef base::Callback<void(const user_manager::UserImage& user_image)> | 32 typedef base::Callback<void(const user_manager::UserImage& user_image)> |
| 35 LoadedCallback; | 33 LoadedCallback; |
| 36 | 34 |
| 37 // All file I/O, decoding and resizing are done via |background_task_runner|. | 35 // All file I/O, decoding and resizing are done via |background_task_runner|. |
| 38 UserImageLoader( | 36 UserImageLoader( |
| 39 ImageDecoder::ImageCodec image_codec, | 37 ImageDecoder::ImageCodec image_codec, |
| 40 scoped_refptr<base::SequencedTaskRunner> background_task_runner); | 38 scoped_refptr<base::SequencedTaskRunner> background_task_runner); |
| 41 | 39 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 60 ImageInfo(const std::string& file_path, | 58 ImageInfo(const std::string& file_path, |
| 61 int pixels_per_side, | 59 int pixels_per_side, |
| 62 const LoadedCallback& loaded_cb); | 60 const LoadedCallback& loaded_cb); |
| 63 ~ImageInfo(); | 61 ~ImageInfo(); |
| 64 | 62 |
| 65 const std::string file_path; | 63 const std::string file_path; |
| 66 const int pixels_per_side; | 64 const int pixels_per_side; |
| 67 const LoadedCallback loaded_cb; | 65 const LoadedCallback loaded_cb; |
| 68 }; | 66 }; |
| 69 | 67 |
| 70 typedef std::map<const ImageDecoder*, ImageInfo> ImageInfoMap; | 68 class UserImageRequest : public ImageDecoder::ImageRequest { |
| 69 public: |
| 70 UserImageRequest(const ImageInfo& image_info, |
| 71 const std::string& image_data, |
| 72 UserImageLoader* user_image_loader); |
| 71 | 73 |
| 72 ~UserImageLoader() override; | 74 // ImageDecoder::ImageRequest implementation. These callbacks will only be |
| 75 // invoked via user_image_loader_'s background_task_runner_. |
| 76 void OnImageDecoded(const SkBitmap& decoded_image) override; |
| 77 void OnDecodeImageFailed() override; |
| 78 |
| 79 private: |
| 80 ~UserImageRequest() override; |
| 81 |
| 82 const ImageInfo image_info_; |
| 83 std::vector<unsigned char> image_data_; |
| 84 UserImageLoader* user_image_loader_; |
| 85 }; |
| 86 |
| 87 ~UserImageLoader(); |
| 73 | 88 |
| 74 // Reads the image from |image_info.file_path| and starts the decoding | 89 // Reads the image from |image_info.file_path| and starts the decoding |
| 75 // process. This method may only be invoked via the |background_task_runner_|. | 90 // process. This method may only be invoked via the |background_task_runner_|. |
| 76 void ReadAndDecodeImage(const ImageInfo& image_info); | 91 void ReadAndDecodeImage(const ImageInfo& image_info); |
| 77 | 92 |
| 78 // Decodes the image |data|. This method may only be invoked via the | 93 // Decodes the image |data|. This method may only be invoked via the |
| 79 // |background_task_runner_|. | 94 // |background_task_runner_|. |
| 80 void DecodeImage(const scoped_ptr<std::string> data, | 95 void DecodeImage(const scoped_ptr<std::string> data, |
| 81 const ImageInfo& image_info); | 96 const ImageInfo& image_info); |
| 82 | 97 |
| 83 // ImageDecoder::Delegate implementation. These callbacks will only be invoked | |
| 84 // via the |background_task_runner_|. | |
| 85 void OnImageDecoded(const ImageDecoder* decoder, | |
| 86 const SkBitmap& decoded_image) override; | |
| 87 void OnDecodeImageFailed(const ImageDecoder* decoder) override; | |
| 88 | |
| 89 // The foreground task runner on which |this| is instantiated, Start() is | 98 // The foreground task runner on which |this| is instantiated, Start() is |
| 90 // called and LoadedCallbacks are invoked. | 99 // called and LoadedCallbacks are invoked. |
| 91 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; | 100 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; |
| 92 | 101 |
| 93 // The background task runner on which file I/O, image decoding and resizing | 102 // The background task runner on which file I/O, image decoding and resizing |
| 94 // are done. | 103 // are done. |
| 95 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 104 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 96 | 105 |
| 97 // Specify how the file should be decoded in the utility process. | 106 // Specify how the file should be decoded in the utility process. |
| 98 const ImageDecoder::ImageCodec image_codec_; | 107 const ImageDecoder::ImageCodec image_codec_; |
| 99 | 108 |
| 100 // Holds information about the images currently being decoded. Accessed via | |
| 101 // |background_task_runner_| only. | |
| 102 ImageInfoMap image_info_map_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(UserImageLoader); | 109 DISALLOW_COPY_AND_ASSIGN(UserImageLoader); |
| 105 }; | 110 }; |
| 106 | 111 |
| 107 } // namespace chromeos | 112 } // namespace chromeos |
| 108 | 113 |
| 109 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_ | 114 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_ |
| OLD | NEW |