| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_context.h" | 5 #include "chrome/browser/chromeos/arc/arc_auth_context.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" |
| 7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 8 #include "chrome/browser/chromeos/arc/arc_auth_context_delegate.h" | |
| 9 #include "chrome/browser/chromeos/arc/arc_support_host.h" | 9 #include "chrome/browser/chromeos/arc/arc_support_host.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
| 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 14 #include "components/signin/core/browser/signin_manager_base.h" | 14 #include "components/signin/core/browser/signin_manager_base.h" |
| 15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/storage_partition.h" | 16 #include "content/public/browser/storage_partition.h" |
| 17 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
| 18 #include "google_apis/gaia/gaia_auth_fetcher.h" | 18 #include "google_apis/gaia/gaia_auth_fetcher.h" |
| 19 #include "google_apis/gaia/gaia_constants.h" | 19 #include "google_apis/gaia/gaia_constants.h" |
| 20 | 20 |
| 21 namespace arc { | 21 namespace arc { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 constexpr int kRefreshTokenTimeoutSeconds = 10; | 25 constexpr base::TimeDelta kRefreshTokenTimeout = |
| 26 base::TimeDelta::FromSeconds(10); |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 ArcAuthContext::ArcAuthContext(ArcAuthContextDelegate* delegate, | 30 ArcAuthContext::ArcAuthContext(Profile* profile) { |
| 30 Profile* profile) | |
| 31 : delegate_(delegate) { | |
| 32 // Reuse storage used in ARC OptIn platform app. | 31 // Reuse storage used in ARC OptIn platform app. |
| 33 const std::string site_url = base::StringPrintf( | 32 const std::string site_url = base::StringPrintf( |
| 34 "%s://%s/persist?%s", content::kGuestScheme, ArcSupportHost::kHostAppId, | 33 "%s://%s/persist?%s", content::kGuestScheme, ArcSupportHost::kHostAppId, |
| 35 ArcSupportHost::kStorageId); | 34 ArcSupportHost::kStorageId); |
| 36 storage_partition_ = content::BrowserContext::GetStoragePartitionForSite( | 35 storage_partition_ = content::BrowserContext::GetStoragePartitionForSite( |
| 37 profile, GURL(site_url)); | 36 profile, GURL(site_url)); |
| 38 CHECK(storage_partition_); | 37 CHECK(storage_partition_); |
| 39 | 38 |
| 40 // Get token service and account ID to fetch auth tokens. | 39 // Get token service and account ID to fetch auth tokens. |
| 41 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 40 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 42 const SigninManagerBase* const signin_manager = | 41 const SigninManagerBase* const signin_manager = |
| 43 SigninManagerFactory::GetForProfile(profile); | 42 SigninManagerFactory::GetForProfile(profile); |
| 44 CHECK(token_service_ && signin_manager); | 43 CHECK(token_service_ && signin_manager); |
| 45 account_id_ = signin_manager->GetAuthenticatedAccountId(); | 44 account_id_ = signin_manager->GetAuthenticatedAccountId(); |
| 46 } | 45 } |
| 47 | 46 |
| 48 ArcAuthContext::~ArcAuthContext() { | 47 ArcAuthContext::~ArcAuthContext() { |
| 49 token_service_->RemoveObserver(this); | 48 token_service_->RemoveObserver(this); |
| 50 } | 49 } |
| 51 | 50 |
| 51 void ArcAuthContext::Prepare(const PrepareCallback& callback) { |
| 52 if (context_prepared_) { |
| 53 callback.Run(storage_partition_->GetURLRequestContext()); |
| 54 return; |
| 55 } |
| 56 |
| 57 callback_ = callback; |
| 58 token_service_->RemoveObserver(this); |
| 59 refresh_token_timeout_.Stop(); |
| 60 |
| 61 if (!token_service_->RefreshTokenIsAvailable(account_id_)) { |
| 62 token_service_->AddObserver(this); |
| 63 refresh_token_timeout_.Start(FROM_HERE, kRefreshTokenTimeout, this, |
| 64 &ArcAuthContext::OnRefreshTokenTimeout); |
| 65 return; |
| 66 } |
| 67 |
| 68 StartFetchers(); |
| 69 } |
| 70 |
| 52 void ArcAuthContext::OnRefreshTokenAvailable(const std::string& account_id) { | 71 void ArcAuthContext::OnRefreshTokenAvailable(const std::string& account_id) { |
| 53 if (account_id != account_id_) | 72 if (account_id != account_id_) |
| 54 return; | 73 return; |
| 55 OnRefreshTokensLoaded(); | 74 OnRefreshTokensLoaded(); |
| 56 } | 75 } |
| 57 | 76 |
| 58 void ArcAuthContext::OnRefreshTokensLoaded() { | 77 void ArcAuthContext::OnRefreshTokensLoaded() { |
| 59 token_service_->RemoveObserver(this); | 78 token_service_->RemoveObserver(this); |
| 60 refresh_token_timeout_.Stop(); | 79 refresh_token_timeout_.Stop(); |
| 61 StartFetchers(); | 80 StartFetchers(); |
| 62 } | 81 } |
| 63 | 82 |
| 64 void ArcAuthContext::OnRefreshTokenTimeout() { | 83 void ArcAuthContext::OnRefreshTokenTimeout() { |
| 65 VLOG(2) << "Failed to wait for refresh token."; | 84 VLOG(2) << "Failed to wait for refresh token."; |
| 66 token_service_->RemoveObserver(this); | 85 token_service_->RemoveObserver(this); |
| 67 delegate_->OnPrepareContextFailed(); | 86 base::ResetAndReturn(&callback_).Run(nullptr); |
| 68 } | 87 } |
| 69 | 88 |
| 70 void ArcAuthContext::OnMergeSessionSuccess(const std::string& data) { | 89 void ArcAuthContext::StartFetchers() { |
| 71 context_prepared_ = true; | 90 DCHECK(!refresh_token_timeout_.IsRunning()); |
| 72 ResetFetchers(); | 91 ResetFetchers(); |
| 73 delegate_->OnContextReady(); | 92 ubertoken_fetcher_.reset( |
| 74 } | 93 new UbertokenFetcher(token_service_, this, GaiaConstants::kChromeOSSource, |
| 75 | 94 storage_partition_->GetURLRequestContext())); |
| 76 void ArcAuthContext::OnMergeSessionFailure( | 95 ubertoken_fetcher_->StartFetchingToken(account_id_); |
| 77 const GoogleServiceAuthError& error) { | |
| 78 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; | |
| 79 ResetFetchers(); | |
| 80 delegate_->OnPrepareContextFailed(); | |
| 81 } | 96 } |
| 82 | 97 |
| 83 void ArcAuthContext::OnUbertokenSuccess(const std::string& token) { | 98 void ArcAuthContext::OnUbertokenSuccess(const std::string& token) { |
| 84 ResetFetchers(); | 99 ResetFetchers(); |
| 85 merger_fetcher_.reset( | 100 merger_fetcher_.reset( |
| 86 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, | 101 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, |
| 87 storage_partition_->GetURLRequestContext())); | 102 storage_partition_->GetURLRequestContext())); |
| 88 merger_fetcher_->StartMergeSession(token, std::string()); | 103 merger_fetcher_->StartMergeSession(token, std::string()); |
| 89 } | 104 } |
| 90 | 105 |
| 91 void ArcAuthContext::OnUbertokenFailure(const GoogleServiceAuthError& error) { | 106 void ArcAuthContext::OnUbertokenFailure(const GoogleServiceAuthError& error) { |
| 92 VLOG(2) << "Failed to get ubertoken " << error.ToString() << "."; | 107 VLOG(2) << "Failed to get ubertoken " << error.ToString() << "."; |
| 93 ResetFetchers(); | 108 ResetFetchers(); |
| 94 delegate_->OnPrepareContextFailed(); | 109 base::ResetAndReturn(&callback_).Run(nullptr); |
| 95 } | 110 } |
| 96 | 111 |
| 97 void ArcAuthContext::PrepareContext() { | 112 void ArcAuthContext::OnMergeSessionSuccess(const std::string& data) { |
| 98 if (context_prepared_) { | 113 context_prepared_ = true; |
| 99 delegate_->OnContextReady(); | 114 ResetFetchers(); |
| 100 return; | 115 base::ResetAndReturn(&callback_) |
| 101 } | 116 .Run(storage_partition_->GetURLRequestContext()); |
| 102 | |
| 103 token_service_->RemoveObserver(this); | |
| 104 refresh_token_timeout_.Stop(); | |
| 105 | |
| 106 if (!token_service_->RefreshTokenIsAvailable(account_id_)) { | |
| 107 token_service_->AddObserver(this); | |
| 108 refresh_token_timeout_.Start( | |
| 109 FROM_HERE, base::TimeDelta::FromSeconds(kRefreshTokenTimeoutSeconds), | |
| 110 this, &ArcAuthContext::OnRefreshTokenTimeout); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 StartFetchers(); | |
| 115 } | 117 } |
| 116 | 118 |
| 117 void ArcAuthContext::StartFetchers() { | 119 void ArcAuthContext::OnMergeSessionFailure( |
| 118 DCHECK(!refresh_token_timeout_.IsRunning()); | 120 const GoogleServiceAuthError& error) { |
| 121 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; |
| 119 ResetFetchers(); | 122 ResetFetchers(); |
| 120 ubertoken_fetcher_.reset( | 123 base::ResetAndReturn(&callback_).Run(nullptr); |
| 121 new UbertokenFetcher(token_service_, this, GaiaConstants::kChromeOSSource, | |
| 122 storage_partition_->GetURLRequestContext())); | |
| 123 ubertoken_fetcher_->StartFetchingToken(account_id_); | |
| 124 } | 124 } |
| 125 | 125 |
| 126 void ArcAuthContext::ResetFetchers() { | 126 void ArcAuthContext::ResetFetchers() { |
| 127 merger_fetcher_.reset(); | 127 merger_fetcher_.reset(); |
| 128 ubertoken_fetcher_.reset(); | 128 ubertoken_fetcher_.reset(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 net::URLRequestContextGetter* ArcAuthContext::GetURLRequestContext() { | |
| 132 DCHECK(context_prepared_); | |
| 133 return storage_partition_->GetURLRequestContext(); | |
| 134 } | |
| 135 | |
| 136 } // namespace arc | 131 } // namespace arc |
| OLD | NEW |