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

Side by Side Diff: headless/public/util/deterministic_dispatcher.cc

Issue 2260793002: Headless utility classes for making deterministic protocol handlers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing comments Created 4 years, 4 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "headless/public/util/deterministic_dispatcher.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "headless/public/util/managed_dispatch_url_request_job.h"
12
13 namespace headless {
14
15 DeterministicDispatcher::DeterministicDispatcher(
16 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner)
17 : io_thread_task_runner_(std::move(io_thread_task_runner)),
18 dispatch_pending_(false) {}
19
20 DeterministicDispatcher::~DeterministicDispatcher() {}
21
22 void DeterministicDispatcher::JobCreated(ManagedDispatchURLRequestJob* job) {
23 base::AutoLock lock(lock_);
24 pending_requests_.push_back(job);
25 }
26
27 void DeterministicDispatcher::JobKilled(ManagedDispatchURLRequestJob* job) {
28 base::AutoLock lock(lock_);
29 for (auto it = pending_requests_.begin(); it != pending_requests_.end();
30 it++) {
31 if (*it == job) {
32 pending_requests_.erase(it);
33 break;
34 }
35 }
36 ready_status_map_.erase(job);
37 // We rely on JobDeleted getting called to call MaybeDispatchJobLocked.
38 }
39
40 void DeterministicDispatcher::JobFailed(ManagedDispatchURLRequestJob* job,
41 net::Error error) {
42 base::AutoLock lock(lock_);
43 ready_status_map_[job] = error;
44 MaybeDispatchJobLocked();
45 }
46
47 void DeterministicDispatcher::DataReady(ManagedDispatchURLRequestJob* job) {
48 base::AutoLock lock(lock_);
49 ready_status_map_[job] = net::OK;
50 MaybeDispatchJobLocked();
51 }
52
53 void DeterministicDispatcher::JobDeleted(ManagedDispatchURLRequestJob* job) {
54 base::AutoLock lock(lock_);
55 MaybeDispatchJobLocked();
56 }
57
58 void DeterministicDispatcher::MaybeDispatchJobLocked() {
59 if (dispatch_pending_ || ready_status_map_.empty())
60 return;
61
62 dispatch_pending_ = true;
63 io_thread_task_runner_->PostTask(
64 FROM_HERE,
65 base::Bind(&DeterministicDispatcher::MaybeDispatchJobOnIOThreadTask,
66 base::Unretained(this)));
67 }
68
69 void DeterministicDispatcher::MaybeDispatchJobOnIOThreadTask() {
70 ManagedDispatchURLRequestJob* job;
71 net::Error job_status;
72
73 {
74 base::AutoLock lock(lock_);
75 CHECK(!pending_requests_.empty());
76 dispatch_pending_ = false;
77 job = pending_requests_.front();
78 StatusMap::const_iterator it = ready_status_map_.find(job);
79 // Bail out if the oldest job is not be ready for dispatch yet.
80 if (it == ready_status_map_.end())
81 return;
82
83 job_status = it->second;
84 ready_status_map_.erase(it);
85 pending_requests_.pop_front();
86 }
87
88 if (job_status == net::OK) {
89 job->OnHeadersComplete();
90 } else {
91 job->OnStartError(job_status);
92 }
93 }
94
95 } // namespace headless
OLDNEW
« no previous file with comments | « headless/public/util/deterministic_dispatcher.h ('k') | headless/public/util/deterministic_dispatcher_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698