Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / signin / profile_oauth2_token_service_request.cc
blobb388aa2a282993665116412661aae5e3d1567944
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"
7 #include "base/bind.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.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "google_apis/gaia/oauth2_access_token_consumer.h"
19 class ProfileOAuth2TokenServiceRequest::Core
20 : public base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>,
21 public OAuth2TokenService::Consumer {
22 public:
23 // Note the thread where an instance of Core is constructed is referred to as
24 // the "owner thread" here. This will be the thread of |owner_task_runner_|.
25 Core(Profile* profile,
26 ProfileOAuth2TokenServiceRequest* owner);
27 // Starts fetching an OAuth2 access token for |account_id| and |scopes|. It
28 // should be called on the owner thread.
29 void Start(
30 const std::string& account_id,
31 const OAuth2TokenService::ScopeSet& scopes);
32 // Stops the OAuth2 access token fetching. It should be called on the owner
33 // thread.
34 void Stop();
36 OAuth2TokenService::Request* request();
38 // OAuth2TokenService::Consumer. It should be called on the UI thread.
39 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
40 const std::string& access_token,
41 const base::Time& expiration_time) OVERRIDE;
42 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
43 const GoogleServiceAuthError& error) OVERRIDE;
45 private:
46 friend class
47 base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>;
49 // Note this can be destructed on the owner thread or on the UI thread,
50 // depending on the reference count.
51 virtual ~Core();
53 // Starts an OAuth2TokenService::Request on the UI thread.
54 void StartOnUIThread(
55 const std::string& account_id,
56 const OAuth2TokenService::ScopeSet& scopes);
57 // Stops the OAuth2TokenService::Request on the UI thread.
58 void StopOnUIThread();
60 // Method posted to the owner thread to call back |owner_|. Note when this
61 // posted task is actually running on the owner thread, it is possible that
62 // |owner_| has been reset NULL.
63 void InformOwnerOnGetTokenSuccess(std::string access_token,
64 base::Time expiration_time);
65 void InformOwnerOnGetTokenFailure(GoogleServiceAuthError error);
67 // The profile with which this instance was initialized.
68 Profile* const profile_;
70 // The object to call back when fetching completes. |owner_| should be
71 // called back only on the owner thread.
72 ProfileOAuth2TokenServiceRequest* owner_;
73 // Task runner on which |owner_| should be called back.
74 scoped_refptr<base::SingleThreadTaskRunner> owner_task_runner_;
76 // OAuth2TokenService request for fetching OAuth2 access token; it should be
77 // created, reset and accessed only on the UI thread.
78 scoped_ptr<OAuth2TokenService::Request> request_;
80 DISALLOW_COPY_AND_ASSIGN(Core);
83 ProfileOAuth2TokenServiceRequest::Core::Core(
84 Profile* profile,
85 ProfileOAuth2TokenServiceRequest* owner)
86 : OAuth2TokenService::Consumer("oauth2_token_service"),
87 profile_(profile),
88 owner_(owner),
89 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
90 DCHECK(profile);
91 DCHECK(owner);
94 ProfileOAuth2TokenServiceRequest::Core::~Core() {
97 void ProfileOAuth2TokenServiceRequest::Core::Start(
98 const std::string& account_id,
99 const OAuth2TokenService::ScopeSet& scopes) {
100 DCHECK(owner_task_runner_->BelongsToCurrentThread());
102 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
103 StartOnUIThread(account_id, scopes);
104 } else {
105 content::BrowserThread::PostTask(
106 content::BrowserThread::UI,
107 FROM_HERE,
108 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread,
109 this, account_id, scopes));
113 void ProfileOAuth2TokenServiceRequest::Core::Stop() {
114 DCHECK(owner_task_runner_->BelongsToCurrentThread());
116 // Detaches |owner_| from this instance so |owner_| will be called back only
117 // if |Stop()| has never been called.
118 owner_ = NULL;
119 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
120 StopOnUIThread();
121 } else {
122 content::BrowserThread::PostTask(
123 content::BrowserThread::UI,
124 FROM_HERE,
125 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread,
126 this));
130 OAuth2TokenService::Request* ProfileOAuth2TokenServiceRequest::Core::request() {
131 return request_.get();
134 void ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread() {
135 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
136 request_.reset();
139 void ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread(
140 const std::string& account_id,
141 const OAuth2TokenService::ScopeSet& scopes) {
142 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
144 ProfileOAuth2TokenService* service =
145 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
146 DCHECK(service);
147 std::string account_id_to_use =
148 account_id.empty() ? service->GetPrimaryAccountId() : account_id;
149 request_.reset(
150 service->StartRequest(account_id_to_use, scopes, this).release());
153 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenSuccess(
154 const OAuth2TokenService::Request* request,
155 const std::string& access_token,
156 const base::Time& expiration_time) {
157 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
158 DCHECK_EQ(request_.get(), request);
159 owner_task_runner_->PostTask(FROM_HERE, base::Bind(
160 &ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess,
161 this,
162 access_token,
163 expiration_time));
164 request_.reset();
167 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenFailure(
168 const OAuth2TokenService::Request* request,
169 const GoogleServiceAuthError& error) {
170 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
171 DCHECK_EQ(request_.get(), request);
172 owner_task_runner_->PostTask(FROM_HERE, base::Bind(
173 &ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure,
174 this,
175 error));
176 request_.reset();
179 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess(
180 std::string access_token,
181 base::Time expiration_time) {
182 DCHECK(owner_task_runner_->BelongsToCurrentThread());
184 if (owner_)
185 owner_->consumer_->OnGetTokenSuccess(owner_, access_token, expiration_time);
188 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure(
189 GoogleServiceAuthError error) {
190 DCHECK(owner_task_runner_->BelongsToCurrentThread());
192 if (owner_)
193 owner_->consumer_->OnGetTokenFailure(owner_, error);
196 // static
197 ProfileOAuth2TokenServiceRequest*
198 ProfileOAuth2TokenServiceRequest::CreateAndStart(
199 Profile* profile,
200 const std::string& account_id,
201 const OAuth2TokenService::ScopeSet& scopes,
202 OAuth2TokenService::Consumer* consumer) {
203 return new ProfileOAuth2TokenServiceRequest(profile, account_id, scopes,
204 consumer);
207 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest(
208 Profile* profile,
209 const std::string& account_id,
210 const OAuth2TokenService::ScopeSet& scopes,
211 OAuth2TokenService::Consumer* consumer)
212 : consumer_(consumer),
213 core_(new Core(profile, this)) {
214 core_->Start(account_id, scopes);
217 ProfileOAuth2TokenServiceRequest::~ProfileOAuth2TokenServiceRequest() {
218 DCHECK(CalledOnValidThread());
219 core_->Stop();
222 std::string ProfileOAuth2TokenServiceRequest::GetAccountId() const {
223 return core_->request()->GetAccountId();