1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef __IPC_GLUE_MESSAGEPUMP_H__
8 #define __IPC_GLUE_MESSAGEPUMP_H__
10 #include "base/message_pump_default.h"
12 # include "base/message_pump_win.h"
13 #elif defined(XP_DARWIN)
14 # include "base/message_pump_mac.h"
17 #include "base/time.h"
18 #include "mozilla/Attributes.h"
19 #include "mozilla/Mutex.h"
21 #include "nsIThreadInternal.h"
31 class MessagePump
: public base::MessagePumpDefault
{
32 friend class DoWorkRunnable
;
35 explicit MessagePump(nsISerialEventTarget
* aEventTarget
);
37 // From base::MessagePump.
38 virtual void Run(base::MessagePump::Delegate
* aDelegate
) override
;
40 // From base::MessagePump.
41 virtual void ScheduleWork() override
;
43 // From base::MessagePump.
44 virtual void ScheduleWorkForNestedLoop() override
;
46 // From base::MessagePump.
47 virtual void ScheduleDelayedWork(
48 const base::TimeTicks
& aDelayedWorkTime
) override
;
50 virtual nsISerialEventTarget
* GetXPCOMThread() override
;
53 virtual ~MessagePump();
56 // Only called by DoWorkRunnable.
57 void DoDelayedWork(base::MessagePump::Delegate
* aDelegate
);
60 nsISerialEventTarget
* mEventTarget
;
62 // mDelayedWorkTimer and mEventTarget are set in Run() by this class or its
64 nsCOMPtr
<nsITimer
> mDelayedWorkTimer
;
67 // Only accessed by this class.
68 RefPtr
<DoWorkRunnable
> mDoWorkEvent
;
71 class MessagePumpForChildProcess final
: public MessagePump
{
73 MessagePumpForChildProcess() : MessagePump(nullptr), mFirstRun(true) {}
75 virtual void Run(base::MessagePump::Delegate
* aDelegate
) override
;
78 ~MessagePumpForChildProcess() = default;
83 class MessagePumpForNonMainThreads final
: public MessagePump
{
85 explicit MessagePumpForNonMainThreads(nsISerialEventTarget
* aEventTarget
)
86 : MessagePump(aEventTarget
) {}
88 virtual void Run(base::MessagePump::Delegate
* aDelegate
) override
;
91 ~MessagePumpForNonMainThreads() = default;
95 // Extends the TYPE_UI message pump to process xpcom events.
96 class MessagePumpForNonMainUIThreads final
: public base::MessagePumpForUI
,
97 public nsIThreadObserver
{
99 NS_DECL_ISUPPORTS_INHERITED
100 NS_DECL_NSITHREADOBSERVER
103 explicit MessagePumpForNonMainUIThreads(nsISerialEventTarget
* aEventTarget
)
104 : mInWait(false), mWaitLock("mInWait") {}
106 // The main run loop for this thread.
107 virtual void DoRunLoop() override
;
109 virtual nsISerialEventTarget
* GetXPCOMThread() override
{
110 return nullptr; // not sure what to do with this one
115 MutexAutoLock
lock(mWaitLock
);
120 MutexAutoLock
lock(mWaitLock
);
125 MutexAutoLock
lock(mWaitLock
);
130 ~MessagePumpForNonMainUIThreads() {}
132 bool mInWait
MOZ_GUARDED_BY(mWaitLock
);
133 mozilla::Mutex mWaitLock
;
135 #elif defined(XP_DARWIN)
136 // Extends the CFRunLoopBase message pump to process xpcom events. Based on
137 // MessagePumpNSRunLoop.
138 class MessagePumpForNonMainUIThreads final
139 : public base::MessagePumpCFRunLoopBase
,
140 public nsIThreadObserver
{
142 NS_DECL_ISUPPORTS_INHERITED
143 NS_DECL_NSITHREADOBSERVER
146 explicit MessagePumpForNonMainUIThreads(nsISerialEventTarget
* aEventTarget
);
148 void DoRun(base::MessagePump::Delegate
* aDelegate
) override
;
149 void Quit() override
;
151 nsISerialEventTarget
* GetXPCOMThread() override
{ return mEventTarget
; }
154 ~MessagePumpForNonMainUIThreads();
156 nsISerialEventTarget
* mEventTarget
;
158 // A source that doesn't do anything but provide something signalable
159 // attached to the run loop. This source will be signalled when Quit
160 // is called, to cause the loop to wake up so that it can stop.
161 CFRunLoopSourceRef quit_source_
;
163 // False after Quit is called.
166 DISALLOW_COPY_AND_ASSIGN(MessagePumpForNonMainUIThreads
);
168 #endif // defined(XP_DARWIN)
170 #if defined(MOZ_WIDGET_ANDROID)
172 * The MessagePumpForAndroidUI exists to enable IPDL in the Android UI thread.
173 * The Android UI thread event loop is controlled by Android. This prevents
174 * running an existing MessagePump implementation in the Android UI thread. In
175 * order to enable IPDL on the Android UI thread it is necessary to have a
176 * non-looping MessagePump. This class enables forwarding of nsIRunnables from
177 * MessageLoop::PostTask_Helper to the registered nsIEventTarget with out the
178 * need to control the event loop. The only member function that should be
179 * invoked is GetXPCOMThread. All other member functions will invoke MOZ_CRASH
181 class MessagePumpForAndroidUI
: public base::MessagePump
{
183 explicit MessagePumpForAndroidUI(nsISerialEventTarget
* aEventTarget
)
184 : mEventTarget(aEventTarget
) {}
186 virtual void Run(Delegate
* delegate
);
188 virtual void ScheduleWork();
189 virtual void ScheduleDelayedWork(const base::TimeTicks
& delayed_work_time
);
190 virtual nsISerialEventTarget
* GetXPCOMThread() { return mEventTarget
; }
193 ~MessagePumpForAndroidUI() {}
194 MessagePumpForAndroidUI() {}
196 nsISerialEventTarget
* mEventTarget
;
198 #endif // defined(MOZ_WIDGET_ANDROID)
200 } /* namespace ipc */
201 } /* namespace mozilla */
203 #endif /* __IPC_GLUE_MESSAGEPUMP_H__ */