Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / avatar / user_image_loader.h
blobdea30ce8fb489acb956f1ae9d689381116f0a8b3
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_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "chrome/browser/image_decoder.h"
15 class SkBitmap;
17 namespace base {
18 class SequencedTaskRunner;
21 namespace user_manager {
22 class UserImage;
25 namespace chromeos {
27 // Helper that reads, decodes and optionally resizes an image on a background
28 // thread. Returns the image in the form of an SkBitmap.
29 class UserImageLoader : public base::RefCountedThreadSafe<UserImageLoader> {
30 public:
31 // Callback used to return the result of an image load operation.
32 typedef base::Callback<void(const user_manager::UserImage& user_image)>
33 LoadedCallback;
35 // All file I/O, decoding and resizing are done via |background_task_runner|.
36 UserImageLoader(
37 ImageDecoder::ImageCodec image_codec,
38 scoped_refptr<base::SequencedTaskRunner> background_task_runner);
40 // Load an image in the background and call |loaded_cb| with the resulting
41 // UserImage (which may be empty in case of error). If |pixels_per_side| is
42 // positive, the image is cropped to a square and shrunk so that it does not
43 // exceed |pixels_per_side|x|pixels_per_side|. The first variant of this
44 // method reads the image from |filepath| on disk, the second processes |data|
45 // read into memory already.
46 void Start(const std::string& filepath,
47 int pixels_per_side,
48 const LoadedCallback& loaded_cb);
49 void Start(scoped_ptr<std::string> data,
50 int pixels_per_side,
51 const LoadedCallback& loaded_cb);
53 private:
54 friend class base::RefCountedThreadSafe<UserImageLoader>;
56 // Contains attributes we need to know about each image we decode.
57 struct ImageInfo {
58 ImageInfo(const std::string& file_path,
59 int pixels_per_side,
60 const LoadedCallback& loaded_cb);
61 ~ImageInfo();
63 const std::string file_path;
64 const int pixels_per_side;
65 const LoadedCallback loaded_cb;
68 class UserImageRequest : public ImageDecoder::ImageRequest {
69 public:
70 UserImageRequest(const ImageInfo& image_info,
71 const std::string& image_data,
72 const scoped_refptr<UserImageLoader>& user_image_loader);
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;
79 private:
80 ~UserImageRequest() override;
82 const ImageInfo image_info_;
83 std::vector<unsigned char> image_data_;
84 scoped_refptr<UserImageLoader> user_image_loader_;
87 ~UserImageLoader();
89 // Reads the image from |image_info.file_path| and starts the decoding
90 // process. This method may only be invoked via the |background_task_runner_|.
91 void ReadAndDecodeImage(const ImageInfo& image_info);
93 // Decodes the image |data|. This method may only be invoked via the
94 // |background_task_runner_|.
95 void DecodeImage(const scoped_ptr<std::string> data,
96 const ImageInfo& image_info);
98 // The foreground task runner on which |this| is instantiated, Start() is
99 // called and LoadedCallbacks are invoked.
100 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_;
102 // The background task runner on which file I/O, image decoding and resizing
103 // are done.
104 scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
106 // Specify how the file should be decoded in the utility process.
107 const ImageDecoder::ImageCodec image_codec_;
109 DISALLOW_COPY_AND_ASSIGN(UserImageLoader);
112 } // namespace chromeos
114 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USERS_AVATAR_USER_IMAGE_LOADER_H_