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"
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"
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
),
27 token_service_(ProfileOAuth2TokenServiceFactory::GetForProfile(profile
)),
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.
36 SigninManagerBase
* signin_manager
=
37 SigninManagerFactory::GetForProfile(profile
);
38 // Again, this might be NULL, and if it is, we can't proceed.
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
46 token_service_
->AddObserver(this);
52 SpeechAuthHelper::~SpeechAuthHelper() {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
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
);
78 auth_token_request_
.reset();
81 // TODO(amistry): Implement backoff.
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
)
92 void SpeechAuthHelper::ScheduleTokenFetch(const base::TimeDelta
& fetch_delay
) {
93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
94 content::BrowserThread::PostDelayedTask(
95 content::BrowserThread::UI
,
97 base::Bind(&SpeechAuthHelper::FetchAuthToken
,
98 weak_factory_
.GetWeakPtr()),
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_
,
115 std::string
SpeechAuthHelper::GetToken() const {
116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
120 std::string
SpeechAuthHelper::GetScope() const {
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
125 } // namespace app_list