Chromium Code Reviews| 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 "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 14 #include "components/signin/core/browser/signin_manager.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 | |
| 17 namespace app_list { | |
| 18 | |
| 19 static const char* kAuthScope = | |
| 20 "https://www.googleapis.com/auth/webhistory"; | |
| 21 static const int kMinTokenRefreshDelaySeconds = 300; // 5 minutes | |
| 22 | |
| 23 | |
| 24 SpeechAuthHelper::SpeechAuthHelper(Profile* profile) | |
| 25 : OAuth2TokenService::Consumer(kAuthScope), | |
| 26 profile_(profile), | |
| 27 weak_factory_(this) { | |
| 28 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 29 SigninManagerBase* signin_manager = | |
| 30 SigninManagerFactory::GetForProfile(profile_); | |
| 31 std::string account_id = signin_manager->GetAuthenticatedAccountId(); | |
| 32 ProfileOAuth2TokenService* token_service = | |
| 33 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
|
calamity
2014/12/09 02:21:16
Hmm. Can we factor token_service into a member of
Anand Mistry (off Chromium)
2014/12/10 02:13:25
Done.
| |
| 34 if (token_service && !token_service->RefreshTokenIsAvailable(account_id)) { | |
|
calamity
2014/12/09 02:21:16
Split this into
if (token_service)
return;
if
Anand Mistry (off Chromium)
2014/12/10 02:13:25
Done.
| |
| 35 // Wait for the OAuth2 refresh token to be available before trying to obtain | |
| 36 // a speech token. | |
| 37 token_service->AddObserver(this); | |
|
calamity
2014/12/09 02:21:16
Does this mean this is only added as an observer s
Anand Mistry (off Chromium)
2014/12/10 02:13:25
Yes. It only needs to be when the oauth2 refresh t
| |
| 38 } else { | |
| 39 FetchAuthToken(); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 SpeechAuthHelper::~SpeechAuthHelper() { | |
| 44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 45 ProfileOAuth2TokenService* token_service = | |
| 46 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 47 if (token_service) | |
| 48 token_service->RemoveObserver(this); | |
|
calamity
2014/12/09 02:21:16
It doesn't seem to match up with the RemoveObserve
Anand Mistry (off Chromium)
2014/12/10 02:13:25
What policy?
| |
| 49 } | |
| 50 | |
| 51 void SpeechAuthHelper::OnGetTokenSuccess( | |
| 52 const OAuth2TokenService::Request* request, | |
| 53 const std::string& access_token, | |
| 54 const base::Time& expiration_time) { | |
| 55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 56 auth_token_ = access_token; | |
| 57 auth_token_request_.reset(); | |
| 58 | |
| 59 base::Time now = base::Time::Now(); | |
|
calamity
2014/12/09 02:21:16
It's not great to use a base::Time::Now() in code.
calamity
2014/12/09 07:11:20
Another option is to take a base::Clock() in your
Anand Mistry (off Chromium)
2014/12/10 02:13:25
The problem is that this doesn't buy any extra tes
calamity
2014/12/10 02:26:13
The objective was to add more determinism since ba
Anand Mistry (off Chromium)
2014/12/10 03:52:14
Ok, gotcha. Done.
| |
| 60 base::TimeDelta delay = expiration_time - now; | |
| 61 if (delay.InSeconds() < kMinTokenRefreshDelaySeconds) | |
| 62 delay = base::TimeDelta::FromSeconds(kMinTokenRefreshDelaySeconds); | |
| 63 ScheduleTokenFetch(delay); | |
| 64 } | |
| 65 | |
| 66 void SpeechAuthHelper::OnGetTokenFailure( | |
| 67 const OAuth2TokenService::Request* request, | |
| 68 const GoogleServiceAuthError& error) { | |
| 69 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 70 auth_token_ = ""; | |
| 71 auth_token_request_.reset(); | |
| 72 | |
| 73 // Try again later. | |
| 74 // TODO(amistry): Implement backoff. | |
| 75 ScheduleTokenFetch( | |
| 76 base::TimeDelta::FromSeconds(kMinTokenRefreshDelaySeconds)); | |
| 77 } | |
| 78 | |
| 79 void SpeechAuthHelper::OnRefreshTokenAvailable(const std::string& account_id) { | |
| 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 81 SigninManagerBase* signin_manager = | |
| 82 SigninManagerFactory::GetForProfile(profile_); | |
| 83 if (signin_manager && | |
| 84 signin_manager->GetAuthenticatedAccountId() == account_id) { | |
| 85 FetchAuthToken(); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void SpeechAuthHelper::ScheduleTokenFetch(const base::TimeDelta& fetch_delay) { | |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 91 content::BrowserThread::PostDelayedTask( | |
| 92 content::BrowserThread::UI, | |
| 93 FROM_HERE, | |
| 94 base::Bind(&SpeechAuthHelper::FetchAuthToken, | |
| 95 weak_factory_.GetWeakPtr()), | |
| 96 fetch_delay); | |
| 97 } | |
| 98 | |
| 99 void SpeechAuthHelper::FetchAuthToken() { | |
| 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 101 ProfileOAuth2TokenService* token_service = | |
| 102 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 103 if (token_service) { | |
|
calamity
2014/12/09 02:21:16
Is it possible for this to be null when called fro
Anand Mistry (off Chromium)
2014/12/10 02:13:25
Added comment here.
| |
| 104 OAuth2TokenService::ScopeSet scopes; | |
| 105 scopes.insert(kAuthScope); | |
| 106 SigninManagerBase* signin_manager = | |
| 107 SigninManagerFactory::GetForProfile(profile_); | |
| 108 auth_token_request_ = token_service->StartRequest( | |
| 109 signin_manager->GetAuthenticatedAccountId(), | |
| 110 scopes, | |
| 111 this); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 std::string SpeechAuthHelper::GetToken() { | |
| 116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 117 return auth_token_; | |
| 118 } | |
| 119 | |
| 120 std::string SpeechAuthHelper::GetScope() { | |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 122 return kAuthScope; | |
| 123 } | |
| 124 | |
| 125 } // namespace app_list | |
| OLD | NEW |