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