Chromium Code Reviews| 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. |update_avatar| can only be true when a managed user |
| 54 // name of the the user. |callback| is called with the result of the | 54 // is being imported, in this case the managed user's avatar will also |
| 55 // registration. We use the info here and not the profile, because on | 55 // be updated on the sync server. |info| contains necessary information like |
| 56 // Chrome OS the profile of the managed user does not yet exist. | 56 // the display name of the user and his avatar. |callback| is called |
| 57 // with the result of the registration. We use the info here and not the | |
| 58 // profile, because on Chrome OS the profile of the managed user does not | |
| 59 // yet exist. | |
| 57 virtual void Register(const std::string& managed_user_id, | 60 virtual void Register(const std::string& managed_user_id, |
| 61 bool update_avatar, | |
| 58 const ManagedUserRegistrationInfo& info, | 62 const ManagedUserRegistrationInfo& info, |
| 59 const RegistrationCallback& callback) OVERRIDE; | 63 const RegistrationCallback& callback) OVERRIDE; |
| 60 | 64 |
| 61 // ManagedUserSyncServiceObserver: | 65 // ManagedUserSyncServiceObserver: |
| 62 virtual void OnManagedUserAcknowledged(const std::string& managed_user_id) | 66 virtual void OnManagedUserAcknowledged(const std::string& managed_user_id) |
| 63 OVERRIDE; | 67 OVERRIDE; |
| 64 virtual void OnManagedUsersSyncingStopped() OVERRIDE; | 68 virtual void OnManagedUsersSyncingStopped() OVERRIDE; |
| 65 | 69 |
| 66 private: | 70 private: |
| 67 // Fetches the managed user token when we have the device name. | 71 // Fetches the managed user token when we have the device name. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 93 PrefService* prefs_; | 97 PrefService* prefs_; |
| 94 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher_; | 98 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher_; |
| 95 | 99 |
| 96 // A |BrowserContextKeyedService| owned by the custodian profile. | 100 // A |BrowserContextKeyedService| owned by the custodian profile. |
| 97 ManagedUserSyncService* managed_user_sync_service_; | 101 ManagedUserSyncService* managed_user_sync_service_; |
| 98 | 102 |
| 99 std::string pending_managed_user_id_; | 103 std::string pending_managed_user_id_; |
| 100 std::string pending_managed_user_token_; | 104 std::string pending_managed_user_token_; |
| 101 bool pending_managed_user_acknowledged_; | 105 bool pending_managed_user_acknowledged_; |
| 102 bool is_existing_managed_user_; | 106 bool is_existing_managed_user_; |
| 107 bool update_avatar_; | |
| 103 RegistrationCallback callback_; | 108 RegistrationCallback callback_; |
| 104 | 109 |
| 105 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationUtilityImpl); | 110 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationUtilityImpl); |
| 106 }; | 111 }; |
| 107 | 112 |
| 108 } // namespace | 113 } // namespace |
| 109 | 114 |
| 110 ManagedUserRegistrationInfo::ManagedUserRegistrationInfo(const string16& name, | 115 ManagedUserRegistrationInfo::ManagedUserRegistrationInfo(const string16& name, |
| 111 int avatar_index) | 116 int avatar_index) |
| 112 : avatar_index(avatar_index), | 117 : avatar_index(avatar_index), |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 | 180 |
| 176 ManagedUserRegistrationUtilityImpl::ManagedUserRegistrationUtilityImpl( | 181 ManagedUserRegistrationUtilityImpl::ManagedUserRegistrationUtilityImpl( |
| 177 PrefService* prefs, | 182 PrefService* prefs, |
| 178 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, | 183 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, |
| 179 ManagedUserSyncService* service) | 184 ManagedUserSyncService* service) |
| 180 : weak_ptr_factory_(this), | 185 : weak_ptr_factory_(this), |
| 181 prefs_(prefs), | 186 prefs_(prefs), |
| 182 token_fetcher_(token_fetcher.Pass()), | 187 token_fetcher_(token_fetcher.Pass()), |
| 183 managed_user_sync_service_(service), | 188 managed_user_sync_service_(service), |
| 184 pending_managed_user_acknowledged_(false), | 189 pending_managed_user_acknowledged_(false), |
| 185 is_existing_managed_user_(false) { | 190 is_existing_managed_user_(false), |
| 191 update_avatar_(false) { | |
| 186 managed_user_sync_service_->AddObserver(this); | 192 managed_user_sync_service_->AddObserver(this); |
| 187 } | 193 } |
| 188 | 194 |
| 189 ManagedUserRegistrationUtilityImpl::~ManagedUserRegistrationUtilityImpl() { | 195 ManagedUserRegistrationUtilityImpl::~ManagedUserRegistrationUtilityImpl() { |
| 190 managed_user_sync_service_->RemoveObserver(this); | 196 managed_user_sync_service_->RemoveObserver(this); |
| 191 CancelPendingRegistration(); | 197 CancelPendingRegistration(); |
| 192 } | 198 } |
| 193 | 199 |
| 194 void ManagedUserRegistrationUtilityImpl::Register( | 200 void ManagedUserRegistrationUtilityImpl::Register( |
| 195 const std::string& managed_user_id, | 201 const std::string& managed_user_id, |
| 202 bool update_avatar, | |
| 196 const ManagedUserRegistrationInfo& info, | 203 const ManagedUserRegistrationInfo& info, |
| 197 const RegistrationCallback& callback) { | 204 const RegistrationCallback& callback) { |
| 198 DCHECK(pending_managed_user_id_.empty()); | 205 DCHECK(pending_managed_user_id_.empty()); |
| 199 callback_ = callback; | 206 callback_ = callback; |
| 200 pending_managed_user_id_ = managed_user_id; | 207 pending_managed_user_id_ = managed_user_id; |
| 201 | 208 |
| 202 const DictionaryValue* dict = prefs_->GetDictionary(prefs::kManagedUsers); | 209 const DictionaryValue* dict = prefs_->GetDictionary(prefs::kManagedUsers); |
| 203 is_existing_managed_user_ = dict->HasKey(managed_user_id); | 210 is_existing_managed_user_ = dict->HasKey(managed_user_id); |
| 204 if (!is_existing_managed_user_) { | 211 if (!is_existing_managed_user_) { |
| 212 DCHECK(!update_avatar); | |
| 205 managed_user_sync_service_->AddManagedUser(pending_managed_user_id_, | 213 managed_user_sync_service_->AddManagedUser(pending_managed_user_id_, |
| 206 base::UTF16ToUTF8(info.name), | 214 base::UTF16ToUTF8(info.name), |
| 207 info.master_key, | 215 info.master_key, |
| 208 info.avatar_index); | 216 info.avatar_index); |
| 209 } else { | 217 } else { |
| 218 if (update_avatar) { | |
|
Bernhard Bauer
2013/09/13 11:24:20
Couldn't we infer this from the information we alr
ibra
2013/09/13 14:16:50
Done.
| |
| 219 update_avatar_ = true; | |
| 220 managed_user_sync_service_->UpdateManagedUserAvatar(managed_user_id, | |
| 221 info.avatar_index); | |
| 222 } | |
| 223 | |
| 210 // User already exists, don't wait for acknowledgment. | 224 // User already exists, don't wait for acknowledgment. |
| 211 OnManagedUserAcknowledged(managed_user_id); | 225 OnManagedUserAcknowledged(managed_user_id); |
| 212 } | 226 } |
| 213 | 227 |
| 214 browser_sync::DeviceInfo::GetClientName( | 228 browser_sync::DeviceInfo::GetClientName( |
| 215 base::Bind(&ManagedUserRegistrationUtilityImpl::FetchToken, | 229 base::Bind(&ManagedUserRegistrationUtilityImpl::FetchToken, |
| 216 weak_ptr_factory_.GetWeakPtr())); | 230 weak_ptr_factory_.GetWeakPtr())); |
| 217 } | 231 } |
| 218 | 232 |
| 219 void ManagedUserRegistrationUtilityImpl::CancelPendingRegistration() { | 233 void ManagedUserRegistrationUtilityImpl::CancelPendingRegistration() { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 pending_managed_user_token_.clear(); | 289 pending_managed_user_token_.clear(); |
| 276 CompleteRegistration(run_callback, error); | 290 CompleteRegistration(run_callback, error); |
| 277 } | 291 } |
| 278 | 292 |
| 279 void ManagedUserRegistrationUtilityImpl::CompleteRegistration( | 293 void ManagedUserRegistrationUtilityImpl::CompleteRegistration( |
| 280 bool run_callback, | 294 bool run_callback, |
| 281 const GoogleServiceAuthError& error) { | 295 const GoogleServiceAuthError& error) { |
| 282 if (callback_.is_null()) | 296 if (callback_.is_null()) |
| 283 return; | 297 return; |
| 284 | 298 |
| 285 // We check that the user being registered is not an existing managed | 299 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()); | 300 DCHECK(!pending_managed_user_id_.empty()); |
| 290 // Remove the pending managed user if we weren't successful. | 301 |
| 291 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 302 if (!is_existing_managed_user_) { |
| 292 bool success = | 303 // Remove the pending managed user if we weren't successful. |
| 293 update->RemoveWithoutPathExpansion(pending_managed_user_id_, NULL); | 304 // However, check that we are not importing a managed user |
| 294 DCHECK(success); | 305 // before deleting it from sync to avoid accidental deletion of |
| 295 managed_user_sync_service_->DeleteManagedUser(pending_managed_user_id_); | 306 // existing managed users by just canceling the registration for example. |
| 307 managed_user_sync_service_->DeleteManagedUser(pending_managed_user_id_); | |
| 308 } else if (update_avatar_) { | |
| 309 // Canceling (or failing) a managed user import that did set the avatar | |
| 310 // should undo this change. | |
| 311 managed_user_sync_service_->ClearManagedUserAvatar( | |
| 312 pending_managed_user_id_); | |
| 313 } | |
| 296 } | 314 } |
| 297 | 315 |
| 298 if (run_callback) | 316 if (run_callback) |
| 299 callback_.Run(error, pending_managed_user_token_); | 317 callback_.Run(error, pending_managed_user_token_); |
| 300 callback_.Reset(); | 318 callback_.Reset(); |
| 301 } | 319 } |
| 302 | 320 |
| 303 } // namespace | 321 } // namespace |
| OLD | NEW |