Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_auth_service.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_auth_service.cc b/chrome/browser/chromeos/arc/arc_auth_service.cc |
| index 672eb8a6006065bdecdc53a3c4b7fca868fe66e1..73ac8c7c5ec1b9096651692bb2341a964ec444fd 100644 |
| --- a/chrome/browser/chromeos/arc/arc_auth_service.cc |
| +++ b/chrome/browser/chromeos/arc/arc_auth_service.cc |
| @@ -52,6 +52,8 @@ namespace arc { |
| namespace { |
| +constexpr size_t kMinVersionForOnAccountInformationReady = 5; |
| + |
| // Weak pointer. This class is owned by ArcServiceManager. |
| ArcAuthService* g_arc_auth_service = nullptr; |
| @@ -113,6 +115,65 @@ ProvisioningResult ConvertArcSignInFailureReasonToProvisioningResult( |
| } // namespace |
| +class ArcAuthService::AccountInformationNotifier { |
| + public: |
| + explicit AccountInformationNotifier( |
| + const GetAuthCodeDeprecatedCallback& auth_callback) |
| + : callback_type_(CallbackType::AUTH_CODE), |
| + auth_callback_(auth_callback) {} |
| + |
| + explicit AccountInformationNotifier( |
| + const GetAuthCodeAndAccountTypeDeprecatedCallback& auth_account_callback) |
| + : callback_type_(CallbackType::AUTH_CODE_AND_ACCOUNT), |
| + auth_account_callback_(auth_account_callback) {} |
| + |
| + explicit AccountInformationNotifier( |
| + const AccountInformationCallback& account_information_callback) |
| + : callback_type_(CallbackType::ACCOUNT_INFORMATION), |
| + account_information_callback_(account_information_callback) {} |
| + |
| + void Notify(bool is_enforced, |
| + const std::string& auth_code, |
| + mojom::ChromeAccountType account_type, |
| + bool is_managed) { |
| + switch (callback_type_) { |
| + case CallbackType::AUTH_CODE: |
| + DCHECK(!auth_callback_.is_null()); |
| + auth_callback_.Run(auth_code, is_enforced); |
| + break; |
| + case CallbackType::AUTH_CODE_AND_ACCOUNT: |
| + DCHECK(!auth_account_callback_.is_null()); |
| + auth_account_callback_.Run(auth_code, is_enforced, account_type); |
| + break; |
| + case CallbackType::ACCOUNT_INFORMATION: |
| + DCHECK(!account_information_callback_.is_null()); |
| + mojom::AccountInformationPtr account_information = |
| + mojom::AccountInformation::New(); |
| + if (!is_enforced) { |
| + account_information->auth_code = nullptr; |
| + } else { |
| + account_information->auth_code = auth_code; |
| + } |
| + account_information->account_type = account_type; |
| + account_information->is_managed = is_managed; |
| + account_information_callback_.Run(std::move(account_information)); |
| + break; |
| + } |
| + } |
| + |
| + private: |
| + enum class CallbackType { |
| + AUTH_CODE, |
| + AUTH_CODE_AND_ACCOUNT, |
| + ACCOUNT_INFORMATION |
| + }; |
| + |
| + const CallbackType callback_type_; |
| + const GetAuthCodeDeprecatedCallback auth_callback_; |
| + const GetAuthCodeAndAccountTypeDeprecatedCallback auth_account_callback_; |
| + const AccountInformationCallback account_information_callback_; |
| +}; |
| + |
| ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) |
| : ArcService(bridge_service), binding_(this), weak_ptr_factory_(this) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| @@ -278,49 +339,27 @@ std::string ArcAuthService::GetAndResetAuthCode() { |
| return auth_code; |
| } |
| -void ArcAuthService::GetAuthCodeDeprecated( |
| - const GetAuthCodeDeprecatedCallback& callback) { |
| +void ArcAuthService::GetAuthCodeDeprecated0( |
| + const GetAuthCodeDeprecated0Callback& callback) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| DCHECK(!IsOptInVerificationDisabled()); |
| callback.Run(GetAndResetAuthCode()); |
| } |
| -void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { |
| - DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| - // GetAuthCodeAndAccountType operation must not be in progress. |
| - DCHECK(auth_account_callback_.is_null()); |
| - |
| - const std::string auth_code = GetAndResetAuthCode(); |
| - const bool verification_disabled = IsOptInVerificationDisabled(); |
| - if (!auth_code.empty() || verification_disabled) { |
| - callback.Run(auth_code, !verification_disabled); |
| - return; |
| - } |
| - |
| - auth_callback_ = callback; |
| - PrepareContextForAuthCodeRequest(); |
| +void ArcAuthService::GetAuthCodeDeprecated( |
| + const GetAuthCodeDeprecatedCallback& callback) { |
| + RequestAccountInformationInternal( |
| + base::MakeUnique<ArcAuthService::AccountInformationNotifier>(callback)); |
| } |
| -void ArcAuthService::GetAuthCodeAndAccountType( |
| - const GetAuthCodeAndAccountTypeCallback& callback) { |
| - DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| - // GetAuthCode operation must not be in progress. |
| - DCHECK(auth_callback_.is_null()); |
| - |
| - const std::string auth_code = GetAndResetAuthCode(); |
| - const bool verification_disabled = IsOptInVerificationDisabled(); |
| - if (!auth_code.empty() || verification_disabled) { |
| - callback.Run(auth_code, !verification_disabled, |
| - mojom::ChromeAccountType::USER_ACCOUNT); |
| - return; |
| - } |
| - |
| - auth_account_callback_ = callback; |
| - PrepareContextForAuthCodeRequest(); |
| +void ArcAuthService::GetAuthCodeAndAccountTypeDeprecated( |
| + const GetAuthCodeAndAccountTypeDeprecatedCallback& callback) { |
| + RequestAccountInformationInternal( |
| + base::MakeUnique<ArcAuthService::AccountInformationNotifier>(callback)); |
| } |
| bool ArcAuthService::IsAuthCodeRequest() const { |
| - return !auth_callback_.is_null() || !auth_account_callback_.is_null(); |
| + return account_information_notifier_ != nullptr; |
| } |
| void ArcAuthService::PrepareContextForAuthCodeRequest() { |
| @@ -339,18 +378,20 @@ void ArcAuthService::PrepareContextForAuthCodeRequest() { |
| void ArcAuthService::OnSignInComplete() { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| DCHECK_EQ(state_, State::ACTIVE); |
| - DCHECK(!sign_in_time_.is_null()); |
| - arc_sign_in_timer_.Stop(); |
| + CloseUI(); |
| - if (!IsOptInVerificationDisabled() && |
| - !profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) { |
| + if (profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) |
| + return; |
| + |
| + if (!IsOptInVerificationDisabled()) { |
| playstore_launcher_.reset( |
| new ArcAppLauncher(profile_, kPlayStoreAppId, true)); |
| } |
| profile_->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); |
|
hidehiko
2016/11/03 00:27:21
Let's move this to L386.
Luis Héctor Chávez
2016/11/03 20:08:09
Done.
|
| - CloseUI(); |
| + DCHECK(!sign_in_time_.is_null()); |
| + arc_sign_in_timer_.Stop(); |
| UpdateProvisioningTiming(base::Time::Now() - sign_in_time_, true, |
| policy_util::IsAccountManaged(profile_)); |
| UpdateProvisioningResultUMA(ProvisioningResult::SUCCESS, |
| @@ -365,17 +406,55 @@ void ArcAuthService::OnSignInFailed(mojom::ArcSignInFailureReason reason) { |
| ConvertArcSignInFailureReasonToProvisioningResult(reason)); |
| } |
| +void ArcAuthService::RequestAccountInformation() { |
| + RequestAccountInformationInternal( |
| + base::MakeUnique<ArcAuthService::AccountInformationNotifier>( |
| + base::Bind(&ArcAuthService::OnAccountInformationReady, |
| + weak_ptr_factory_.GetWeakPtr()))); |
| +} |
| + |
| +void ArcAuthService::OnAccountInformationReady( |
| + mojom::AccountInformationPtr account_information) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + auto* instance = arc_bridge_service()->auth()->GetInstanceForMethod( |
| + "OnAccountInformationReady", kMinVersionForOnAccountInformationReady); |
| + DCHECK(instance); |
| + instance->OnAccountInformationReady(std::move(account_information)); |
| +} |
| + |
| +void ArcAuthService::RequestAccountInformationInternal( |
| + std::unique_ptr<ArcAuthService::AccountInformationNotifier> |
| + account_information_notifier) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + // No other auth code-related operation may be in progress. |
| + DCHECK(!account_information_notifier_); |
| + |
| + const std::string auth_code = GetAndResetAuthCode(); |
| + const bool is_enforced = !IsOptInVerificationDisabled(); |
| + if (!auth_code.empty() || !is_enforced) { |
| + account_information_notifier->Notify( |
| + is_enforced, auth_code, mojom::ChromeAccountType::USER_ACCOUNT, |
| + policy_util::IsAccountManaged(profile_)); |
| + return; |
| + } |
| + |
| + account_information_notifier_ = std::move(account_information_notifier); |
| + PrepareContextForAuthCodeRequest(); |
| +} |
| + |
| void ArcAuthService::OnSignInFailedInternal(ProvisioningResult result) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| DCHECK_EQ(state_, State::ACTIVE); |
| - DCHECK(!sign_in_time_.is_null()); |
| - arc_sign_in_timer_.Stop(); |
| + if (!sign_in_time_.is_null()) { |
| + arc_sign_in_timer_.Stop(); |
| - UpdateProvisioningTiming(base::Time::Now() - sign_in_time_, false, |
| - policy_util::IsAccountManaged(profile_)); |
| - UpdateOptInCancelUMA(OptInCancelReason::CLOUD_PROVISION_FLOW_FAIL); |
| - UpdateProvisioningResultUMA(result, policy_util::IsAccountManaged(profile_)); |
| + UpdateProvisioningTiming(base::Time::Now() - sign_in_time_, false, |
| + policy_util::IsAccountManaged(profile_)); |
| + UpdateOptInCancelUMA(OptInCancelReason::CLOUD_PROVISION_FLOW_FAIL); |
| + UpdateProvisioningResultUMA(result, |
| + policy_util::IsAccountManaged(profile_)); |
| + } |
| int error_message_id; |
| switch (result) { |
| @@ -431,8 +510,8 @@ void ArcAuthService::OnSignInFailedInternal(ProvisioningResult result) { |
| l10n_util::GetStringUTF16(error_message_id)); |
| } |
| -void ArcAuthService::GetIsAccountManaged( |
| - const GetIsAccountManagedCallback& callback) { |
| +void ArcAuthService::GetIsAccountManagedDeprecated( |
| + const GetIsAccountManagedDeprecatedCallback& callback) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| callback.Run(policy_util::IsAccountManaged(profile_)); |
| @@ -652,8 +731,7 @@ void ArcAuthService::OnOptInPreferenceChanged() { |
| void ArcAuthService::ShutdownBridge() { |
| arc_sign_in_timer_.Stop(); |
| playstore_launcher_.reset(); |
| - auth_callback_.Reset(); |
| - auth_account_callback_.Reset(); |
| + account_information_notifier_.reset(); |
| android_management_checker_.reset(); |
| auth_code_fetcher_.reset(); |
| arc_bridge_service()->RequestStop(); |
| @@ -722,16 +800,12 @@ void ArcAuthService::SetAuthCodeAndStartArc(const std::string& auth_code) { |
| if (IsAuthCodeRequest()) { |
| DCHECK_EQ(state_, State::FETCHING_CODE); |
| SetState(State::ACTIVE); |
| - if (!auth_callback_.is_null()) { |
| - auth_callback_.Run(auth_code, !IsOptInVerificationDisabled()); |
| - auth_callback_.Reset(); |
| - return; |
| - } else { |
| - auth_account_callback_.Run(auth_code, !IsOptInVerificationDisabled(), |
| - mojom::ChromeAccountType::USER_ACCOUNT); |
| - auth_account_callback_.Reset(); |
| - return; |
| - } |
| + account_information_notifier_->Notify( |
| + !IsOptInVerificationDisabled(), auth_code, |
| + mojom::ChromeAccountType::USER_ACCOUNT, |
| + policy_util::IsAccountManaged(profile_)); |
| + account_information_notifier_.reset(); |
| + return; |
| } |
| if (state_ != State::FETCHING_CODE) { |