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

Side by Side Diff: net/cert_net/cert_net_fetcher_impl.cc

Issue 2894863002: Rename TaskRunner::RunsTasksOnCurrentThread() in //net (Closed)
Patch Set: fixed build error Created 3 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
« no previous file with comments | « net/cert/cert_database_mac.cc ('k') | net/disk_cache/blockfile/backend_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 // Overview 5 // Overview
6 // 6 //
7 // The main entry point is CertNetFetcherImpl. This is an implementation of 7 // The main entry point is CertNetFetcherImpl. This is an implementation of
8 // CertNetFetcher that provides a service for fetching network requests. 8 // CertNetFetcher that provides a service for fetching network requests.
9 // 9 //
10 // The interface for CertNetFetcher is synchronous, however allows 10 // The interface for CertNetFetcher is synchronous, however allows
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 // RequestCore tracks an outstanding call to Fetch(). It is 198 // RequestCore tracks an outstanding call to Fetch(). It is
199 // reference-counted for ease of sharing between threads. 199 // reference-counted for ease of sharing between threads.
200 class RequestCore : public base::RefCountedThreadSafe<RequestCore> { 200 class RequestCore : public base::RefCountedThreadSafe<RequestCore> {
201 public: 201 public:
202 explicit RequestCore(scoped_refptr<base::SingleThreadTaskRunner> task_runner) 202 explicit RequestCore(scoped_refptr<base::SingleThreadTaskRunner> task_runner)
203 : completion_event_(base::WaitableEvent::ResetPolicy::MANUAL, 203 : completion_event_(base::WaitableEvent::ResetPolicy::MANUAL,
204 base::WaitableEvent::InitialState::NOT_SIGNALED), 204 base::WaitableEvent::InitialState::NOT_SIGNALED),
205 task_runner_(std::move(task_runner)) {} 205 task_runner_(std::move(task_runner)) {}
206 206
207 void AttachedToJob(Job* job) { 207 void AttachedToJob(Job* job) {
208 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 208 DCHECK(task_runner_->RunsTasksInCurrentSequence());
209 DCHECK(!job_); 209 DCHECK(!job_);
210 // Requests should not be attached to jobs after they have been signalled 210 // Requests should not be attached to jobs after they have been signalled
211 // with a cancellation error (which happens via either Cancel() or 211 // with a cancellation error (which happens via either Cancel() or
212 // SignalImmediateError()). 212 // SignalImmediateError()).
213 DCHECK_NE(error_, ERR_ABORTED); 213 DCHECK_NE(error_, ERR_ABORTED);
214 job_ = job; 214 job_ = job;
215 } 215 }
216 216
217 void OnJobCompleted(Job* job, 217 void OnJobCompleted(Job* job,
218 Error error, 218 Error error,
219 const std::vector<uint8_t>& response_body) { 219 const std::vector<uint8_t>& response_body) {
220 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 220 DCHECK(task_runner_->RunsTasksInCurrentSequence());
221 221
222 DCHECK_EQ(job_, job); 222 DCHECK_EQ(job_, job);
223 job_ = nullptr; 223 job_ = nullptr;
224 224
225 error_ = error; 225 error_ = error;
226 bytes_ = response_body; 226 bytes_ = response_body;
227 completion_event_.Signal(); 227 completion_event_.Signal();
228 } 228 }
229 229
230 // Detaches this request from its job (if it is attached to any) and 230 // Detaches this request from its job (if it is attached to any) and
231 // signals completion with ERR_ABORTED. Can be called from any thread. 231 // signals completion with ERR_ABORTED. Can be called from any thread.
232 void CancelJob(); 232 void CancelJob();
233 233
234 // Can be used to signal that an error was encountered before the request was 234 // Can be used to signal that an error was encountered before the request was
235 // attached to a job. Can be called from any thread. 235 // attached to a job. Can be called from any thread.
236 void SignalImmediateError(); 236 void SignalImmediateError();
237 237
238 // Should only be called once. 238 // Should only be called once.
239 void WaitForResult(Error* error, std::vector<uint8_t>* bytes) { 239 void WaitForResult(Error* error, std::vector<uint8_t>* bytes) {
240 DCHECK(!task_runner_->RunsTasksOnCurrentThread()); 240 DCHECK(!task_runner_->RunsTasksInCurrentSequence());
241 241
242 completion_event_.Wait(); 242 completion_event_.Wait();
243 *bytes = std::move(bytes_); 243 *bytes = std::move(bytes_);
244 *error = error_; 244 *error = error_;
245 245
246 error_ = ERR_UNEXPECTED; 246 error_ = ERR_UNEXPECTED;
247 } 247 }
248 248
249 private: 249 private:
250 friend class base::RefCountedThreadSafe<RequestCore>; 250 friend class base::RefCountedThreadSafe<RequestCore>;
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 // also used for notifying a failure to start the URLRequest. 377 // also used for notifying a failure to start the URLRequest.
378 base::OneShotTimer timer_; 378 base::OneShotTimer timer_;
379 379
380 // Non-owned pointer to the AsyncCertNetFetcherImpl that created this job. 380 // Non-owned pointer to the AsyncCertNetFetcherImpl that created this job.
381 AsyncCertNetFetcherImpl* parent_; 381 AsyncCertNetFetcherImpl* parent_;
382 382
383 DISALLOW_COPY_AND_ASSIGN(Job); 383 DISALLOW_COPY_AND_ASSIGN(Job);
384 }; 384 };
385 385
386 void RequestCore::CancelJob() { 386 void RequestCore::CancelJob() {
387 if (!task_runner_->RunsTasksOnCurrentThread()) { 387 if (!task_runner_->RunsTasksInCurrentSequence()) {
388 task_runner_->PostTask(FROM_HERE, 388 task_runner_->PostTask(FROM_HERE,
389 base::Bind(&RequestCore::CancelJob, this)); 389 base::Bind(&RequestCore::CancelJob, this));
390 return; 390 return;
391 } 391 }
392 392
393 if (job_) { 393 if (job_) {
394 auto* job = job_; 394 auto* job = job_;
395 job_ = nullptr; 395 job_ = nullptr;
396 job->DetachRequest(this); 396 job->DetachRequest(this);
397 } 397 }
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 private: 704 private:
705 scoped_refptr<RequestCore> core_; 705 scoped_refptr<RequestCore> core_;
706 }; 706 };
707 707
708 class CertNetFetcherImpl : public CertNetFetcher { 708 class CertNetFetcherImpl : public CertNetFetcher {
709 public: 709 public:
710 explicit CertNetFetcherImpl(URLRequestContext* context) 710 explicit CertNetFetcherImpl(URLRequestContext* context)
711 : task_runner_(base::ThreadTaskRunnerHandle::Get()), context_(context) {} 711 : task_runner_(base::ThreadTaskRunnerHandle::Get()), context_(context) {}
712 712
713 void Shutdown() override { 713 void Shutdown() override {
714 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 714 DCHECK(task_runner_->RunsTasksInCurrentSequence());
715 if (impl_) { 715 if (impl_) {
716 impl_->Shutdown(); 716 impl_->Shutdown();
717 impl_.reset(); 717 impl_.reset();
718 } 718 }
719 context_ = nullptr; 719 context_ = nullptr;
720 } 720 }
721 721
722 std::unique_ptr<Request> FetchCaIssuers(const GURL& url, 722 std::unique_ptr<Request> FetchCaIssuers(const GURL& url,
723 int timeout_milliseconds, 723 int timeout_milliseconds,
724 int max_response_bytes) override { 724 int max_response_bytes) override {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 return DoFetch(std::move(request_params)); 762 return DoFetch(std::move(request_params));
763 } 763 }
764 764
765 private: 765 private:
766 ~CertNetFetcherImpl() override { 766 ~CertNetFetcherImpl() override {
767 // The fetcher must be shutdown (at which point |context_| will be set to 767 // The fetcher must be shutdown (at which point |context_| will be set to
768 // null) before destruction. 768 // null) before destruction.
769 DCHECK(!context_); 769 DCHECK(!context_);
770 } 770 }
771 771
772 void DoFetchOnNetworkThread(std::unique_ptr<RequestParams> request_params, 772 void DoFetchOnNetworkSequence(std::unique_ptr<RequestParams> request_params,
773 scoped_refptr<RequestCore> request) { 773 scoped_refptr<RequestCore> request) {
774 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 774 DCHECK(task_runner_->RunsTasksInCurrentSequence());
775 775
776 if (!context_) { 776 if (!context_) {
777 // The fetcher might have been shutdown between when this task was posted 777 // The fetcher might have been shutdown between when this task was posted
778 // and when it is running. In this case, signal the request and do not 778 // and when it is running. In this case, signal the request and do not
779 // start a network request. 779 // start a network request.
780 request->SignalImmediateError(); 780 request->SignalImmediateError();
781 return; 781 return;
782 } 782 }
783 783
784 if (!impl_) { 784 if (!impl_) {
785 impl_.reset(new AsyncCertNetFetcherImpl(context_)); 785 impl_.reset(new AsyncCertNetFetcherImpl(context_));
786 } 786 }
787 787
788 impl_->Fetch(std::move(request_params), request); 788 impl_->Fetch(std::move(request_params), request);
789 } 789 }
790 790
791 std::unique_ptr<Request> DoFetch( 791 std::unique_ptr<Request> DoFetch(
792 std::unique_ptr<RequestParams> request_params) { 792 std::unique_ptr<RequestParams> request_params) {
793 scoped_refptr<RequestCore> request_core = new RequestCore(task_runner_); 793 scoped_refptr<RequestCore> request_core = new RequestCore(task_runner_);
794 794
795 // If the fetcher has already been shutdown, DoFetchOnNetworkThread will 795 // If the fetcher has already been shutdown, DoFetchOnNetworkSequence will
796 // signal the request with an error. However, if the fetcher shuts down 796 // signal the request with an error. However, if the fetcher shuts down
797 // before DoFetchOnNetworkThread runs and PostTask still returns true, then 797 // before DoFetchOnNetworkSequence runs and PostTask still returns true,
798 // the request will hang (that is, WaitForResult will not return). 798 // then the request will hang (that is, WaitForResult will not return).
799 if (!task_runner_->PostTask( 799 if (!task_runner_->PostTask(
800 FROM_HERE, 800 FROM_HERE,
801 base::Bind(&CertNetFetcherImpl::DoFetchOnNetworkThread, this, 801 base::Bind(&CertNetFetcherImpl::DoFetchOnNetworkSequence, this,
802 base::Passed(&request_params), request_core))) { 802 base::Passed(&request_params), request_core))) {
803 request_core->SignalImmediateError(); 803 request_core->SignalImmediateError();
804 } 804 }
805 805
806 return base::MakeUnique<CertNetFetcherRequestImpl>(std::move(request_core)); 806 return base::MakeUnique<CertNetFetcherRequestImpl>(std::move(request_core));
807 } 807 }
808 808
809 private: 809 private:
810 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 810 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
811 // Not owned. |context_| must stay valid until Shutdown() is called. 811 // Not owned. |context_| must stay valid until Shutdown() is called.
812 URLRequestContext* context_ = nullptr; 812 URLRequestContext* context_ = nullptr;
813 std::unique_ptr<AsyncCertNetFetcherImpl> impl_; 813 std::unique_ptr<AsyncCertNetFetcherImpl> impl_;
814 }; 814 };
815 815
816 } // namespace 816 } // namespace
817 817
818 scoped_refptr<CertNetFetcher> CreateCertNetFetcher(URLRequestContext* context) { 818 scoped_refptr<CertNetFetcher> CreateCertNetFetcher(URLRequestContext* context) {
819 return make_scoped_refptr(new CertNetFetcherImpl(context)); 819 return make_scoped_refptr(new CertNetFetcherImpl(context));
820 } 820 }
821 821
822 } // namespace net 822 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/cert_database_mac.cc ('k') | net/disk_cache/blockfile/backend_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698