Bug 1797433 [wpt PR 36660] - [anchor-position] Add tests for fixed position in multic...
[gecko.git] / ipc / glue / MessagePump.h
blob831c3e3f06e7b98af3465218af4badbe3cb15cc4
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"
11 #if defined(XP_WIN)
12 # include "base/message_pump_win.h"
13 #endif
15 #include "base/time.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/Mutex.h"
18 #include "nsCOMPtr.h"
19 #include "nsIThreadInternal.h"
21 class nsIEventTarget;
22 class nsITimer;
24 namespace mozilla {
25 namespace ipc {
27 class DoWorkRunnable;
29 class MessagePump : public base::MessagePumpDefault {
30 friend class DoWorkRunnable;
32 public:
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;
50 protected:
51 virtual ~MessagePump();
53 private:
54 // Only called by DoWorkRunnable.
55 void DoDelayedWork(base::MessagePump::Delegate* aDelegate);
57 protected:
58 nsISerialEventTarget* mEventTarget;
60 // mDelayedWorkTimer and mEventTarget are set in Run() by this class or its
61 // subclasses.
62 nsCOMPtr<nsITimer> mDelayedWorkTimer;
64 private:
65 // Only accessed by this class.
66 RefPtr<DoWorkRunnable> mDoWorkEvent;
69 class MessagePumpForChildProcess final : public MessagePump {
70 public:
71 MessagePumpForChildProcess() : MessagePump(nullptr), mFirstRun(true) {}
73 virtual void Run(base::MessagePump::Delegate* aDelegate) override;
75 private:
76 ~MessagePumpForChildProcess() = default;
78 bool mFirstRun;
81 class MessagePumpForNonMainThreads final : public MessagePump {
82 public:
83 explicit MessagePumpForNonMainThreads(nsISerialEventTarget* aEventTarget)
84 : MessagePump(aEventTarget) {}
86 virtual void Run(base::MessagePump::Delegate* aDelegate) override;
88 private:
89 ~MessagePumpForNonMainThreads() = default;
92 #if defined(XP_WIN)
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 {
97 public:
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
106 public:
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
117 protected:
118 void SetInWait() {
119 MutexAutoLock lock(mWaitLock);
120 mInWait = true;
123 void ClearInWait() {
124 MutexAutoLock lock(mWaitLock);
125 mInWait = false;
128 bool GetInWait() {
129 MutexAutoLock lock(mWaitLock);
130 return mInWait;
133 private:
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 {
153 public:
154 explicit MessagePumpForAndroidUI(nsISerialEventTarget* aEventTarget)
155 : mEventTarget(aEventTarget) {}
157 virtual void Run(Delegate* delegate);
158 virtual void Quit();
159 virtual void ScheduleWork();
160 virtual void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time);
161 virtual nsISerialEventTarget* GetXPCOMThread() { return mEventTarget; }
163 private:
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__ */