Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: chrome/browser/ui/app_list/speech_auth_helper.cc

Issue 778393002: Add OAuth2 authentication for some voice transcription requests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 token_service_(ProfileOAuth2TokenServiceFactory::GetForProfile(profile)),
28 weak_factory_(this) {
29 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
30
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;
35
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;
41
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();
49 }
50 }
51
52 SpeechAuthHelper::~SpeechAuthHelper() {
53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
54 if (token_service_)
55 token_service_->RemoveObserver(this);
calamity 2014/12/10 02:26:13 If the refresh token is available, I would expect
Anand Mistry (off Chromium) 2014/12/10 03:52:14 It doesn't. RemoveObserver() gracefully handles NU
56 }
57
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();
65
66 base::Time now = base::Time::Now();
67 base::TimeDelta delay = expiration_time - now;
68 if (delay.InSeconds() < kMinTokenRefreshDelaySeconds)
69 delay = base::TimeDelta::FromSeconds(kMinTokenRefreshDelaySeconds);
70 ScheduleTokenFetch(delay);
71 }
72
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();
79
80 // Try again later.
81 // TODO(amistry): Implement backoff.
82 ScheduleTokenFetch(
83 base::TimeDelta::FromSeconds(kMinTokenRefreshDelaySeconds));
84 }
85
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();
90 }
91
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);
100 }
101
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);
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
OLDNEW
« no previous file with comments | « chrome/browser/ui/app_list/speech_auth_helper.h ('k') | chrome/browser/ui/app_list/speech_auth_helper_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698