summaryrefslogtreecommitdiffstats
path: root/ipc/glue/MessagePump.h
blob: 50b97efd79804d12613b390fc7a81aa53a71f041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef __IPC_GLUE_MESSAGEPUMP_H__
#define __IPC_GLUE_MESSAGEPUMP_H__

#include "base/message_pump_default.h"
#if defined(XP_WIN)
#  include "base/message_pump_win.h"
#elif defined(XP_DARWIN)
#  include "base/message_pump_mac.h"
#endif

#include "base/time.h"
#include "mozilla/Attributes.h"
#include "mozilla/Mutex.h"
#include "nsCOMPtr.h"
#include "nsIThreadInternal.h"

class nsIEventTarget;
class nsITimer;

namespace mozilla {
namespace ipc {

class DoWorkRunnable;

class MessagePump : public base::MessagePumpDefault {
  friend class DoWorkRunnable;

 public:
  explicit MessagePump(nsISerialEventTarget* aEventTarget);

  // From base::MessagePump.
  virtual void Run(base::MessagePump::Delegate* aDelegate) override;

  // From base::MessagePump.
  virtual void ScheduleWork() override;

  // From base::MessagePump.
  virtual void ScheduleWorkForNestedLoop() override;

  // From base::MessagePump.
  virtual void ScheduleDelayedWork(
      const base::TimeTicks& aDelayedWorkTime) override;

  virtual nsISerialEventTarget* GetXPCOMThread() override;

 protected:
  virtual ~MessagePump();

 private:
  // Only called by DoWorkRunnable.
  void DoDelayedWork(base::MessagePump::Delegate* aDelegate);

 protected:
  nsISerialEventTarget* mEventTarget;

  // mDelayedWorkTimer and mEventTarget are set in Run() by this class or its
  // subclasses.
  nsCOMPtr<nsITimer> mDelayedWorkTimer;

 private:
  // Only accessed by this class.
  RefPtr<DoWorkRunnable> mDoWorkEvent;
};

class MessagePumpForChildProcess final : public MessagePump {
 public:
  MessagePumpForChildProcess() : MessagePump(nullptr), mFirstRun(true) {}

  virtual void Run(base::MessagePump::Delegate* aDelegate) override;

 private:
  ~MessagePumpForChildProcess() = default;

  bool mFirstRun;
};

class MessagePumpForNonMainThreads final : public MessagePump {
 public:
  explicit MessagePumpForNonMainThreads(nsISerialEventTarget* aEventTarget)
      : MessagePump(aEventTarget) {}

  virtual void Run(base::MessagePump::Delegate* aDelegate) override;

 private:
  ~MessagePumpForNonMainThreads() = default;
};

#if defined(XP_WIN)
// Extends the TYPE_UI message pump to process xpcom events.
class MessagePumpForNonMainUIThreads final : public base::MessagePumpForUI,
                                             public nsIThreadObserver {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_NSITHREADOBSERVER

 public:
  explicit MessagePumpForNonMainUIThreads(nsISerialEventTarget* aEventTarget)
      : mInWait(false), mWaitLock("mInWait") {}

  // The main run loop for this thread.
  virtual void DoRunLoop() override;

  virtual nsISerialEventTarget* GetXPCOMThread() override {
    return nullptr;  // not sure what to do with this one
  }

 protected:
  void SetInWait() {
    MutexAutoLock lock(mWaitLock);
    mInWait = true;
  }

  void ClearInWait() {
    MutexAutoLock lock(mWaitLock);
    mInWait = false;
  }

  bool GetInWait() {
    MutexAutoLock lock(mWaitLock);
    return mInWait;
  }

 private:
  ~MessagePumpForNonMainUIThreads() {}

  bool mInWait MOZ_GUARDED_BY(mWaitLock);
  mozilla::Mutex mWaitLock;
};
#elif defined(XP_DARWIN)
// Extends the CFRunLoopBase message pump to process xpcom events. Based on
// MessagePumpNSRunLoop.
class MessagePumpForNonMainUIThreads final
    : public base::MessagePumpCFRunLoopBase,
      public nsIThreadObserver {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_NSITHREADOBSERVER

 public:
  explicit MessagePumpForNonMainUIThreads(nsISerialEventTarget* aEventTarget);

  void DoRun(base::MessagePump::Delegate* aDelegate) override;
  void Quit() override;

  nsISerialEventTarget* GetXPCOMThread() override { return mEventTarget; }

 private:
  ~MessagePumpForNonMainUIThreads();

  nsISerialEventTarget* mEventTarget;

  // A source that doesn't do anything but provide something signalable
  // attached to the run loop.  This source will be signalled when Quit
  // is called, to cause the loop to wake up so that it can stop.
  CFRunLoopSourceRef quit_source_;

  // False after Quit is called.
  bool keep_running_;

  DISALLOW_COPY_AND_ASSIGN(MessagePumpForNonMainUIThreads);
};
#endif  // defined(XP_DARWIN)

#if defined(MOZ_WIDGET_ANDROID)
/*`
 * The MessagePumpForAndroidUI exists to enable IPDL in the Android UI thread.
 * The Android UI thread event loop is controlled by Android. This prevents
 * running an existing MessagePump implementation in the Android UI thread. In
 * order to enable IPDL on the Android UI thread it is necessary to have a
 * non-looping MessagePump. This class enables forwarding of nsIRunnables from
 * MessageLoop::PostTask_Helper to the registered nsIEventTarget with out the
 * need to control the event loop. The only member function that should be
 * invoked is GetXPCOMThread. All other member functions will invoke MOZ_CRASH
 */
class MessagePumpForAndroidUI : public base::MessagePump {
 public:
  explicit MessagePumpForAndroidUI(nsISerialEventTarget* aEventTarget)
      : mEventTarget(aEventTarget) {}

  virtual void Run(Delegate* delegate);
  virtual void Quit();
  virtual void ScheduleWork();
  virtual void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time);
  virtual nsISerialEventTarget* GetXPCOMThread() { return mEventTarget; }

 private:
  ~MessagePumpForAndroidUI() {}
  MessagePumpForAndroidUI() {}

  nsISerialEventTarget* mEventTarget;
};
#endif  // defined(MOZ_WIDGET_ANDROID)

} /* namespace ipc */
} /* namespace mozilla */

#endif /* __IPC_GLUE_MESSAGEPUMP_H__ */