OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "media/base/serial_runner.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 // Converts a bound function accepting a Closure into a bound function |
| 15 // accepting a PipelineStatusCB. Since closures have no way of reporting a |
| 16 // status |status_cb| is executed with PIPELINE_OK. |
| 17 static void RunBoundClosure( |
| 18 const SerialRunner::BoundClosure& bound_closure, |
| 19 const PipelineStatusCB& status_cb) { |
| 20 bound_closure.Run(base::Bind(status_cb, PIPELINE_OK)); |
| 21 } |
| 22 |
| 23 // Runs |status_cb| with |last_status| on |message_loop|, trampolining if |
| 24 // necessary. |
| 25 static void RunOnMessageLoop( |
| 26 const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| 27 const PipelineStatusCB& status_cb, |
| 28 PipelineStatus last_status) { |
| 29 if (!message_loop->BelongsToCurrentThread()) { |
| 30 message_loop->PostTask(FROM_HERE, base::Bind( |
| 31 &RunOnMessageLoop, message_loop, status_cb, last_status)); |
| 32 return; |
| 33 } |
| 34 status_cb.Run(last_status); |
| 35 } |
| 36 |
| 37 SerialRunner::Queue::Queue() {} |
| 38 SerialRunner::Queue::~Queue() {} |
| 39 |
| 40 void SerialRunner::Queue::Push( |
| 41 const BoundClosure& bound_closure) { |
| 42 bound_fns_.push(base::Bind(&RunBoundClosure, bound_closure)); |
| 43 } |
| 44 |
| 45 void SerialRunner::Queue::Push( |
| 46 const BoundPipelineStatusCB& bound_status_cb) { |
| 47 bound_fns_.push(bound_status_cb); |
| 48 } |
| 49 |
| 50 SerialRunner::BoundPipelineStatusCB SerialRunner::Queue::Pop() { |
| 51 BoundPipelineStatusCB bound_fn = bound_fns_.front(); |
| 52 bound_fns_.pop(); |
| 53 return bound_fn; |
| 54 } |
| 55 |
| 56 SerialRunner::SerialRunner( |
| 57 const Queue& bound_fns, const PipelineStatusCB& done_cb) |
| 58 : weak_this_(this), |
| 59 message_loop_(base::MessageLoopProxy::current()), |
| 60 bound_fns_(bound_fns), |
| 61 done_cb_(done_cb) { |
| 62 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 63 &SerialRunner::RunNextInSeries, weak_this_.GetWeakPtr(), |
| 64 PIPELINE_OK)); |
| 65 } |
| 66 |
| 67 SerialRunner::~SerialRunner() {} |
| 68 |
| 69 scoped_ptr<SerialRunner> SerialRunner::Run( |
| 70 const Queue& bound_fns, const PipelineStatusCB& done_cb) { |
| 71 scoped_ptr<SerialRunner> callback_series( |
| 72 new SerialRunner(bound_fns, done_cb)); |
| 73 return callback_series.Pass(); |
| 74 } |
| 75 |
| 76 void SerialRunner::RunNextInSeries(PipelineStatus last_status) { |
| 77 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 78 DCHECK(!done_cb_.is_null()); |
| 79 |
| 80 if (bound_fns_.empty() || last_status != PIPELINE_OK) { |
| 81 base::ResetAndReturn(&done_cb_).Run(last_status); |
| 82 return; |
| 83 } |
| 84 |
| 85 BoundPipelineStatusCB bound_fn = bound_fns_.Pop(); |
| 86 bound_fn.Run(base::Bind(&RunOnMessageLoop, message_loop_, base::Bind( |
| 87 &SerialRunner::RunNextInSeries, weak_this_.GetWeakPtr()))); |
| 88 } |
| 89 |
| 90 } // namespace media |
OLD | NEW |