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