OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/app_list/speech_auth_helper.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/time/clock.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 15 #include "components/signin/core/browser/signin_manager.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 |
| 18 namespace app_list { |
| 19 |
| 20 static const char* kAuthScope = |
| 21 "https://www.googleapis.com/auth/webhistory"; |
| 22 static const int kMinTokenRefreshDelaySeconds = 300; // 5 minutes |
| 23 |
| 24 |
| 25 SpeechAuthHelper::SpeechAuthHelper(Profile* profile, base::Clock* clock) |
| 26 : OAuth2TokenService::Consumer(kAuthScope), |
| 27 profile_(profile), |
| 28 clock_(clock), |
| 29 token_service_(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)), |
| 30 weak_factory_(this) { |
| 31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 32 |
| 33 // If token_service_ is NULL, we can't do anything. This might be NULL if the |
| 34 // profile is a guest user. |
| 35 if (!token_service_) |
| 36 return; |
| 37 |
| 38 SigninManagerBase* signin_manager = |
| 39 SigninManagerFactory::GetForProfile(profile); |
| 40 // Again, this might be NULL, and if it is, we can't proceed. |
| 41 if (!signin_manager) |
| 42 return; |
| 43 |
| 44 authenticated_account_id_ = signin_manager->GetAuthenticatedAccountId(); |
| 45 if (!token_service_->RefreshTokenIsAvailable(authenticated_account_id_)) { |
| 46 // Wait for the OAuth2 refresh token to be available before trying to obtain |
| 47 // a speech token. |
| 48 token_service_->AddObserver(this); |
| 49 } else { |
| 50 FetchAuthToken(); |
| 51 } |
| 52 } |
| 53 |
| 54 SpeechAuthHelper::~SpeechAuthHelper() { |
| 55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 56 if (token_service_) |
| 57 token_service_->RemoveObserver(this); |
| 58 } |
| 59 |
| 60 void SpeechAuthHelper::OnGetTokenSuccess( |
| 61 const OAuth2TokenService::Request* request, |
| 62 const std::string& access_token, |
| 63 const base::Time& expiration_time) { |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 65 auth_token_ = access_token; |
| 66 auth_token_request_.reset(); |
| 67 |
| 68 base::Time now = clock_->Now(); |
| 69 base::TimeDelta delay = expiration_time - now; |
| 70 if (delay.InSeconds() < kMinTokenRefreshDelaySeconds) |
| 71 delay = base::TimeDelta::FromSeconds(kMinTokenRefreshDelaySeconds); |
| 72 ScheduleTokenFetch(delay); |
| 73 } |
| 74 |
| 75 void SpeechAuthHelper::OnGetTokenFailure( |
| 76 const OAuth2TokenService::Request* request, |
| 77 const GoogleServiceAuthError& error) { |
| 78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 79 auth_token_ = ""; |
| 80 auth_token_request_.reset(); |
| 81 |
| 82 // Try again later. |
| 83 // TODO(amistry): Implement backoff. |
| 84 ScheduleTokenFetch( |
| 85 base::TimeDelta::FromSeconds(kMinTokenRefreshDelaySeconds)); |
| 86 } |
| 87 |
| 88 void SpeechAuthHelper::OnRefreshTokenAvailable(const std::string& account_id) { |
| 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 90 if (authenticated_account_id_ == account_id) |
| 91 FetchAuthToken(); |
| 92 } |
| 93 |
| 94 void SpeechAuthHelper::ScheduleTokenFetch(const base::TimeDelta& fetch_delay) { |
| 95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 96 content::BrowserThread::PostDelayedTask( |
| 97 content::BrowserThread::UI, |
| 98 FROM_HERE, |
| 99 base::Bind(&SpeechAuthHelper::FetchAuthToken, |
| 100 weak_factory_.GetWeakPtr()), |
| 101 fetch_delay); |
| 102 } |
| 103 |
| 104 void SpeechAuthHelper::FetchAuthToken() { |
| 105 // The process of fetching and refreshing OAuth tokens is started from the |
| 106 // consustructor, and so token_service_ and authenticated_account_id_ are |
| 107 // guaranteed to be valid at this point. |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 109 OAuth2TokenService::ScopeSet scopes; |
| 110 scopes.insert(kAuthScope); |
| 111 auth_token_request_ = token_service_->StartRequest( |
| 112 authenticated_account_id_, |
| 113 scopes, |
| 114 this); |
| 115 } |
| 116 |
| 117 std::string SpeechAuthHelper::GetToken() { |
| 118 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 119 return auth_token_; |
| 120 } |
| 121 |
| 122 std::string SpeechAuthHelper::GetScope() { |
| 123 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 124 return kAuthScope; |
| 125 } |
| 126 |
| 127 } // namespace app_list |
OLD | NEW |