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_MANAGER_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_ 7 #pragma once 8 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/hash_tables.h" 14 #include "base/memory/ref_counted.h" 15 #include "base/synchronization/lock.h" 16 #include "chrome/browser/chromeos/login/user_image_loader.h" 17 #include "content/common/notification_observer.h" 18 #include "content/common/notification_registrar.h" 19 #include "third_party/skia/include/core/SkBitmap.h" 20 21 class FilePath; 22 class PrefService; 23 24 namespace base { 25 template<typename> struct DefaultLazyInstanceTraits; 26 } 27 28 namespace chromeos { 29 class RemoveUserDelegate; 30 31 // This class provides a mechanism for discovering users who have logged 32 // into this chromium os device before and updating that list. 33 class UserManager : public UserImageLoader::Delegate, 34 public NotificationObserver { 35 public: 36 // A class representing information about a previously logged in user. 37 class User { 38 public: 39 User(); 40 ~User(); 41 42 // The email the user used to log in. 43 void set_email(const std::string& email) { email_ = email; } 44 const std::string& email() const { return email_; } 45 46 // Returns the name to display for this user. 47 std::string GetDisplayName() const; 48 49 // Tooltip contains user's display name and his email domain to distinguish 50 // this user from the other one with the same display name. 51 std::string GetNameTooltip() const; 52 53 // The image for this user. 54 void set_image(const SkBitmap& image) { image_ = image; } 55 const SkBitmap& image() const { return image_; } 56 57 private: 58 std::string email_; 59 SkBitmap image_; 60 }; 61 62 // Gets a shared instance of a UserManager. Not thread-safe...should 63 // only be called from the main UI thread. 64 static UserManager* Get(); 65 66 // Registers user manager preferences. 67 static void RegisterPrefs(PrefService* local_state); 68 69 // Returns a list of the users who have logged into this device previously. 70 // It is sorted in order of recency, with most recent at the beginning. 71 virtual std::vector<User> GetUsers() const; 72 73 // Indicates that user just started incognito session. 74 virtual void OffTheRecordUserLoggedIn(); 75 76 // Indicates that a user with the given email has just logged in. 77 // The persistent list will be updated accordingly. 78 virtual void UserLoggedIn(const std::string& email); 79 80 // Removes the user from the device. Note, it will verify that the given user 81 // isn't the owner, so calling this method for the owner will take no effect. 82 // Note, |delegate| can be NULL. 83 virtual void RemoveUser(const std::string& email, 84 RemoveUserDelegate* delegate); 85 86 // Removes the user from the persistent list only. Also removes the user's 87 // picture. 88 virtual void RemoveUserFromList(const std::string& email); 89 90 // Returns true if given user has logged into the device before. 91 virtual bool IsKnownUser(const std::string& email); 92 93 // Returns the logged-in user. 94 virtual const User& logged_in_user() const; 95 96 // Sets image for logged-in user and sends LOGIN_USER_IMAGE_CHANGED 97 // notification about the image changed via NotificationService. 98 void SetLoggedInUserImage(const SkBitmap& image); 99 100 // Tries to load logged-in user image from disk and sets it for the user. 101 void LoadLoggedInUserImage(const FilePath& path); 102 103 // Saves image to file and saves image path in local state preferences. 104 void SaveUserImage(const std::string& username, 105 const SkBitmap& image); 106 107 // Saves user image path for the user. Can be used to set default images. 108 void SaveUserImagePath(const std::string& username, 109 const std::string& image_path); 110 111 // Returns the index of user's default image or -1 if the image is not 112 // default. 113 int GetUserDefaultImageIndex(const std::string& username); 114 115 // chromeos::UserImageLoader::Delegate implementation. 116 virtual void OnImageLoaded(const std::string& username, 117 const SkBitmap& image, 118 bool save_image); 119 120 // NotificationObserver implementation. 121 virtual void Observe(NotificationType type, 122 const NotificationSource& source, 123 const NotificationDetails& details); 124 125 // Accessor for current_user_is_owner_ 126 virtual bool current_user_is_owner() const; 127 virtual void set_current_user_is_owner(bool current_user_is_owner); 128 129 // Accessor for current_user_is_new_. 130 bool current_user_is_new() const { 131 return current_user_is_new_; 132 } 133 134 bool user_is_logged_in() const { return user_is_logged_in_; } 135 136 // Returns true if we're logged in as a Guest. 137 bool IsLoggedInAsGuest() const; 138 139 protected: 140 UserManager(); 141 virtual ~UserManager(); 142 143 // Returns image filepath for the given user. 144 FilePath GetImagePathForUser(const std::string& username); 145 146 private: 147 // Notifies on new user session. 148 void NotifyOnLogin(); 149 150 // Sets one of the default images to the specified user and saves this 151 // setting in local state. 152 void SetDefaultUserImage(const std::string& username); 153 154 // Loads user image from its file. 155 scoped_refptr<UserImageLoader> image_loader_; 156 157 // Cache for user images. Stores image for each username. 158 typedef base::hash_map<std::string, SkBitmap> UserImages; 159 mutable UserImages user_images_; 160 161 // The logged-in user. 162 User logged_in_user_; 163 164 // Cached flag of whether currently logged-in user is owner or not. 165 // May be accessed on different threads, requires locking. 166 bool current_user_is_owner_; 167 mutable base::Lock current_user_is_owner_lock_; 168 169 // Cached flag of whether the currently logged-in user existed before this 170 // login. 171 bool current_user_is_new_; 172 173 // Cached flag of whether any user is logged in at the moment. 174 bool user_is_logged_in_; 175 176 NotificationRegistrar registrar_; 177 178 friend struct base::DefaultLazyInstanceTraits<UserManager>; 179 180 DISALLOW_COPY_AND_ASSIGN(UserManager); 181 }; 182 183 typedef std::vector<UserManager::User> UserVector; 184 185 } // namespace chromeos 186 187 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_ 188