OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "headless/public/util/deterministic_dispatcher.h" | 5 #include "headless/public/util/deterministic_dispatcher.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "headless/public/util/managed_dispatch_url_request_job.h" | 11 #include "headless/public/util/managed_dispatch_url_request_job.h" |
12 | 12 |
13 namespace headless { | 13 namespace headless { |
14 | 14 |
15 DeterministicDispatcher::DeterministicDispatcher( | 15 DeterministicDispatcher::DeterministicDispatcher( |
16 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner) | 16 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner) |
17 : io_thread_task_runner_(std::move(io_thread_task_runner)), | 17 : io_thread_task_runner_(std::move(io_thread_task_runner)), |
18 dispatch_pending_(false) {} | 18 dispatch_pending_(false), |
| 19 weak_ptr_factory_(this) {} |
19 | 20 |
20 DeterministicDispatcher::~DeterministicDispatcher() {} | 21 DeterministicDispatcher::~DeterministicDispatcher() {} |
21 | 22 |
22 void DeterministicDispatcher::JobCreated(ManagedDispatchURLRequestJob* job) { | 23 void DeterministicDispatcher::JobCreated(ManagedDispatchURLRequestJob* job) { |
23 base::AutoLock lock(lock_); | 24 base::AutoLock lock(lock_); |
24 pending_requests_.push_back(job); | 25 pending_requests_.push_back(job); |
25 } | 26 } |
26 | 27 |
27 void DeterministicDispatcher::JobKilled(ManagedDispatchURLRequestJob* job) { | 28 void DeterministicDispatcher::JobKilled(ManagedDispatchURLRequestJob* job) { |
28 base::AutoLock lock(lock_); | 29 base::AutoLock lock(lock_); |
(...skipping 27 matching lines...) Expand all Loading... |
56 } | 57 } |
57 | 58 |
58 void DeterministicDispatcher::MaybeDispatchJobLocked() { | 59 void DeterministicDispatcher::MaybeDispatchJobLocked() { |
59 if (dispatch_pending_ || ready_status_map_.empty()) | 60 if (dispatch_pending_ || ready_status_map_.empty()) |
60 return; | 61 return; |
61 | 62 |
62 dispatch_pending_ = true; | 63 dispatch_pending_ = true; |
63 io_thread_task_runner_->PostTask( | 64 io_thread_task_runner_->PostTask( |
64 FROM_HERE, | 65 FROM_HERE, |
65 base::Bind(&DeterministicDispatcher::MaybeDispatchJobOnIOThreadTask, | 66 base::Bind(&DeterministicDispatcher::MaybeDispatchJobOnIOThreadTask, |
66 base::Unretained(this))); | 67 weak_ptr_factory_.GetWeakPtr())); |
67 } | 68 } |
68 | 69 |
69 void DeterministicDispatcher::MaybeDispatchJobOnIOThreadTask() { | 70 void DeterministicDispatcher::MaybeDispatchJobOnIOThreadTask() { |
70 ManagedDispatchURLRequestJob* job; | 71 ManagedDispatchURLRequestJob* job; |
71 net::Error job_status; | 72 net::Error job_status; |
72 | 73 |
73 { | 74 { |
74 base::AutoLock lock(lock_); | 75 base::AutoLock lock(lock_); |
75 CHECK(!pending_requests_.empty()); | |
76 dispatch_pending_ = false; | 76 dispatch_pending_ = false; |
| 77 // If the job got deleted, |pending_requests_| may be empty. |
| 78 if (pending_requests_.empty()) |
| 79 return; |
77 job = pending_requests_.front(); | 80 job = pending_requests_.front(); |
78 StatusMap::const_iterator it = ready_status_map_.find(job); | 81 StatusMap::const_iterator it = ready_status_map_.find(job); |
79 // Bail out if the oldest job is not be ready for dispatch yet. | 82 // Bail out if the oldest job is not be ready for dispatch yet. |
80 if (it == ready_status_map_.end()) | 83 if (it == ready_status_map_.end()) |
81 return; | 84 return; |
82 | 85 |
83 job_status = it->second; | 86 job_status = it->second; |
84 ready_status_map_.erase(it); | 87 ready_status_map_.erase(it); |
85 pending_requests_.pop_front(); | 88 pending_requests_.pop_front(); |
86 } | 89 } |
87 | 90 |
88 if (job_status == net::OK) { | 91 if (job_status == net::OK) { |
89 job->OnHeadersComplete(); | 92 job->OnHeadersComplete(); |
90 } else { | 93 } else { |
91 job->OnStartError(job_status); | 94 job->OnStartError(job_status); |
92 } | 95 } |
93 } | 96 } |
94 | 97 |
95 } // namespace headless | 98 } // namespace headless |
OLD | NEW |