1 // Copyright 2018 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 "base/message_loop/message_loop.h"
6 
7 #include <utility>
8 
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/message_loop/message_pump_default.h"
13 #include "base/message_loop/message_pump_for_io.h"
14 #include "base/message_loop/message_pump_for_ui.h"
15 #include "base/optional.h"
16 #include "base/run_loop.h"
17 #include "base/task/sequence_manager/sequence_manager.h"
18 #include "base/task/sequence_manager/sequence_manager_impl.h"
19 #include "base/task/sequence_manager/task_queue.h"
20 #include "build/build_config.h"
21 
22 #if defined(OS_MACOSX)
23 #include "base/message_loop/message_pump_mac.h"
24 #endif
25 
26 namespace base {
27 
MessageLoop(MessagePumpType type)28 MessageLoop::MessageLoop(MessagePumpType type) : MessageLoop(type, nullptr) {
29   // For TYPE_CUSTOM you must either use
30   // MessageLoop(std::unique_ptr<MessagePump> pump) or
31   // MessageLoop::CreateUnbound()
32   DCHECK_NE(type_, MessagePumpType::CUSTOM);
33   BindToCurrentThread();
34 }
35 
MessageLoop(std::unique_ptr<MessagePump> pump)36 MessageLoop::MessageLoop(std::unique_ptr<MessagePump> pump)
37     : MessageLoop(MessagePumpType::CUSTOM, std::move(pump)) {
38   BindToCurrentThread();
39 }
40 
~MessageLoop()41 MessageLoop::~MessageLoop() {
42   // Clean up any unprocessed tasks, but take care: deleting a task could
43   // result in the addition of more tasks (e.g., via DeleteSoon). This is taken
44   // care by the queue as it will prevent further tasks from being posted to its
45   // associated TaskRunner instances.
46   default_task_queue_->ShutdownTaskQueue();
47 
48   // If |pump_| is non-null, this message loop has been bound and should be the
49   // current one on this thread. Otherwise, this loop is being destructed before
50   // it was bound to a thread, so a different message loop (or no loop at all)
51   // may be current.
52   DCHECK((pump_ && IsBoundToCurrentThread()) ||
53          (!pump_ && !IsBoundToCurrentThread()));
54 
55 // iOS just attaches to the loop, it doesn't Run it.
56 // TODO(stuartmorgan): Consider wiring up a Detach().
57 #if !defined(OS_IOS)
58   // There should be no active RunLoops on this thread, unless this MessageLoop
59   // isn't bound to the current thread (see other condition at the top of this
60   // method).
61   DCHECK((!pump_ && !IsBoundToCurrentThread()) ||
62          !RunLoop::IsRunningOnCurrentThread());
63 #endif  // !defined(OS_IOS)
64 }
65 
IsType(MessagePumpType type) const66 bool MessageLoop::IsType(MessagePumpType type) const {
67   return type_ == type;
68 }
69 
70 // TODO(gab): Migrate TaskObservers to RunLoop as part of separating concerns
71 // between MessageLoop and RunLoop and making MessageLoop a swappable
72 // implementation detail. http://crbug.com/703346
AddTaskObserver(TaskObserver * task_observer)73 void MessageLoop::AddTaskObserver(TaskObserver* task_observer) {
74   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
75   sequence_manager_->AddTaskObserver(task_observer);
76 }
77 
RemoveTaskObserver(TaskObserver * task_observer)78 void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) {
79   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
80   sequence_manager_->RemoveTaskObserver(task_observer);
81 }
82 
IsBoundToCurrentThread() const83 bool MessageLoop::IsBoundToCurrentThread() const {
84   return sequence_manager_->IsBoundToCurrentThread();
85 }
86 
IsIdleForTesting()87 bool MessageLoop::IsIdleForTesting() {
88   return sequence_manager_->IsIdleForTesting();
89 }
90 
91 //------------------------------------------------------------------------------
92 
93 // static
CreateUnbound(MessagePumpType type)94 std::unique_ptr<MessageLoop> MessageLoop::CreateUnbound(MessagePumpType type) {
95   return WrapUnique(new MessageLoop(type, nullptr));
96 }
97 
98 // static
CreateUnbound(std::unique_ptr<MessagePump> custom_pump)99 std::unique_ptr<MessageLoop> MessageLoop::CreateUnbound(
100     std::unique_ptr<MessagePump> custom_pump) {
101   return WrapUnique(
102       new MessageLoop(MessagePumpType::CUSTOM, std::move(custom_pump)));
103 }
104 
MessageLoop(MessagePumpType type,std::unique_ptr<MessagePump> custom_pump)105 MessageLoop::MessageLoop(MessagePumpType type,
106                          std::unique_ptr<MessagePump> custom_pump)
107     : sequence_manager_(
108           sequence_manager::internal::SequenceManagerImpl::CreateUnbound(
109               sequence_manager::SequenceManager::Settings::Builder()
110                   .SetMessagePumpType(type)
111                   .Build())),
112       default_task_queue_(CreateDefaultTaskQueue()),
113       type_(type),
114       custom_pump_(std::move(custom_pump)) {
115   // Bound in BindToCurrentThread();
116   DETACH_FROM_THREAD(bound_thread_checker_);
117 }
118 
119 scoped_refptr<sequence_manager::TaskQueue>
CreateDefaultTaskQueue()120 MessageLoop::CreateDefaultTaskQueue() {
121   auto default_task_queue = sequence_manager_->CreateTaskQueue(
122       sequence_manager::TaskQueue::Spec("default_tq"));
123   sequence_manager_->SetTaskRunner(default_task_queue->task_runner());
124   return default_task_queue;
125 }
126 
BindToCurrentThread()127 void MessageLoop::BindToCurrentThread() {
128   DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
129   thread_id_ = PlatformThread::CurrentId();
130 
131   DCHECK(!pump_);
132 
133   std::unique_ptr<MessagePump> pump = CreateMessagePump();
134   pump_ = pump.get();
135 
136   DCHECK(!MessageLoopCurrent::IsSet())
137       << "should only have one message loop per thread";
138 
139   sequence_manager_->BindToCurrentThread(std::move(pump));
140 }
141 
CreateMessagePump()142 std::unique_ptr<MessagePump> MessageLoop::CreateMessagePump() {
143   if (custom_pump_) {
144     return std::move(custom_pump_);
145   } else {
146     return MessagePump::Create(type_);
147   }
148 }
149 
SetTimerSlack(TimerSlack timer_slack)150 void MessageLoop::SetTimerSlack(TimerSlack timer_slack) {
151   sequence_manager_->SetTimerSlack(timer_slack);
152 }
153 
task_runner() const154 scoped_refptr<SingleThreadTaskRunner> MessageLoop::task_runner() const {
155   return sequence_manager_->GetTaskRunner();
156 }
157 
SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner)158 void MessageLoop::SetTaskRunner(
159     scoped_refptr<SingleThreadTaskRunner> task_runner) {
160   DCHECK(task_runner);
161   sequence_manager_->SetTaskRunner(task_runner);
162 }
163 
164 }  // namespace base
165