OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_USER_IMAGE_LOADER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/synchronization/lock.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" |
14 #include "chrome/browser/image_decoder.h" | 16 #include "chrome/browser/image_decoder.h" |
15 | 17 |
16 class MessageLoop; | 18 class MessageLoop; |
17 class SkBitmap; | 19 class SkBitmap; |
18 | 20 |
19 namespace chromeos { | 21 namespace chromeos { |
20 | 22 |
21 class UserImage; | 23 class UserImage; |
22 | 24 |
23 // A facility to read a file containing user image asynchronously in the IO | 25 // A facility to read a file containing user image asynchronously in the IO |
24 // thread. Returns the image in the form of an SkBitmap. | 26 // thread. Returns the image in the form of an SkBitmap. |
25 class UserImageLoader : public base::RefCountedThreadSafe<UserImageLoader>, | 27 class UserImageLoader : public base::RefCountedThreadSafe<UserImageLoader>, |
26 public ImageDecoder::Delegate { | 28 public ImageDecoder::Delegate { |
27 public: | 29 public: |
28 // Callback used to indicate that image has been loaded. | 30 // Callback used to indicate that image has been loaded. |
29 typedef base::Callback<void(const UserImage& user_image)> LoadedCallback; | 31 typedef base::Callback<void(const UserImage& user_image)> LoadedCallback; |
30 | 32 |
31 explicit UserImageLoader(ImageDecoder::ImageCodec image_codec); | 33 explicit UserImageLoader(ImageDecoder::ImageCodec image_codec); |
32 | 34 |
33 // Start reading the image from |filepath| on the file thread. Calls | 35 // Start reading the image from |filepath| on a worker thread pool. Calls |
34 // |loaded_cb| when image has been successfully loaded. | 36 // |loaded_cb| when image has been successfully loaded. |
35 // If |size| is positive, image is cropped and (if needed) downsized to | 37 // If |size| is positive, image is cropped and (if needed) downsized to |
36 // |size|x|size| pixels. | 38 // |size|x|size| pixels. |
37 void Start(const std::string& filepath, int size, | 39 void Start(const std::string& filepath, int size, |
38 const LoadedCallback& loaded_cb); | 40 const LoadedCallback& loaded_cb); |
39 | 41 |
40 private: | 42 private: |
41 friend class base::RefCountedThreadSafe<UserImageLoader>; | 43 friend class base::RefCountedThreadSafe<UserImageLoader>; |
42 | 44 |
43 // Contains attributes we need to know about each image we decode. | 45 // Contains attributes we need to know about each image we decode. |
44 struct ImageInfo { | 46 struct ImageInfo { |
45 ImageInfo(int size, const LoadedCallback& loaded_cb); | 47 ImageInfo(int size, const LoadedCallback& loaded_cb); |
46 ~ImageInfo(); | 48 ~ImageInfo(); |
47 | 49 |
48 const int size; | 50 const int size; |
49 const LoadedCallback loaded_cb; | 51 const LoadedCallback loaded_cb; |
50 }; | 52 }; |
51 | 53 |
52 typedef std::map<const ImageDecoder*, ImageInfo> ImageInfoMap; | 54 typedef std::map<const ImageDecoder*, ImageInfo> ImageInfoMap; |
53 | 55 |
54 virtual ~UserImageLoader(); | 56 virtual ~UserImageLoader(); |
55 | 57 |
56 // Method that reads the file on the file thread and starts decoding it in | 58 // Method that reads the file on the worker thread pool and starts decoding it |
57 // sandboxed process. | 59 // in a sandboxed process. |
58 void LoadImage(const std::string& filepath, const ImageInfo& image_info); | 60 void LoadImage(const std::string& filepath, |
| 61 const ImageInfo& image_info, |
| 62 scoped_refptr<base::SequencedTaskRunner> task_runner); |
59 | 63 |
60 // ImageDecoder::Delegate implementation. | 64 // ImageDecoder::Delegate implementation. |
61 virtual void OnImageDecoded(const ImageDecoder* decoder, | 65 virtual void OnImageDecoded(const ImageDecoder* decoder, |
62 const SkBitmap& decoded_image) OVERRIDE; | 66 const SkBitmap& decoded_image) OVERRIDE; |
63 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE; | 67 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE; |
64 | 68 |
65 // The message loop object of the thread in which we notify the delegate. | 69 // The message loop object of the thread in which we notify the delegate. |
66 MessageLoop* target_message_loop_; | 70 MessageLoop* target_message_loop_; |
67 | 71 |
68 // Specify how the file should be decoded in the utility process. | 72 // Specify how the file should be decoded in the utility process. |
69 const ImageDecoder::ImageCodec image_codec_; | 73 const ImageDecoder::ImageCodec image_codec_; |
70 | 74 |
| 75 // Proctect image_info_map_ |
| 76 base::Lock lock_; |
| 77 |
71 // Holds info structures about all images we're trying to decode. | 78 // Holds info structures about all images we're trying to decode. |
72 // Accessed only on FILE thread. | 79 // Accessed on multiple worker threads. |
73 ImageInfoMap image_info_map_; | 80 ImageInfoMap image_info_map_; |
74 | 81 |
75 DISALLOW_COPY_AND_ASSIGN(UserImageLoader); | 82 DISALLOW_COPY_AND_ASSIGN(UserImageLoader); |
76 }; | 83 }; |
77 | 84 |
78 } // namespace chromeos | 85 } // namespace chromeos |
79 | 86 |
80 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ | 87 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ |
OLD | NEW |