user_manager.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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/ref_counted.h"
15#include "chrome/browser/chromeos/login/user_image_loader.h"
16#include "chrome/common/notification_observer.h"
17#include "chrome/common/notification_registrar.h"
18#include "third_party/skia/include/core/SkBitmap.h"
19
20class FilePath;
21class PrefService;
22
23namespace chromeos {
24
25// This class provides a mechanism for discovering users who have logged
26// into this chromium os device before and updating that list.
27class UserManager : public UserImageLoader::Delegate,
28                    public NotificationObserver {
29 public:
30  // A class representing information about a previously logged in user.
31  class User {
32   public:
33    User();
34    ~User() {}
35
36    // The email the user used to log in.
37    void set_email(const std::string& email) { email_ = email; }
38    const std::string& email() const { return email_; }
39
40    // Returns the name to display for this user.
41    std::string GetDisplayName() const;
42
43    // Tooltip contains user's display name and his email domain to distinguish
44    // this user from the other one with the same display name.
45    std::string GetNameTooltip() const;
46
47    // The image for this user.
48    void set_image(const SkBitmap& image) { image_ = image; }
49    const SkBitmap& image() const { return image_; }
50
51   private:
52    std::string email_;
53    SkBitmap image_;
54  };
55
56  // Gets a shared instance of a UserManager. Not thread-safe...should
57  // only be called from the main UI thread.
58  static UserManager* Get();
59
60  // Registers user manager preferences.
61  static void RegisterPrefs(PrefService* local_state);
62
63  // Returns a list of the users who have logged into this device previously.
64  // It is sorted in order of recency, with most recent at the beginning.
65  virtual std::vector<User> GetUsers() const;
66
67  // Indicates that user just started off the record session.
68  virtual void OffTheRecordUserLoggedIn();
69
70  // Indicates that a user with the given email has just logged in.
71  // The persistent list will be updated accordingly.
72  virtual void UserLoggedIn(const std::string& email);
73
74  // Remove user from persistent list. NOTE: user's data won't be removed.
75  virtual void RemoveUser(const std::string& email);
76
77  // Returns true if given user has logged into the device before.
78  virtual bool IsKnownUser(const std::string& email);
79
80  // Returns the logged-in user.
81  virtual const User& logged_in_user() const {
82    return logged_in_user_;
83  }
84
85  // Sets image for logged-in user and sends LOGIN_USER_IMAGE_CHANGED
86  // notification about the image changed via NotificationService.
87  void SetLoggedInUserImage(const SkBitmap& image);
88
89  // Saves image to file and saves image path in local state preferences.
90  void SaveUserImage(const std::string& username,
91                     const SkBitmap& image);
92
93  // chromeos::UserImageLoader::Delegate implementation.
94  virtual void OnImageLoaded(const std::string& username,
95                             const SkBitmap& image);
96
97  // NotificationObserver implementation.
98  virtual void Observe(NotificationType type,
99                       const NotificationSource& source,
100                       const NotificationDetails& details);
101
102  // Accessor for current_user_is_owner_
103  virtual bool current_user_is_owner() const {
104    return current_user_is_owner_;
105  }
106  virtual void set_current_user_is_owner(bool current_user_is_owner) {
107    current_user_is_owner_ = current_user_is_owner;
108  }
109
110  // Accessor for current_user_is_new_.
111  bool current_user_is_new() const {
112    return current_user_is_new_;
113  }
114
115  bool user_is_logged_in() const { return user_is_logged_in_; }
116
117  // Returns true if we're logged in as a Guest.
118  bool IsLoggedInAsGuest() const;
119
120 protected:
121  UserManager();
122  virtual ~UserManager();
123
124  // Returns image filepath for the given user.
125  FilePath GetImagePathForUser(const std::string& username);
126
127 private:
128  // Notifies on new user session.
129  void NotifyOnLogin();
130
131  // Sets one of the default images to the specified user and saves this
132  // setting in local state.
133  void SetDefaultUserImage(const std::string& username);
134
135  // Loads user image from its file.
136  scoped_refptr<UserImageLoader> image_loader_;
137
138  // Cache for user images. Stores image for each username.
139  typedef base::hash_map<std::string, SkBitmap> UserImages;
140  mutable UserImages user_images_;
141
142  // The logged-in user.
143  User logged_in_user_;
144
145  // Cached flag of whether currently logged-in user is owner or not.
146  bool current_user_is_owner_;
147
148  // Cached flag of whether the currently logged-in user existed before this
149  // login.
150  bool current_user_is_new_;
151
152  // Cached flag of whether any user is logged in at the moment.
153  bool user_is_logged_in_;
154
155  NotificationRegistrar registrar_;
156
157  DISALLOW_COPY_AND_ASSIGN(UserManager);
158};
159
160typedef std::vector<UserManager::User> UserVector;
161
162}  // namespace chromeos
163
164#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_USER_MANAGER_H_
165