Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: chrome/browser/policy/user_policy_identity_strategy.cc

Issue 6979011: Move user cloud policy to BrowserProcess. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/user_policy_identity_strategy.h" 5 #include "chrome/browser/policy/user_policy_identity_strategy.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "chrome/browser/browser_signin.h" 8 #include "chrome/browser/browser_signin.h"
9 #include "chrome/browser/net/gaia/token_service.h" 9 #include "chrome/browser/net/gaia/token_service.h"
10 #include "chrome/browser/policy/proto/device_management_backend.pb.h" 10 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
11 #include "chrome/browser/policy/proto/device_management_constants.h" 11 #include "chrome/browser/policy/proto/device_management_constants.h"
12 #include "chrome/browser/policy/proto/device_management_local.pb.h" 12 #include "chrome/browser/policy/proto/device_management_local.pb.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/guid.h" 13 #include "chrome/common/guid.h"
15 #include "chrome/common/net/gaia/gaia_constants.h" 14 #include "chrome/common/net/gaia/gaia_constants.h"
16 #include "content/browser/browser_thread.h" 15 #include "content/browser/browser_thread.h"
17 #include "content/common/notification_details.h" 16 #include "content/common/notification_details.h"
18 #include "content/common/notification_service.h" 17 #include "content/common/notification_service.h"
19 #include "content/common/notification_source.h" 18 #include "content/common/notification_source.h"
20 19
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/login/user_manager.h"
23 #endif
24 20
25 namespace policy { 21 namespace policy {
26 22
27 namespace em = enterprise_management; 23 namespace em = enterprise_management;
28 24
29 // Responsible for managing the on-disk token cache. 25 // Responsible for managing the on-disk token cache.
30 class UserPolicyIdentityStrategy::TokenCache 26 class UserPolicyIdentityStrategy::TokenCache
31 : public base::RefCountedThreadSafe< 27 : public base::RefCountedThreadSafe<
32 UserPolicyIdentityStrategy::TokenCache> { 28 UserPolicyIdentityStrategy::TokenCache> {
33 public: 29 public:
34 TokenCache(const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy, 30 TokenCache(const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy,
35 const FilePath& cache_file); 31 const FilePath& cache_file);
36 32
37 void Load(); 33 void Load();
38 void Store(const std::string& token, const std::string& device_id); 34 void Store(const std::string& token, const std::string& device_id);
39 35
36 bool load_initiated();
37 bool load_finished();
Mattias Nissler (ping if slow) 2011/05/26 10:20:20 what do you need these for?
sfeuz 2011/05/31 07:32:31 Currently there is the following potential race co
Mattias Nissler (ping if slow) 2011/05/31 14:14:19 I see and agree we should handle that condition gr
sfeuz 2011/06/03 08:30:35 Done.
38
40 private: 39 private:
41 friend class base::RefCountedThreadSafe< 40 friend class base::RefCountedThreadSafe<
42 UserPolicyIdentityStrategy::TokenCache>; 41 UserPolicyIdentityStrategy::TokenCache>;
43 ~TokenCache() {} 42 ~TokenCache() {}
44 void LoadOnFileThread(); 43 void LoadOnFileThread();
45 void NotifyOnUIThread(const std::string& token, 44 void NotifyOnUIThread(const std::string& token,
46 const std::string& device_id); 45 const std::string& device_id);
47 void StoreOnFileThread(const std::string& token, 46 void StoreOnFileThread(const std::string& token,
48 const std::string& device_id); 47 const std::string& device_id);
49 48
50 const base::WeakPtr<UserPolicyIdentityStrategy> identity_strategy_; 49 const base::WeakPtr<UserPolicyIdentityStrategy> identity_strategy_;
51 const FilePath cache_file_; 50 const FilePath cache_file_;
51 bool load_finished_;
52 bool load_initiated_;
52 53
53 DISALLOW_COPY_AND_ASSIGN(TokenCache); 54 DISALLOW_COPY_AND_ASSIGN(TokenCache);
54 }; 55 };
55 56
56 UserPolicyIdentityStrategy::TokenCache::TokenCache( 57 UserPolicyIdentityStrategy::TokenCache::TokenCache(
57 const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy, 58 const base::WeakPtr<UserPolicyIdentityStrategy>& identity_strategy,
58 const FilePath& cache_file) 59 const FilePath& cache_file)
59 : identity_strategy_(identity_strategy), 60 : identity_strategy_(identity_strategy),
60 cache_file_(cache_file) {} 61 cache_file_(cache_file),
62 load_finished_(false),
63 load_initiated_(false) {}
61 64
62 void UserPolicyIdentityStrategy::TokenCache::Load() { 65 void UserPolicyIdentityStrategy::TokenCache::Load() {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67 DCHECK(!load_initiated());
68 load_initiated_ = true;
64 BrowserThread::PostTask( 69 BrowserThread::PostTask(
65 BrowserThread::FILE, FROM_HERE, 70 BrowserThread::FILE, FROM_HERE,
66 NewRunnableMethod( 71 NewRunnableMethod(
67 this, &UserPolicyIdentityStrategy::TokenCache::LoadOnFileThread)); 72 this, &UserPolicyIdentityStrategy::TokenCache::LoadOnFileThread));
68 } 73 }
69 74
70 void UserPolicyIdentityStrategy::TokenCache::Store( 75 void UserPolicyIdentityStrategy::TokenCache::Store(
71 const std::string& token, 76 const std::string& token,
72 const std::string& device_id) { 77 const std::string& device_id) {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(...skipping 27 matching lines...) Expand all
101 this, 106 this,
102 &UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread, 107 &UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread,
103 device_token, 108 device_token,
104 device_id)); 109 device_id));
105 } 110 }
106 111
107 void UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread( 112 void UserPolicyIdentityStrategy::TokenCache::NotifyOnUIThread(
108 const std::string& token, 113 const std::string& token,
109 const std::string& device_id) { 114 const std::string& device_id) {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
116 DCHECK(!load_finished());
117 load_finished_ = true;
111 if (identity_strategy_.get()) 118 if (identity_strategy_.get())
112 identity_strategy_->OnCacheLoaded(token, device_id); 119 identity_strategy_->OnCacheLoaded(token, device_id);
113 } 120 }
114 121
115 void UserPolicyIdentityStrategy::TokenCache::StoreOnFileThread( 122 void UserPolicyIdentityStrategy::TokenCache::StoreOnFileThread(
116 const std::string& token, 123 const std::string& token,
117 const std::string& device_id) { 124 const std::string& device_id) {
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
119 em::DeviceCredentials device_credentials; 126 em::DeviceCredentials device_credentials;
120 device_credentials.set_device_token(token); 127 device_credentials.set_device_token(token);
121 device_credentials.set_device_id(device_id); 128 device_credentials.set_device_id(device_id);
122 std::string data; 129 std::string data;
123 bool success = device_credentials.SerializeToString(&data); 130 bool success = device_credentials.SerializeToString(&data);
124 if (!success) { 131 if (!success) {
125 LOG(WARNING) << "Failed serialize device token data, will not write " 132 LOG(WARNING) << "Failed serialize device token data, will not write "
126 << cache_file_.value(); 133 << cache_file_.value();
127 return; 134 return;
128 } 135 }
129 136
130 if (!file_util::CreateDirectory(cache_file_.DirName())) { 137 if (!file_util::CreateDirectory(cache_file_.DirName())) {
131 LOG(WARNING) << "Failed to create directory " 138 LOG(WARNING) << "Failed to create directory "
132 << cache_file_.DirName().value(); 139 << cache_file_.DirName().value();
133 return; 140 return;
134 } 141 }
135 142
136 file_util::WriteFile(cache_file_, data.c_str(), data.length()); 143 file_util::WriteFile(cache_file_, data.c_str(), data.length());
137 } 144 }
138 145
146 bool UserPolicyIdentityStrategy::TokenCache::load_initiated() {
147 return load_initiated_;
148 }
149
150 bool UserPolicyIdentityStrategy::TokenCache::load_finished() {
151 return load_finished_;
152 }
153
139 UserPolicyIdentityStrategy::UserPolicyIdentityStrategy( 154 UserPolicyIdentityStrategy::UserPolicyIdentityStrategy(
140 Profile* profile, 155 const std::string& user_name,
141 const FilePath& cache_file) 156 const FilePath& cache_file)
142 : profile_(profile), 157 : user_name_(user_name),
143 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { 158 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
144 cache_ = new TokenCache(weak_ptr_factory_.GetWeakPtr(), cache_file); 159 cache_ = new TokenCache(weak_ptr_factory_.GetWeakPtr(), cache_file);
145 registrar_.Add(this,
146 NotificationType::TOKEN_AVAILABLE,
147 Source<TokenService>(profile->GetTokenService()));
148
149 // Register for the event of user login. The device management token won't
150 // be fetched until we know the domain of the currently logged in user.
151 #if defined(OS_CHROMEOS)
152 registrar_.Add(this,
153 NotificationType::LOGIN_USER_CHANGED,
154 NotificationService::AllSources());
155 #else
156 registrar_.Add(this,
157 NotificationType::GOOGLE_SIGNIN_SUCCESSFUL,
158 Source<Profile>(profile_));
159 #endif
160 } 160 }
161 161
162 UserPolicyIdentityStrategy::~UserPolicyIdentityStrategy() {} 162 UserPolicyIdentityStrategy::~UserPolicyIdentityStrategy() {}
163 163
164 void UserPolicyIdentityStrategy::LoadTokenCache() { 164 void UserPolicyIdentityStrategy::LoadTokenCache() {
165 cache_->Load(); 165 cache_->Load();
166 } 166 }
167 167
168 std::string UserPolicyIdentityStrategy::GetDeviceToken() { 168 std::string UserPolicyIdentityStrategy::GetDeviceToken() {
169 return device_token_; 169 return device_token_;
(...skipping 15 matching lines...) Expand all
185 UserPolicyIdentityStrategy::GetPolicyRegisterType() { 185 UserPolicyIdentityStrategy::GetPolicyRegisterType() {
186 return em::DeviceRegisterRequest::USER; 186 return em::DeviceRegisterRequest::USER;
187 } 187 }
188 188
189 std::string UserPolicyIdentityStrategy::GetPolicyType() { 189 std::string UserPolicyIdentityStrategy::GetPolicyType() {
190 return kChromeUserPolicyType; 190 return kChromeUserPolicyType;
191 } 191 }
192 192
193 bool UserPolicyIdentityStrategy::GetCredentials(std::string* username, 193 bool UserPolicyIdentityStrategy::GetCredentials(std::string* username,
194 std::string* auth_token) { 194 std::string* auth_token) {
195 *username = GetCurrentUser(); 195 *username = user_name_;
196 *auth_token = profile_->GetTokenService()->GetTokenForService( 196 *auth_token = auth_token_;
197 GaiaConstants::kDeviceManagementService);
198 197
199 return !username->empty() && !auth_token->empty() && !device_id_.empty(); 198 return !username->empty() && !auth_token->empty() && !device_id_.empty();
200 } 199 }
201 200
202 void UserPolicyIdentityStrategy::OnDeviceTokenAvailable( 201 void UserPolicyIdentityStrategy::OnDeviceTokenAvailable(
203 const std::string& token) { 202 const std::string& token) {
204 DCHECK(!device_id_.empty()); 203 DCHECK(!device_id_.empty());
205 device_token_ = token; 204 device_token_ = token;
206 cache_->Store(device_token_, device_id_); 205 cache_->Store(device_token_, device_id_);
207 NotifyDeviceTokenChanged(); 206 NotifyDeviceTokenChanged();
208 } 207 }
209 208
210 std::string UserPolicyIdentityStrategy::GetCurrentUser() {
211 #if defined(OS_CHROMEOS)
212 // TODO(mnissler) On CrOS it seems impossible to figure out what user belongs
213 // to a profile. Revisit after multi-profile support landed.
214 return chromeos::UserManager::Get()->logged_in_user().email();
215 #else
216 return profile_->GetBrowserSignin()->GetSignedInUsername();
217 #endif
218 }
219
220 void UserPolicyIdentityStrategy::CheckAndTriggerFetch() { 209 void UserPolicyIdentityStrategy::CheckAndTriggerFetch() {
221 if (!GetCurrentUser().empty() && 210 if (!user_name_.empty() && !auth_token_.empty() && cache_->load_finished()) {
222 profile_->GetTokenService()->HasTokenForService(
223 GaiaConstants::kDeviceManagementService)) {
224 // For user tokens, there is no actual identifier. We generate a random 211 // For user tokens, there is no actual identifier. We generate a random
225 // identifier instead each time we ask for the token. 212 // identifier instead each time we ask for the token.
226 device_id_ = guid::GenerateGUID(); 213 device_id_ = guid::GenerateGUID();
227 NotifyAuthChanged(); 214 NotifyAuthChanged();
228 } 215 }
229 } 216 }
230 217
218 void UserPolicyIdentityStrategy::SetAuthToken(const std::string& auth_token) {
219 auth_token_ = auth_token;
220 CheckAndTriggerFetch();
221 }
222
231 void UserPolicyIdentityStrategy::OnCacheLoaded(const std::string& token, 223 void UserPolicyIdentityStrategy::OnCacheLoaded(const std::string& token,
232 const std::string& device_id) { 224 const std::string& device_id) {
233 if (!token.empty() && !device_id.empty()) { 225 if (!token.empty() && !device_id.empty()) {
234 device_token_ = token; 226 device_token_ = token;
235 device_id_ = device_id; 227 device_id_ = device_id;
236 NotifyDeviceTokenChanged(); 228 NotifyDeviceTokenChanged();
237 } else { 229 } else {
238 CheckAndTriggerFetch(); 230 CheckAndTriggerFetch();
239 } 231 }
240 } 232 }
241 233
242 void UserPolicyIdentityStrategy::Observe(NotificationType type,
243 const NotificationSource& source,
244 const NotificationDetails& details) {
245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
246 if (type == NotificationType::TOKEN_AVAILABLE) {
247 if (Source<TokenService>(source).ptr() == profile_->GetTokenService()) {
248 const TokenService::TokenAvailableDetails* token_details =
249 Details<const TokenService::TokenAvailableDetails>(details).ptr();
250 if (token_details->service() == GaiaConstants::kDeviceManagementService)
251 if (device_token_.empty()) {
252 // Request a new device management server token, but only in case we
253 // don't already have it.
254 CheckAndTriggerFetch();
255 }
256 }
257 #if defined(OS_CHROMEOS)
258 } else if (type == NotificationType::LOGIN_USER_CHANGED) {
259 CheckAndTriggerFetch();
260 #else
261 } else if (type == NotificationType::GOOGLE_SIGNIN_SUCCESSFUL) {
262 if (profile_ == Source<Profile>(source).ptr())
263 CheckAndTriggerFetch();
264 #endif
265 } else {
266 NOTREACHED();
267 }
268 }
269 234
270 } // namespace policy 235 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698