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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/image_decoder.h"
19 class SequencedTaskRunner;
22 namespace user_manager {
28 // Helper that reads, decodes and optionally resizes an image on a background
29 // thread. Returns the image in the form of an SkBitmap.
30 class UserImageLoader : public base::RefCountedThreadSafe<UserImageLoader>,
31 public ImageDecoder::Delegate {
33 // Callback used to return the result of an image load operation.
34 typedef base::Callback<void(const user_manager::UserImage& user_image)>
37 // All file I/O, decoding and resizing are done via |background_task_runner|.
39 ImageDecoder::ImageCodec image_codec,
40 scoped_refptr<base::SequencedTaskRunner> background_task_runner);
42 // Load an image in the background and call |loaded_cb| with the resulting
43 // UserImage (which may be empty in case of error). If |pixels_per_side| is
44 // positive, the image is cropped to a square and shrunk so that it does not
45 // exceed |pixels_per_side|x|pixels_per_side|. The first variant of this
46 // method reads the image from |filepath| on disk, the second processes |data|
47 // read into memory already.
48 void Start(const std::string& filepath,
50 const LoadedCallback& loaded_cb);
51 void Start(scoped_ptr<std::string> data,
53 const LoadedCallback& loaded_cb);
56 friend class base::RefCountedThreadSafe<UserImageLoader>;
58 // Contains attributes we need to know about each image we decode.
60 ImageInfo(const std::string& file_path,
62 const LoadedCallback& loaded_cb);
65 const std::string file_path;
66 const int pixels_per_side;
67 const LoadedCallback loaded_cb;
70 typedef std::map<const ImageDecoder*, ImageInfo> ImageInfoMap;
72 virtual ~UserImageLoader();
74 // 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_|.
76 void ReadAndDecodeImage(const ImageInfo& image_info);
78 // Decodes the image |data|. This method may only be invoked via the
79 // |background_task_runner_|.
80 void DecodeImage(const scoped_ptr<std::string> data,
81 const ImageInfo& image_info);
83 // ImageDecoder::Delegate implementation. These callbacks will only be invoked
84 // via the |background_task_runner_|.
85 virtual void OnImageDecoded(const ImageDecoder* decoder,
86 const SkBitmap& decoded_image) override;
87 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) override;
89 // The foreground task runner on which |this| is instantiated, Start() is
90 // called and LoadedCallbacks are invoked.
91 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_;
93 // The background task runner on which file I/O, image decoding and resizing
95 scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
97 // Specify how the file should be decoded in the utility process.
98 const ImageDecoder::ImageCodec image_codec_;
100 // Holds information about the images currently being decoded. Accessed via
101 // |background_task_runner_| only.
102 ImageInfoMap image_info_map_;
104 DISALLOW_COPY_AND_ASSIGN(UserImageLoader);
107 } // namespace chromeos
109 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_