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

Side by Side Diff: google_apis/gaia/oauth2_token_service_request.cc

Issue 299943003: Refactor ProfileOAuth2TokenServiceRequest into OAuth2TokenServiceRequest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove useless return value from Invalidate. Created 6 years, 6 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/signin/profile_oauth2_token_service_request.h" 5 #include "google_apis/gaia/oauth2_token_service_request.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.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" 12 #include "google_apis/gaia/google_service_auth_error.h"
19 #include "google_apis/gaia/oauth2_access_token_consumer.h" 13 #include "google_apis/gaia/oauth2_access_token_consumer.h"
20 14
21 class ProfileOAuth2TokenServiceRequest::Core 15 OAuth2TokenServiceRequest::TokenServiceProvider::TokenServiceProvider() {
22 : public base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>, 16 }
23 public OAuth2TokenService::Consumer { 17
18 OAuth2TokenServiceRequest::TokenServiceProvider::~TokenServiceProvider() {
19 }
20
21 // Core serves as the base class for OAuth2TokenService operations. Each
22 // operation should be modeled as a derived type.
23 //
24 // Core is used like this:
25 //
26 // 1. Constructed on owner thread.
27 //
28 // 2. Start() is called on owner thread, which calls StartOnTokenServiceThread()
29 // on token service thread.
30 //
31 // 3. Request is executed.
32 //
33 // 4. Stop() is called on owner thread, which calls StopOnTokenServiceThread()
34 // on token service thread.
35 //
36 // 5. Core is destroyed on owner thread.
37 class OAuth2TokenServiceRequest::Core
38 : public base::NonThreadSafe,
39 public base::RefCountedThreadSafe<OAuth2TokenServiceRequest::Core> {
24 public: 40 public:
25 // Note the thread where an instance of Core is constructed is referred to as 41 // 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_|. 42 // the "owner thread" here.
27 Core(Profile* profile, 43 Core(OAuth2TokenServiceRequest* owner, TokenServiceProvider* provider);
28 ProfileOAuth2TokenServiceRequest* owner); 44
29 // Starts fetching an OAuth2 access token for |account_id| and |scopes|. It 45 // Starts the core. Must be called on the owner thread.
30 // should be called on the owner thread. 46 void Start();
31 void Start( 47
32 const std::string& account_id, 48 // Stops the core. Must be called on the owner thread.
33 const OAuth2TokenService::ScopeSet& scopes);
34 // Stops the OAuth2 access token fetching. It should be called on the owner
35 // thread.
36 void Stop(); 49 void Stop();
37 50
38 OAuth2TokenService::Request* request(); 51 // Returns true if this object has been stopped. Must be called on the owner
52 // thread.
53 bool IsStopped() const;
39 54
40 // OAuth2TokenService::Consumer. It should be called on the UI thread. 55 protected:
56 // Core must be destroyed on the owner thread. If data members must be
57 // cleaned up or destroyed on the token service thread, do so in the
58 // StopOnTokenServiceThread method.
59 virtual ~Core();
60
61 // Called on the token service thread.
62 virtual void StartOnTokenServiceThread() = 0;
63
64 // Called on the token service thread.
65 virtual void StopOnTokenServiceThread() = 0;
66
67 base::SingleThreadTaskRunner* token_service_task_runner();
68 OAuth2TokenService* token_service();
69 OAuth2TokenServiceRequest* owner();
70
71 private:
72 friend class base::RefCountedThreadSafe<OAuth2TokenServiceRequest::Core>;
73
74 scoped_refptr<base::SingleThreadTaskRunner> token_service_task_runner_;
75 OAuth2TokenServiceRequest* owner_;
76 TokenServiceProvider* provider_;
77 DISALLOW_COPY_AND_ASSIGN(Core);
78 };
79
80 OAuth2TokenServiceRequest::Core::Core(OAuth2TokenServiceRequest* owner,
81 TokenServiceProvider* provider)
82 : owner_(owner), provider_(provider) {
83 DCHECK(owner_);
84 DCHECK(provider_);
85 token_service_task_runner_ = provider_->GetTokenServiceTaskRunner();
86 DCHECK(token_service_task_runner_);
87 }
88
89 OAuth2TokenServiceRequest::Core::~Core() {
90 }
91
92 void OAuth2TokenServiceRequest::Core::Start() {
93 DCHECK(CalledOnValidThread());
94 token_service_task_runner_->PostTask(
95 FROM_HERE,
96 base::Bind(&OAuth2TokenServiceRequest::Core::StartOnTokenServiceThread,
97 this));
98 }
99
100 void OAuth2TokenServiceRequest::Core::Stop() {
101 DCHECK(CalledOnValidThread());
102 DCHECK(!IsStopped());
103
104 // Detaches |owner_| from this instance so |owner_| will be called back only
105 // if |Stop()| has never been called.
106 owner_ = NULL;
107 token_service_task_runner_->PostTask(
blundell 2014/06/04 13:55:14 There's a bug here. This object requires that it's
maniscalco 2014/06/04 16:41:38 Good catch. If we use PostTaskAndReply, the "owne
maniscalco 2014/06/04 17:29:42 blundell, I want to make sure I understand the Pos
108 FROM_HERE,
109 base::Bind(&OAuth2TokenServiceRequest::Core::StopOnTokenServiceThread,
110 this));
111 }
112
113 bool OAuth2TokenServiceRequest::Core::IsStopped() const {
114 DCHECK(CalledOnValidThread());
115 return owner_ == NULL;
116 }
117
118 base::SingleThreadTaskRunner*
119 OAuth2TokenServiceRequest::Core::token_service_task_runner() {
120 return token_service_task_runner_;
121 }
122
123 OAuth2TokenService* OAuth2TokenServiceRequest::Core::token_service() {
124 DCHECK(token_service_task_runner_->BelongsToCurrentThread());
125 return provider_->GetTokenService();
126 }
127
128 OAuth2TokenServiceRequest* OAuth2TokenServiceRequest::Core::owner() {
129 DCHECK(CalledOnValidThread());
130 return owner_;
131 }
132
133 namespace {
134
135 // An implementation of Core for getting an access token.
136 class RequestCore : public OAuth2TokenServiceRequest::Core,
137 public OAuth2TokenService::Consumer {
138 public:
139 RequestCore(OAuth2TokenServiceRequest* owner,
140 OAuth2TokenServiceRequest::TokenServiceProvider* provider,
141 OAuth2TokenService::Consumer* consumer,
142 const std::string& account_id,
143 const OAuth2TokenService::ScopeSet& scopes);
144
145 // OAuth2TokenService::Consumer. Must be called on the token service thread.
41 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 146 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
42 const std::string& access_token, 147 const std::string& access_token,
43 const base::Time& expiration_time) OVERRIDE; 148 const base::Time& expiration_time) OVERRIDE;
44 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, 149 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
45 const GoogleServiceAuthError& error) OVERRIDE; 150 const GoogleServiceAuthError& error) OVERRIDE;
46 151
47 private: 152 private:
48 friend class 153 friend class base::RefCountedThreadSafe<RequestCore>;
49 base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>;
50 154
51 // Note this can be destructed on the owner thread or on the UI thread, 155 // Must be destroyed on the owner thread.
52 // depending on the reference count. 156 virtual ~RequestCore();
53 virtual ~Core();
54 157
55 // Starts an OAuth2TokenService::Request on the UI thread. 158 // Core implementation.
56 void StartOnUIThread( 159 virtual void StartOnTokenServiceThread() OVERRIDE;
57 const std::string& account_id, 160 virtual void StopOnTokenServiceThread() OVERRIDE;
58 const OAuth2TokenService::ScopeSet& scopes);
59 // Stops the OAuth2TokenService::Request on the UI thread.
60 void StopOnUIThread();
61 161
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, 162 void InformOwnerOnGetTokenSuccess(std::string access_token,
66 base::Time expiration_time); 163 base::Time expiration_time);
67 void InformOwnerOnGetTokenFailure(GoogleServiceAuthError error); 164 void InformOwnerOnGetTokenFailure(GoogleServiceAuthError error);
68 165
69 // The profile with which this instance was initialized.
70 Profile* const profile_;
71
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_; 166 scoped_refptr<base::SingleThreadTaskRunner> owner_task_runner_;
167 OAuth2TokenService::Consumer* const consumer_;
168 std::string account_id_;
169 OAuth2TokenService::ScopeSet scopes_;
77 170
78 // OAuth2TokenService request for fetching OAuth2 access token; it should be 171 // OAuth2TokenService request for fetching OAuth2 access token; it should be
79 // created, reset and accessed only on the UI thread. 172 // created, reset and accessed only on the token service thread.
80 scoped_ptr<OAuth2TokenService::Request> request_; 173 scoped_ptr<OAuth2TokenService::Request> request_;
81 174
82 DISALLOW_COPY_AND_ASSIGN(Core); 175 DISALLOW_COPY_AND_ASSIGN(RequestCore);
83 }; 176 };
84 177
85 ProfileOAuth2TokenServiceRequest::Core::Core( 178 RequestCore::RequestCore(
86 Profile* profile, 179 OAuth2TokenServiceRequest* owner,
87 ProfileOAuth2TokenServiceRequest* owner) 180 OAuth2TokenServiceRequest::TokenServiceProvider* provider,
88 : OAuth2TokenService::Consumer("oauth2_token_service"), 181 OAuth2TokenService::Consumer* consumer,
89 profile_(profile), 182 const std::string& account_id,
90 owner_(owner), 183 const OAuth2TokenService::ScopeSet& scopes)
91 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()) { 184 : OAuth2TokenServiceRequest::Core(owner, provider),
92 DCHECK(profile); 185 OAuth2TokenService::Consumer("oauth2_token_service"),
93 DCHECK(owner); 186 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()),
187 consumer_(consumer),
188 account_id_(account_id),
189 scopes_(scopes) {
190 DCHECK(consumer_);
191 DCHECK(!account_id_.empty());
192 DCHECK(!scopes_.empty());
94 } 193 }
95 194
96 ProfileOAuth2TokenServiceRequest::Core::~Core() { 195 RequestCore::~RequestCore() {
97 } 196 }
98 197
99 void ProfileOAuth2TokenServiceRequest::Core::Start( 198 void RequestCore::StartOnTokenServiceThread() {
100 const std::string& account_id, 199 DCHECK(token_service_task_runner()->BelongsToCurrentThread());
101 const OAuth2TokenService::ScopeSet& scopes) { 200 request_ = token_service()->StartRequest(account_id_, scopes_, this).Pass();
102 DCHECK(owner_task_runner_->BelongsToCurrentThread()); 201 }
103 202
104 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { 203 void RequestCore::StopOnTokenServiceThread() {
105 StartOnUIThread(account_id, scopes); 204 DCHECK(token_service_task_runner()->BelongsToCurrentThread());
106 } else { 205 request_.reset();
107 content::BrowserThread::PostTask( 206 }
108 content::BrowserThread::UI, 207
109 FROM_HERE, 208 void RequestCore::OnGetTokenSuccess(const OAuth2TokenService::Request* request,
110 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread, 209 const std::string& access_token,
111 this, account_id, scopes)); 210 const base::Time& expiration_time) {
211 DCHECK(token_service_task_runner()->BelongsToCurrentThread());
212 DCHECK_EQ(request_.get(), request);
213 owner_task_runner_->PostTask(
214 FROM_HERE,
215 base::Bind(&RequestCore::InformOwnerOnGetTokenSuccess,
216 this,
217 access_token,
218 expiration_time));
219 request_.reset();
220 }
221
222 void RequestCore::OnGetTokenFailure(const OAuth2TokenService::Request* request,
223 const GoogleServiceAuthError& error) {
224 DCHECK(token_service_task_runner()->BelongsToCurrentThread());
225 DCHECK_EQ(request_.get(), request);
226 owner_task_runner_->PostTask(
227 FROM_HERE,
228 base::Bind(&RequestCore::InformOwnerOnGetTokenFailure, this, error));
229 request_.reset();
230 }
231
232 void RequestCore::InformOwnerOnGetTokenSuccess(std::string access_token,
233 base::Time expiration_time) {
234 DCHECK(CalledOnValidThread());
235 if (!IsStopped()) {
236 consumer_->OnGetTokenSuccess(owner(), access_token, expiration_time);
112 } 237 }
113 } 238 }
114 239
115 void ProfileOAuth2TokenServiceRequest::Core::Stop() { 240 void RequestCore::InformOwnerOnGetTokenFailure(GoogleServiceAuthError error) {
116 DCHECK(owner_task_runner_->BelongsToCurrentThread()); 241 DCHECK(CalledOnValidThread());
117 242 if (!IsStopped()) {
118 // Detaches |owner_| from this instance so |owner_| will be called back only 243 consumer_->OnGetTokenFailure(owner(), error);
119 // if |Stop()| has never been called.
120 owner_ = NULL;
121 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
122 StopOnUIThread();
123 } else {
124 content::BrowserThread::PostTask(
125 content::BrowserThread::UI,
126 FROM_HERE,
127 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread,
128 this));
129 } 244 }
130 } 245 }
131 246
132 OAuth2TokenService::Request* ProfileOAuth2TokenServiceRequest::Core::request() { 247 // An implementation of Core for invalidating an access token.
133 return request_.get(); 248 class InvalidateCore : public OAuth2TokenServiceRequest::Core {
249 public:
250 InvalidateCore(OAuth2TokenServiceRequest* owner,
251 OAuth2TokenServiceRequest::TokenServiceProvider* provider,
252 const std::string& access_token,
253 const std::string& account_id,
254 const OAuth2TokenService::ScopeSet& scopes);
255
256 private:
257 friend class base::RefCountedThreadSafe<InvalidateCore>;
258
259 // Must be destroyed on the owner thread.
260 virtual ~InvalidateCore();
261
262 // Core implementation.
263 virtual void StartOnTokenServiceThread() OVERRIDE;
264 virtual void StopOnTokenServiceThread() OVERRIDE;
265
266 std::string access_token_;
267 std::string account_id_;
268 OAuth2TokenService::ScopeSet scopes_;
269
270 DISALLOW_COPY_AND_ASSIGN(InvalidateCore);
271 };
272
273 InvalidateCore::InvalidateCore(
274 OAuth2TokenServiceRequest* owner,
275 OAuth2TokenServiceRequest::TokenServiceProvider* provider,
276 const std::string& access_token,
277 const std::string& account_id,
278 const OAuth2TokenService::ScopeSet& scopes)
279 : OAuth2TokenServiceRequest::Core(owner, provider),
280 access_token_(access_token),
281 account_id_(account_id),
282 scopes_(scopes) {
283 DCHECK(!access_token_.empty());
284 DCHECK(!account_id_.empty());
285 DCHECK(!scopes.empty());
134 } 286 }
135 287
136 void ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread() { 288 InvalidateCore::~InvalidateCore() {
137 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
138 request_.reset();
139 } 289 }
140 290
141 void ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread( 291 void InvalidateCore::StartOnTokenServiceThread() {
142 const std::string& account_id, 292 DCHECK(token_service_task_runner()->BelongsToCurrentThread());
143 const OAuth2TokenService::ScopeSet& scopes) { 293 token_service()->InvalidateToken(account_id_, scopes_, access_token_);
144 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
145
146 ProfileOAuth2TokenService* service =
147 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
148 DCHECK(service);
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()
154 : account_id;
155 request_.reset(
156 service->StartRequest(account_id_to_use, scopes, this).release());
157 } 294 }
158 295
159 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenSuccess( 296 void InvalidateCore::StopOnTokenServiceThread() {
160 const OAuth2TokenService::Request* request, 297 DCHECK(token_service_task_runner()->BelongsToCurrentThread());
161 const std::string& access_token, 298 // Nothing to do.
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,
167 this,
168 access_token,
169 expiration_time));
170 request_.reset();
171 } 299 }
172 300
173 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenFailure( 301 } // namespace
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,
180 this,
181 error));
182 request_.reset();
183 }
184 302
185 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess( 303 // static
186 std::string access_token, 304 scoped_ptr<OAuth2TokenServiceRequest> OAuth2TokenServiceRequest::CreateAndStart(
187 base::Time expiration_time) { 305 TokenServiceProvider* provider,
188 DCHECK(owner_task_runner_->BelongsToCurrentThread()); 306 const std::string& account_id,
189 307 const OAuth2TokenService::ScopeSet& scopes,
190 if (owner_) 308 OAuth2TokenService::Consumer* consumer) {
191 owner_->consumer_->OnGetTokenSuccess(owner_, access_token, expiration_time); 309 scoped_ptr<OAuth2TokenServiceRequest> request(
192 } 310 new OAuth2TokenServiceRequest(account_id));
193 311 scoped_refptr<Core> core(
194 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure( 312 new RequestCore(request.get(), provider, consumer, account_id, scopes));
195 GoogleServiceAuthError error) { 313 request->StartWithCore(core);
196 DCHECK(owner_task_runner_->BelongsToCurrentThread()); 314 return request.Pass();
197
198 if (owner_)
199 owner_->consumer_->OnGetTokenFailure(owner_, error);
200 } 315 }
201 316
202 // static 317 // static
203 ProfileOAuth2TokenServiceRequest* 318 void OAuth2TokenServiceRequest::InvalidateToken(
204 ProfileOAuth2TokenServiceRequest::CreateAndStart( 319 OAuth2TokenServiceRequest::TokenServiceProvider* provider,
205 Profile* profile, 320 const std::string& account_id,
206 const std::string& account_id, 321 const OAuth2TokenService::ScopeSet& scopes,
207 const OAuth2TokenService::ScopeSet& scopes, 322 const std::string& access_token) {
208 OAuth2TokenService::Consumer* consumer) { 323 scoped_ptr<OAuth2TokenServiceRequest> request(
209 return new ProfileOAuth2TokenServiceRequest(profile, account_id, scopes, 324 new OAuth2TokenServiceRequest(account_id));
210 consumer); 325 scoped_refptr<Core> core(new InvalidateCore(
326 request.get(), provider, access_token, account_id, scopes));
327 request->StartWithCore(core);
211 } 328 }
212 329
213 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest( 330 OAuth2TokenServiceRequest::~OAuth2TokenServiceRequest() {
214 Profile* profile,
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);
221 }
222
223 ProfileOAuth2TokenServiceRequest::~ProfileOAuth2TokenServiceRequest() {
224 DCHECK(CalledOnValidThread());
225 core_->Stop(); 331 core_->Stop();
226 } 332 }
227 333
228 std::string ProfileOAuth2TokenServiceRequest::GetAccountId() const { 334 std::string OAuth2TokenServiceRequest::GetAccountId() const {
229 return core_->request()->GetAccountId(); 335 return account_id_;
230 } 336 }
231 337
338 OAuth2TokenServiceRequest::OAuth2TokenServiceRequest(
339 const std::string& account_id)
340 : account_id_(account_id) {
341 DCHECK(!account_id_.empty());
342 }
343
344 void OAuth2TokenServiceRequest::StartWithCore(const scoped_refptr<Core>& core) {
345 DCHECK(core);
346 core_ = core;
347 core_->Start();
348 }
OLDNEW
« no previous file with comments | « google_apis/gaia/oauth2_token_service_request.h ('k') | google_apis/gaia/oauth2_token_service_request_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698