Chromium Code Reviews| 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); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 57 | 56 |
| 58 void ArcAuthContext::OnRefreshTokensLoaded() { | 57 void ArcAuthContext::OnRefreshTokensLoaded() { |
| 59 token_service_->RemoveObserver(this); | 58 token_service_->RemoveObserver(this); |
| 60 refresh_token_timeout_.Stop(); | 59 refresh_token_timeout_.Stop(); |
| 61 StartFetchers(); | 60 StartFetchers(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void ArcAuthContext::OnRefreshTokenTimeout() { | 63 void ArcAuthContext::OnRefreshTokenTimeout() { |
| 65 VLOG(2) << "Failed to wait for refresh token."; | 64 VLOG(2) << "Failed to wait for refresh token."; |
| 66 token_service_->RemoveObserver(this); | 65 token_service_->RemoveObserver(this); |
| 67 delegate_->OnPrepareContextFailed(); | 66 base::ResetAndReturn(&callback_).Run(nullptr); |
| 68 } | 67 } |
| 69 | 68 |
| 70 void ArcAuthContext::OnMergeSessionSuccess(const std::string& data) { | 69 void ArcAuthContext::OnMergeSessionSuccess(const std::string& data) { |
| 71 context_prepared_ = true; | 70 context_prepared_ = true; |
| 72 ResetFetchers(); | 71 ResetFetchers(); |
| 73 delegate_->OnContextReady(); | 72 base::ResetAndReturn(&callback_) |
| 73 .Run(storage_partition_->GetURLRequestContext()); | |
| 74 } | 74 } |
| 75 | 75 |
| 76 void ArcAuthContext::OnMergeSessionFailure( | 76 void ArcAuthContext::OnMergeSessionFailure( |
| 77 const GoogleServiceAuthError& error) { | 77 const GoogleServiceAuthError& error) { |
| 78 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; | 78 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; |
| 79 ResetFetchers(); | 79 ResetFetchers(); |
| 80 delegate_->OnPrepareContextFailed(); | 80 base::ResetAndReturn(&callback_).Run(nullptr); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void ArcAuthContext::OnUbertokenSuccess(const std::string& token) { | 83 void ArcAuthContext::OnUbertokenSuccess(const std::string& token) { |
| 84 ResetFetchers(); | 84 ResetFetchers(); |
| 85 merger_fetcher_.reset( | 85 merger_fetcher_.reset( |
| 86 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, | 86 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, |
| 87 storage_partition_->GetURLRequestContext())); | 87 storage_partition_->GetURLRequestContext())); |
| 88 merger_fetcher_->StartMergeSession(token, std::string()); | 88 merger_fetcher_->StartMergeSession(token, std::string()); |
| 89 base::ResetAndReturn(&callback_).Run(nullptr); | |
| 89 } | 90 } |
| 90 | 91 |
| 91 void ArcAuthContext::OnUbertokenFailure(const GoogleServiceAuthError& error) { | 92 void ArcAuthContext::OnUbertokenFailure(const GoogleServiceAuthError& error) { |
| 92 VLOG(2) << "Failed to get ubertoken " << error.ToString() << "."; | 93 VLOG(2) << "Failed to get ubertoken " << error.ToString() << "."; |
| 93 ResetFetchers(); | 94 ResetFetchers(); |
| 94 delegate_->OnPrepareContextFailed(); | |
|
Luis Héctor Chávez
2016/11/15 23:27:37
Is this intentionally missing? It _seems_ like it
hidehiko
2016/11/16 01:00:34
Good catch. unittest should capture this. A follow
| |
| 95 } | 95 } |
| 96 | 96 |
| 97 void ArcAuthContext::PrepareContext() { | 97 void ArcAuthContext::Prepare(const PrepareCallback& callback) { |
| 98 if (context_prepared_) { | 98 if (context_prepared_) { |
| 99 delegate_->OnContextReady(); | 99 callback.Run(storage_partition_->GetURLRequestContext()); |
| 100 return; | 100 return; |
| 101 } | 101 } |
| 102 | 102 |
| 103 callback_ = callback; | |
| 103 token_service_->RemoveObserver(this); | 104 token_service_->RemoveObserver(this); |
| 104 refresh_token_timeout_.Stop(); | 105 refresh_token_timeout_.Stop(); |
| 105 | 106 |
| 106 if (!token_service_->RefreshTokenIsAvailable(account_id_)) { | 107 if (!token_service_->RefreshTokenIsAvailable(account_id_)) { |
| 107 token_service_->AddObserver(this); | 108 token_service_->AddObserver(this); |
| 108 refresh_token_timeout_.Start( | 109 refresh_token_timeout_.Start(FROM_HERE, kRefreshTokenTimeout, this, |
| 109 FROM_HERE, base::TimeDelta::FromSeconds(kRefreshTokenTimeoutSeconds), | 110 &ArcAuthContext::OnRefreshTokenTimeout); |
| 110 this, &ArcAuthContext::OnRefreshTokenTimeout); | |
| 111 return; | 111 return; |
| 112 } | 112 } |
| 113 | 113 |
| 114 StartFetchers(); | 114 StartFetchers(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void ArcAuthContext::StartFetchers() { | 117 void ArcAuthContext::StartFetchers() { |
| 118 DCHECK(!refresh_token_timeout_.IsRunning()); | 118 DCHECK(!refresh_token_timeout_.IsRunning()); |
| 119 ResetFetchers(); | 119 ResetFetchers(); |
| 120 ubertoken_fetcher_.reset( | 120 ubertoken_fetcher_.reset( |
| 121 new UbertokenFetcher(token_service_, this, GaiaConstants::kChromeOSSource, | 121 new UbertokenFetcher(token_service_, this, GaiaConstants::kChromeOSSource, |
| 122 storage_partition_->GetURLRequestContext())); | 122 storage_partition_->GetURLRequestContext())); |
| 123 ubertoken_fetcher_->StartFetchingToken(account_id_); | 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 |