| 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_constants.h" |
| 14 #include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h" | 15 #include "chrome/browser/managed_mode/managed_user_refresh_token_fetcher.h" |
| 16 #include "chrome/browser/managed_mode/managed_user_shared_settings_service.h" |
| 17 #include "chrome/browser/managed_mode/managed_user_shared_settings_service_facto
ry.h" |
| 15 #include "chrome/browser/managed_mode/managed_user_sync_service.h" | 18 #include "chrome/browser/managed_mode/managed_user_sync_service.h" |
| 16 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h" | 19 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 21 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 20 #include "chrome/browser/sync/glue/device_info.h" | 23 #include "chrome/browser/sync/glue/device_info.h" |
| 21 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 23 #include "google_apis/gaia/gaia_urls.h" | 26 #include "google_apis/gaia/gaia_urls.h" |
| 24 #include "google_apis/gaia/google_service_auth_error.h" | 27 #include "google_apis/gaia/google_service_auth_error.h" |
| 25 | 28 |
| 26 using base::DictionaryValue; | 29 using base::DictionaryValue; |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 30 ManagedUserRegistrationUtility* g_instance_for_tests = NULL; | 33 ManagedUserRegistrationUtility* g_instance_for_tests = NULL; |
| 31 | 34 |
| 32 // Actual implementation of ManagedUserRegistrationUtility. | 35 // Actual implementation of ManagedUserRegistrationUtility. |
| 33 class ManagedUserRegistrationUtilityImpl | 36 class ManagedUserRegistrationUtilityImpl |
| 34 : public ManagedUserRegistrationUtility, | 37 : public ManagedUserRegistrationUtility, |
| 35 public ManagedUserSyncServiceObserver { | 38 public ManagedUserSyncServiceObserver { |
| 36 public: | 39 public: |
| 37 ManagedUserRegistrationUtilityImpl( | 40 ManagedUserRegistrationUtilityImpl( |
| 38 PrefService* prefs, | 41 PrefService* prefs, |
| 39 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, | 42 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, |
| 40 ManagedUserSyncService* service); | 43 ManagedUserSyncService* service, |
| 44 ManagedUserSharedSettingsService* shared_settings_service); |
| 41 | 45 |
| 42 virtual ~ManagedUserRegistrationUtilityImpl(); | 46 virtual ~ManagedUserRegistrationUtilityImpl(); |
| 43 | 47 |
| 44 // Registers a new managed user with the server. |managed_user_id| is a new | 48 // Registers a new managed user with the server. |managed_user_id| is a new |
| 45 // unique ID for the new managed user. If its value is the same as that of | 49 // unique ID for the new managed user. If its value is the same as that of |
| 46 // of one of the existing managed users, then the same user will be created | 50 // of one of the existing managed users, then the same user will be created |
| 47 // on this machine (and if he has no avatar in sync, his avatar will | 51 // on this machine (and if he has no avatar in sync, his avatar will |
| 48 // be updated). |info| contains necessary information like | 52 // be updated). |info| contains necessary information like |
| 49 // the display name of the user and his avatar. |callback| is called | 53 // the display name of the user and his avatar. |callback| is called |
| 50 // with the result of the registration. We use the info here and not the | 54 // with the result of the registration. We use the info here and not the |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // Cancels any registration currently in progress, without calling the | 88 // Cancels any registration currently in progress, without calling the |
| 85 // callback or reporting an error. | 89 // callback or reporting an error. |
| 86 void CancelPendingRegistration(); | 90 void CancelPendingRegistration(); |
| 87 | 91 |
| 88 PrefService* prefs_; | 92 PrefService* prefs_; |
| 89 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher_; | 93 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher_; |
| 90 | 94 |
| 91 // A |BrowserContextKeyedService| owned by the custodian profile. | 95 // A |BrowserContextKeyedService| owned by the custodian profile. |
| 92 ManagedUserSyncService* managed_user_sync_service_; | 96 ManagedUserSyncService* managed_user_sync_service_; |
| 93 | 97 |
| 98 // A |BrowserContextKeyedService| owned by the custodian profile. |
| 99 ManagedUserSharedSettingsService* managed_user_shared_settings_service_; |
| 100 |
| 94 std::string pending_managed_user_id_; | 101 std::string pending_managed_user_id_; |
| 95 std::string pending_managed_user_token_; | 102 std::string pending_managed_user_token_; |
| 96 bool pending_managed_user_acknowledged_; | 103 bool pending_managed_user_acknowledged_; |
| 97 bool is_existing_managed_user_; | 104 bool is_existing_managed_user_; |
| 98 bool avatar_updated_; | 105 bool avatar_updated_; |
| 99 RegistrationCallback callback_; | 106 RegistrationCallback callback_; |
| 100 | 107 |
| 101 base::WeakPtrFactory<ManagedUserRegistrationUtilityImpl> weak_ptr_factory_; | 108 base::WeakPtrFactory<ManagedUserRegistrationUtilityImpl> weak_ptr_factory_; |
| 102 | 109 |
| 103 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationUtilityImpl); | 110 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationUtilityImpl); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 134 | 141 |
| 135 ProfileOAuth2TokenService* token_service = | 142 ProfileOAuth2TokenService* token_service = |
| 136 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 143 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 137 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher = | 144 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher = |
| 138 ManagedUserRefreshTokenFetcher::Create( | 145 ManagedUserRefreshTokenFetcher::Create( |
| 139 token_service, | 146 token_service, |
| 140 token_service->GetPrimaryAccountId(), | 147 token_service->GetPrimaryAccountId(), |
| 141 profile->GetRequestContext()); | 148 profile->GetRequestContext()); |
| 142 ManagedUserSyncService* managed_user_sync_service = | 149 ManagedUserSyncService* managed_user_sync_service = |
| 143 ManagedUserSyncServiceFactory::GetForProfile(profile); | 150 ManagedUserSyncServiceFactory::GetForProfile(profile); |
| 151 ManagedUserSharedSettingsService* managed_user_shared_settings_service = |
| 152 ManagedUserSharedSettingsServiceFactory::GetForBrowserContext(profile); |
| 144 return make_scoped_ptr(ManagedUserRegistrationUtility::CreateImpl( | 153 return make_scoped_ptr(ManagedUserRegistrationUtility::CreateImpl( |
| 145 profile->GetPrefs(), | 154 profile->GetPrefs(), |
| 146 token_fetcher.Pass(), | 155 token_fetcher.Pass(), |
| 147 managed_user_sync_service)); | 156 managed_user_sync_service, |
| 157 managed_user_shared_settings_service)); |
| 148 } | 158 } |
| 149 | 159 |
| 150 // static | 160 // static |
| 151 std::string ManagedUserRegistrationUtility::GenerateNewManagedUserId() { | 161 std::string ManagedUserRegistrationUtility::GenerateNewManagedUserId() { |
| 152 std::string new_managed_user_id; | 162 std::string new_managed_user_id; |
| 153 base::Base64Encode(base::RandBytesAsString(8), &new_managed_user_id); | 163 base::Base64Encode(base::RandBytesAsString(8), &new_managed_user_id); |
| 154 return new_managed_user_id; | 164 return new_managed_user_id; |
| 155 } | 165 } |
| 156 | 166 |
| 157 // static | 167 // static |
| 158 void ManagedUserRegistrationUtility::SetUtilityForTests( | 168 void ManagedUserRegistrationUtility::SetUtilityForTests( |
| 159 ManagedUserRegistrationUtility* utility) { | 169 ManagedUserRegistrationUtility* utility) { |
| 160 if (g_instance_for_tests) | 170 if (g_instance_for_tests) |
| 161 delete g_instance_for_tests; | 171 delete g_instance_for_tests; |
| 162 g_instance_for_tests = utility; | 172 g_instance_for_tests = utility; |
| 163 } | 173 } |
| 164 | 174 |
| 165 // static | 175 // static |
| 166 ManagedUserRegistrationUtility* ManagedUserRegistrationUtility::CreateImpl( | 176 ManagedUserRegistrationUtility* ManagedUserRegistrationUtility::CreateImpl( |
| 167 PrefService* prefs, | 177 PrefService* prefs, |
| 168 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, | 178 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, |
| 169 ManagedUserSyncService* service) { | 179 ManagedUserSyncService* service, |
| 180 ManagedUserSharedSettingsService* shared_settings_service) { |
| 170 return new ManagedUserRegistrationUtilityImpl(prefs, | 181 return new ManagedUserRegistrationUtilityImpl(prefs, |
| 171 token_fetcher.Pass(), | 182 token_fetcher.Pass(), |
| 172 service); | 183 service, |
| 184 shared_settings_service); |
| 173 } | 185 } |
| 174 | 186 |
| 175 namespace { | 187 namespace { |
| 176 | 188 |
| 177 ManagedUserRegistrationUtilityImpl::ManagedUserRegistrationUtilityImpl( | 189 ManagedUserRegistrationUtilityImpl::ManagedUserRegistrationUtilityImpl( |
| 178 PrefService* prefs, | 190 PrefService* prefs, |
| 179 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, | 191 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher, |
| 180 ManagedUserSyncService* service) | 192 ManagedUserSyncService* service, |
| 193 ManagedUserSharedSettingsService* shared_settings_service) |
| 181 : prefs_(prefs), | 194 : prefs_(prefs), |
| 182 token_fetcher_(token_fetcher.Pass()), | 195 token_fetcher_(token_fetcher.Pass()), |
| 183 managed_user_sync_service_(service), | 196 managed_user_sync_service_(service), |
| 197 managed_user_shared_settings_service_(shared_settings_service), |
| 184 pending_managed_user_acknowledged_(false), | 198 pending_managed_user_acknowledged_(false), |
| 185 is_existing_managed_user_(false), | 199 is_existing_managed_user_(false), |
| 186 avatar_updated_(false), | 200 avatar_updated_(false), |
| 187 weak_ptr_factory_(this) { | 201 weak_ptr_factory_(this) { |
| 188 managed_user_sync_service_->AddObserver(this); | 202 managed_user_sync_service_->AddObserver(this); |
| 189 } | 203 } |
| 190 | 204 |
| 191 ManagedUserRegistrationUtilityImpl::~ManagedUserRegistrationUtilityImpl() { | 205 ManagedUserRegistrationUtilityImpl::~ManagedUserRegistrationUtilityImpl() { |
| 192 managed_user_sync_service_->RemoveObserver(this); | 206 managed_user_sync_service_->RemoveObserver(this); |
| 193 CancelPendingRegistration(); | 207 CancelPendingRegistration(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 211 info.avatar_index); | 225 info.avatar_index); |
| 212 } else { | 226 } else { |
| 213 avatar_updated_ = | 227 avatar_updated_ = |
| 214 managed_user_sync_service_->UpdateManagedUserAvatarIfNeeded( | 228 managed_user_sync_service_->UpdateManagedUserAvatarIfNeeded( |
| 215 managed_user_id, | 229 managed_user_id, |
| 216 info.avatar_index); | 230 info.avatar_index); |
| 217 | 231 |
| 218 // User already exists, don't wait for acknowledgment. | 232 // User already exists, don't wait for acknowledgment. |
| 219 OnManagedUserAcknowledged(managed_user_id); | 233 OnManagedUserAcknowledged(managed_user_id); |
| 220 } | 234 } |
| 235 #if defined(OS_CHROMEOS) |
| 236 const char* kAvatarKey = managed_users::kChromeOSAvatarIndex; |
| 237 #else |
| 238 const char* kAvatarKey = managed_users::kChromeAvatarIndex; |
| 239 #endif |
| 240 managed_user_shared_settings_service_->SetValue( |
| 241 pending_managed_user_id_, kAvatarKey, |
| 242 base::FundamentalValue(info.avatar_index)); |
| 221 | 243 |
| 222 browser_sync::DeviceInfo::GetClientName( | 244 browser_sync::DeviceInfo::GetClientName( |
| 223 base::Bind(&ManagedUserRegistrationUtilityImpl::FetchToken, | 245 base::Bind(&ManagedUserRegistrationUtilityImpl::FetchToken, |
| 224 weak_ptr_factory_.GetWeakPtr())); | 246 weak_ptr_factory_.GetWeakPtr())); |
| 225 } | 247 } |
| 226 | 248 |
| 227 void ManagedUserRegistrationUtilityImpl::CancelPendingRegistration() { | 249 void ManagedUserRegistrationUtilityImpl::CancelPendingRegistration() { |
| 228 AbortPendingRegistration( | 250 AbortPendingRegistration( |
| 229 false, // Don't run the callback. The error will be ignored. | 251 false, // Don't run the callback. The error will be ignored. |
| 230 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | 252 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 pending_managed_user_id_); | 328 pending_managed_user_id_); |
| 307 } | 329 } |
| 308 } | 330 } |
| 309 | 331 |
| 310 if (run_callback) | 332 if (run_callback) |
| 311 callback_.Run(error, pending_managed_user_token_); | 333 callback_.Run(error, pending_managed_user_token_); |
| 312 callback_.Reset(); | 334 callback_.Reset(); |
| 313 } | 335 } |
| 314 | 336 |
| 315 } // namespace | 337 } // namespace |
| OLD | NEW |