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.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_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
;
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
{
31 // Callback used to return the result of an image load operation.
32 typedef base::Callback
<void(const UserImage
& user_image
)> LoadedCallback
;
34 // All file I/O, decoding and resizing are done via |background_task_runner|.
36 ImageDecoder::ImageCodec image_codec
,
37 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
);
39 // Load an image in the background and call |loaded_cb| with the result. If
40 // |size| is positive, the image is cropped to a square and shrunk so that it
41 // does not exceed |size|x|size|. The first variant of this method reads the
42 // image from |filepath| on disk, the second processes |data| read into memory
44 void Start(const std::string
& filepath
,
46 const LoadedCallback
& loaded_cb
);
47 void Start(scoped_ptr
<std::string
> data
,
49 const LoadedCallback
& loaded_cb
);
52 friend class base::RefCountedThreadSafe
<UserImageLoader
>;
54 // Contains attributes we need to know about each image we decode.
56 ImageInfo(int size
, const LoadedCallback
& loaded_cb
);
60 const LoadedCallback loaded_cb
;
63 typedef std::map
<const ImageDecoder
*, ImageInfo
> ImageInfoMap
;
65 virtual ~UserImageLoader();
67 // Reads the image from |filepath| and starts the decoding process. This
68 // method may only be invoked via the |background_task_runner_|.
69 void ReadAndDecodeImage(const std::string
& filepath
,
70 const ImageInfo
& image_info
);
72 // Decodes the image |data|. This method may only be invoked via the
73 // |background_task_runner_|.
74 void DecodeImage(const scoped_ptr
<std::string
> data
,
75 const ImageInfo
& image_info
);
77 // ImageDecoder::Delegate implementation. These callbacks will only be invoked
78 // via the |background_task_runner_|.
79 virtual void OnImageDecoded(const ImageDecoder
* decoder
,
80 const SkBitmap
& decoded_image
) OVERRIDE
;
81 virtual void OnDecodeImageFailed(const ImageDecoder
* decoder
) OVERRIDE
;
83 // The foreground task runner on which |this| is instantiated, Start() is
84 // called and LoadedCallbacks are invoked.
85 scoped_refptr
<base::SequencedTaskRunner
> foreground_task_runner_
;
87 // The background task runner on which file I/O, image decoding and resizing
89 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner_
;
91 // Specify how the file should be decoded in the utility process.
92 const ImageDecoder::ImageCodec image_codec_
;
94 // Holds information about the images currently being decoded. Accessed via
95 // |background_task_runner_| only.
96 ImageInfoMap image_info_map_
;
98 DISALLOW_COPY_AND_ASSIGN(UserImageLoader
);
101 } // namespace chromeos
103 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_