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 #ifndef REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_
6 #define REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/single_thread_task_runner.h"
16 // A wrapper around |SingleThreadTaskRunner| that provides automatic lifetime
17 // management, by posting a caller-supplied task to the underlying task runner
18 // when no more references remain.
19 class AutoThreadTaskRunner
: public base::SingleThreadTaskRunner
{
21 // Constructs an instance of |AutoThreadTaskRunner| wrapping |task_runner|.
22 // |stop_task| is posted to |task_runner| when the last reference to
23 // the AutoThreadTaskRunner is dropped.
24 AutoThreadTaskRunner(scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
25 const base::Closure
& stop_task
);
27 // SingleThreadTaskRunner implementation
28 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
29 const base::Closure
& task
,
30 base::TimeDelta delay
) override
;
31 bool PostNonNestableDelayedTask(const tracked_objects::Location
& from_here
,
32 const base::Closure
& task
,
33 base::TimeDelta delay
) override
;
34 bool RunsTasksOnCurrentThread() const override
;
37 ~AutoThreadTaskRunner() override
;
39 // Task posted to |task_runner_| to notify the caller that it may be stopped.
40 base::Closure stop_task_
;
42 // The wrapped task runner.
43 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
45 DISALLOW_COPY_AND_ASSIGN(AutoThreadTaskRunner
);
48 } // namespace remoting
50 #endif // REMOTING_BASE_AUTO_THREAD_TASK_RUNNER_H_