Home | History | Annotate | Download | only in login
      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_MANAGER_IMPL_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_MANAGER_IMPL_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/time/time.h"
     14 #include "base/timer/timer.h"
     15 #include "chrome/browser/chromeos/login/user.h"
     16 #include "chrome/browser/chromeos/login/user_image_loader.h"
     17 #include "chrome/browser/chromeos/login/user_image_manager.h"
     18 #include "chrome/browser/profiles/profile_downloader_delegate.h"
     19 #include "ui/gfx/image/image_skia.h"
     20 
     21 class ProfileDownloader;
     22 class UserImage;
     23 
     24 namespace base {
     25 class FilePath;
     26 }
     27 
     28 namespace chromeos {
     29 
     30 class UserImageManagerImpl : public UserImageManager,
     31                              public ProfileDownloaderDelegate {
     32  public:
     33   UserImageManagerImpl();
     34 
     35   // UserImageManager implemenation:
     36   virtual ~UserImageManagerImpl();
     37   virtual void LoadUserImages(const UserList& users) OVERRIDE;
     38   virtual void UserLoggedIn(const std::string& email,
     39                             bool user_is_new,
     40                             bool user_is_local) OVERRIDE;
     41   virtual void SaveUserDefaultImageIndex(const std::string& username,
     42                                          int image_index) OVERRIDE;
     43   virtual void SaveUserImage(const std::string& username,
     44                              const UserImage& user_image) OVERRIDE;
     45   virtual void SaveUserImageFromFile(const std::string& username,
     46                                      const base::FilePath& path) OVERRIDE;
     47   virtual void SaveUserImageFromProfileImage(
     48       const std::string& username) OVERRIDE;
     49   virtual void DeleteUserImage(const std::string& username) OVERRIDE;
     50   virtual void DownloadProfileImage(const std::string& reason) OVERRIDE;
     51   virtual const gfx::ImageSkia& DownloadedProfileImage() const OVERRIDE;
     52   virtual void Shutdown() OVERRIDE;
     53 
     54  private:
     55   friend class UserImageManagerTest;
     56 
     57   // Non-const for testing purposes.
     58   static int user_image_migration_delay_sec;
     59 
     60   // ProfileDownloaderDelegate implementation:
     61   virtual bool NeedsProfilePicture() const OVERRIDE;
     62   virtual int GetDesiredImageSideLength() const OVERRIDE;
     63   virtual Profile* GetBrowserProfile() OVERRIDE;
     64   virtual std::string GetCachedPictureURL() const OVERRIDE;
     65   virtual void OnProfileDownloadSuccess(ProfileDownloader* downloader) OVERRIDE;
     66   virtual void OnProfileDownloadFailure(
     67       ProfileDownloader* downloader,
     68       ProfileDownloaderDelegate::FailureReason reason) OVERRIDE;
     69 
     70   // Returns image filepath for the given user.
     71   base::FilePath GetImagePathForUser(const std::string& username);
     72 
     73   // Sets one of the default images for the specified user and saves this
     74   // setting in local state.
     75   // Does not send LOGIN_USER_IMAGE_CHANGED notification.
     76   void SetInitialUserImage(const std::string& username);
     77 
     78   // Sets image for user |username| and sends LOGIN_USER_IMAGE_CHANGED
     79   // notification unless this is a new user and image is set for the first time.
     80   // If |image| is empty, sets a stub image for the user.
     81   void SetUserImage(const std::string& username,
     82                     int image_index,
     83                     const GURL& image_url,
     84                     const UserImage& user_image);
     85 
     86   // Saves image to file, updates local state preferences to given image index
     87   // and sends LOGIN_USER_IMAGE_CHANGED notification.
     88   void SaveUserImageInternal(const std::string& username,
     89                              int image_index,
     90                              const GURL& image_url,
     91                              const UserImage& user_image);
     92 
     93   // Saves image to file with specified path and sends LOGIN_USER_IMAGE_CHANGED
     94   // notification. Runs on FILE thread. Posts task for saving image info to
     95   // Local State on UI thread.
     96   void SaveImageToFile(const std::string& username,
     97                        const UserImage& user_image,
     98                        const base::FilePath& image_path,
     99                        int image_index,
    100                        const GURL& image_url);
    101 
    102   // Stores path to the image and its index in local state. Runs on UI thread.
    103   // If |is_async| is true, it has been posted from the FILE thread after
    104   // saving the image.
    105   void SaveImageToLocalState(const std::string& username,
    106                              const std::string& image_path,
    107                              int image_index,
    108                              const GURL& image_url,
    109                              bool is_async);
    110 
    111   // Saves |image| to the specified |image_path|. Runs on FILE thread.
    112   bool SaveBitmapToFile(const UserImage& user_image,
    113                         const base::FilePath& image_path);
    114 
    115   // Initializes |downloaded_profile_image_| with the picture of the logged-in
    116   // user.
    117   void InitDownloadedProfileImage();
    118 
    119   // Download user's profile data, including full name and picture, when
    120   // |download_image| is true.
    121   // |reason| is an arbitrary string (used to report UMA histograms with
    122   // download times).
    123   void DownloadProfileData(const std::string& reason, bool download_image);
    124 
    125   // Scheduled call for downloading profile data.
    126   void DownloadProfileDataScheduled();
    127 
    128   // Delayed call to retry downloading profile data.
    129   void DownloadProfileDataRetry(bool download_image);
    130 
    131   // Migrates image info for the current user and deletes the old image, if any.
    132   void MigrateUserImage();
    133 
    134   // Deletes old user image and dictionary entry.
    135   void DeleteOldUserImage(const std::string& username);
    136 
    137   // Loader for JPEG user images.
    138   scoped_refptr<UserImageLoader> image_loader_;
    139 
    140   // Unsafe loader instance for all user images formats.
    141   scoped_refptr<UserImageLoader> unsafe_image_loader_;
    142 
    143   // Download user profile image on login to update it if it's changed.
    144   scoped_ptr<ProfileDownloader> profile_image_downloader_;
    145 
    146   // Arbitrary string passed to the last |DownloadProfileImage| call.
    147   std::string profile_image_download_reason_;
    148 
    149   // Time when the profile image download has started.
    150   base::Time profile_image_load_start_time_;
    151 
    152   // True if the last user image required async save operation (which may not
    153   // have been completed yet). This flag is used to avoid races when user image
    154   // is first set with |SaveUserImage| and then with |SaveUserImagePath|.
    155   bool last_image_set_async_;
    156 
    157   // Result of the last successful profile image download, if any.
    158   gfx::ImageSkia downloaded_profile_image_;
    159 
    160   // Data URL for |downloaded_profile_image_|.
    161   std::string downloaded_profile_image_data_url_;
    162 
    163   // Original URL of |downloaded_profile_image_|, from which it was downloaded.
    164   GURL profile_image_url_;
    165 
    166   // True when |profile_image_downloader_| is fetching profile picture (not
    167   // just full name).
    168   bool downloading_profile_image_;
    169 
    170   // Timer triggering DownloadProfileDataScheduled for refreshing profile data.
    171   base::RepeatingTimer<UserImageManagerImpl> profile_download_timer_;
    172 
    173   // Users that need image migration to JPEG.
    174   std::set<std::string> users_to_migrate_;
    175 
    176   // If |true|, current user image should be migrated right after it is loaded.
    177   bool migrate_current_user_on_load_;
    178 
    179   DISALLOW_COPY_AND_ASSIGN(UserImageManagerImpl);
    180 };
    181 
    182 }  // namespace chromeos
    183 
    184 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_USER_IMAGE_MANAGER_IMPL_H_
    185