| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/signin/profile_oauth2_token_service_request.h" | |
| 6 | |
| 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_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" | |
| 20 | |
| 21 class ProfileOAuth2TokenServiceRequest::Core | |
| 22 : public base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>, | |
| 23 public OAuth2TokenService::Consumer { | |
| 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, | |
| 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, | |
| 101 const OAuth2TokenService::ScopeSet& scopes) { | |
| 102 DCHECK(owner_task_runner_->BelongsToCurrentThread()); | |
| 103 | |
| 104 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | |
| 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 } | |
| 212 | |
| 213 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest( | |
| 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(); | |
| 226 } | |
| 227 | |
| 228 std::string ProfileOAuth2TokenServiceRequest::GetAccountId() const { | |
| 229 return core_->request()->GetAccountId(); | |
| 230 } | |
| 231 | |
| OLD | NEW |