1 // Copyright (c) 2011 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 #pragma once 8 9 #include <map> 10 #include <string> 11 12 #include "base/basictypes.h" 13 #include "base/memory/ref_counted.h" 14 #include "chrome/browser/chromeos/login/image_decoder.h" 15 16 class MessageLoop; 17 class SkBitmap; 18 19 namespace chromeos { 20 21 // A facility to read a file containing user image asynchronously in the IO 22 // thread. Returns the image in the form of an SkBitmap. 23 class UserImageLoader : public base::RefCountedThreadSafe<UserImageLoader>, 24 public ImageDecoder::Delegate { 25 public: 26 class Delegate { 27 public: 28 // Invoked when user image has been read. 29 // |should_save_image| indicates if user image should be saved somewhere 30 // for later use. 31 virtual void OnImageLoaded(const std::string& username, 32 const SkBitmap& image, 33 bool should_save_image) = 0; 34 35 protected: ~Delegate()36 virtual ~Delegate() {} 37 }; 38 39 explicit UserImageLoader(Delegate* delegate); 40 41 // Start reading the image for |username| from |filepath| on the file thread. 42 // |should_save_image| is passed to OnImageLoaded handler. 43 void Start(const std::string& username, 44 const std::string& filepath, 45 bool should_save_image); 46 set_delegate(Delegate * delegate)47 void set_delegate(Delegate* delegate) { delegate_ = delegate; } 48 49 private: 50 friend class base::RefCountedThreadSafe<UserImageLoader>; 51 52 // Contains attributes we need to know about each image we decode. 53 struct ImageInfo { ImageInfoImageInfo54 ImageInfo(const std::string& username, bool should_save) 55 : username(username), 56 should_save_image(should_save) { 57 } 58 59 std::string username; 60 bool should_save_image; 61 }; 62 63 typedef std::map<const ImageDecoder*, ImageInfo> ImageInfoMap; 64 65 virtual ~UserImageLoader(); 66 67 // Method that reads the file on the file thread and starts decoding it in 68 // sandboxed process. 69 void LoadImage(const std::string& filepath, const ImageInfo& image_info); 70 71 // ImageDecoder::Delegate implementation. 72 virtual void OnImageDecoded(const ImageDecoder* decoder, 73 const SkBitmap& decoded_image); 74 virtual void OnDecodeImageFailed(const ImageDecoder* decoder); 75 76 // Notifies the delegate that image was loaded, on delegate's thread. 77 void NotifyDelegate(const SkBitmap& image, const ImageInfo& image_info); 78 79 // The message loop object of the thread in which we notify the delegate. 80 MessageLoop* target_message_loop_; 81 82 // Delegate to notify about finishing the load of the image. 83 Delegate* delegate_; 84 85 // Holds info structures about all images we're trying to decode. 86 // Accessed only on FILE thread. 87 ImageInfoMap image_info_map_; 88 89 DISALLOW_COPY_AND_ASSIGN(UserImageLoader); 90 }; 91 92 } // namespace chromeos 93 94 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_LOADER_H_ 95