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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: headless/public/util/deterministic_dispatcher.cc
diff --git a/headless/public/util/deterministic_dispatcher.cc b/headless/public/util/deterministic_dispatcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..70696eaeaa98a2736c7c31e7d927f0334328e719
--- /dev/null
+++ b/headless/public/util/deterministic_dispatcher.cc
@@ -0,0 +1,95 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "headless/public/util/deterministic_dispatcher.h"
+
+#include <utility>
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "headless/public/util/managed_dispatch_url_request_job.h"
+
+namespace headless {
+
+DeterministicDispatcher::DeterministicDispatcher(
+ scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner)
+ : io_thread_task_runner_(std::move(io_thread_task_runner)),
+ dispatch_pending_(false) {}
+
+DeterministicDispatcher::~DeterministicDispatcher() {}
+
+void DeterministicDispatcher::JobCreated(ManagedDispatchURLRequestJob* job) {
+ base::AutoLock lock(lock_);
+ pending_requests_.push_back(job);
+}
+
+void DeterministicDispatcher::JobKilled(ManagedDispatchURLRequestJob* job) {
+ base::AutoLock lock(lock_);
+ for (auto it = pending_requests_.begin(); it != pending_requests_.end();
+ it++) {
+ if (*it == job) {
+ pending_requests_.erase(it);
+ break;
+ }
+ }
+ ready_status_map_.erase(job);
+ // We rely on JobDeleted getting called to call MaybeDispatchJobLocked.
+}
+
+void DeterministicDispatcher::JobFailed(ManagedDispatchURLRequestJob* job,
+ net::Error error) {
+ base::AutoLock lock(lock_);
+ ready_status_map_[job] = error;
+ MaybeDispatchJobLocked();
+}
+
+void DeterministicDispatcher::DataReady(ManagedDispatchURLRequestJob* job) {
+ base::AutoLock lock(lock_);
+ ready_status_map_[job] = net::OK;
+ MaybeDispatchJobLocked();
+}
+
+void DeterministicDispatcher::JobDeleted(ManagedDispatchURLRequestJob* job) {
+ base::AutoLock lock(lock_);
+ MaybeDispatchJobLocked();
+}
+
+void DeterministicDispatcher::MaybeDispatchJobLocked() {
+ if (dispatch_pending_ || ready_status_map_.empty())
+ return;
+
+ dispatch_pending_ = true;
+ io_thread_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&DeterministicDispatcher::MaybeDispatchJobOnIOThreadTask,
+ base::Unretained(this)));
+}
+
+void DeterministicDispatcher::MaybeDispatchJobOnIOThreadTask() {
+ ManagedDispatchURLRequestJob* job;
+ net::Error job_status;
+
+ {
+ base::AutoLock lock(lock_);
+ CHECK(!pending_requests_.empty());
+ dispatch_pending_ = false;
+ job = pending_requests_.front();
+ StatusMap::const_iterator it = ready_status_map_.find(job);
+ // Bail out if the oldest job is not be ready for dispatch yet.
+ if (it == ready_status_map_.end())
+ return;
+
+ job_status = it->second;
+ ready_status_map_.erase(it);
+ pending_requests_.pop_front();
+ }
+
+ if (job_status == net::OK) {
+ job->OnHeadersComplete();
+ } else {
+ job->OnStartError(job_status);
+ }
+}
+
+} // namespace headless
« 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