1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_PROFILES_PROFILE_HELPER_H_
#define CHROME_BROWSER_CHROMEOS_PROFILES_PROFILE_HELPER_H_
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/callback_forward.h"
#include "base/files/file_path.h"
#include "chrome/browser/browsing_data/browsing_data_remover.h"
#include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h"
#include "components/user_manager/user_manager.h"
class Profile;
class User;
namespace base {
class FilePath;
}
namespace extensions {
class ExtensionGarbageCollectorChromeOSUnitTest;
}
namespace chromeos {
// This helper class is used on Chrome OS to keep track of currently
// active user profile.
// Whenever active user is changed (either add another user into session or
// switch between users), ActiveUserHashChanged() will be called thus
// internal state |active_user_id_hash_| will be updated.
// Typical use cases for using this class:
// 1. Get "signin profile" which is a special type of profile that is only used
// during signin flow: GetSigninProfile()
// 2. Get profile dir of an active user, used by ProfileManager:
// GetActiveUserProfileDir()
// 3. Get mapping from user_id_hash to Profile instance/profile path etc.
class ProfileHelper
: public BrowsingDataRemover::Observer,
public OAuth2LoginManager::Observer,
public user_manager::UserManager::UserSessionStateObserver {
public:
ProfileHelper();
~ProfileHelper() override;
// Returns ProfileHelper instance. This class is not singleton and is owned
// by BrowserProcess/BrowserProcessPlatformPart. This method keeps that
// knowledge in one place.
static ProfileHelper* Get();
// Returns Profile instance that corresponds to |user_id_hash|.
static Profile* GetProfileByUserIdHash(const std::string& user_id_hash);
// Returns profile path that corresponds to a given |user_id_hash|.
static base::FilePath GetProfilePathByUserIdHash(
const std::string& user_id_hash);
// Returns the path that corresponds to the sign-in profile.
static base::FilePath GetSigninProfileDir();
// Returns OffTheRecord profile for use during signing phase.
static Profile* GetSigninProfile();
// Returns user_id hash for |profile| instance or empty string if hash
// could not be extracted from |profile|.
static std::string GetUserIdHashFromProfile(Profile* profile);
// Returns user profile dir in a format [u-user_id_hash].
static base::FilePath GetUserProfileDir(const std::string& user_id_hash);
// Returns true if |profile| is the signin Profile. This can be used during
// construction of the signin Profile to determine if that Profile is the
// signin Profile.
static bool IsSigninProfile(Profile* profile);
// Returns true when |profile| corresponds to owner's profile.
static bool IsOwnerProfile(Profile* profile);
// Returns true when |profile| corresponds to the primary user profile
// of the current session.
static bool IsPrimaryProfile(Profile* profile);
// Initialize a bunch of services that are tied to a browser profile.
// TODO(dzhioev): Investigate whether or not this method is needed.
void ProfileStartup(Profile* profile, bool process_startup);
// Returns active user profile dir in a format [u-$hash].
base::FilePath GetActiveUserProfileDir();
// Should called once after UserManager instance has been created.
void Initialize();
// Returns hash for active user ID which is used to identify that user profile
// on Chrome OS.
std::string active_user_id_hash() { return active_user_id_hash_; }
// Clears site data (cookies, history, etc) for signin profile.
// Callback can be empty. Not thread-safe.
void ClearSigninProfile(const base::Closure& on_clear_callback);
// Returns profile of the |user| if it is created and fully initialized.
// Otherwise, returns NULL.
Profile* GetProfileByUser(const user_manager::User* user);
// DEPRECATED
// Returns profile of the |user| if user's profile is created and fully
// initialized. Otherwise, if some user is active, returns his profile.
// Otherwise, returns signin profile.
// Behaviour of this function does not correspond to its name and can be
// very surprising, that's why it should not be used anymore.
// Use |GetProfileByUser| instead.
// TODO(dzhioev): remove this method. http://crbug.com/361528
Profile* GetProfileByUserUnsafe(const user_manager::User* user);
// Returns NULL if User is not created.
user_manager::User* GetUserByProfile(Profile* profile);
static std::string GetUserIdHashByUserIdForTesting(
const std::string& user_id);
private:
friend class CryptohomeAuthenticatorTest;
friend class DeviceSettingsTestBase;
friend class extensions::ExtensionGarbageCollectorChromeOSUnitTest;
friend class FakeChromeUserManager;
friend class KioskTest;
friend class MockUserManager;
friend class MultiProfileUserControllerTest;
friend class ProfileHelperTest;
friend class ProfileListChromeOSTest;
friend class SessionStateDelegateChromeOSTest;
friend class SystemTrayDelegateChromeOSTest;
// BrowsingDataRemover::Observer implementation:
void OnBrowsingDataRemoverDone() override;
// OAuth2LoginManager::Observer overrides.
void OnSessionRestoreStateChanged(
Profile* user_profile,
OAuth2LoginManager::SessionRestoreState state) override;
// user_manager::UserManager::UserSessionStateObserver implementation:
void ActiveUserHashChanged(const std::string& hash) override;
// Associates |user| with profile with the same user_id,
// for GetUserByProfile() testing.
void SetProfileToUserMappingForTesting(user_manager::User* user);
// Enables/disables testing code path in GetUserByProfile() like
// always return primary user (when always_return_primary_user_for_testing is
// set).
static void SetProfileToUserForTestingEnabled(bool enabled);
// Enables/disables testing GetUserByProfile() by always returning
// primary user.
static void SetAlwaysReturnPrimaryUserForTesting(bool value);
// Associates |profile| with |user|, for GetProfileByUser() testing.
void SetUserToProfileMappingForTesting(const user_manager::User* user,
Profile* profile);
// Identifies path to active user profile on Chrome OS.
std::string active_user_id_hash_;
// True if signin profile clearing now.
bool signin_profile_clear_requested_;
// List of callbacks called after signin profile clearance.
std::vector<base::Closure> on_clear_callbacks_;
// Used for testing by unit tests and FakeUserManager/MockUserManager.
std::map<const user_manager::User*, Profile*> user_to_profile_for_testing_;
// When this list is not empty GetUserByProfile() will find user that has
// the same user_id as |profile|->GetProfileName().
user_manager::UserList user_list_for_testing_;
// If true testing code path is used in GetUserByProfile() even if
// user_list_for_testing_ list is empty. In that case primary user will always
// be returned.
static bool enable_profile_to_user_testing;
// If true and enable_profile_to_user_testing is true then primary user will
// always be returned by GetUserByProfile().
static bool always_return_primary_user_for_testing;
DISALLOW_COPY_AND_ASSIGN(ProfileHelper);
};
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_PROFILES_PROFILE_HELPER_H_
|