| OLD | NEW |
| 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.h" | 13 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
| 17 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 17 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 18 | 18 |
| 19 class ProfileOAuth2TokenServiceRequest::Core | 19 class ProfileOAuth2TokenServiceRequest::Core |
| 20 : public base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>, | 20 : public base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>, |
| 21 public OAuth2TokenService::Consumer { | 21 public OAuth2TokenService::Consumer { |
| 22 public: | 22 public: |
| 23 // Note the thread where an instance of Core is constructed is referred to as | 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_|. | 24 // the "owner thread" here. This will be the thread of |owner_task_runner_|. |
| 25 Core(Profile* profile, | 25 Core(Profile* profile, |
| 26 ProfileOAuth2TokenServiceRequest* owner); | 26 ProfileOAuth2TokenServiceRequest* owner); |
| 27 // Starts fetching an OAuth2 access token for |scopes|. It should be called | 27 // Starts fetching an OAuth2 access token for |account_id| and |scopes|. It |
| 28 // on the owner thread. | 28 // should be called on the owner thread. |
| 29 void Start(const OAuth2TokenService::ScopeSet& scopes); | 29 void Start( |
| 30 const std::string& account_id, |
| 31 const OAuth2TokenService::ScopeSet& scopes); |
| 30 // Stops the OAuth2 access token fetching. It should be called on the owner | 32 // Stops the OAuth2 access token fetching. It should be called on the owner |
| 31 // thread. | 33 // thread. |
| 32 void Stop(); | 34 void Stop(); |
| 33 | 35 |
| 34 // OAuth2TokenService::Consumer. It should be called on the UI thread. | 36 // OAuth2TokenService::Consumer. It should be called on the UI thread. |
| 35 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 37 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 36 const std::string& access_token, | 38 const std::string& access_token, |
| 37 const base::Time& expiration_time) OVERRIDE; | 39 const base::Time& expiration_time) OVERRIDE; |
| 38 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 40 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 39 const GoogleServiceAuthError& error) OVERRIDE; | 41 const GoogleServiceAuthError& error) OVERRIDE; |
| 40 | 42 |
| 41 private: | 43 private: |
| 42 friend class | 44 friend class |
| 43 base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>; | 45 base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>; |
| 44 | 46 |
| 45 // Note this can be destructed on the owner thread or on the UI thread, | 47 // Note this can be destructed on the owner thread or on the UI thread, |
| 46 // depending on the reference count. | 48 // depending on the reference count. |
| 47 virtual ~Core(); | 49 virtual ~Core(); |
| 48 | 50 |
| 49 // Starts an OAuth2TokenService::Request on the UI thread. | 51 // Starts an OAuth2TokenService::Request on the UI thread. |
| 50 void StartOnUIThread(const OAuth2TokenService::ScopeSet& scopes); | 52 void StartOnUIThread( |
| 53 const std::string& account_id, |
| 54 const OAuth2TokenService::ScopeSet& scopes); |
| 51 // Stops the OAuth2TokenService::Request on the UI thread. | 55 // Stops the OAuth2TokenService::Request on the UI thread. |
| 52 void StopOnUIThread(); | 56 void StopOnUIThread(); |
| 53 | 57 |
| 54 // Method posted to the owner thread to call back |owner_|. Note when this | 58 // Method posted to the owner thread to call back |owner_|. Note when this |
| 55 // posted task is actually running on the owner thread, it is possible that | 59 // posted task is actually running on the owner thread, it is possible that |
| 56 // |owner_| has been reset NULL. | 60 // |owner_| has been reset NULL. |
| 57 void InformOwnerOnGetTokenSuccess(std::string access_token, | 61 void InformOwnerOnGetTokenSuccess(std::string access_token, |
| 58 base::Time expiration_time); | 62 base::Time expiration_time); |
| 59 void InformOwnerOnGetTokenFailure(GoogleServiceAuthError error); | 63 void InformOwnerOnGetTokenFailure(GoogleServiceAuthError error); |
| 60 | 64 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 81 owner_(owner), | 85 owner_(owner), |
| 82 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 86 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 83 DCHECK(profile); | 87 DCHECK(profile); |
| 84 DCHECK(owner); | 88 DCHECK(owner); |
| 85 } | 89 } |
| 86 | 90 |
| 87 ProfileOAuth2TokenServiceRequest::Core::~Core() { | 91 ProfileOAuth2TokenServiceRequest::Core::~Core() { |
| 88 } | 92 } |
| 89 | 93 |
| 90 void ProfileOAuth2TokenServiceRequest::Core::Start( | 94 void ProfileOAuth2TokenServiceRequest::Core::Start( |
| 95 const std::string& account_id, |
| 91 const OAuth2TokenService::ScopeSet& scopes) { | 96 const OAuth2TokenService::ScopeSet& scopes) { |
| 92 DCHECK(owner_task_runner_->BelongsToCurrentThread()); | 97 DCHECK(owner_task_runner_->BelongsToCurrentThread()); |
| 93 | 98 |
| 94 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 99 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 95 StartOnUIThread(scopes); | 100 StartOnUIThread(account_id, scopes); |
| 96 } else { | 101 } else { |
| 97 content::BrowserThread::PostTask( | 102 content::BrowserThread::PostTask( |
| 98 content::BrowserThread::UI, | 103 content::BrowserThread::UI, |
| 99 FROM_HERE, | 104 FROM_HERE, |
| 100 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread, | 105 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread, |
| 101 this, scopes)); | 106 this, account_id, scopes)); |
| 102 } | 107 } |
| 103 } | 108 } |
| 104 | 109 |
| 105 void ProfileOAuth2TokenServiceRequest::Core::Stop() { | 110 void ProfileOAuth2TokenServiceRequest::Core::Stop() { |
| 106 DCHECK(owner_task_runner_->BelongsToCurrentThread()); | 111 DCHECK(owner_task_runner_->BelongsToCurrentThread()); |
| 107 | 112 |
| 108 // Detaches |owner_| from this instance so |owner_| will be called back only | 113 // Detaches |owner_| from this instance so |owner_| will be called back only |
| 109 // if |Stop()| has never been called. | 114 // if |Stop()| has never been called. |
| 110 owner_ = NULL; | 115 owner_ = NULL; |
| 111 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 116 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 112 StopOnUIThread(); | 117 StopOnUIThread(); |
| 113 } else { | 118 } else { |
| 114 content::BrowserThread::PostTask( | 119 content::BrowserThread::PostTask( |
| 115 content::BrowserThread::UI, | 120 content::BrowserThread::UI, |
| 116 FROM_HERE, | 121 FROM_HERE, |
| 117 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread, | 122 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread, |
| 118 this)); | 123 this)); |
| 119 } | 124 } |
| 120 } | 125 } |
| 121 | 126 |
| 122 void ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread() { | 127 void ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread() { |
| 123 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 128 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 124 request_.reset(); | 129 request_.reset(); |
| 125 } | 130 } |
| 126 | 131 |
| 127 void ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread( | 132 void ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread( |
| 133 const std::string& account_id, |
| 128 const OAuth2TokenService::ScopeSet& scopes) { | 134 const OAuth2TokenService::ScopeSet& scopes) { |
| 129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 135 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 130 | 136 |
| 131 ProfileOAuth2TokenService* service = | 137 ProfileOAuth2TokenService* service = |
| 132 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 138 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| 133 DCHECK(service); | 139 DCHECK(service); |
| 134 request_.reset(service->StartRequest(scopes, this).release()); | 140 request_.reset(service->StartRequest(account_id, scopes, this).release()); |
| 135 } | 141 } |
| 136 | 142 |
| 137 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenSuccess( | 143 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenSuccess( |
| 138 const OAuth2TokenService::Request* request, | 144 const OAuth2TokenService::Request* request, |
| 139 const std::string& access_token, | 145 const std::string& access_token, |
| 140 const base::Time& expiration_time) { | 146 const base::Time& expiration_time) { |
| 141 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 142 DCHECK_EQ(request_.get(), request); | 148 DCHECK_EQ(request_.get(), request); |
| 143 owner_task_runner_->PostTask(FROM_HERE, base::Bind( | 149 owner_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 144 &ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess, | 150 &ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 174 DCHECK(owner_task_runner_->BelongsToCurrentThread()); | 180 DCHECK(owner_task_runner_->BelongsToCurrentThread()); |
| 175 | 181 |
| 176 if (owner_) | 182 if (owner_) |
| 177 owner_->consumer_->OnGetTokenFailure(owner_, error); | 183 owner_->consumer_->OnGetTokenFailure(owner_, error); |
| 178 } | 184 } |
| 179 | 185 |
| 180 // static | 186 // static |
| 181 ProfileOAuth2TokenServiceRequest* | 187 ProfileOAuth2TokenServiceRequest* |
| 182 ProfileOAuth2TokenServiceRequest::CreateAndStart( | 188 ProfileOAuth2TokenServiceRequest::CreateAndStart( |
| 183 Profile* profile, | 189 Profile* profile, |
| 190 const std::string& account_id, |
| 184 const OAuth2TokenService::ScopeSet& scopes, | 191 const OAuth2TokenService::ScopeSet& scopes, |
| 185 OAuth2TokenService::Consumer* consumer) { | 192 OAuth2TokenService::Consumer* consumer) { |
| 186 return new ProfileOAuth2TokenServiceRequest(profile, scopes, consumer); | 193 return new ProfileOAuth2TokenServiceRequest(profile, account_id, scopes, |
| 194 consumer); |
| 187 } | 195 } |
| 188 | 196 |
| 189 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest( | 197 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest( |
| 190 Profile* profile, | 198 Profile* profile, |
| 199 const std::string& account_id, |
| 191 const OAuth2TokenService::ScopeSet& scopes, | 200 const OAuth2TokenService::ScopeSet& scopes, |
| 192 OAuth2TokenService::Consumer* consumer) | 201 OAuth2TokenService::Consumer* consumer) |
| 193 : consumer_(consumer), | 202 : consumer_(consumer), |
| 194 core_(new Core(profile, this)) { | 203 core_(new Core(profile, this)) { |
| 195 core_->Start(scopes); | 204 core_->Start(account_id, scopes); |
| 196 } | 205 } |
| 197 | 206 |
| 198 ProfileOAuth2TokenServiceRequest::~ProfileOAuth2TokenServiceRequest() { | 207 ProfileOAuth2TokenServiceRequest::~ProfileOAuth2TokenServiceRequest() { |
| 199 DCHECK(CalledOnValidThread()); | 208 DCHECK(CalledOnValidThread()); |
| 200 core_->Stop(); | 209 core_->Stop(); |
| 201 } | 210 } |
| OLD | NEW |