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 #ifndef MEDIA_BASE_SERIAL_RUNNER_H_ 6 #define MEDIA_BASE_SERIAL_RUNNER_H_ 7 8 #include <queue> 9 10 #include "base/callback.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "media/base/pipeline_status.h" 15 16 namespace base { 17 class MessageLoopProxy; 18 } 19 20 namespace media { 21 22 // Runs a series of bound functions accepting Closures or PipelineStatusCB. 23 // SerialRunner doesn't use regular Closure/PipelineStatusCBs as it late binds 24 // the completion callback as the series progresses. 25 class SerialRunner { 26 public: 27 typedef base::Callback<void(const base::Closure&)> BoundClosure; 28 typedef base::Callback<void(const PipelineStatusCB&)> BoundPipelineStatusCB; 29 30 // Serial queue of bound functions to run. 31 class Queue { 32 public: 33 Queue(); 34 ~Queue(); 35 36 void Push(const BoundClosure& bound_fn); 37 void Push(const BoundPipelineStatusCB& bound_fn); 38 39 private: 40 friend class SerialRunner; 41 42 BoundPipelineStatusCB Pop(); 43 bool empty(); 44 45 std::queue<BoundPipelineStatusCB> bound_fns_; 46 }; 47 48 // Executes the bound functions in series, executing |done_cb| when finished. 49 // 50 // All bound functions are executed on the thread that Run() is called on, 51 // including |done_cb|. 52 // 53 // Deleting the object will prevent execution of any unstarted bound 54 // functions, including |done_cb|. 55 static scoped_ptr<SerialRunner> Run( 56 const Queue& bound_fns, const PipelineStatusCB& done_cb); 57 58 private: 59 friend struct base::DefaultDeleter<SerialRunner>; 60 61 SerialRunner(const Queue& bound_fns, const PipelineStatusCB& done_cb); 62 ~SerialRunner(); 63 64 void RunNextInSeries(PipelineStatus last_status); 65 66 base::WeakPtrFactory<SerialRunner> weak_this_; 67 scoped_refptr<base::MessageLoopProxy> message_loop_; 68 Queue bound_fns_; 69 PipelineStatusCB done_cb_; 70 71 DISALLOW_COPY_AND_ASSIGN(SerialRunner); 72 }; 73 74 } // namespace media 75 76 #endif // MEDIA_BASE_SERIAL_RUNNER_H_ 77