1 // Copyright 2012 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/signin/profile_oauth2_token_service_request.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "components/signin/core/browser/profile_oauth2_token_service.h"
16 #include "components/signin/core/browser/signin_manager.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "google_apis/gaia/google_service_auth_error.h"
19 #include "google_apis/gaia/oauth2_access_token_consumer.h"
21 class ProfileOAuth2TokenServiceRequest::Core
22 : public base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>,
23 public OAuth2TokenService::Consumer {
25 // Note the thread where an instance of Core is constructed is referred to as
26 // the "owner thread" here. This will be the thread of |owner_task_runner_|.
27 Core(Profile* profile,
28 ProfileOAuth2TokenServiceRequest* owner);
29 // Starts fetching an OAuth2 access token for |account_id| and |scopes|. It
30 // should be called on the owner thread.
32 const std::string& account_id,
33 const OAuth2TokenService::ScopeSet& scopes);
34 // Stops the OAuth2 access token fetching. It should be called on the owner
38 OAuth2TokenService::Request* request();
40 // OAuth2TokenService::Consumer. It should be called on the UI thread.
41 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
42 const std::string& access_token,
43 const base::Time& expiration_time) OVERRIDE;
44 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
45 const GoogleServiceAuthError& error) OVERRIDE;
49 base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>;
51 // Note this can be destructed on the owner thread or on the UI thread,
52 // depending on the reference count.
55 // Starts an OAuth2TokenService::Request on the UI thread.
57 const std::string& account_id,
58 const OAuth2TokenService::ScopeSet& scopes);
59 // Stops the OAuth2TokenService::Request on the UI thread.
60 void StopOnUIThread();
62 // Method posted to the owner thread to call back |owner_|. Note when this
63 // posted task is actually running on the owner thread, it is possible that
64 // |owner_| has been reset NULL.
65 void InformOwnerOnGetTokenSuccess(std::string access_token,
66 base::Time expiration_time);
67 void InformOwnerOnGetTokenFailure(GoogleServiceAuthError error);
69 // The profile with which this instance was initialized.
70 Profile* const profile_;
72 // The object to call back when fetching completes. |owner_| should be
73 // called back only on the owner thread.
74 ProfileOAuth2TokenServiceRequest* owner_;
75 // Task runner on which |owner_| should be called back.
76 scoped_refptr<base::SingleThreadTaskRunner> owner_task_runner_;
78 // OAuth2TokenService request for fetching OAuth2 access token; it should be
79 // created, reset and accessed only on the UI thread.
80 scoped_ptr<OAuth2TokenService::Request> request_;
82 DISALLOW_COPY_AND_ASSIGN(Core);
85 ProfileOAuth2TokenServiceRequest::Core::Core(
87 ProfileOAuth2TokenServiceRequest* owner)
88 : OAuth2TokenService::Consumer("oauth2_token_service"),
91 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
96 ProfileOAuth2TokenServiceRequest::Core::~Core() {
99 void ProfileOAuth2TokenServiceRequest::Core::Start(
100 const std::string& account_id,
101 const OAuth2TokenService::ScopeSet& scopes) {
102 DCHECK(owner_task_runner_->BelongsToCurrentThread());
104 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
105 StartOnUIThread(account_id, scopes);
107 content::BrowserThread::PostTask(
108 content::BrowserThread::UI,
110 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread,
111 this, account_id, scopes));
115 void ProfileOAuth2TokenServiceRequest::Core::Stop() {
116 DCHECK(owner_task_runner_->BelongsToCurrentThread());
118 // Detaches |owner_| from this instance so |owner_| will be called back only
119 // if |Stop()| has never been called.
121 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
124 content::BrowserThread::PostTask(
125 content::BrowserThread::UI,
127 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread,
132 OAuth2TokenService::Request* ProfileOAuth2TokenServiceRequest::Core::request() {
133 return request_.get();
136 void ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread() {
137 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
141 void ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread(
142 const std::string& account_id,
143 const OAuth2TokenService::ScopeSet& scopes) {
144 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
146 ProfileOAuth2TokenService* service =
147 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
149 SigninManagerBase* signin_manager =
150 SigninManagerFactory::GetForProfile(profile_);
151 DCHECK(signin_manager);
152 std::string account_id_to_use =
153 account_id.empty() ? signin_manager->GetAuthenticatedAccountId()
156 service->StartRequest(account_id_to_use, scopes, this).release());
159 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenSuccess(
160 const OAuth2TokenService::Request* request,
161 const std::string& access_token,
162 const base::Time& expiration_time) {
163 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
164 DCHECK_EQ(request_.get(), request);
165 owner_task_runner_->PostTask(FROM_HERE, base::Bind(
166 &ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess,
173 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenFailure(
174 const OAuth2TokenService::Request* request,
175 const GoogleServiceAuthError& error) {
176 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
177 DCHECK_EQ(request_.get(), request);
178 owner_task_runner_->PostTask(FROM_HERE, base::Bind(
179 &ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure,
185 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess(
186 std::string access_token,
187 base::Time expiration_time) {
188 DCHECK(owner_task_runner_->BelongsToCurrentThread());
191 owner_->consumer_->OnGetTokenSuccess(owner_, access_token, expiration_time);
194 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure(
195 GoogleServiceAuthError error) {
196 DCHECK(owner_task_runner_->BelongsToCurrentThread());
199 owner_->consumer_->OnGetTokenFailure(owner_, error);
203 ProfileOAuth2TokenServiceRequest*
204 ProfileOAuth2TokenServiceRequest::CreateAndStart(
206 const std::string& account_id,
207 const OAuth2TokenService::ScopeSet& scopes,
208 OAuth2TokenService::Consumer* consumer) {
209 return new ProfileOAuth2TokenServiceRequest(profile, account_id, scopes,
213 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest(
215 const std::string& account_id,
216 const OAuth2TokenService::ScopeSet& scopes,
217 OAuth2TokenService::Consumer* consumer)
218 : consumer_(consumer),
219 core_(new Core(profile, this)) {
220 core_->Start(account_id, scopes);
223 ProfileOAuth2TokenServiceRequest::~ProfileOAuth2TokenServiceRequest() {
224 DCHECK(CalledOnValidThread());
228 std::string ProfileOAuth2TokenServiceRequest::GetAccountId() const {
229 return core_->request()->GetAccountId();