| 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" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h" | 14 #include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h" |
| 15 #include "chrome/browser/managed_mode/managed_user_service.h" | 15 #include "chrome/browser/managed_mode/managed_user_service.h" |
| 16 #include "chrome/browser/managed_mode/managed_user_service_factory.h" | 16 #include "chrome/browser/managed_mode/managed_user_service_factory.h" |
| 17 #include "chrome/browser/managed_mode/managed_user_sync_service.h" | 17 #include "chrome/browser/managed_mode/managed_user_sync_service.h" |
| 18 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h" | 18 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h" |
| 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 20 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 20 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 22 #include "chrome/browser/sync/glue/device_info.h" | 22 #include "chrome/browser/sync/glue/device_info.h" |
| 23 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 25 #include "google_apis/gaia/gaia_urls.h" | 25 #include "google_apis/gaia/gaia_urls.h" |
| 26 #include "google_apis/gaia/google_service_auth_error.h" | 26 #include "google_apis/gaia/google_service_auth_error.h" |
| 27 | 27 |
| 28 using base::DictionaryValue; | 28 using base::DictionaryValue; |
| 29 | 29 |
| 30 // How long to wait before aborting user registration. If this is changed, the | |
| 31 // histogram limits in the BrowserOptionsHandler should also be updated. | |
| 32 const int kRegistrationTimeoutMS = 30 * 1000; | |
| 33 const char kAcknowledged[] = "acknowledged"; | 30 const char kAcknowledged[] = "acknowledged"; |
| 34 const char kName[] = "name"; | 31 const char kName[] = "name"; |
| 35 const char kMasterKey[] = "masterKey"; | 32 const char kMasterKey[] = "masterKey"; |
| 36 | 33 |
| 37 ManagedUserRegistrationInfo::ManagedUserRegistrationInfo(const string16& name) | 34 ManagedUserRegistrationInfo::ManagedUserRegistrationInfo(const string16& name) |
| 38 : name(name) { | 35 : name(name) { |
| 39 } | 36 } |
| 40 | 37 |
| 41 ManagedUserRegistrationUtility::ManagedUserRegistrationUtility( | 38 ManagedUserRegistrationUtility::ManagedUserRegistrationUtility( |
| 42 PrefService* prefs, | 39 PrefService* prefs, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 return new_managed_user_id; | 75 return new_managed_user_id; |
| 79 } | 76 } |
| 80 | 77 |
| 81 void ManagedUserRegistrationUtility::Register( | 78 void ManagedUserRegistrationUtility::Register( |
| 82 const std::string& managed_user_id, | 79 const std::string& managed_user_id, |
| 83 const ManagedUserRegistrationInfo& info, | 80 const ManagedUserRegistrationInfo& info, |
| 84 const RegistrationCallback& callback) { | 81 const RegistrationCallback& callback) { |
| 85 DCHECK(pending_managed_user_id_.empty()); | 82 DCHECK(pending_managed_user_id_.empty()); |
| 86 callback_ = callback; | 83 callback_ = callback; |
| 87 pending_managed_user_id_ = managed_user_id; | 84 pending_managed_user_id_ = managed_user_id; |
| 88 StartRegistrationTimer(); | |
| 89 | 85 |
| 90 const DictionaryValue* dict = prefs_->GetDictionary(prefs::kManagedUsers); | 86 const DictionaryValue* dict = prefs_->GetDictionary(prefs::kManagedUsers); |
| 91 is_existing_managed_user_ = dict->HasKey(managed_user_id); | 87 is_existing_managed_user_ = dict->HasKey(managed_user_id); |
| 92 if (!is_existing_managed_user_) { | 88 if (!is_existing_managed_user_) { |
| 93 managed_user_sync_service_->AddManagedUser(pending_managed_user_id_, | 89 managed_user_sync_service_->AddManagedUser(pending_managed_user_id_, |
| 94 base::UTF16ToUTF8(info.name), | 90 base::UTF16ToUTF8(info.name), |
| 95 info.master_key); | 91 info.master_key); |
| 96 } else { | 92 } else { |
| 97 // User already exists, don't wait for acknowledgment. | 93 // User already exists, don't wait for acknowledgment. |
| 98 OnManagedUserAcknowledged(managed_user_id); | 94 OnManagedUserAcknowledged(managed_user_id); |
| 99 } | 95 } |
| 100 | 96 |
| 101 browser_sync::DeviceInfo::GetClientName( | 97 browser_sync::DeviceInfo::GetClientName( |
| 102 base::Bind(&ManagedUserRegistrationUtility::FetchToken, | 98 base::Bind(&ManagedUserRegistrationUtility::FetchToken, |
| 103 weak_ptr_factory_.GetWeakPtr(), info.name)); | 99 weak_ptr_factory_.GetWeakPtr(), info.name)); |
| 104 } | 100 } |
| 105 | 101 |
| 106 void ManagedUserRegistrationUtility::StartRegistrationTimer() { | |
| 107 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 108 switches::kNoManagedUserRegistrationTimeout)) { | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 registration_timer_.Start( | |
| 113 FROM_HERE, | |
| 114 base::TimeDelta::FromMilliseconds(kRegistrationTimeoutMS), | |
| 115 base::Bind( | |
| 116 &ManagedUserRegistrationUtility::AbortPendingRegistration, | |
| 117 weak_ptr_factory_.GetWeakPtr(), | |
| 118 true, // Run the callback. | |
| 119 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED))); | |
| 120 } | |
| 121 | |
| 122 void ManagedUserRegistrationUtility::CancelPendingRegistration() { | 102 void ManagedUserRegistrationUtility::CancelPendingRegistration() { |
| 123 AbortPendingRegistration( | 103 AbortPendingRegistration( |
| 124 false, // Don't run the callback. The error will be ignored. | 104 false, // Don't run the callback. The error will be ignored. |
| 125 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | 105 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); |
| 126 } | 106 } |
| 127 | 107 |
| 128 void ManagedUserRegistrationUtility::OnManagedUserAcknowledged( | 108 void ManagedUserRegistrationUtility::OnManagedUserAcknowledged( |
| 129 const std::string& managed_user_id) { | 109 const std::string& managed_user_id) { |
| 130 DCHECK_EQ(pending_managed_user_id_, managed_user_id); | 110 DCHECK_EQ(pending_managed_user_id_, managed_user_id); |
| 131 DCHECK(!pending_managed_user_acknowledged_); | 111 DCHECK(!pending_managed_user_acknowledged_); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 void ManagedUserRegistrationUtility::AbortPendingRegistration( | 154 void ManagedUserRegistrationUtility::AbortPendingRegistration( |
| 175 bool run_callback, | 155 bool run_callback, |
| 176 const GoogleServiceAuthError& error) { | 156 const GoogleServiceAuthError& error) { |
| 177 pending_managed_user_token_.clear(); | 157 pending_managed_user_token_.clear(); |
| 178 CompleteRegistration(run_callback, error); | 158 CompleteRegistration(run_callback, error); |
| 179 } | 159 } |
| 180 | 160 |
| 181 void ManagedUserRegistrationUtility::CompleteRegistration( | 161 void ManagedUserRegistrationUtility::CompleteRegistration( |
| 182 bool run_callback, | 162 bool run_callback, |
| 183 const GoogleServiceAuthError& error) { | 163 const GoogleServiceAuthError& error) { |
| 184 registration_timer_.Stop(); | |
| 185 if (callback_.is_null()) | 164 if (callback_.is_null()) |
| 186 return; | 165 return; |
| 187 | 166 |
| 188 // We check that the user being registered is not an existing managed | 167 // We check that the user being registered is not an existing managed |
| 189 // user before deleting it from sync to avoid accidental deletion of | 168 // user before deleting it from sync to avoid accidental deletion of |
| 190 // existing managed users by just canceling the registration for example. | 169 // existing managed users by just canceling the registration for example. |
| 191 if (pending_managed_user_token_.empty() && !is_existing_managed_user_) { | 170 if (pending_managed_user_token_.empty() && !is_existing_managed_user_) { |
| 192 DCHECK(!pending_managed_user_id_.empty()); | 171 DCHECK(!pending_managed_user_id_.empty()); |
| 193 // Remove the pending managed user if we weren't successful. | 172 // Remove the pending managed user if we weren't successful. |
| 194 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); | 173 DictionaryPrefUpdate update(prefs_, prefs::kManagedUsers); |
| 195 bool success = | 174 bool success = |
| 196 update->RemoveWithoutPathExpansion(pending_managed_user_id_, NULL); | 175 update->RemoveWithoutPathExpansion(pending_managed_user_id_, NULL); |
| 197 DCHECK(success); | 176 DCHECK(success); |
| 198 managed_user_sync_service_->DeleteManagedUser(pending_managed_user_id_); | 177 managed_user_sync_service_->DeleteManagedUser(pending_managed_user_id_); |
| 199 } | 178 } |
| 200 | 179 |
| 201 if (run_callback) | 180 if (run_callback) |
| 202 callback_.Run(error, pending_managed_user_token_); | 181 callback_.Run(error, pending_managed_user_token_); |
| 203 callback_.Reset(); | 182 callback_.Reset(); |
| 204 } | 183 } |
| OLD | NEW |