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"
15 #include "base/time.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/Mutex.h"
19 #include "nsIThreadInternal.h"
29 class MessagePump
: public base::MessagePumpDefault
{
30 friend class DoWorkRunnable
;
33 explicit MessagePump(nsISerialEventTarget
* aEventTarget
);
35 // From base::MessagePump.
36 virtual void Run(base::MessagePump::Delegate
* aDelegate
) override
;
38 // From base::MessagePump.
39 virtual void ScheduleWork() override
;
41 // From base::MessagePump.
42 virtual void ScheduleWorkForNestedLoop() override
;
44 // From base::MessagePump.
45 virtual void ScheduleDelayedWork(
46 const base::TimeTicks
& aDelayedWorkTime
) override
;
48 virtual nsISerialEventTarget
* GetXPCOMThread() override
;
51 virtual ~MessagePump();
54 // Only called by DoWorkRunnable.
55 void DoDelayedWork(base::MessagePump::Delegate
* aDelegate
);
58 nsISerialEventTarget
* mEventTarget
;
60 // mDelayedWorkTimer and mEventTarget are set in Run() by this class or its
62 nsCOMPtr
<nsITimer
> mDelayedWorkTimer
;
65 // Only accessed by this class.
66 RefPtr
<DoWorkRunnable
> mDoWorkEvent
;
69 class MessagePumpForChildProcess final
: public MessagePump
{
71 MessagePumpForChildProcess() : MessagePump(nullptr), mFirstRun(true) {}
73 virtual void Run(base::MessagePump::Delegate
* aDelegate
) override
;
76 ~MessagePumpForChildProcess() = default;
81 class MessagePumpForNonMainThreads final
: public MessagePump
{
83 explicit MessagePumpForNonMainThreads(nsISerialEventTarget
* aEventTarget
)
84 : MessagePump(aEventTarget
) {}
86 virtual void Run(base::MessagePump::Delegate
* aDelegate
) override
;
89 ~MessagePumpForNonMainThreads() = default;
93 // Extends the TYPE_UI message pump to process xpcom events. Currently only
94 // implemented for Win.
95 class MessagePumpForNonMainUIThreads final
: public base::MessagePumpForUI
,
96 public nsIThreadObserver
{
98 // We don't want xpcom refing, chromium controls our lifetime via
99 // RefCountedThreadSafe.
100 NS_IMETHOD_(MozExternalRefCountType
) AddRef(void) override
{ return 2; }
101 NS_IMETHOD_(MozExternalRefCountType
) Release(void) override
{ return 1; }
102 NS_IMETHOD
QueryInterface(REFNSIID aIID
, void** aInstancePtr
) override
;
104 NS_DECL_NSITHREADOBSERVER
107 explicit MessagePumpForNonMainUIThreads(nsIEventTarget
* aEventTarget
)
108 : mInWait(false), mWaitLock("mInWait") {}
110 // The main run loop for this thread.
111 virtual void DoRunLoop() override
;
113 virtual nsISerialEventTarget
* GetXPCOMThread() override
{
114 return nullptr; // not sure what to do with this one
119 MutexAutoLock
lock(mWaitLock
);
124 MutexAutoLock
lock(mWaitLock
);
129 MutexAutoLock
lock(mWaitLock
);
134 ~MessagePumpForNonMainUIThreads() {}
136 bool mInWait
MOZ_GUARDED_BY(mWaitLock
);
137 mozilla::Mutex mWaitLock
;
139 #endif // defined(XP_WIN)
141 #if defined(MOZ_WIDGET_ANDROID)
143 * The MessagePumpForAndroidUI exists to enable IPDL in the Android UI thread.
144 * The Android UI thread event loop is controlled by Android. This prevents
145 * running an existing MessagePump implementation in the Android UI thread. In
146 * order to enable IPDL on the Android UI thread it is necessary to have a
147 * non-looping MessagePump. This class enables forwarding of nsIRunnables from
148 * MessageLoop::PostTask_Helper to the registered nsIEventTarget with out the
149 * need to control the event loop. The only member function that should be
150 * invoked is GetXPCOMThread. All other member functions will invoke MOZ_CRASH
152 class MessagePumpForAndroidUI
: public base::MessagePump
{
154 explicit MessagePumpForAndroidUI(nsISerialEventTarget
* aEventTarget
)
155 : mEventTarget(aEventTarget
) {}
157 virtual void Run(Delegate
* delegate
);
159 virtual void ScheduleWork();
160 virtual void ScheduleDelayedWork(const base::TimeTicks
& delayed_work_time
);
161 virtual nsISerialEventTarget
* GetXPCOMThread() { return mEventTarget
; }
164 ~MessagePumpForAndroidUI() {}
165 MessagePumpForAndroidUI() {}
167 nsISerialEventTarget
* mEventTarget
;
169 #endif // defined(MOZ_WIDGET_ANDROID)
171 } /* namespace ipc */
172 } /* namespace mozilla */
174 #endif /* __IPC_GLUE_MESSAGEPUMP_H__ */