Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ui / app_list / speech_auth_helper.cc
blob63a3e8fbad32c9adebdd396b40fbf13d96153fcc
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.
5 #include "chrome/browser/ui/app_list/speech_auth_helper.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/time/clock.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"
17 namespace app_list {
19 static const char* kAuthScope =
20 "https://www.googleapis.com/auth/webhistory";
21 static const int kMinTokenRefreshDelaySeconds = 300; // 5 minutes
24 SpeechAuthHelper::SpeechAuthHelper(Profile* profile, base::Clock* clock)
25 : OAuth2TokenService::Consumer(kAuthScope),
26 clock_(clock),
27 token_service_(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)),
28 weak_factory_(this) {
29 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
31 // If token_service_ is NULL, we can't do anything. This might be NULL if the
32 // profile is a guest user.
33 if (!token_service_)
34 return;
36 SigninManagerBase* signin_manager =
37 SigninManagerFactory::GetForProfile(profile);
38 // Again, this might be NULL, and if it is, we can't proceed.
39 if (!signin_manager)
40 return;
42 authenticated_account_id_ = signin_manager->GetAuthenticatedAccountId();
43 if (!token_service_->RefreshTokenIsAvailable(authenticated_account_id_)) {
44 // Wait for the OAuth2 refresh token to be available before trying to obtain
45 // a speech token.
46 token_service_->AddObserver(this);
47 } else {
48 FetchAuthToken();
52 SpeechAuthHelper::~SpeechAuthHelper() {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
54 if (token_service_)
55 token_service_->RemoveObserver(this);
58 void SpeechAuthHelper::OnGetTokenSuccess(
59 const OAuth2TokenService::Request* request,
60 const std::string& access_token,
61 const base::Time& expiration_time) {
62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
63 auth_token_ = access_token;
64 auth_token_request_.reset();
66 base::Time now = clock_->Now();
67 base::TimeDelta delay = expiration_time - now;
68 if (delay.InSeconds() < kMinTokenRefreshDelaySeconds)
69 delay = base::TimeDelta::FromSeconds(kMinTokenRefreshDelaySeconds);
70 ScheduleTokenFetch(delay);
73 void SpeechAuthHelper::OnGetTokenFailure(
74 const OAuth2TokenService::Request* request,
75 const GoogleServiceAuthError& error) {
76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
77 auth_token_ = "";
78 auth_token_request_.reset();
80 // Try again later.
81 // TODO(amistry): Implement backoff.
82 ScheduleTokenFetch(
83 base::TimeDelta::FromSeconds(kMinTokenRefreshDelaySeconds));
86 void SpeechAuthHelper::OnRefreshTokenAvailable(const std::string& account_id) {
87 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
88 if (authenticated_account_id_ == account_id)
89 FetchAuthToken();
92 void SpeechAuthHelper::ScheduleTokenFetch(const base::TimeDelta& fetch_delay) {
93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
94 content::BrowserThread::PostDelayedTask(
95 content::BrowserThread::UI,
96 FROM_HERE,
97 base::Bind(&SpeechAuthHelper::FetchAuthToken,
98 weak_factory_.GetWeakPtr()),
99 fetch_delay);
102 void SpeechAuthHelper::FetchAuthToken() {
103 // The process of fetching and refreshing OAuth tokens is started from the
104 // consustructor, and so token_service_ and authenticated_account_id_ are
105 // guaranteed to be valid at this point.
106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
107 OAuth2TokenService::ScopeSet scopes;
108 scopes.insert(kAuthScope);
109 auth_token_request_ = token_service_->StartRequest(
110 authenticated_account_id_,
111 scopes,
112 this);
115 std::string SpeechAuthHelper::GetToken() const {
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
117 return auth_token_;
120 std::string SpeechAuthHelper::GetScope() const {
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
122 return kAuthScope;
125 } // namespace app_list