Bug 1891340 - Part 1: Add parameters to customize the before and after icon tints...
[gecko.git] / ipc / glue / MessagePump.h
blob50b97efd79804d12613b390fc7a81aa53a71f041
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 #elif defined(XP_DARWIN)
14 # include "base/message_pump_mac.h"
15 #endif
17 #include "base/time.h"
18 #include "mozilla/Attributes.h"
19 #include "mozilla/Mutex.h"
20 #include "nsCOMPtr.h"
21 #include "nsIThreadInternal.h"
23 class nsIEventTarget;
24 class nsITimer;
26 namespace mozilla {
27 namespace ipc {
29 class DoWorkRunnable;
31 class MessagePump : public base::MessagePumpDefault {
32 friend class DoWorkRunnable;
34 public:
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;
52 protected:
53 virtual ~MessagePump();
55 private:
56 // Only called by DoWorkRunnable.
57 void DoDelayedWork(base::MessagePump::Delegate* aDelegate);
59 protected:
60 nsISerialEventTarget* mEventTarget;
62 // mDelayedWorkTimer and mEventTarget are set in Run() by this class or its
63 // subclasses.
64 nsCOMPtr<nsITimer> mDelayedWorkTimer;
66 private:
67 // Only accessed by this class.
68 RefPtr<DoWorkRunnable> mDoWorkEvent;
71 class MessagePumpForChildProcess final : public MessagePump {
72 public:
73 MessagePumpForChildProcess() : MessagePump(nullptr), mFirstRun(true) {}
75 virtual void Run(base::MessagePump::Delegate* aDelegate) override;
77 private:
78 ~MessagePumpForChildProcess() = default;
80 bool mFirstRun;
83 class MessagePumpForNonMainThreads final : public MessagePump {
84 public:
85 explicit MessagePumpForNonMainThreads(nsISerialEventTarget* aEventTarget)
86 : MessagePump(aEventTarget) {}
88 virtual void Run(base::MessagePump::Delegate* aDelegate) override;
90 private:
91 ~MessagePumpForNonMainThreads() = default;
94 #if defined(XP_WIN)
95 // Extends the TYPE_UI message pump to process xpcom events.
96 class MessagePumpForNonMainUIThreads final : public base::MessagePumpForUI,
97 public nsIThreadObserver {
98 public:
99 NS_DECL_ISUPPORTS_INHERITED
100 NS_DECL_NSITHREADOBSERVER
102 public:
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
113 protected:
114 void SetInWait() {
115 MutexAutoLock lock(mWaitLock);
116 mInWait = true;
119 void ClearInWait() {
120 MutexAutoLock lock(mWaitLock);
121 mInWait = false;
124 bool GetInWait() {
125 MutexAutoLock lock(mWaitLock);
126 return mInWait;
129 private:
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 {
141 public:
142 NS_DECL_ISUPPORTS_INHERITED
143 NS_DECL_NSITHREADOBSERVER
145 public:
146 explicit MessagePumpForNonMainUIThreads(nsISerialEventTarget* aEventTarget);
148 void DoRun(base::MessagePump::Delegate* aDelegate) override;
149 void Quit() override;
151 nsISerialEventTarget* GetXPCOMThread() override { return mEventTarget; }
153 private:
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.
164 bool keep_running_;
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 {
182 public:
183 explicit MessagePumpForAndroidUI(nsISerialEventTarget* aEventTarget)
184 : mEventTarget(aEventTarget) {}
186 virtual void Run(Delegate* delegate);
187 virtual void Quit();
188 virtual void ScheduleWork();
189 virtual void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time);
190 virtual nsISerialEventTarget* GetXPCOMThread() { return mEventTarget; }
192 private:
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__ */