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 BASE_MESSAGE_LOOP_MESSAGE_LOOP_TASK_RUNNER_H_
6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_TASK_RUNNER_H_
7 
8 #include "base/base_export.h"
9 #include "base/callback.h"
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/pending_task.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/platform_thread.h"
16 
17 namespace base {
18 namespace internal {
19 
20 class IncomingTaskQueue;
21 
22 // A stock implementation of SingleThreadTaskRunner that is created and managed
23 // by a MessageLoop. For now a MessageLoopTaskRunner can only be created as
24 // part of a MessageLoop.
25 class BASE_EXPORT MessageLoopTaskRunner : public SingleThreadTaskRunner {
26  public:
27   explicit MessageLoopTaskRunner(
28       scoped_refptr<IncomingTaskQueue> incoming_queue);
29 
30   // Initialize this message loop task runner on the current thread.
31   void BindToCurrentThread();
32 
33   // SingleThreadTaskRunner implementation
34   bool PostDelayedTask(const Location& from_here,
35                        OnceClosure task,
36                        TimeDelta delay) override;
37   bool PostNonNestableDelayedTask(const Location& from_here,
38                                   OnceClosure task,
39                                   TimeDelta delay) override;
40   bool RunsTasksInCurrentSequence() const override;
41 
42  private:
43   friend class RefCountedThreadSafe<MessageLoopTaskRunner>;
44   ~MessageLoopTaskRunner() override;
45 
46   // The incoming queue receiving all posted tasks.
47   scoped_refptr<IncomingTaskQueue> incoming_queue_;
48 
49   // ID of the thread |this| was created on.  Could be accessed on multiple
50   // threads, protected by |valid_thread_id_lock_|.
51   PlatformThreadId valid_thread_id_;
52   mutable Lock valid_thread_id_lock_;
53 
54   DISALLOW_COPY_AND_ASSIGN(MessageLoopTaskRunner);
55 };
56 
57 }  // namespace internal
58 }  // namespace base
59 
60 #endif  // BASE_MESSAGE_LOOP_MESSAGE_LOOP_TASK_RUNNER_H_
61