OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/ptr_util.h" |
15 #include "base/message_loop/message_pump_default.h" | 16 #include "base/message_loop/message_pump_default.h" |
16 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
17 #include "base/metrics/statistics_recorder.h" | 18 #include "base/metrics/statistics_recorder.h" |
18 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
19 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 20 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
20 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
21 #include "base/threading/thread_local.h" | 22 #include "base/threading/thread_local.h" |
22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
23 #include "base/trace_event/trace_event.h" | 24 #include "base/trace_event/trace_event.h" |
24 #include "base/tracked_objects.h" | 25 #include "base/tracked_objects.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 #elif defined(OS_POSIX) | 97 #elif defined(OS_POSIX) |
97 typedef MessagePumpLibevent MessagePumpForIO; | 98 typedef MessagePumpLibevent MessagePumpForIO; |
98 #endif | 99 #endif |
99 | 100 |
100 #if !defined(OS_NACL_SFI) | 101 #if !defined(OS_NACL_SFI) |
101 MessagePumpForIO* ToPumpIO(MessagePump* pump) { | 102 MessagePumpForIO* ToPumpIO(MessagePump* pump) { |
102 return static_cast<MessagePumpForIO*>(pump); | 103 return static_cast<MessagePumpForIO*>(pump); |
103 } | 104 } |
104 #endif // !defined(OS_NACL_SFI) | 105 #endif // !defined(OS_NACL_SFI) |
105 | 106 |
106 scoped_ptr<MessagePump> ReturnPump(scoped_ptr<MessagePump> pump) { | 107 std::unique_ptr<MessagePump> ReturnPump(std::unique_ptr<MessagePump> pump) { |
107 return pump; | 108 return pump; |
108 } | 109 } |
109 | 110 |
110 } // namespace | 111 } // namespace |
111 | 112 |
112 //------------------------------------------------------------------------------ | 113 //------------------------------------------------------------------------------ |
113 | 114 |
114 MessageLoop::TaskObserver::TaskObserver() { | 115 MessageLoop::TaskObserver::TaskObserver() { |
115 } | 116 } |
116 | 117 |
117 MessageLoop::TaskObserver::~TaskObserver() { | 118 MessageLoop::TaskObserver::~TaskObserver() { |
118 } | 119 } |
119 | 120 |
120 MessageLoop::DestructionObserver::~DestructionObserver() { | 121 MessageLoop::DestructionObserver::~DestructionObserver() { |
121 } | 122 } |
122 | 123 |
123 //------------------------------------------------------------------------------ | 124 //------------------------------------------------------------------------------ |
124 | 125 |
125 MessageLoop::MessageLoop(Type type) | 126 MessageLoop::MessageLoop(Type type) |
126 : MessageLoop(type, MessagePumpFactoryCallback()) { | 127 : MessageLoop(type, MessagePumpFactoryCallback()) { |
127 BindToCurrentThread(); | 128 BindToCurrentThread(); |
128 } | 129 } |
129 | 130 |
130 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) | 131 MessageLoop::MessageLoop(std::unique_ptr<MessagePump> pump) |
131 : MessageLoop(TYPE_CUSTOM, Bind(&ReturnPump, Passed(&pump))) { | 132 : MessageLoop(TYPE_CUSTOM, Bind(&ReturnPump, Passed(&pump))) { |
132 BindToCurrentThread(); | 133 BindToCurrentThread(); |
133 } | 134 } |
134 | 135 |
135 MessageLoop::~MessageLoop() { | 136 MessageLoop::~MessageLoop() { |
136 // If |pump_| is non-null, this message loop has been bound and should be the | 137 // If |pump_| is non-null, this message loop has been bound and should be the |
137 // current one on this thread. Otherwise, this loop is being destructed before | 138 // current one on this thread. Otherwise, this loop is being destructed before |
138 // it was bound to a thread, so a different message loop (or no loop at all) | 139 // it was bound to a thread, so a different message loop (or no loop at all) |
139 // may be current. | 140 // may be current. |
140 DCHECK((pump_ && current() == this) || (!pump_ && current() != this)); | 141 DCHECK((pump_ && current() == this) || (!pump_ && current() != this)); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // static | 200 // static |
200 bool MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { | 201 bool MessageLoop::InitMessagePumpForUIFactory(MessagePumpFactory* factory) { |
201 if (message_pump_for_ui_factory_) | 202 if (message_pump_for_ui_factory_) |
202 return false; | 203 return false; |
203 | 204 |
204 message_pump_for_ui_factory_ = factory; | 205 message_pump_for_ui_factory_ = factory; |
205 return true; | 206 return true; |
206 } | 207 } |
207 | 208 |
208 // static | 209 // static |
209 scoped_ptr<MessagePump> MessageLoop::CreateMessagePumpForType(Type type) { | 210 std::unique_ptr<MessagePump> MessageLoop::CreateMessagePumpForType(Type type) { |
210 // TODO(rvargas): Get rid of the OS guards. | 211 // TODO(rvargas): Get rid of the OS guards. |
211 #if defined(USE_GLIB) && !defined(OS_NACL) | 212 #if defined(USE_GLIB) && !defined(OS_NACL) |
212 typedef MessagePumpGlib MessagePumpForUI; | 213 typedef MessagePumpGlib MessagePumpForUI; |
213 #elif defined(OS_LINUX) && !defined(OS_NACL) | 214 #elif defined(OS_LINUX) && !defined(OS_NACL) |
214 typedef MessagePumpLibevent MessagePumpForUI; | 215 typedef MessagePumpLibevent MessagePumpForUI; |
215 #endif | 216 #endif |
216 | 217 |
217 #if defined(OS_IOS) || defined(OS_MACOSX) | 218 #if defined(OS_IOS) || defined(OS_MACOSX) |
218 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(MessagePumpMac::Create()) | 219 #define MESSAGE_PUMP_UI std::unique_ptr<MessagePump>(MessagePumpMac::Create()) |
219 #elif defined(OS_NACL) | 220 #elif defined(OS_NACL) |
220 // Currently NaCl doesn't have a UI MessageLoop. | 221 // Currently NaCl doesn't have a UI MessageLoop. |
221 // TODO(abarth): Figure out if we need this. | 222 // TODO(abarth): Figure out if we need this. |
222 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>() | 223 #define MESSAGE_PUMP_UI std::unique_ptr<MessagePump>() |
223 #else | 224 #else |
224 #define MESSAGE_PUMP_UI scoped_ptr<MessagePump>(new MessagePumpForUI()) | 225 #define MESSAGE_PUMP_UI std::unique_ptr<MessagePump>(new MessagePumpForUI()) |
225 #endif | 226 #endif |
226 | 227 |
227 #if defined(OS_MACOSX) | 228 #if defined(OS_MACOSX) |
228 // Use an OS native runloop on Mac to support timer coalescing. | 229 // Use an OS native runloop on Mac to support timer coalescing. |
229 #define MESSAGE_PUMP_DEFAULT \ | 230 #define MESSAGE_PUMP_DEFAULT \ |
230 scoped_ptr<MessagePump>(new MessagePumpCFRunLoop()) | 231 std::unique_ptr<MessagePump>(new MessagePumpCFRunLoop()) |
231 #else | 232 #else |
232 #define MESSAGE_PUMP_DEFAULT scoped_ptr<MessagePump>(new MessagePumpDefault()) | 233 #define MESSAGE_PUMP_DEFAULT \ |
| 234 std::unique_ptr<MessagePump>(new MessagePumpDefault()) |
233 #endif | 235 #endif |
234 | 236 |
235 if (type == MessageLoop::TYPE_UI) { | 237 if (type == MessageLoop::TYPE_UI) { |
236 if (message_pump_for_ui_factory_) | 238 if (message_pump_for_ui_factory_) |
237 return message_pump_for_ui_factory_(); | 239 return message_pump_for_ui_factory_(); |
238 return MESSAGE_PUMP_UI; | 240 return MESSAGE_PUMP_UI; |
239 } | 241 } |
240 if (type == MessageLoop::TYPE_IO) | 242 if (type == MessageLoop::TYPE_IO) |
241 return scoped_ptr<MessagePump>(new MessagePumpForIO()); | 243 return std::unique_ptr<MessagePump>(new MessagePumpForIO()); |
242 | 244 |
243 #if defined(OS_ANDROID) | 245 #if defined(OS_ANDROID) |
244 if (type == MessageLoop::TYPE_JAVA) | 246 if (type == MessageLoop::TYPE_JAVA) |
245 return scoped_ptr<MessagePump>(new MessagePumpForUI()); | 247 return std::unique_ptr<MessagePump>(new MessagePumpForUI()); |
246 #endif | 248 #endif |
247 | 249 |
248 DCHECK_EQ(MessageLoop::TYPE_DEFAULT, type); | 250 DCHECK_EQ(MessageLoop::TYPE_DEFAULT, type); |
249 return MESSAGE_PUMP_DEFAULT; | 251 return MESSAGE_PUMP_DEFAULT; |
250 } | 252 } |
251 | 253 |
252 void MessageLoop::AddDestructionObserver( | 254 void MessageLoop::AddDestructionObserver( |
253 DestructionObserver* destruction_observer) { | 255 DestructionObserver* destruction_observer) { |
254 DCHECK_EQ(this, current()); | 256 DCHECK_EQ(this, current()); |
255 destruction_observers_.AddObserver(destruction_observer); | 257 destruction_observers_.AddObserver(destruction_observer); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 | 370 |
369 bool MessageLoop::IsIdleForTesting() { | 371 bool MessageLoop::IsIdleForTesting() { |
370 // We only check the incoming queue, since we don't want to lock the work | 372 // We only check the incoming queue, since we don't want to lock the work |
371 // queue. | 373 // queue. |
372 return incoming_task_queue_->IsIdleForTesting(); | 374 return incoming_task_queue_->IsIdleForTesting(); |
373 } | 375 } |
374 | 376 |
375 //------------------------------------------------------------------------------ | 377 //------------------------------------------------------------------------------ |
376 | 378 |
377 // static | 379 // static |
378 scoped_ptr<MessageLoop> MessageLoop::CreateUnbound( | 380 std::unique_ptr<MessageLoop> MessageLoop::CreateUnbound( |
379 Type type, MessagePumpFactoryCallback pump_factory) { | 381 Type type, |
380 return make_scoped_ptr(new MessageLoop(type, pump_factory)); | 382 MessagePumpFactoryCallback pump_factory) { |
| 383 return WrapUnique(new MessageLoop(type, pump_factory)); |
381 } | 384 } |
382 | 385 |
383 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) | 386 MessageLoop::MessageLoop(Type type, MessagePumpFactoryCallback pump_factory) |
384 : type_(type), | 387 : type_(type), |
385 #if defined(OS_WIN) | 388 #if defined(OS_WIN) |
386 pending_high_res_tasks_(0), | 389 pending_high_res_tasks_(0), |
387 in_high_res_mode_(false), | 390 in_high_res_mode_(false), |
388 #endif | 391 #endif |
389 nestable_tasks_allowed_(true), | 392 nestable_tasks_allowed_(true), |
390 #if defined(OS_WIN) | 393 #if defined(OS_WIN) |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 const tracked_objects::Location& from_here, | 670 const tracked_objects::Location& from_here, |
668 void(*releaser)(const void*), | 671 void(*releaser)(const void*), |
669 const void* object) { | 672 const void* object) { |
670 PostNonNestableTask(from_here, Bind(releaser, object)); | 673 PostNonNestableTask(from_here, Bind(releaser, object)); |
671 } | 674 } |
672 | 675 |
673 #if !defined(OS_NACL) | 676 #if !defined(OS_NACL) |
674 //------------------------------------------------------------------------------ | 677 //------------------------------------------------------------------------------ |
675 // MessageLoopForUI | 678 // MessageLoopForUI |
676 | 679 |
677 MessageLoopForUI::MessageLoopForUI(scoped_ptr<MessagePump> pump) | 680 MessageLoopForUI::MessageLoopForUI(std::unique_ptr<MessagePump> pump) |
678 : MessageLoop(TYPE_UI, Bind(&ReturnPump, Passed(&pump))) { | 681 : MessageLoop(TYPE_UI, Bind(&ReturnPump, Passed(&pump))) {} |
679 } | |
680 | 682 |
681 #if defined(OS_ANDROID) | 683 #if defined(OS_ANDROID) |
682 void MessageLoopForUI::Start() { | 684 void MessageLoopForUI::Start() { |
683 // No Histogram support for UI message loop as it is managed by Java side | 685 // No Histogram support for UI message loop as it is managed by Java side |
684 static_cast<MessagePumpForUI*>(pump_.get())->Start(this); | 686 static_cast<MessagePumpForUI*>(pump_.get())->Start(this); |
685 } | 687 } |
686 #endif | 688 #endif |
687 | 689 |
688 #if defined(OS_IOS) | 690 #if defined(OS_IOS) |
689 void MessageLoopForUI::Attach() { | 691 void MessageLoopForUI::Attach() { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 persistent, | 748 persistent, |
747 mode, | 749 mode, |
748 controller, | 750 controller, |
749 delegate); | 751 delegate); |
750 } | 752 } |
751 #endif | 753 #endif |
752 | 754 |
753 #endif // !defined(OS_NACL_SFI) | 755 #endif // !defined(OS_NACL_SFI) |
754 | 756 |
755 } // namespace base | 757 } // namespace base |
OLD | NEW |