1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 #include "MessagePump.h"
9 using namespace mozilla::ipc
;
11 NS_IMPL_ADDREF_INHERITED(MessagePumpForNonMainUIThreads
, MessagePump
)
12 NS_IMPL_RELEASE_INHERITED(MessagePumpForNonMainUIThreads
, MessagePump
)
13 NS_IMPL_QUERY_INTERFACE(MessagePumpForNonMainUIThreads
, nsIThreadObserver
)
15 #define CHECK_QUIT_STATE \
17 if (state_->should_quit) { \
22 void MessagePumpForNonMainUIThreads::DoRunLoop() {
23 MOZ_RELEASE_ASSERT(!NS_IsMainThread(),
24 "Use mozilla::ipc::MessagePump instead!");
26 // If this is a chromium thread and no nsThread is associated
27 // with it, this call will create a new nsThread.
28 nsIThread
* thread
= NS_GetCurrentThread();
31 // Set the main thread observer so we can wake up when
32 // xpcom events need to get processed.
33 nsCOMPtr
<nsIThreadInternal
> ti(do_QueryInterface(thread
));
35 ti
->SetObserver(this);
38 bool didWork
= NS_ProcessNextEvent(thread
, false);
40 didWork
|= ProcessNextWindowsMessage();
43 didWork
|= state_
->delegate
->DoWork();
46 didWork
|= state_
->delegate
->DoDelayedWork(&delayed_work_time_
);
47 if (didWork
&& delayed_work_time_
.is_null()) {
48 KillTimer(message_hwnd_
, reinterpret_cast<UINT_PTR
>(this));
56 DebugOnly
<bool> didIdleWork
= state_
->delegate
->DoIdleWork();
57 MOZ_ASSERT(!didIdleWork
);
61 bool hasWork
= NS_HasPendingEvents(thread
);
62 if (didWork
|| hasWork
) {
66 WaitForWork(); // Calls MsgWaitForMultipleObjectsEx(QS_ALLINPUT)
72 ti
->SetObserver(nullptr);
76 MessagePumpForNonMainUIThreads::OnDispatchedEvent() {
77 // If our thread is sleeping in DoRunLoop's call to WaitForWork() and an
78 // event posts to the nsIThread event queue - break our thread out of
79 // chromium's WaitForWork.
87 MessagePumpForNonMainUIThreads::OnProcessNextEvent(nsIThreadInternal
* thread
,
93 MessagePumpForNonMainUIThreads::AfterProcessNextEvent(nsIThreadInternal
* thread
,
94 bool eventWasProcessed
) {