[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / media / base / serial_runner.cc
blob257cd910ae8a6e56ca1b9fdb5f56d6fd7c04c91b
1 // Copyright (c) 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.
5 #include "media/base/serial_runner.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/single_thread_task_runner.h"
13 namespace media {
15 // Converts a bound function accepting a Closure into a bound function
16 // accepting a PipelineStatusCB. Since closures have no way of reporting a
17 // status |status_cb| is executed with PIPELINE_OK.
18 static void RunBoundClosure(
19 const SerialRunner::BoundClosure& bound_closure,
20 const PipelineStatusCB& status_cb) {
21 bound_closure.Run(base::Bind(status_cb, PIPELINE_OK));
24 // Runs |status_cb| with |last_status| on |task_runner|.
25 static void RunOnTaskRunner(
26 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
27 const PipelineStatusCB& status_cb,
28 PipelineStatus last_status) {
29 // Force post to permit cancellation of a series in the scenario where all
30 // bound functions run on the same thread.
31 task_runner->PostTask(FROM_HERE, base::Bind(status_cb, last_status));
34 SerialRunner::Queue::Queue() {}
35 SerialRunner::Queue::~Queue() {}
37 void SerialRunner::Queue::Push(
38 const BoundClosure& bound_closure) {
39 bound_fns_.push(base::Bind(&RunBoundClosure, bound_closure));
42 void SerialRunner::Queue::Push(
43 const BoundPipelineStatusCB& bound_status_cb) {
44 bound_fns_.push(bound_status_cb);
47 SerialRunner::BoundPipelineStatusCB SerialRunner::Queue::Pop() {
48 BoundPipelineStatusCB bound_fn = bound_fns_.front();
49 bound_fns_.pop();
50 return bound_fn;
53 bool SerialRunner::Queue::empty() {
54 return bound_fns_.empty();
57 SerialRunner::SerialRunner(const Queue& bound_fns,
58 const PipelineStatusCB& done_cb)
59 : task_runner_(base::MessageLoopProxy::current()),
60 bound_fns_(bound_fns),
61 done_cb_(done_cb),
62 weak_factory_(this) {
63 // Respect both cancellation and calling stack guarantees for |done_cb|
64 // when empty.
65 if (bound_fns_.empty()) {
66 task_runner_->PostTask(FROM_HERE,
67 base::Bind(&SerialRunner::RunNextInSeries,
68 weak_factory_.GetWeakPtr(),
69 PIPELINE_OK));
70 return;
73 RunNextInSeries(PIPELINE_OK);
76 SerialRunner::~SerialRunner() {}
78 scoped_ptr<SerialRunner> SerialRunner::Run(
79 const Queue& bound_fns, const PipelineStatusCB& done_cb) {
80 scoped_ptr<SerialRunner> callback_series(
81 new SerialRunner(bound_fns, done_cb));
82 return callback_series.Pass();
85 void SerialRunner::RunNextInSeries(PipelineStatus last_status) {
86 DCHECK(task_runner_->BelongsToCurrentThread());
87 DCHECK(!done_cb_.is_null());
89 if (bound_fns_.empty() || last_status != PIPELINE_OK) {
90 base::ResetAndReturn(&done_cb_).Run(last_status);
91 return;
94 BoundPipelineStatusCB bound_fn = bound_fns_.Pop();
95 bound_fn.Run(base::Bind(
96 &RunOnTaskRunner,
97 task_runner_,
98 base::Bind(&SerialRunner::RunNextInSeries, weak_factory_.GetWeakPtr())));
101 } // namespace media