Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/chromeos/arc/arc_auth_service.h" | 5 #include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/strings/string16.h" | |
| 13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 14 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "chrome/browser/browser_process.h" | |
| 17 #include "chrome/browser/browser_process_platform_part.h" | |
| 15 #include "chrome/browser/chromeos/arc/arc_auth_notification.h" | 18 #include "chrome/browser/chromeos/arc/arc_auth_notification.h" |
| 16 #include "chrome/browser/chromeos/arc/arc_optin_uma.h" | 19 #include "chrome/browser/chromeos/arc/arc_optin_uma.h" |
| 20 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | |
| 17 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 21 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 18 #include "chrome/browser/extensions/extension_util.h" | 22 #include "chrome/browser/extensions/extension_util.h" |
| 19 #include "chrome/browser/policy/profile_policy_connector.h" | 23 #include "chrome/browser/policy/profile_policy_connector.h" |
| 20 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 24 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
| 21 #include "chrome/browser/prefs/pref_service_syncable_util.h" | 25 #include "chrome/browser/prefs/pref_service_syncable_util.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 27 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 24 #include "chrome/browser/signin/signin_manager_factory.h" | 28 #include "chrome/browser/signin/signin_manager_factory.h" |
| 25 #include "chrome/browser/ui/app_list/arc/arc_app_launcher.h" | 29 #include "chrome/browser/ui/app_list/arc/arc_app_launcher.h" |
| 26 #include "chrome/browser/ui/extensions/app_launch_params.h" | 30 #include "chrome/browser/ui/extensions/app_launch_params.h" |
| 27 #include "chrome/browser/ui/extensions/application_launch.h" | 31 #include "chrome/browser/ui/extensions/application_launch.h" |
| 28 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
| 29 #include "chrome/grit/generated_resources.h" | 33 #include "chrome/grit/generated_resources.h" |
| 30 #include "chromeos/chromeos_switches.h" | 34 #include "chromeos/chromeos_switches.h" |
| 31 #include "components/arc/arc_bridge_service.h" | 35 #include "components/arc/arc_bridge_service.h" |
| 36 #include "components/policy/core/browser/browser_policy_connector.h" | |
| 37 #include "components/policy/core/common/cloud/device_management_service.h" | |
| 32 #include "components/pref_registry/pref_registry_syncable.h" | 38 #include "components/pref_registry/pref_registry_syncable.h" |
| 33 #include "components/prefs/pref_service.h" | 39 #include "components/prefs/pref_service.h" |
| 34 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 40 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 35 #include "components/signin/core/browser/signin_manager_base.h" | 41 #include "components/signin/core/browser/signin_manager_base.h" |
| 36 #include "components/syncable_prefs/pref_service_syncable.h" | 42 #include "components/syncable_prefs/pref_service_syncable.h" |
| 37 #include "components/user_manager/user.h" | 43 #include "components/user_manager/user.h" |
| 38 #include "content/public/browser/storage_partition.h" | 44 #include "content/public/browser/storage_partition.h" |
| 39 #include "content/public/common/url_constants.h" | 45 #include "content/public/common/url_constants.h" |
| 40 #include "extensions/browser/app_window/app_window_registry.h" | 46 #include "extensions/browser/app_window/app_window_registry.h" |
| 41 #include "extensions/browser/extension_prefs.h" | 47 #include "extensions/browser/extension_prefs.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 53 base::LazyInstance<base::ThreadChecker> thread_checker = | 59 base::LazyInstance<base::ThreadChecker> thread_checker = |
| 54 LAZY_INSTANCE_INITIALIZER; | 60 LAZY_INSTANCE_INITIALIZER; |
| 55 | 61 |
| 56 const char kPlayStoreAppId[] = "gpkmicpkkebkmabiaedjognfppcchdfa"; | 62 const char kPlayStoreAppId[] = "gpkmicpkkebkmabiaedjognfppcchdfa"; |
| 57 const char kArcSupportExtensionId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; | 63 const char kArcSupportExtensionId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; |
| 58 const char kArcSupportStorageId[] = "arc_support"; | 64 const char kArcSupportStorageId[] = "arc_support"; |
| 59 | 65 |
| 60 // Skip creating UI in unit tests | 66 // Skip creating UI in unit tests |
| 61 bool disable_ui_for_testing = false; | 67 bool disable_ui_for_testing = false; |
| 62 | 68 |
| 69 // Do check Android management requirement in browser tests. | |
| 70 bool enable_check_android_management_for_testing = false; | |
| 71 const char* fake_access_token = nullptr; | |
| 72 | |
| 63 const char kStateStopped[] = "STOPPED"; | 73 const char kStateStopped[] = "STOPPED"; |
| 64 const char kStateFetchingCode[] = "FETCHING_CODE"; | 74 const char kStateFetchingCode[] = "FETCHING_CODE"; |
| 65 const char kStateActive[] = "ACTIVE"; | 75 const char kStateActive[] = "ACTIVE"; |
| 76 | |
| 66 } // namespace | 77 } // namespace |
| 67 | 78 |
| 68 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) | 79 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) |
| 69 : ArcService(bridge_service), binding_(this) { | 80 : ArcService(bridge_service), |
| 81 OAuth2TokenService::Consumer("arc_auth_service"), | |
| 82 binding_(this), | |
| 83 weak_ptr_factory_(this) { | |
| 70 DCHECK(!arc_auth_service); | 84 DCHECK(!arc_auth_service); |
| 71 DCHECK(thread_checker.Get().CalledOnValidThread()); | 85 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 72 | 86 |
| 73 arc_auth_service = this; | 87 arc_auth_service = this; |
| 74 | 88 |
| 75 arc_bridge_service()->AddObserver(this); | 89 arc_bridge_service()->AddObserver(this); |
| 76 } | 90 } |
| 77 | 91 |
| 78 ArcAuthService::~ArcAuthService() { | 92 ArcAuthService::~ArcAuthService() { |
| 79 DCHECK(thread_checker.Get().CalledOnValidThread()); | 93 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 104 void ArcAuthService::DisableUIForTesting() { | 118 void ArcAuthService::DisableUIForTesting() { |
| 105 disable_ui_for_testing = true; | 119 disable_ui_for_testing = true; |
| 106 } | 120 } |
| 107 | 121 |
| 108 // static | 122 // static |
| 109 bool ArcAuthService::IsOptInVerificationDisabled() { | 123 bool ArcAuthService::IsOptInVerificationDisabled() { |
| 110 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 124 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 111 chromeos::switches::kDisableArcOptInVerification); | 125 chromeos::switches::kDisableArcOptInVerification); |
| 112 } | 126 } |
| 113 | 127 |
| 128 // static | |
| 129 void ArcAuthService::EnableCheckAndroidManagementForTesting( | |
| 130 const char* access_token) { | |
| 131 enable_check_android_management_for_testing = true; | |
| 132 fake_access_token = access_token; | |
| 133 } | |
| 134 | |
| 114 void ArcAuthService::OnAuthInstanceReady() { | 135 void ArcAuthService::OnAuthInstanceReady() { |
| 115 arc_bridge_service()->auth_instance()->Init( | 136 arc_bridge_service()->auth_instance()->Init( |
| 116 binding_.CreateInterfacePtrAndBind()); | 137 binding_.CreateInterfacePtrAndBind()); |
| 117 } | 138 } |
| 118 | 139 |
| 119 std::string ArcAuthService::GetAndResetAuthCode() { | 140 std::string ArcAuthService::GetAndResetAuthCode() { |
| 120 DCHECK(thread_checker.Get().CalledOnValidThread()); | 141 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 121 std::string auth_code; | 142 std::string auth_code; |
| 122 auth_code_.swap(auth_code); | 143 auth_code_.swap(auth_code); |
| 123 return auth_code; | 144 return auth_code; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 const user_manager::User* const primary_user = | 223 const user_manager::User* const primary_user = |
| 203 user_manager::UserManager::Get()->GetPrimaryUser(); | 224 user_manager::UserManager::Get()->GetPrimaryUser(); |
| 204 Profile* const profile = | 225 Profile* const profile = |
| 205 chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user); | 226 chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user); |
| 206 const policy::ProfilePolicyConnector* const profile_policy_connector = | 227 const policy::ProfilePolicyConnector* const profile_policy_connector = |
| 207 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile); | 228 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile); |
| 208 callback.Run(profile_policy_connector->IsManaged()); | 229 callback.Run(profile_policy_connector->IsManaged()); |
| 209 } | 230 } |
| 210 | 231 |
| 211 void ArcAuthService::SetState(State state) { | 232 void ArcAuthService::SetState(State state) { |
| 212 if (state_ == state) | 233 if (state_ == state && !enable_check_android_management_for_testing) |
|
xiyuan
2016/04/26 17:48:10
Why we need this? Could ArcAuthServiceStateObserve
Polina Bondarenko
2016/04/27 15:19:57
We need this to test ManagedAndroidAccount scenari
xiyuan
2016/04/27 16:00:57
It is strange to observe a state change which does
Polina Bondarenko
2016/05/02 12:22:06
Sorry, I've missed this comment. Yes, that's much
| |
| 213 return; | 234 return; |
| 214 | 235 |
| 215 state_ = state; | 236 state_ = state; |
| 216 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); | 237 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| 217 } | 238 } |
| 218 | 239 |
| 219 void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { | 240 void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { |
| 220 DCHECK(profile && profile != profile_); | 241 DCHECK(profile && profile != profile_); |
| 221 DCHECK(thread_checker.Get().CalledOnValidThread()); | 242 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 222 | 243 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 237 // Reuse storage used in ARC OptIn platform app. | 258 // Reuse storage used in ARC OptIn platform app. |
| 238 const std::string site_url = | 259 const std::string site_url = |
| 239 base::StringPrintf("%s://%s/persist?%s", content::kGuestScheme, | 260 base::StringPrintf("%s://%s/persist?%s", content::kGuestScheme, |
| 240 kArcSupportExtensionId, kArcSupportStorageId); | 261 kArcSupportExtensionId, kArcSupportStorageId); |
| 241 storage_partition_ = content::BrowserContext::GetStoragePartitionForSite( | 262 storage_partition_ = content::BrowserContext::GetStoragePartitionForSite( |
| 242 profile_, GURL(site_url)); | 263 profile_, GURL(site_url)); |
| 243 CHECK(storage_partition_); | 264 CHECK(storage_partition_); |
| 244 | 265 |
| 245 // In case UI is disabled we assume that ARC is opted-in. | 266 // In case UI is disabled we assume that ARC is opted-in. |
| 246 if (!IsOptInVerificationDisabled()) { | 267 if (!IsOptInVerificationDisabled()) { |
| 268 if (!disable_ui_for_testing || enable_check_android_management_for_testing) | |
| 269 StartAndroidManagementClient(); | |
| 270 | |
| 247 pref_change_registrar_.Init(profile_->GetPrefs()); | 271 pref_change_registrar_.Init(profile_->GetPrefs()); |
| 248 pref_change_registrar_.Add( | 272 pref_change_registrar_.Add( |
| 249 prefs::kArcEnabled, | 273 prefs::kArcEnabled, |
| 250 base::Bind(&ArcAuthService::OnOptInPreferenceChanged, | 274 base::Bind(&ArcAuthService::OnOptInPreferenceChanged, |
| 251 base::Unretained(this))); | 275 base::Unretained(this))); |
| 252 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { | 276 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { |
| 253 OnOptInPreferenceChanged(); | 277 OnOptInPreferenceChanged(); |
| 254 } else { | 278 } else { |
| 255 UpdateEnabledStateUMA(false); | 279 UpdateEnabledStateUMA(false); |
| 256 PrefServiceSyncableFromProfile(profile_)->AddObserver(this); | 280 PrefServiceSyncableFromProfile(profile_)->AddObserver(this); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 280 } | 304 } |
| 281 | 305 |
| 282 void ArcAuthService::Shutdown() { | 306 void ArcAuthService::Shutdown() { |
| 283 ShutdownBridgeAndCloseUI(); | 307 ShutdownBridgeAndCloseUI(); |
| 284 if (profile_) { | 308 if (profile_) { |
| 285 syncable_prefs::PrefServiceSyncable* pref_service_syncable = | 309 syncable_prefs::PrefServiceSyncable* pref_service_syncable = |
| 286 PrefServiceSyncableFromProfile(profile_); | 310 PrefServiceSyncableFromProfile(profile_); |
| 287 pref_service_syncable->RemoveObserver(this); | 311 pref_service_syncable->RemoveObserver(this); |
| 288 pref_service_syncable->RemoveSyncedPrefObserver(prefs::kArcEnabled, this); | 312 pref_service_syncable->RemoveSyncedPrefObserver(prefs::kArcEnabled, this); |
| 289 } | 313 } |
| 314 android_management_client_.reset(); | |
| 290 pref_change_registrar_.RemoveAll(); | 315 pref_change_registrar_.RemoveAll(); |
| 291 profile_ = nullptr; | 316 profile_ = nullptr; |
| 292 } | 317 } |
| 293 | 318 |
| 294 void ArcAuthService::ShowUI(UIPage page, const base::string16& status) { | 319 void ArcAuthService::ShowUI(UIPage page, const base::string16& status) { |
| 295 if (disable_ui_for_testing || IsOptInVerificationDisabled()) | 320 if (disable_ui_for_testing || IsOptInVerificationDisabled()) |
| 296 return; | 321 return; |
| 297 | 322 |
| 298 SetUIPage(page, status); | 323 SetUIPage(page, status); |
| 299 const extensions::AppWindowRegistry* const app_window_registry = | 324 const extensions::AppWindowRegistry* const app_window_registry = |
| 300 extensions::AppWindowRegistry::Get(profile_); | 325 extensions::AppWindowRegistry::Get(profile_); |
| 301 DCHECK(app_window_registry); | 326 DCHECK(app_window_registry); |
| 302 if (app_window_registry->GetCurrentAppWindowForApp(kArcSupportExtensionId)) | 327 if (app_window_registry->GetCurrentAppWindowForApp(kArcSupportExtensionId)) |
| 303 return; | 328 return; |
| 304 | 329 |
| 305 const extensions::Extension* extension = | 330 const extensions::Extension* extension = |
| 306 extensions::ExtensionRegistry::Get(profile_)->GetInstalledExtension( | 331 extensions::ExtensionRegistry::Get(profile_)->GetInstalledExtension( |
| 307 kArcSupportExtensionId); | 332 kArcSupportExtensionId); |
| 308 CHECK(extension && | 333 CHECK(extension && |
| 309 extensions::util::IsAppLaunchable(kArcSupportExtensionId, profile_)); | 334 extensions::util::IsAppLaunchable(kArcSupportExtensionId, profile_)); |
| 310 | 335 |
| 311 OpenApplication(CreateAppLaunchParamsUserContainer( | 336 OpenApplication(CreateAppLaunchParamsUserContainer( |
| 312 profile_, extension, NEW_WINDOW, extensions::SOURCE_CHROME_INTERNAL)); | 337 profile_, extension, NEW_WINDOW, extensions::SOURCE_CHROME_INTERNAL)); |
| 313 } | 338 } |
| 314 | 339 |
| 315 void ArcAuthService::OnMergeSessionSuccess(const std::string& data) { | 340 void ArcAuthService::OnMergeSessionSuccess(const std::string& data) { |
| 316 DCHECK(thread_checker.Get().CalledOnValidThread()); | 341 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 317 | 342 |
| 318 DCHECK(!initial_opt_in_); | 343 DCHECK(!initial_opt_in_); |
| 319 context_prepared_ = true; | 344 CheckAndroidManagement(); |
| 320 ShowUI(UIPage::LSO_PROGRESS, base::string16()); | |
| 321 } | 345 } |
| 322 | 346 |
| 323 void ArcAuthService::OnMergeSessionFailure( | 347 void ArcAuthService::OnMergeSessionFailure( |
| 324 const GoogleServiceAuthError& error) { | 348 const GoogleServiceAuthError& error) { |
| 325 DCHECK(thread_checker.Get().CalledOnValidThread()); | 349 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 326 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; | 350 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; |
| 327 OnPrepareContextFailed(); | 351 OnPrepareContextFailed(); |
| 328 } | 352 } |
| 329 | 353 |
| 330 void ArcAuthService::OnUbertokenSuccess(const std::string& token) { | 354 void ArcAuthService::OnUbertokenSuccess(const std::string& token) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 360 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { | 384 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { |
| 361 if (state_ != State::ACTIVE) { | 385 if (state_ != State::ACTIVE) { |
| 362 CloseUI(); | 386 CloseUI(); |
| 363 auth_code_.clear(); | 387 auth_code_.clear(); |
| 364 | 388 |
| 365 if (!profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) { | 389 if (!profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) { |
| 366 // Need pre-fetch auth code and show OptIn UI if needed. | 390 // Need pre-fetch auth code and show OptIn UI if needed. |
| 367 initial_opt_in_ = true; | 391 initial_opt_in_ = true; |
| 368 StartUI(); | 392 StartUI(); |
| 369 } else { | 393 } else { |
| 370 // Ready to start Arc. | 394 // Ready to start Arc, but check android management first. |
| 371 StartArc(); | 395 if (!disable_ui_for_testing || |
| 396 enable_check_android_management_for_testing) { | |
| 397 CheckAndroidManagement(); | |
| 398 } else { | |
| 399 StartArc(); | |
| 400 } | |
| 372 } | 401 } |
| 373 | 402 |
| 374 UpdateEnabledStateUMA(true); | 403 UpdateEnabledStateUMA(true); |
| 375 } | 404 } |
| 376 } else { | 405 } else { |
| 377 if (state_ != State::STOPPED) | 406 if (state_ != State::STOPPED) |
| 378 UpdateEnabledStateUMA(false); | 407 UpdateEnabledStateUMA(false); |
| 379 ShutdownBridgeAndCloseUI(); | 408 ShutdownBridgeAndCloseUI(); |
| 380 } | 409 } |
| 381 } | 410 } |
| 382 | 411 |
| 383 void ArcAuthService::ShutdownBridge() { | 412 void ArcAuthService::ShutdownBridge() { |
| 384 playstore_launcher_.reset(); | 413 playstore_launcher_.reset(); |
| 385 auth_callback_.reset(); | 414 auth_callback_.reset(); |
| 386 ubertoken_fethcher_.reset(); | 415 ubertoken_fethcher_.reset(); |
| 387 merger_fetcher_.reset(); | 416 merger_fetcher_.reset(); |
| 417 token_service_ = nullptr; | |
| 418 account_id_ = ""; | |
| 388 arc_bridge_service()->Shutdown(); | 419 arc_bridge_service()->Shutdown(); |
| 389 SetState(State::STOPPED); | 420 SetState(State::STOPPED); |
| 390 } | 421 } |
| 391 | 422 |
| 392 void ArcAuthService::ShutdownBridgeAndCloseUI() { | 423 void ArcAuthService::ShutdownBridgeAndCloseUI() { |
| 393 ShutdownBridge(); | 424 ShutdownBridge(); |
| 394 CloseUI(); | 425 CloseUI(); |
| 395 } | 426 } |
| 396 | 427 |
| 397 void ArcAuthService::ShutdownBridgeAndShowUI(UIPage page, | 428 void ArcAuthService::ShutdownBridgeAndShowUI(UIPage page, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 460 if (ui_page_ == UIPage::ERROR) | 491 if (ui_page_ == UIPage::ERROR) |
| 461 UpdateOptInActionUMA(OptInActionType::RETRY); | 492 UpdateOptInActionUMA(OptInActionType::RETRY); |
| 462 | 493 |
| 463 initial_opt_in_ = false; | 494 initial_opt_in_ = false; |
| 464 StartUI(); | 495 StartUI(); |
| 465 } | 496 } |
| 466 | 497 |
| 467 void ArcAuthService::CancelAuthCode() { | 498 void ArcAuthService::CancelAuthCode() { |
| 468 DCHECK(thread_checker.Get().CalledOnValidThread()); | 499 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 469 | 500 |
| 470 if (state_ != State::FETCHING_CODE) | 501 if (state_ != State::FETCHING_CODE && ui_page_ != UIPage::ERROR) |
| 471 return; | 502 return; |
| 472 | 503 |
| 473 // Update UMA with user cancel only if error is not currently shown. | 504 // Update UMA with user cancel only if error is not currently shown. |
| 474 if (ui_page_ != UIPage::ERROR && ui_page_ != UIPage::NO_PAGE) | 505 if (ui_page_ != UIPage::ERROR && ui_page_ != UIPage::NO_PAGE) |
| 475 UpdateOptInCancelUMA(OptInCancelReason::USER_CANCEL); | 506 UpdateOptInCancelUMA(OptInCancelReason::USER_CANCEL); |
| 476 | 507 |
| 477 DisableArc(); | 508 DisableArc(); |
| 478 } | 509 } |
| 479 | 510 |
| 480 void ArcAuthService::EnableArc() { | 511 void ArcAuthService::EnableArc() { |
| 481 DCHECK(thread_checker.Get().CalledOnValidThread()); | 512 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 482 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); | 513 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); |
| 483 } | 514 } |
| 484 | 515 |
| 485 void ArcAuthService::DisableArc() { | 516 void ArcAuthService::DisableArc() { |
| 486 DCHECK(thread_checker.Get().CalledOnValidThread()); | 517 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 487 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, false); | 518 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, false); |
| 488 } | 519 } |
| 489 | 520 |
| 490 void ArcAuthService::PrepareContext() { | 521 void ArcAuthService::PrepareContext() { |
| 491 DCHECK(thread_checker.Get().CalledOnValidThread()); | 522 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 492 | 523 |
| 493 // Get auth token to continue. | 524 // Get token service and account ID to fetch auth tokens. |
| 494 ProfileOAuth2TokenService* token_service = | 525 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| 495 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 496 SigninManagerBase* signin_manager = | 526 SigninManagerBase* signin_manager = |
| 497 SigninManagerFactory::GetForProfile(profile_); | 527 SigninManagerFactory::GetForProfile(profile_); |
| 498 CHECK(token_service && signin_manager); | 528 CHECK(token_service_ && signin_manager); |
| 499 const std::string& account_id = signin_manager->GetAuthenticatedAccountId(); | 529 account_id_ = signin_manager->GetAuthenticatedAccountId(); |
| 530 | |
| 500 ubertoken_fethcher_.reset( | 531 ubertoken_fethcher_.reset( |
| 501 new UbertokenFetcher(token_service, this, GaiaConstants::kChromeOSSource, | 532 new UbertokenFetcher(token_service_, this, GaiaConstants::kChromeOSSource, |
| 502 storage_partition_->GetURLRequestContext())); | 533 storage_partition_->GetURLRequestContext())); |
| 503 ubertoken_fethcher_->StartFetchingToken(account_id); | 534 ubertoken_fethcher_->StartFetchingToken(account_id_); |
| 504 } | 535 } |
| 505 | 536 |
| 506 void ArcAuthService::StartUI() { | 537 void ArcAuthService::StartUI() { |
| 507 DCHECK(thread_checker.Get().CalledOnValidThread()); | 538 DCHECK(thread_checker.Get().CalledOnValidThread()); |
| 508 | 539 |
| 509 SetState(State::FETCHING_CODE); | 540 SetState(State::FETCHING_CODE); |
| 510 | 541 |
| 511 if (initial_opt_in_) { | 542 if (initial_opt_in_) { |
| 512 initial_opt_in_ = false; | 543 initial_opt_in_ = false; |
| 513 ShowUI(UIPage::START, base::string16()); | 544 ShowUI(UIPage::START, base::string16()); |
| 514 } else if (context_prepared_) { | 545 } else if (context_prepared_) { |
| 515 ShowUI(UIPage::LSO_PROGRESS, base::string16()); | 546 ShowUI(UIPage::LSO_PROGRESS, base::string16()); |
| 516 } else { | 547 } else { |
| 517 PrepareContext(); | 548 PrepareContext(); |
| 518 } | 549 } |
| 519 } | 550 } |
| 520 | 551 |
| 521 void ArcAuthService::OnPrepareContextFailed() { | 552 void ArcAuthService::OnPrepareContextFailed() { |
| 522 DCHECK_EQ(state_, State::FETCHING_CODE); | 553 DCHECK_EQ(state_, State::FETCHING_CODE); |
| 523 | 554 |
| 524 ShutdownBridgeAndShowUI( | 555 ShutdownBridgeAndShowUI( |
| 525 UIPage::ERROR, | 556 UIPage::ERROR, |
| 526 l10n_util::GetStringUTF16(IDS_ARC_SERVER_COMMUNICATION_ERROR)); | 557 l10n_util::GetStringUTF16(IDS_ARC_SERVER_COMMUNICATION_ERROR)); |
| 527 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR); | 558 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR); |
| 528 } | 559 } |
| 529 | 560 |
| 561 void ArcAuthService::OnGetTokenSuccess( | |
| 562 const OAuth2TokenService::Request* request, | |
| 563 const std::string& access_token, | |
| 564 const base::Time& expiration_time) { | |
| 565 DCHECK_EQ(token_request_.get(), request); | |
| 566 android_management_client_->CheckAndroidManagement( | |
| 567 access_token, base::Bind(&ArcAuthService::OnAndroidManagementChecked, | |
| 568 weak_ptr_factory_.GetWeakPtr())); | |
| 569 } | |
| 570 | |
| 571 void ArcAuthService::OnGetTokenFailure( | |
| 572 const OAuth2TokenService::Request* request, | |
| 573 const GoogleServiceAuthError& error) { | |
| 574 DCHECK_EQ(token_request_.get(), request); | |
| 575 OnAndroidManagementChecked( | |
| 576 policy::AndroidManagementClient::Result::RESULT_ERROR); | |
| 577 } | |
| 578 | |
| 579 void ArcAuthService::StartAndroidManagementClient() { | |
| 580 policy::BrowserPolicyConnectorChromeOS* const connector = | |
| 581 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | |
| 582 policy::DeviceManagementService* const service = | |
| 583 connector->device_management_service(); | |
| 584 service->ScheduleInitialization(0); | |
| 585 android_management_client_.reset(new policy::AndroidManagementClient( | |
| 586 service, g_browser_process->system_request_context())); | |
| 587 } | |
| 588 | |
| 589 void ArcAuthService::CheckAndroidManagement() { | |
| 590 // Do not send requests for Chrome OS managed users. | |
| 591 if (policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile_) | |
| 592 ->IsManaged()) { | |
| 593 OnAndroidManagementChecked( | |
| 594 policy::AndroidManagementClient::Result::RESULT_UNMANAGED); | |
| 595 return; | |
| 596 } | |
| 597 | |
| 598 // Do not send requests for well-known consumer domains. | |
| 599 if (policy::BrowserPolicyConnector::IsNonEnterpriseUser( | |
| 600 profile_->GetProfileUserName())) { | |
| 601 OnAndroidManagementChecked( | |
| 602 policy::AndroidManagementClient::Result::RESULT_UNMANAGED); | |
| 603 return; | |
| 604 } | |
| 605 | |
| 606 // Do not request access token for testing, use |fake_access_token|. | |
| 607 if (enable_check_android_management_for_testing) { | |
| 608 android_management_client_->CheckAndroidManagement( | |
| 609 fake_access_token, | |
| 610 base::Bind(&ArcAuthService::OnAndroidManagementChecked, | |
| 611 weak_ptr_factory_.GetWeakPtr())); | |
| 612 return; | |
| 613 } | |
| 614 | |
| 615 DCHECK(!token_request_); | |
| 616 // The user must be signed in already. | |
| 617 DCHECK(token_service_->RefreshTokenIsAvailable(account_id_)); | |
| 618 | |
| 619 OAuth2TokenService::ScopeSet scopes; | |
| 620 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | |
| 621 scopes.insert(GaiaConstants::kOAuthWrapBridgeUserInfoScope); | |
| 622 token_request_ = token_service_->StartRequest(account_id_, scopes, this); | |
|
xiyuan
2016/04/26 17:48:09
Think we should move access token fetching etc int
Polina Bondarenko
2016/04/27 15:19:57
Done.
| |
| 623 } | |
| 624 | |
| 625 void ArcAuthService::OnAndroidManagementChecked( | |
| 626 policy::AndroidManagementClient::Result result) { | |
| 627 switch (result) { | |
| 628 case policy::AndroidManagementClient::Result::RESULT_UNMANAGED: | |
| 629 context_prepared_ = true; | |
| 630 if (!profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) | |
| 631 ShowUI(UIPage::LSO_PROGRESS, base::string16()); | |
| 632 else | |
| 633 StartArc(); | |
| 634 break; | |
| 635 case policy::AndroidManagementClient::Result::RESULT_MANAGED: | |
| 636 ShutdownBridgeAndShowUI( | |
| 637 UIPage::ERROR, | |
| 638 l10n_util::GetStringUTF16(IDS_ARC_ANDROID_MANAGEMENT_REQUIRED_ERROR)); | |
| 639 UpdateOptInCancelUMA(OptInCancelReason::ANDROID_MANAGEMENT_REQUIRED); | |
| 640 break; | |
| 641 case policy::AndroidManagementClient::Result::RESULT_ERROR: | |
| 642 ShutdownBridgeAndShowUI( | |
| 643 UIPage::ERROR, | |
| 644 l10n_util::GetStringUTF16(IDS_ARC_SERVER_COMMUNICATION_ERROR)); | |
| 645 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR); | |
| 646 break; | |
| 647 default: | |
| 648 NOTREACHED(); | |
| 649 } | |
| 650 } | |
| 651 | |
| 530 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { | 652 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { |
| 531 switch (state) { | 653 switch (state) { |
| 532 case ArcAuthService::State::STOPPED: | 654 case ArcAuthService::State::STOPPED: |
| 533 return os << kStateStopped; | 655 return os << kStateStopped; |
| 534 case ArcAuthService::State::FETCHING_CODE: | 656 case ArcAuthService::State::FETCHING_CODE: |
| 535 return os << kStateFetchingCode; | 657 return os << kStateFetchingCode; |
| 536 case ArcAuthService::State::ACTIVE: | 658 case ArcAuthService::State::ACTIVE: |
| 537 return os << kStateActive; | 659 return os << kStateActive; |
| 538 default: | 660 default: |
| 539 NOTREACHED(); | 661 NOTREACHED(); |
| 540 return os; | 662 return os; |
| 541 } | 663 } |
| 542 } | 664 } |
| 543 | 665 |
| 544 } // namespace arc | 666 } // namespace arc |
| OLD | NEW |