OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/managed_mode/managed_user_registration_utility.h" | 5 #include "chrome/browser/managed_mode/managed_user_registration_utility.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 ManagedUserRegistrationUtilityImpl( | 43 ManagedUserRegistrationUtilityImpl( |
44 PrefService* prefs, | 44 PrefService* prefs, |
45 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, | 45 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, |
46 ManagedUserSyncService* service); | 46 ManagedUserSyncService* service); |
47 | 47 |
48 virtual ~ManagedUserRegistrationUtilityImpl(); | 48 virtual ~ManagedUserRegistrationUtilityImpl(); |
49 | 49 |
50 // Registers a new managed user with the server. |managed_user_id| is a new | 50 // Registers a new managed user with the server. |managed_user_id| is a new |
51 // unique ID for the new managed user. If its value is the same as that of | 51 // unique ID for the new managed user. If its value is the same as that of |
52 // of one of the existing managed users, then the same user will be created | 52 // of one of the existing managed users, then the same user will be created |
53 // on this machine. |info| contains necessary information like the display | 53 // on this machine (and if he has no avatar in sync, his avatar will |
54 // name of the the user. |callback| is called with the result of the | 54 // be updated). |info| contains necessary information like |
55 // registration. We use the info here and not the profile, because on | 55 // the display name of the user and his avatar. |callback| is called |
56 // Chrome OS the profile of the managed user does not yet exist. | 56 // with the result of the registration. We use the info here and not the |
57 // profile, because on Chrome OS the profile of the managed user does not | |
58 // yet exist. | |
57 virtual void Register(const std::string& managed_user_id, | 59 virtual void Register(const std::string& managed_user_id, |
58 const ManagedUserRegistrationInfo& info, | 60 const ManagedUserRegistrationInfo& info, |
59 const RegistrationCallback& callback) OVERRIDE; | 61 const RegistrationCallback& callback) OVERRIDE; |
60 | 62 |
61 // ManagedUserSyncServiceObserver: | 63 // ManagedUserSyncServiceObserver: |
62 virtual void OnManagedUserAcknowledged(const std::string& managed_user_id) | 64 virtual void OnManagedUserAcknowledged(const std::string& managed_user_id) |
63 OVERRIDE; | 65 OVERRIDE; |
64 virtual void OnManagedUsersSyncingStopped() OVERRIDE; | 66 virtual void OnManagedUsersSyncingStopped() OVERRIDE; |
65 | 67 |
66 private: | 68 private: |
(...skipping 26 matching lines...) Expand all Loading... | |
93 PrefService* prefs_; | 95 PrefService* prefs_; |
94 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher_; | 96 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher_; |
95 | 97 |
96 // A |BrowserContextKeyedService| owned by the custodian profile. | 98 // A |BrowserContextKeyedService| owned by the custodian profile. |
97 ManagedUserSyncService* managed_user_sync_service_; | 99 ManagedUserSyncService* managed_user_sync_service_; |
98 | 100 |
99 std::string pending_managed_user_id_; | 101 std::string pending_managed_user_id_; |
100 std::string pending_managed_user_token_; | 102 std::string pending_managed_user_token_; |
101 bool pending_managed_user_acknowledged_; | 103 bool pending_managed_user_acknowledged_; |
102 bool is_existing_managed_user_; | 104 bool is_existing_managed_user_; |
105 bool avatar_updated_; | |
103 RegistrationCallback callback_; | 106 RegistrationCallback callback_; |
104 | 107 |
105 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationUtilityImpl); | 108 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationUtilityImpl); |
106 }; | 109 }; |
107 | 110 |
108 } // namespace | 111 } // namespace |
109 | 112 |
110 ManagedUserRegistrationInfo::ManagedUserRegistrationInfo(const string16& name, | 113 ManagedUserRegistrationInfo::ManagedUserRegistrationInfo(const string16& name, |
111 int avatar_index) | 114 int avatar_index) |
112 : avatar_index(avatar_index), | 115 : avatar_index(avatar_index), |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 | 178 |
176 ManagedUserRegistrationUtilityImpl::ManagedUserRegistrationUtilityImpl( | 179 ManagedUserRegistrationUtilityImpl::ManagedUserRegistrationUtilityImpl( |
177 PrefService* prefs, | 180 PrefService* prefs, |
178 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, | 181 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, |
179 ManagedUserSyncService* service) | 182 ManagedUserSyncService* service) |
180 : weak_ptr_factory_(this), | 183 : weak_ptr_factory_(this), |
181 prefs_(prefs), | 184 prefs_(prefs), |
182 token_fetcher_(token_fetcher.Pass()), | 185 token_fetcher_(token_fetcher.Pass()), |
183 managed_user_sync_service_(service), | 186 managed_user_sync_service_(service), |
184 pending_managed_user_acknowledged_(false), | 187 pending_managed_user_acknowledged_(false), |
185 is_existing_managed_user_(false) { | 188 is_existing_managed_user_(false), |
189 avatar_updated_(false) { | |
186 managed_user_sync_service_->AddObserver(this); | 190 managed_user_sync_service_->AddObserver(this); |
187 } | 191 } |
188 | 192 |
189 ManagedUserRegistrationUtilityImpl::~ManagedUserRegistrationUtilityImpl() { | 193 ManagedUserRegistrationUtilityImpl::~ManagedUserRegistrationUtilityImpl() { |
190 managed_user_sync_service_->RemoveObserver(this); | 194 managed_user_sync_service_->RemoveObserver(this); |
191 CancelPendingRegistration(); | 195 CancelPendingRegistration(); |
192 } | 196 } |
193 | 197 |
194 void ManagedUserRegistrationUtilityImpl::Register( | 198 void ManagedUserRegistrationUtilityImpl::Register( |
195 const std::string& managed_user_id, | 199 const std::string& managed_user_id, |
196 const ManagedUserRegistrationInfo& info, | 200 const ManagedUserRegistrationInfo& info, |
197 const RegistrationCallback& callback) { | 201 const RegistrationCallback& callback) { |
198 DCHECK(pending_managed_user_id_.empty()); | 202 DCHECK(pending_managed_user_id_.empty()); |
199 callback_ = callback; | 203 callback_ = callback; |
200 pending_managed_user_id_ = managed_user_id; | 204 pending_managed_user_id_ = managed_user_id; |
201 | 205 |
202 const DictionaryValue* dict = prefs_->GetDictionary(prefs::kManagedUsers); | 206 const DictionaryValue* dict = prefs_->GetDictionary(prefs::kManagedUsers); |
203 is_existing_managed_user_ = dict->HasKey(managed_user_id); | 207 is_existing_managed_user_ = dict->HasKey(managed_user_id); |
204 if (!is_existing_managed_user_) { | 208 if (!is_existing_managed_user_) { |
205 managed_user_sync_service_->AddManagedUser(pending_managed_user_id_, | 209 managed_user_sync_service_->AddManagedUser(pending_managed_user_id_, |
206 base::UTF16ToUTF8(info.name), | 210 base::UTF16ToUTF8(info.name), |
207 info.master_key, | 211 info.master_key, |
208 info.avatar_index); | 212 info.avatar_index); |
209 } else { | 213 } else { |
214 avatar_updated_ = | |
Bernhard Bauer
2013/09/13 20:17:24
Nit: two spaces less.
ibra
2013/09/16 08:59:09
Done.
| |
215 managed_user_sync_service_->UpdateManagedUserAvatarIfNeeded( | |
216 managed_user_id, | |
217 info.avatar_index); | |
218 | |
210 // User already exists, don't wait for acknowledgment. | 219 // User already exists, don't wait for acknowledgment. |
211 OnManagedUserAcknowledged(managed_user_id); | 220 OnManagedUserAcknowledged(managed_user_id); |
212 } | 221 } |
213 | 222 |
214 browser_sync::DeviceInfo::GetClientName( | 223 browser_sync::DeviceInfo::GetClientName( |
215 base::Bind(&ManagedUserRegistrationUtilityImpl::FetchToken, | 224 base::Bind(&ManagedUserRegistrationUtilityImpl::FetchToken, |
216 weak_ptr_factory_.GetWeakPtr())); | 225 weak_ptr_factory_.GetWeakPtr())); |
217 } | 226 } |
218 | 227 |
219 void ManagedUserRegistrationUtilityImpl::CancelPendingRegistration() { | 228 void ManagedUserRegistrationUtilityImpl::CancelPendingRegistration() { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 pending_managed_user_token_.clear(); | 284 pending_managed_user_token_.clear(); |
276 CompleteRegistration(run_callback, error); | 285 CompleteRegistration(run_callback, error); |
277 } | 286 } |
278 | 287 |
279 void ManagedUserRegistrationUtilityImpl::CompleteRegistration( | 288 void ManagedUserRegistrationUtilityImpl::CompleteRegistration( |
280 bool run_callback, | 289 bool run_callback, |
281 const GoogleServiceAuthError& error) { | 290 const GoogleServiceAuthError& error) { |
282 if (callback_.is_null()) | 291 if (callback_.is_null()) |
283 return; | 292 return; |
284 | 293 |
285 // We check that the user being registered is not an existing managed | 294 if (pending_managed_user_token_.empty()) { |
286 // user before deleting it from sync to avoid accidental deletion of | |
287 // existing managed users by just canceling the registration for example. | |
288 if (pending_managed_user_token_.empty() && !is_existing_managed_user_) { | |
289 DCHECK(!pending_managed_user_id_.empty()); | 295 DCHECK(!pending_managed_user_id_.empty()); |
290 // Remove the pending managed user if we weren't successful. | 296 |
291 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 297 if (!is_existing_managed_user_) { |
292 bool success = | 298 // Remove the pending managed user if we weren't successful. |
293 update->RemoveWithoutPathExpansion(pending_managed_user_id_, NULL); | 299 // However, check that we are not importing a managed user |
294 DCHECK(success); | 300 // before deleting it from sync to avoid accidental deletion of |
295 managed_user_sync_service_->DeleteManagedUser(pending_managed_user_id_); | 301 // existing managed users by just canceling the registration for example. |
302 managed_user_sync_service_->DeleteManagedUser(pending_managed_user_id_); | |
303 } else if (avatar_updated_) { | |
304 // Canceling (or failing) a managed user import that did set the avatar | |
305 // should undo this change. | |
306 managed_user_sync_service_->ClearManagedUserAvatar( | |
307 pending_managed_user_id_); | |
308 } | |
296 } | 309 } |
297 | 310 |
298 if (run_callback) | 311 if (run_callback) |
299 callback_.Run(error, pending_managed_user_token_); | 312 callback_.Run(error, pending_managed_user_token_); |
300 callback_.Reset(); | 313 callback_.Reset(); |
301 } | 314 } |
302 | 315 |
303 } // namespace | 316 } // namespace |
OLD | NEW |