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

Side by Side Diff: chrome/browser/signin/profile_oauth2_token_service_request.cc

Issue 299943003: Refactor ProfileOAuth2TokenServiceRequest into OAuth2TokenServiceRequest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Allow ProfileOAuth2TokenServiceRequestTest to free UI thread resources. Created 6 years, 7 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 2012 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 "chrome/browser/signin/profile_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" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/signin_manager_factory.h" 14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "components/signin/core/browser/profile_oauth2_token_service.h" 15 #include "components/signin/core/browser/profile_oauth2_token_service.h"
16 #include "components/signin/core/browser/signin_manager.h" 16 #include "components/signin/core/browser/signin_manager.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "google_apis/gaia/google_service_auth_error.h" 18 #include "google_apis/gaia/google_service_auth_error.h"
19 #include "google_apis/gaia/oauth2_access_token_consumer.h" 19 #include "google_apis/gaia/oauth2_access_token_consumer.h"
20 20
21 class ProfileOAuth2TokenServiceRequest::Core 21 // Static.
22 : public base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>, 22 ProfileOAuth2TokenServiceRequest*
23 public OAuth2TokenService::Consumer { 23 ProfileOAuth2TokenServiceRequest::CreateAndStart(
24 public:
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.
31 void Start(
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
35 // thread.
36 void Stop();
37
38 OAuth2TokenService::Request* request();
39
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;
46
47 private:
48 friend class
49 base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>;
50
51 // Note this can be destructed on the owner thread or on the UI thread,
52 // depending on the reference count.
53 virtual ~Core();
54
55 // Starts an OAuth2TokenService::Request on the UI thread.
56 void StartOnUIThread(
57 const std::string& account_id,
58 const OAuth2TokenService::ScopeSet& scopes);
59 // Stops the OAuth2TokenService::Request on the UI thread.
60 void StopOnUIThread();
61
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);
68
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_;
77
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_;
81
82 DISALLOW_COPY_AND_ASSIGN(Core);
83 };
84
85 ProfileOAuth2TokenServiceRequest::Core::Core(
86 Profile* profile, 24 Profile* profile,
87 ProfileOAuth2TokenServiceRequest* owner)
88 : OAuth2TokenService::Consumer("oauth2_token_service"),
89 profile_(profile),
90 owner_(owner),
91 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
92 DCHECK(profile);
93 DCHECK(owner);
94 }
95
96 ProfileOAuth2TokenServiceRequest::Core::~Core() {
97 }
98
99 void ProfileOAuth2TokenServiceRequest::Core::Start(
100 const std::string& account_id, 25 const std::string& account_id,
101 const OAuth2TokenService::ScopeSet& scopes) { 26 const OAuth2TokenService::ScopeSet& scopes,
102 DCHECK(owner_task_runner_->BelongsToCurrentThread()); 27 OAuth2TokenService::Consumer* consumer) {
103 28 return new ProfileOAuth2TokenServiceRequest(
104 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { 29 profile, account_id, scopes, consumer);
105 StartOnUIThread(account_id, scopes);
106 } else {
107 content::BrowserThread::PostTask(
108 content::BrowserThread::UI,
109 FROM_HERE,
110 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread,
111 this, account_id, scopes));
112 }
113 }
114
115 void ProfileOAuth2TokenServiceRequest::Core::Stop() {
116 DCHECK(owner_task_runner_->BelongsToCurrentThread());
117
118 // Detaches |owner_| from this instance so |owner_| will be called back only
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 }
130 }
131
132 OAuth2TokenService::Request* ProfileOAuth2TokenServiceRequest::Core::request() {
133 return request_.get();
134 }
135
136 void ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread() {
137 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
138 request_.reset();
139 }
140
141 void ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread(
142 const std::string& account_id,
143 const OAuth2TokenService::ScopeSet& scopes) {
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 }
158
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,
167 this,
168 access_token,
169 expiration_time));
170 request_.reset();
171 }
172
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,
180 this,
181 error));
182 request_.reset();
183 }
184
185 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess(
186 std::string access_token,
187 base::Time expiration_time) {
188 DCHECK(owner_task_runner_->BelongsToCurrentThread());
189
190 if (owner_)
191 owner_->consumer_->OnGetTokenSuccess(owner_, access_token, expiration_time);
192 }
193
194 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure(
195 GoogleServiceAuthError error) {
196 DCHECK(owner_task_runner_->BelongsToCurrentThread());
197
198 if (owner_)
199 owner_->consumer_->OnGetTokenFailure(owner_, error);
200 }
201
202 // static
203 ProfileOAuth2TokenServiceRequest*
204 ProfileOAuth2TokenServiceRequest::CreateAndStart(
205 Profile* profile,
206 const std::string& account_id,
207 const OAuth2TokenService::ScopeSet& scopes,
208 OAuth2TokenService::Consumer* consumer) {
209 return new ProfileOAuth2TokenServiceRequest(profile, account_id, scopes,
210 consumer);
211 } 30 }
212 31
213 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest( 32 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest(
214 Profile* profile, 33 Profile* profile,
215 const std::string& account_id, 34 const std::string& account_id,
216 const OAuth2TokenService::ScopeSet& scopes, 35 const OAuth2TokenService::ScopeSet& scopes,
217 OAuth2TokenService::Consumer* consumer) 36 OAuth2TokenService::Consumer* consumer)
218 : consumer_(consumer), 37 : account_id_(account_id),
219 core_(new Core(profile, this)) { 38 scopes_(scopes),
220 core_->Start(account_id, scopes); 39 consumer_(consumer),
40 weak_ptr_factory_(this) {
41 DCHECK(profile);
42 DCHECK(!account_id.empty());
43 DCHECK(consumer);
44 // We need to get the Profile's OAuth2TokenService and its task runner.
45 //
46 // Call GetTokenService in the UI thread. Then call ContinueWithTokenService
47 // in this thread.
48 GetTokenServiceCallback callback =
49 base::Bind(&ProfileOAuth2TokenServiceRequest::ContinueWithTokenService,
50 weak_ptr_factory_.GetWeakPtr());
51 scoped_refptr<base::MessageLoopProxy> ui_message_loop =
52 content::BrowserThread::GetMessageLoopProxyForThread(
53 content::BrowserThread::UI);
54 ui_message_loop->PostTask(
55 FROM_HERE,
56 base::Bind(&ProfileOAuth2TokenServiceRequest::GetTokenService,
57 profile,
58 base::MessageLoopProxy::current(),
59 callback));
60 }
61
62 // Static.
63 void ProfileOAuth2TokenServiceRequest::GetTokenService(
64 Profile* profile,
65 const scoped_refptr<base::SequencedTaskRunner>& callers_task_runner,
66 const GetTokenServiceCallback& callback) {
67 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
68 OAuth2TokenService* token_service =
69 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
70 DCHECK(token_service);
71 callers_task_runner->PostTask(FROM_HERE, base::Bind(callback, token_service));
72 }
73
74 void ProfileOAuth2TokenServiceRequest::ContinueWithTokenService(
75 OAuth2TokenService* token_service) {
76 DCHECK(CalledOnValidThread());
77 scoped_refptr<base::MessageLoopProxy> token_service_task_runner =
78 content::BrowserThread::GetMessageLoopProxyForThread(
79 content::BrowserThread::UI);
80 proxy_.reset(
81 new OAuth2TokenServiceProxy(token_service_task_runner, token_service));
82 OAuth2TokenServiceProxy::RequestTokenCallback callback =
83 base::Bind(&ProfileOAuth2TokenServiceRequest::RequestTokenDone,
84 weak_ptr_factory_.GetWeakPtr());
85 proxy_->RequestToken(
86 account_id_, scopes_, callback, "profile_oauth2_token_service_request");
87 }
88
89 void ProfileOAuth2TokenServiceRequest::RequestTokenDone(
90 const GoogleServiceAuthError& error,
91 const std::string& access_token,
92 const base::Time& expiration_time) {
93 DCHECK(CalledOnValidThread());
94 if (error.state() == GoogleServiceAuthError::NONE) {
95 consumer_->OnGetTokenSuccess(this, access_token, expiration_time);
96 } else {
97 consumer_->OnGetTokenFailure(this, error);
98 }
221 } 99 }
222 100
223 ProfileOAuth2TokenServiceRequest::~ProfileOAuth2TokenServiceRequest() { 101 ProfileOAuth2TokenServiceRequest::~ProfileOAuth2TokenServiceRequest() {
224 DCHECK(CalledOnValidThread()); 102 DCHECK(CalledOnValidThread());
225 core_->Stop();
226 } 103 }
227 104
228 std::string ProfileOAuth2TokenServiceRequest::GetAccountId() const { 105 std::string ProfileOAuth2TokenServiceRequest::GetAccountId() const {
229 return core_->request()->GetAccountId(); 106 DCHECK(CalledOnValidThread());
107 return account_id_;
230 } 108 }
231
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698