| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/supervised_user/legacy/supervised_user_registration_uti
lity.h" | 5 #include "chrome/browser/supervised_user/legacy/supervised_user_registration_uti
lity.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/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/signin/chrome_signin_client_factory.h" | 15 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 18 #include "chrome/browser/supervised_user/legacy/supervised_user_refresh_token_fe
tcher.h" | 18 #include "chrome/browser/supervised_user/legacy/supervised_user_refresh_token_fe
tcher.h" |
| 19 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_
service.h" | 19 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_
service.h" |
| 20 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_
service_factory.h" | 20 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_
service_factory.h" |
| 21 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_
update.h" | 21 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_
update.h" |
| 22 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h" | 22 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h" |
| 23 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service_fac
tory.h" | 23 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service_fac
tory.h" |
| 24 #include "chrome/browser/supervised_user/supervised_user_constants.h" | 24 #include "chrome/browser/supervised_user/supervised_user_constants.h" |
| 25 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 27 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 27 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 28 #include "components/signin/core/browser/signin_client.h" | 28 #include "components/signin/core/browser/signin_client.h" |
| 29 #include "components/signin/core/browser/signin_manager.h" | 29 #include "components/signin/core/browser/signin_manager.h" |
| 30 #include "components/user_manager/user_id.h" |
| 30 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 31 #include "google_apis/gaia/gaia_urls.h" | 32 #include "google_apis/gaia/gaia_urls.h" |
| 32 #include "google_apis/gaia/google_service_auth_error.h" | 33 #include "google_apis/gaia/google_service_auth_error.h" |
| 33 #include "sync/util/get_session_name.h" | 34 #include "sync/util/get_session_name.h" |
| 34 | 35 |
| 35 using base::DictionaryValue; | 36 using base::DictionaryValue; |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 SupervisedUserRegistrationUtility* g_instance_for_tests = NULL; | 40 SupervisedUserRegistrationUtility* g_instance_for_tests = NULL; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 52 ~SupervisedUserRegistrationUtilityImpl() override; | 53 ~SupervisedUserRegistrationUtilityImpl() override; |
| 53 | 54 |
| 54 // Registers a new supervised user with the server. |supervised_user_id| is a | 55 // Registers a new supervised user with the server. |supervised_user_id| is a |
| 55 // new unique ID for the new supervised user. If its value is the same as that | 56 // new unique ID for the new supervised user. If its value is the same as that |
| 56 // of one of the existing supervised users, then the same user will be created | 57 // of one of the existing supervised users, then the same user will be created |
| 57 // on this machine (and if he has no avatar in sync, his avatar will be | 58 // on this machine (and if he has no avatar in sync, his avatar will be |
| 58 // updated). |info| contains necessary information like the display name of | 59 // updated). |info| contains necessary information like the display name of |
| 59 // the user and his avatar. |callback| is called with the result of the | 60 // the user and his avatar. |callback| is called with the result of the |
| 60 // registration. We use the info here and not the profile, because on Chrome | 61 // registration. We use the info here and not the profile, because on Chrome |
| 61 // OS the profile of the supervised user does not yet exist. | 62 // OS the profile of the supervised user does not yet exist. |
| 62 void Register(const std::string& supervised_user_id, | 63 void Register(const user_manager::UserID& supervised_user_id, |
| 63 const SupervisedUserRegistrationInfo& info, | 64 const SupervisedUserRegistrationInfo& info, |
| 64 const RegistrationCallback& callback) override; | 65 const RegistrationCallback& callback) override; |
| 65 | 66 |
| 66 // SupervisedUserSyncServiceObserver: | 67 // SupervisedUserSyncServiceObserver: |
| 67 void OnSupervisedUserAcknowledged( | 68 void OnSupervisedUserAcknowledged( |
| 68 const std::string& supervised_user_id) override; | 69 const user_manager::UserID& supervised_user_id) override; |
| 69 void OnSupervisedUsersSyncingStopped() override; | 70 void OnSupervisedUsersSyncingStopped() override; |
| 70 void OnSupervisedUsersChanged() override; | 71 void OnSupervisedUsersChanged() override; |
| 71 | 72 |
| 72 private: | 73 private: |
| 73 // Fetches the supervised user token when we have the device name. | 74 // Fetches the supervised user token when we have the device name. |
| 74 void FetchToken(const std::string& client_name); | 75 void FetchToken(const std::string& client_name); |
| 75 | 76 |
| 76 // Called when we have received a token for the supervised user. | 77 // Called when we have received a token for the supervised user. |
| 77 void OnReceivedToken(const GoogleServiceAuthError& error, | 78 void OnReceivedToken(const GoogleServiceAuthError& error, |
| 78 const std::string& token); | 79 const std::string& token); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 101 | 102 |
| 102 PrefService* prefs_; | 103 PrefService* prefs_; |
| 103 scoped_ptr<SupervisedUserRefreshTokenFetcher> token_fetcher_; | 104 scoped_ptr<SupervisedUserRefreshTokenFetcher> token_fetcher_; |
| 104 | 105 |
| 105 // A |KeyedService| owned by the custodian profile. | 106 // A |KeyedService| owned by the custodian profile. |
| 106 SupervisedUserSyncService* supervised_user_sync_service_; | 107 SupervisedUserSyncService* supervised_user_sync_service_; |
| 107 | 108 |
| 108 // A |KeyedService| owned by the custodian profile. | 109 // A |KeyedService| owned by the custodian profile. |
| 109 SupervisedUserSharedSettingsService* supervised_user_shared_settings_service_; | 110 SupervisedUserSharedSettingsService* supervised_user_shared_settings_service_; |
| 110 | 111 |
| 111 std::string pending_supervised_user_id_; | 112 user_manager::UserID pending_supervised_user_id_; |
| 112 std::string pending_supervised_user_token_; | 113 std::string pending_supervised_user_token_; |
| 113 bool pending_supervised_user_acknowledged_; | 114 bool pending_supervised_user_acknowledged_; |
| 114 bool is_existing_supervised_user_; | 115 bool is_existing_supervised_user_; |
| 115 bool avatar_updated_; | 116 bool avatar_updated_; |
| 116 RegistrationCallback callback_; | 117 RegistrationCallback callback_; |
| 117 scoped_ptr<SupervisedUserSharedSettingsUpdate> password_update_; | 118 scoped_ptr<SupervisedUserSharedSettingsUpdate> password_update_; |
| 118 | 119 |
| 119 base::WeakPtrFactory<SupervisedUserRegistrationUtilityImpl> weak_ptr_factory_; | 120 base::WeakPtrFactory<SupervisedUserRegistrationUtilityImpl> weak_ptr_factory_; |
| 120 | 121 |
| 121 DISALLOW_COPY_AND_ASSIGN(SupervisedUserRegistrationUtilityImpl); | 122 DISALLOW_COPY_AND_ASSIGN(SupervisedUserRegistrationUtilityImpl); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 SupervisedUserSharedSettingsService* supervised_user_shared_settings_service = | 172 SupervisedUserSharedSettingsService* supervised_user_shared_settings_service = |
| 172 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(profile); | 173 SupervisedUserSharedSettingsServiceFactory::GetForBrowserContext(profile); |
| 173 return make_scoped_ptr(SupervisedUserRegistrationUtility::CreateImpl( | 174 return make_scoped_ptr(SupervisedUserRegistrationUtility::CreateImpl( |
| 174 profile->GetPrefs(), | 175 profile->GetPrefs(), |
| 175 token_fetcher.Pass(), | 176 token_fetcher.Pass(), |
| 176 supervised_user_sync_service, | 177 supervised_user_sync_service, |
| 177 supervised_user_shared_settings_service)); | 178 supervised_user_shared_settings_service)); |
| 178 } | 179 } |
| 179 | 180 |
| 180 // static | 181 // static |
| 181 std::string SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId() { | 182 user_manager::UserID SupervisedUserRegistrationUtility::GenerateNewSupervisedUse
rId() { |
| 182 std::string new_supervised_user_id; | 183 std::string new_supervised_user_id_str; |
| 183 base::Base64Encode(base::RandBytesAsString(8), &new_supervised_user_id); | 184 base::Base64Encode(base::RandBytesAsString(8), &new_supervised_user_id_str); |
| 184 return new_supervised_user_id; | 185 return user_manager::UserID::FromUserEmail(new_supervised_user_id_str); |
| 185 } | 186 } |
| 186 | 187 |
| 187 // static | 188 // static |
| 188 void SupervisedUserRegistrationUtility::SetUtilityForTests( | 189 void SupervisedUserRegistrationUtility::SetUtilityForTests( |
| 189 SupervisedUserRegistrationUtility* utility) { | 190 SupervisedUserRegistrationUtility* utility) { |
| 190 if (g_instance_for_tests) | 191 if (g_instance_for_tests) |
| 191 delete g_instance_for_tests; | 192 delete g_instance_for_tests; |
| 192 g_instance_for_tests = utility; | 193 g_instance_for_tests = utility; |
| 193 } | 194 } |
| 194 | 195 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 209 | 210 |
| 210 SupervisedUserRegistrationUtilityImpl::SupervisedUserRegistrationUtilityImpl( | 211 SupervisedUserRegistrationUtilityImpl::SupervisedUserRegistrationUtilityImpl( |
| 211 PrefService* prefs, | 212 PrefService* prefs, |
| 212 scoped_ptr<SupervisedUserRefreshTokenFetcher> token_fetcher, | 213 scoped_ptr<SupervisedUserRefreshTokenFetcher> token_fetcher, |
| 213 SupervisedUserSyncService* service, | 214 SupervisedUserSyncService* service, |
| 214 SupervisedUserSharedSettingsService* shared_settings_service) | 215 SupervisedUserSharedSettingsService* shared_settings_service) |
| 215 : prefs_(prefs), | 216 : prefs_(prefs), |
| 216 token_fetcher_(token_fetcher.Pass()), | 217 token_fetcher_(token_fetcher.Pass()), |
| 217 supervised_user_sync_service_(service), | 218 supervised_user_sync_service_(service), |
| 218 supervised_user_shared_settings_service_(shared_settings_service), | 219 supervised_user_shared_settings_service_(shared_settings_service), |
| 220 pending_supervised_user_id_(std::string(), std::string()), |
| 219 pending_supervised_user_acknowledged_(false), | 221 pending_supervised_user_acknowledged_(false), |
| 220 is_existing_supervised_user_(false), | 222 is_existing_supervised_user_(false), |
| 221 avatar_updated_(false), | 223 avatar_updated_(false), |
| 222 weak_ptr_factory_(this) { | 224 weak_ptr_factory_(this) { |
| 223 supervised_user_sync_service_->AddObserver(this); | 225 supervised_user_sync_service_->AddObserver(this); |
| 224 } | 226 } |
| 225 | 227 |
| 226 SupervisedUserRegistrationUtilityImpl:: | 228 SupervisedUserRegistrationUtilityImpl:: |
| 227 ~SupervisedUserRegistrationUtilityImpl() { | 229 ~SupervisedUserRegistrationUtilityImpl() { |
| 228 supervised_user_sync_service_->RemoveObserver(this); | 230 supervised_user_sync_service_->RemoveObserver(this); |
| 229 CancelPendingRegistration(); | 231 CancelPendingRegistration(); |
| 230 } | 232 } |
| 231 | 233 |
| 232 void SupervisedUserRegistrationUtilityImpl::Register( | 234 void SupervisedUserRegistrationUtilityImpl::Register( |
| 233 const std::string& supervised_user_id, | 235 const user_manager::UserID& supervised_user_id, |
| 234 const SupervisedUserRegistrationInfo& info, | 236 const SupervisedUserRegistrationInfo& info, |
| 235 const RegistrationCallback& callback) { | 237 const RegistrationCallback& callback) { |
| 236 DCHECK(pending_supervised_user_id_.empty()); | 238 DCHECK(pending_supervised_user_id_.empty()); |
| 237 callback_ = callback; | 239 callback_ = callback; |
| 238 pending_supervised_user_id_ = supervised_user_id; | 240 pending_supervised_user_id_ = supervised_user_id; |
| 239 | 241 |
| 240 bool need_password_update = !info.password_data.empty(); | 242 bool need_password_update = !info.password_data.empty(); |
| 241 const base::DictionaryValue* dict = | 243 const base::DictionaryValue* dict = |
| 242 prefs_->GetDictionary(prefs::kSupervisedUsers); | 244 prefs_->GetDictionary(prefs::kSupervisedUsers); |
| 243 is_existing_supervised_user_ = dict->HasKey(supervised_user_id); | 245 is_existing_supervised_user_ = dict->HasKey(supervised_user_id.GetUserEmail())
; |
| 244 if (!is_existing_supervised_user_) { | 246 if (!is_existing_supervised_user_) { |
| 245 supervised_user_sync_service_->AddSupervisedUser( | 247 supervised_user_sync_service_->AddSupervisedUser( |
| 246 pending_supervised_user_id_, | 248 pending_supervised_user_id_, |
| 247 base::UTF16ToUTF8(info.name), | 249 base::UTF16ToUTF8(info.name), |
| 248 info.master_key, | 250 info.master_key, |
| 249 info.password_signature_key, | 251 info.password_signature_key, |
| 250 info.password_encryption_key, | 252 info.password_encryption_key, |
| 251 info.avatar_index); | 253 info.avatar_index); |
| 252 } else { | 254 } else { |
| 253 const base::DictionaryValue* value = NULL; | 255 const base::DictionaryValue* value = NULL; |
| 254 bool success = | 256 bool success = |
| 255 dict->GetDictionaryWithoutPathExpansion(supervised_user_id, &value); | 257 dict->GetDictionaryWithoutPathExpansion(supervised_user_id.GetUserEmail(
), &value); |
| 256 DCHECK(success); | 258 DCHECK(success); |
| 257 std::string key; | 259 std::string key; |
| 258 bool need_keys = !info.password_signature_key.empty() || | 260 bool need_keys = !info.password_signature_key.empty() || |
| 259 !info.password_encryption_key.empty(); | 261 !info.password_encryption_key.empty(); |
| 260 bool have_keys = | 262 bool have_keys = |
| 261 value->GetString(SupervisedUserSyncService::kPasswordSignatureKey, | 263 value->GetString(SupervisedUserSyncService::kPasswordSignatureKey, |
| 262 &key) && | 264 &key) && |
| 263 !key.empty() && | 265 !key.empty() && |
| 264 value->GetString(SupervisedUserSyncService::kPasswordEncryptionKey, | 266 value->GetString(SupervisedUserSyncService::kPasswordEncryptionKey, |
| 265 &key) && | 267 &key) && |
| (...skipping 18 matching lines...) Expand all Loading... |
| 284 supervised_user_sync_service_->UpdateSupervisedUserAvatarIfNeeded( | 286 supervised_user_sync_service_->UpdateSupervisedUserAvatarIfNeeded( |
| 285 supervised_user_id, | 287 supervised_user_id, |
| 286 info.avatar_index); | 288 info.avatar_index); |
| 287 } | 289 } |
| 288 #if defined(OS_CHROMEOS) | 290 #if defined(OS_CHROMEOS) |
| 289 const char* kAvatarKey = supervised_users::kChromeOSAvatarIndex; | 291 const char* kAvatarKey = supervised_users::kChromeOSAvatarIndex; |
| 290 #else | 292 #else |
| 291 const char* kAvatarKey = supervised_users::kChromeAvatarIndex; | 293 const char* kAvatarKey = supervised_users::kChromeAvatarIndex; |
| 292 #endif | 294 #endif |
| 293 supervised_user_shared_settings_service_->SetValue( | 295 supervised_user_shared_settings_service_->SetValue( |
| 294 pending_supervised_user_id_, kAvatarKey, | 296 pending_supervised_user_id_.GetUserEmail(), kAvatarKey, |
| 295 base::FundamentalValue(info.avatar_index)); | 297 base::FundamentalValue(info.avatar_index)); |
| 296 if (need_password_update) { | 298 if (need_password_update) { |
| 297 password_update_.reset(new SupervisedUserSharedSettingsUpdate( | 299 password_update_.reset(new SupervisedUserSharedSettingsUpdate( |
| 298 supervised_user_shared_settings_service_, | 300 supervised_user_shared_settings_service_, |
| 299 pending_supervised_user_id_, | 301 pending_supervised_user_id_.GetUserEmail(), |
| 300 supervised_users::kChromeOSPasswordData, | 302 supervised_users::kChromeOSPasswordData, |
| 301 scoped_ptr<base::Value>(info.password_data.DeepCopy()), | 303 scoped_ptr<base::Value>(info.password_data.DeepCopy()), |
| 302 base::Bind( | 304 base::Bind( |
| 303 &SupervisedUserRegistrationUtilityImpl:: | 305 &SupervisedUserRegistrationUtilityImpl:: |
| 304 OnPasswordChangeAcknowledged, | 306 OnPasswordChangeAcknowledged, |
| 305 weak_ptr_factory_.GetWeakPtr()))); | 307 weak_ptr_factory_.GetWeakPtr()))); |
| 306 } | 308 } |
| 307 | 309 |
| 308 syncer::GetSessionName( | 310 syncer::GetSessionName( |
| 309 content::BrowserThread::GetBlockingPool() | 311 content::BrowserThread::GetBlockingPool() |
| 310 ->GetTaskRunnerWithShutdownBehavior( | 312 ->GetTaskRunnerWithShutdownBehavior( |
| 311 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | 313 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), |
| 312 base::Bind(&SupervisedUserRegistrationUtilityImpl::FetchToken, | 314 base::Bind(&SupervisedUserRegistrationUtilityImpl::FetchToken, |
| 313 weak_ptr_factory_.GetWeakPtr())); | 315 weak_ptr_factory_.GetWeakPtr())); |
| 314 } | 316 } |
| 315 | 317 |
| 316 void SupervisedUserRegistrationUtilityImpl::CancelPendingRegistration() { | 318 void SupervisedUserRegistrationUtilityImpl::CancelPendingRegistration() { |
| 317 AbortPendingRegistration( | 319 AbortPendingRegistration( |
| 318 false, // Don't run the callback. The error will be ignored. | 320 false, // Don't run the callback. The error will be ignored. |
| 319 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | 321 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); |
| 320 } | 322 } |
| 321 | 323 |
| 322 void SupervisedUserRegistrationUtilityImpl::OnSupervisedUserAcknowledged( | 324 void SupervisedUserRegistrationUtilityImpl::OnSupervisedUserAcknowledged( |
| 323 const std::string& supervised_user_id) { | 325 const user_manager::UserID& supervised_user_id) { |
| 324 DCHECK_EQ(pending_supervised_user_id_, supervised_user_id); | 326 DCHECK(pending_supervised_user_id_ == supervised_user_id); |
| 325 DCHECK(!pending_supervised_user_acknowledged_); | 327 DCHECK(!pending_supervised_user_acknowledged_); |
| 326 pending_supervised_user_acknowledged_ = true; | 328 pending_supervised_user_acknowledged_ = true; |
| 327 CompleteRegistrationIfReady(); | 329 CompleteRegistrationIfReady(); |
| 328 } | 330 } |
| 329 | 331 |
| 330 void SupervisedUserRegistrationUtilityImpl::OnPasswordChangeAcknowledged( | 332 void SupervisedUserRegistrationUtilityImpl::OnPasswordChangeAcknowledged( |
| 331 bool success) { | 333 bool success) { |
| 332 DCHECK(password_update_); | 334 DCHECK(password_update_); |
| 333 DCHECK(success); | 335 DCHECK(success); |
| 334 password_update_.reset(); | 336 password_update_.reset(); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 pending_supervised_user_id_); | 412 pending_supervised_user_id_); |
| 411 } | 413 } |
| 412 } | 414 } |
| 413 | 415 |
| 414 if (run_callback) | 416 if (run_callback) |
| 415 callback_.Run(error, pending_supervised_user_token_); | 417 callback_.Run(error, pending_supervised_user_token_); |
| 416 callback_.Reset(); | 418 callback_.Reset(); |
| 417 } | 419 } |
| 418 | 420 |
| 419 } // namespace | 421 } // namespace |
| OLD | NEW |