| 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/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/oauth2_token_service.h" | 13 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 14 #include "chrome/browser/signin/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 OAuth2TokenServiceRequest::Core | 19 class ProfileOAuth2TokenServiceRequest::Core |
| 20 : public base::RefCountedThreadSafe<OAuth2TokenServiceRequest::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 OAuth2TokenServiceRequest* 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 |scopes|. It should be called |
| 28 // on the owner thread. | 28 // on the owner thread. |
| 29 void Start(const OAuth2TokenService::ScopeSet& scopes); | 29 void Start(const OAuth2TokenService::ScopeSet& scopes); |
| 30 // Stops the OAuth2 access token fetching. It should be called on the owner | 30 // Stops the OAuth2 access token fetching. It should be called on the owner |
| 31 // thread. | 31 // thread. |
| 32 void Stop(); | 32 void Stop(); |
| 33 | 33 |
| 34 // OAuth2TokenService::Consumer. It should be called on the UI thread. | 34 // OAuth2TokenService::Consumer. It should be called on the UI thread. |
| 35 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 35 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 36 const std::string& access_token, | 36 const std::string& access_token, |
| 37 const base::Time& expiration_time) OVERRIDE; | 37 const base::Time& expiration_time) OVERRIDE; |
| 38 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 38 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 39 const GoogleServiceAuthError& error) OVERRIDE; | 39 const GoogleServiceAuthError& error) OVERRIDE; |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 friend class base::RefCountedThreadSafe<OAuth2TokenServiceRequest::Core>; | 42 friend class |
| 43 base::RefCountedThreadSafe<ProfileOAuth2TokenServiceRequest::Core>; |
| 43 | 44 |
| 44 // Note this can be destructed on the owner thread or on the UI thread, | 45 // Note this can be destructed on the owner thread or on the UI thread, |
| 45 // depending on the reference count. | 46 // depending on the reference count. |
| 46 virtual ~Core(); | 47 virtual ~Core(); |
| 47 | 48 |
| 48 // Starts an OAuth2TokenService::Request on the UI thread. | 49 // Starts an OAuth2TokenService::Request on the UI thread. |
| 49 void StartOnUIThread(OAuth2TokenService::ScopeSet scopes); | 50 void StartOnUIThread(OAuth2TokenService::ScopeSet scopes); |
| 50 // Stops the OAuth2TokenService::Request on the UI thread. | 51 // Stops the OAuth2TokenService::Request on the UI thread. |
| 51 void StopOnUIThread(); | 52 void StopOnUIThread(); |
| 52 | 53 |
| 53 // Method posted to the owner thread to call back |owner_|. Note when this | 54 // Method posted to the owner thread to call back |owner_|. Note when this |
| 54 // posted task is actually running on the owner thread, it is possible that | 55 // posted task is actually running on the owner thread, it is possible that |
| 55 // |owner_| has been reset NULL. | 56 // |owner_| has been reset NULL. |
| 56 void InformOwnerOnGetTokenSuccess(std::string access_token, | 57 void InformOwnerOnGetTokenSuccess(std::string access_token, |
| 57 base::Time expiration_time); | 58 base::Time expiration_time); |
| 58 void InformOwnerOnGetTokenFailure(GoogleServiceAuthError error); | 59 void InformOwnerOnGetTokenFailure(GoogleServiceAuthError error); |
| 59 | 60 |
| 60 // The profile with which this instance was initialized. | 61 // The profile with which this instance was initialized. |
| 61 Profile* const profile_; | 62 Profile* const profile_; |
| 62 | 63 |
| 63 // The object to call back when fetching completes. |owner_| should be | 64 // The object to call back when fetching completes. |owner_| should be |
| 64 // called back only on the owner thread. | 65 // called back only on the owner thread. |
| 65 OAuth2TokenServiceRequest* owner_; | 66 ProfileOAuth2TokenServiceRequest* owner_; |
| 66 // Task runner on which |owner_| should be called back. | 67 // Task runner on which |owner_| should be called back. |
| 67 scoped_refptr<base::SingleThreadTaskRunner> owner_task_runner_; | 68 scoped_refptr<base::SingleThreadTaskRunner> owner_task_runner_; |
| 68 | 69 |
| 69 // OAuth2TokenService request for fetching OAuth2 access token; it should be | 70 // OAuth2TokenService request for fetching OAuth2 access token; it should be |
| 70 // created, reset and accessed only on the UI thread. | 71 // created, reset and accessed only on the UI thread. |
| 71 scoped_ptr<OAuth2TokenService::Request> request_; | 72 scoped_ptr<OAuth2TokenService::Request> request_; |
| 72 | 73 |
| 73 DISALLOW_COPY_AND_ASSIGN(Core); | 74 DISALLOW_COPY_AND_ASSIGN(Core); |
| 74 }; | 75 }; |
| 75 | 76 |
| 76 OAuth2TokenServiceRequest::Core::Core( | 77 ProfileOAuth2TokenServiceRequest::Core::Core( |
| 77 Profile* profile, | 78 Profile* profile, |
| 78 OAuth2TokenServiceRequest* owner) | 79 ProfileOAuth2TokenServiceRequest* owner) |
| 79 : profile_(profile), | 80 : profile_(profile), |
| 80 owner_(owner), | 81 owner_(owner), |
| 81 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 82 owner_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 82 DCHECK(profile); | 83 DCHECK(profile); |
| 83 DCHECK(owner); | 84 DCHECK(owner); |
| 84 } | 85 } |
| 85 | 86 |
| 86 OAuth2TokenServiceRequest::Core::~Core() { | 87 ProfileOAuth2TokenServiceRequest::Core::~Core() { |
| 87 } | 88 } |
| 88 | 89 |
| 89 void OAuth2TokenServiceRequest::Core::Start( | 90 void ProfileOAuth2TokenServiceRequest::Core::Start( |
| 90 const OAuth2TokenService::ScopeSet& scopes) { | 91 const OAuth2TokenService::ScopeSet& scopes) { |
| 91 DCHECK(owner_task_runner_->BelongsToCurrentThread()); | 92 DCHECK(owner_task_runner_->BelongsToCurrentThread()); |
| 92 | 93 |
| 93 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 94 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 94 StartOnUIThread(scopes); | 95 StartOnUIThread(scopes); |
| 95 } else { | 96 } else { |
| 96 content::BrowserThread::PostTask( | 97 content::BrowserThread::PostTask( |
| 97 content::BrowserThread::UI, | 98 content::BrowserThread::UI, |
| 98 FROM_HERE, | 99 FROM_HERE, |
| 99 base::Bind(&OAuth2TokenServiceRequest::Core::StartOnUIThread, | 100 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread, |
| 100 this, scopes)); | 101 this, scopes)); |
| 101 } | 102 } |
| 102 } | 103 } |
| 103 | 104 |
| 104 void OAuth2TokenServiceRequest::Core::Stop() { | 105 void ProfileOAuth2TokenServiceRequest::Core::Stop() { |
| 105 DCHECK(owner_task_runner_->BelongsToCurrentThread()); | 106 DCHECK(owner_task_runner_->BelongsToCurrentThread()); |
| 106 | 107 |
| 107 // Detaches |owner_| from this instance so |owner_| will be called back only | 108 // Detaches |owner_| from this instance so |owner_| will be called back only |
| 108 // if |Stop()| has never been called. | 109 // if |Stop()| has never been called. |
| 109 owner_ = NULL; | 110 owner_ = NULL; |
| 110 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 111 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 111 StopOnUIThread(); | 112 StopOnUIThread(); |
| 112 } else { | 113 } else { |
| 113 content::BrowserThread::PostTask( | 114 content::BrowserThread::PostTask( |
| 114 content::BrowserThread::UI, | 115 content::BrowserThread::UI, |
| 115 FROM_HERE, | 116 FROM_HERE, |
| 116 base::Bind(&OAuth2TokenServiceRequest::Core::StopOnUIThread, this)); | 117 base::Bind(&ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread, |
| 118 this)); |
| 117 } | 119 } |
| 118 } | 120 } |
| 119 | 121 |
| 120 void OAuth2TokenServiceRequest::Core::StopOnUIThread() { | 122 void ProfileOAuth2TokenServiceRequest::Core::StopOnUIThread() { |
| 121 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 123 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 122 request_.reset(); | 124 request_.reset(); |
| 123 } | 125 } |
| 124 | 126 |
| 125 void OAuth2TokenServiceRequest::Core::StartOnUIThread( | 127 void ProfileOAuth2TokenServiceRequest::Core::StartOnUIThread( |
| 126 OAuth2TokenService::ScopeSet scopes) { | 128 OAuth2TokenService::ScopeSet scopes) { |
| 127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 128 | 130 |
| 129 OAuth2TokenService* service = OAuth2TokenServiceFactory::GetForProfile( | 131 ProfileOAuth2TokenService* service = |
| 130 profile_); | 132 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
| 131 DCHECK(service); | 133 DCHECK(service); |
| 132 request_.reset(service->StartRequest(scopes, this).release()); | 134 request_.reset(service->StartRequest(scopes, this).release()); |
| 133 } | 135 } |
| 134 | 136 |
| 135 void OAuth2TokenServiceRequest::Core::OnGetTokenSuccess( | 137 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenSuccess( |
| 136 const OAuth2TokenService::Request* request, | 138 const OAuth2TokenService::Request* request, |
| 137 const std::string& access_token, | 139 const std::string& access_token, |
| 138 const base::Time& expiration_time) { | 140 const base::Time& expiration_time) { |
| 139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 141 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 140 DCHECK_EQ(request_.get(), request); | 142 DCHECK_EQ(request_.get(), request); |
| 141 owner_task_runner_->PostTask(FROM_HERE, base::Bind( | 143 owner_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 142 &OAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess, this, | 144 &ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess, |
| 143 access_token, expiration_time)); | 145 this, |
| 146 access_token, |
| 147 expiration_time)); |
| 144 request_.reset(); | 148 request_.reset(); |
| 145 } | 149 } |
| 146 | 150 |
| 147 void OAuth2TokenServiceRequest::Core::OnGetTokenFailure( | 151 void ProfileOAuth2TokenServiceRequest::Core::OnGetTokenFailure( |
| 148 const OAuth2TokenService::Request* request, | 152 const OAuth2TokenService::Request* request, |
| 149 const GoogleServiceAuthError& error) { | 153 const GoogleServiceAuthError& error) { |
| 150 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 154 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 151 DCHECK_EQ(request_.get(), request); | 155 DCHECK_EQ(request_.get(), request); |
| 152 owner_task_runner_->PostTask(FROM_HERE, base::Bind( | 156 owner_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 153 &OAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure, this, | 157 &ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure, |
| 158 this, |
| 154 error)); | 159 error)); |
| 155 request_.reset(); | 160 request_.reset(); |
| 156 } | 161 } |
| 157 | 162 |
| 158 void OAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess( | 163 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenSuccess( |
| 159 std::string access_token, | 164 std::string access_token, |
| 160 base::Time expiration_time) { | 165 base::Time expiration_time) { |
| 161 DCHECK(owner_task_runner_->BelongsToCurrentThread()); | 166 DCHECK(owner_task_runner_->BelongsToCurrentThread()); |
| 162 | 167 |
| 163 if (owner_) | 168 if (owner_) |
| 164 owner_->consumer_->OnGetTokenSuccess(owner_, access_token, expiration_time); | 169 owner_->consumer_->OnGetTokenSuccess(owner_, access_token, expiration_time); |
| 165 } | 170 } |
| 166 | 171 |
| 167 void OAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure( | 172 void ProfileOAuth2TokenServiceRequest::Core::InformOwnerOnGetTokenFailure( |
| 168 GoogleServiceAuthError error) { | 173 GoogleServiceAuthError error) { |
| 169 DCHECK(owner_task_runner_->BelongsToCurrentThread()); | 174 DCHECK(owner_task_runner_->BelongsToCurrentThread()); |
| 170 | 175 |
| 171 if (owner_) | 176 if (owner_) |
| 172 owner_->consumer_->OnGetTokenFailure(owner_, error); | 177 owner_->consumer_->OnGetTokenFailure(owner_, error); |
| 173 } | 178 } |
| 174 | 179 |
| 175 // static | 180 // static |
| 176 OAuth2TokenServiceRequest* OAuth2TokenServiceRequest::CreateAndStart( | 181 ProfileOAuth2TokenServiceRequest* |
| 177 Profile* profile, | 182 ProfileOAuth2TokenServiceRequest::CreateAndStart( |
| 178 const OAuth2TokenService::ScopeSet& scopes, | 183 Profile* profile, |
| 179 OAuth2TokenService::Consumer* consumer) { | 184 const OAuth2TokenService::ScopeSet& scopes, |
| 180 return new OAuth2TokenServiceRequest(profile, scopes, consumer); | 185 OAuth2TokenService::Consumer* consumer) { |
| 186 return new ProfileOAuth2TokenServiceRequest(profile, scopes, consumer); |
| 181 } | 187 } |
| 182 | 188 |
| 183 OAuth2TokenServiceRequest::OAuth2TokenServiceRequest( | 189 ProfileOAuth2TokenServiceRequest::ProfileOAuth2TokenServiceRequest( |
| 184 Profile* profile, | 190 Profile* profile, |
| 185 const OAuth2TokenService::ScopeSet& scopes, | 191 const OAuth2TokenService::ScopeSet& scopes, |
| 186 OAuth2TokenService::Consumer* consumer) | 192 OAuth2TokenService::Consumer* consumer) |
| 187 : consumer_(consumer), | 193 : consumer_(consumer), |
| 188 core_(new Core(profile, this)) { | 194 core_(new Core(profile, this)) { |
| 189 core_->Start(scopes); | 195 core_->Start(scopes); |
| 190 } | 196 } |
| 191 | 197 |
| 192 OAuth2TokenServiceRequest::~OAuth2TokenServiceRequest() { | 198 ProfileOAuth2TokenServiceRequest::~ProfileOAuth2TokenServiceRequest() { |
| 193 DCHECK(CalledOnValidThread()); | 199 DCHECK(CalledOnValidThread()); |
| 194 core_->Stop(); | 200 core_->Stop(); |
| 195 } | 201 } |
| OLD | NEW |