OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
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" | |
15 | |
16 class SkBitmap; | |
17 | |
18 namespace base { | |
19 class SequencedTaskRunner; | |
20 } | |
21 | |
22 namespace chromeos { | |
23 | |
24 class UserImage; | |
25 | |
26 // Helper that reads, decodes and optionally resizes an image on a background | |
27 // thread. Returns the image in the form of an SkBitmap. | |
28 class UserImageLoader : public base::RefCountedThreadSafe<UserImageLoader>, | |
29 public ImageDecoder::Delegate { | |
30 public: | |
31 // Callback used to return the result of an image load operation. | |
32 typedef base::Callback<void(const UserImage& user_image)> LoadedCallback; | |
33 | |
34 // All file I/O, decoding and resizing are done via |background_task_runner|. | |
35 UserImageLoader( | |
36 ImageDecoder::ImageCodec image_codec, | |
37 scoped_refptr<base::SequencedTaskRunner> background_task_runner); | |
38 | |
39 // Load an image in the background and call |loaded_cb| with the resulting | |
40 // UserImage (which may be empty in case of error). If |pixels_per_side| is | |
41 // positive, the image is cropped to a square and shrunk so that it does not | |
42 // exceed |pixels_per_side|x|pixels_per_side|. The first variant of this | |
43 // method reads the image from |filepath| on disk, the second processes |data| | |
44 // read into memory already. | |
45 void Start(const std::string& filepath, | |
46 int pixels_per_side, | |
47 const LoadedCallback& loaded_cb); | |
48 void Start(scoped_ptr<std::string> data, | |
49 int pixels_per_side, | |
50 const LoadedCallback& loaded_cb); | |
51 | |
52 private: | |
53 friend class base::RefCountedThreadSafe<UserImageLoader>; | |
54 | |
55 // Contains attributes we need to know about each image we decode. | |
56 struct ImageInfo { | |
57 ImageInfo(const std::string& file_path, | |
58 int pixels_per_side, | |
59 const LoadedCallback& loaded_cb); | |
60 ~ImageInfo(); | |
61 | |
62 const std::string file_path; | |
63 const int pixels_per_side; | |
64 const LoadedCallback loaded_cb; | |
65 }; | |
66 | |
67 typedef std::map<const ImageDecoder*, ImageInfo> ImageInfoMap; | |
68 | |
69 virtual ~UserImageLoader(); | |
70 | |
71 // Reads the image from |image_info.file_path| and starts the decoding | |
72 // process. This method may only be invoked via the |background_task_runner_|. | |
73 void ReadAndDecodeImage(const ImageInfo& image_info); | |
74 | |
75 // Decodes the image |data|. This method may only be invoked via the | |
76 // |background_task_runner_|. | |
77 void DecodeImage(const scoped_ptr<std::string> data, | |
78 const ImageInfo& image_info); | |
79 | |
80 // ImageDecoder::Delegate implementation. These callbacks will only be invoked | |
81 // via the |background_task_runner_|. | |
82 virtual void OnImageDecoded(const ImageDecoder* decoder, | |
83 const SkBitmap& decoded_image) OVERRIDE; | |
84 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE; | |
85 | |
86 // The foreground task runner on which |this| is instantiated, Start() is | |
87 // called and LoadedCallbacks are invoked. | |
88 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; | |
89 | |
90 // The background task runner on which file I/O, image decoding and resizing | |
91 // are done. | |
92 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | |
93 | |
94 // Specify how the file should be decoded in the utility process. | |
95 const ImageDecoder::ImageCodec image_codec_; | |
96 | |
97 // Holds information about the images currently being decoded. Accessed via | |
98 // |background_task_runner_| only. | |
99 ImageInfoMap image_info_map_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(UserImageLoader); | |
102 }; | |
103 | |
104 } // namespace chromeos | |
105 | |
106 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ | |
OLD | NEW |