• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2010 The Chromium Authors
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_pump.h"
6 
7 #include "base/check.h"
8 #include "base/message_loop/message_pump_default.h"
9 #include "base/message_loop/message_pump_for_io.h"
10 #include "base/message_loop/message_pump_for_ui.h"
11 #include "base/notreached.h"
12 #include "build/build_config.h"
13 
14 #if BUILDFLAG(IS_APPLE)
15 #include "base/message_loop/message_pump_mac.h"
16 #endif
17 
18 namespace base {
19 
20 namespace {
21 
22 MessagePump::MessagePumpFactory* message_pump_for_ui_factory_ = nullptr;
23 
24 }  // namespace
25 
26 MessagePump::MessagePump() = default;
27 
28 MessagePump::~MessagePump() = default;
29 
SetTimerSlack(TimerSlack)30 void MessagePump::SetTimerSlack(TimerSlack) {
31 }
32 
33 // static
OverrideMessagePumpForUIFactory(MessagePumpFactory * factory)34 void MessagePump::OverrideMessagePumpForUIFactory(MessagePumpFactory* factory) {
35   DCHECK(!message_pump_for_ui_factory_);
36   message_pump_for_ui_factory_ = factory;
37 }
38 
39 // static
IsMessagePumpForUIFactoryOveridden()40 bool MessagePump::IsMessagePumpForUIFactoryOveridden() {
41   return message_pump_for_ui_factory_ != nullptr;
42 }
43 
44 // static
Create(MessagePumpType type)45 std::unique_ptr<MessagePump> MessagePump::Create(MessagePumpType type) {
46   switch (type) {
47     case MessagePumpType::UI:
48       if (message_pump_for_ui_factory_)
49         return message_pump_for_ui_factory_();
50 #if BUILDFLAG(IS_APPLE)
51       return MessagePumpMac::Create();
52 #elif BUILDFLAG(IS_NACL) || BUILDFLAG(IS_AIX)
53       // Currently NaCl and AIX don't have a UI MessagePump.
54       // TODO(abarth): Figure out if we need this.
55       NOTREACHED();
56       return nullptr;
57 #else
58       return std::make_unique<MessagePumpForUI>();
59 #endif
60 
61     case MessagePumpType::IO:
62       return std::make_unique<MessagePumpForIO>();
63 
64 #if BUILDFLAG(IS_ANDROID)
65     case MessagePumpType::JAVA:
66       return std::make_unique<MessagePumpForUI>();
67 #endif
68 
69 #if BUILDFLAG(IS_APPLE)
70     case MessagePumpType::NS_RUNLOOP:
71       return std::make_unique<MessagePumpNSRunLoop>();
72 #endif
73 
74     case MessagePumpType::CUSTOM:
75       NOTREACHED();
76       return nullptr;
77 
78     case MessagePumpType::DEFAULT:
79 #if BUILDFLAG(IS_IOS)
80       // On iOS, a native runloop is always required to pump system work.
81       return std::make_unique<MessagePumpCFRunLoop>();
82 #else
83       return std::make_unique<MessagePumpDefault>();
84 #endif
85   }
86 }
87 
88 }  // namespace base
89