1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "base/message_loop.h"
8 #include "nsBaseAppShell.h"
9 #include "nsExceptionHandler.h"
10 #include "nsThreadUtils.h"
11 #include "nsIObserverService.h"
12 #include "nsServiceManagerUtils.h"
13 #include "mozilla/Services.h"
14 #include "nsXULAppAPI.h"
16 // When processing the next thread event, the appshell may process native
17 // events (if not in performance mode), which can result in suppressing the
18 // next thread event for at most this many ticks:
19 #define THREAD_EVENT_STARVATION_LIMIT PR_MillisecondsToInterval(10)
21 NS_IMPL_ISUPPORTS(nsBaseAppShell
, nsIAppShell
, nsIThreadObserver
, nsIObserver
)
23 nsBaseAppShell::nsBaseAppShell()
24 : mSuspendNativeCount(0)
25 , mEventloopNestingLevel(0)
26 , mBlockedWait(nullptr)
28 , mNativeEventPending(false)
31 , mLastNativeEventTime(0)
32 , mEventloopNestingState(eEventloopNone
)
35 , mBlockNativeEvent(false)
36 , mProcessedGeckoEvents(false)
40 nsBaseAppShell::~nsBaseAppShell()
45 nsBaseAppShell::Init()
47 // Configure ourselves as an observer for the current thread:
49 if (XRE_UseNativeEventProcessing()) {
50 nsCOMPtr
<nsIThreadInternal
> threadInt
=
51 do_QueryInterface(NS_GetCurrentThread());
52 NS_ENSURE_STATE(threadInt
);
54 threadInt
->SetObserver(this);
57 nsCOMPtr
<nsIObserverService
> obsSvc
=
58 mozilla::services::GetObserverService();
60 obsSvc
->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
, false);
64 // Called by nsAppShell's native event callback
66 nsBaseAppShell::NativeEventCallback()
68 if (!mNativeEventPending
.exchange(false))
71 // If DoProcessNextNativeEvent is on the stack, then we assume that we can
72 // just unwind and let nsThread::ProcessNextEvent process the next event.
73 // However, if we are called from a nested native event loop (maybe via some
74 // plug-in or library function), then go ahead and process Gecko events now.
75 if (mEventloopNestingState
== eEventloopXPCOM
) {
76 mEventloopNestingState
= eEventloopOther
;
77 // XXX there is a tiny risk we will never get a new NativeEventCallback,
78 // XXX see discussion in bug 389931.
82 // nsBaseAppShell::Run is not being used to pump events, so this may be
83 // our only opportunity to process pending gecko events.
85 nsIThread
*thread
= NS_GetCurrentThread();
86 bool prevBlockNativeEvent
= mBlockNativeEvent
;
87 if (mEventloopNestingState
== eEventloopOther
) {
88 if (!NS_HasPendingEvents(thread
))
90 // We're in a nested native event loop and have some gecko events to
91 // process. While doing that we block processing native events from the
92 // appshell - instead, we want to get back to the nested native event
93 // loop ASAP (bug 420148).
94 mBlockNativeEvent
= true;
97 IncrementEventloopNestingLevel();
98 EventloopNestingState prevVal
= mEventloopNestingState
;
99 NS_ProcessPendingEvents(thread
, THREAD_EVENT_STARVATION_LIMIT
);
100 mProcessedGeckoEvents
= true;
101 mEventloopNestingState
= prevVal
;
102 mBlockNativeEvent
= prevBlockNativeEvent
;
104 // Continue processing pending events later (we don't want to starve the
105 // embedders event loop).
106 if (NS_HasPendingEvents(thread
))
107 DoProcessMoreGeckoEvents();
109 DecrementEventloopNestingLevel();
112 // Note, this is currently overidden on windows, see comments in nsAppShell for
115 nsBaseAppShell::DoProcessMoreGeckoEvents()
121 // Main thread via OnProcessNextEvent below
123 nsBaseAppShell::DoProcessNextNativeEvent(bool mayWait
)
125 // The next native event to be processed may trigger our NativeEventCallback,
126 // in which case we do not want it to process any thread events since we'll
127 // do that when this function returns.
129 // If the next native event is not our NativeEventCallback, then we may end
130 // up recursing into this function.
132 // However, if the next native event is not our NativeEventCallback, but it
133 // results in another native event loop, then our NativeEventCallback could
134 // fire and it will see mEventloopNestingState as eEventloopOther.
136 EventloopNestingState prevVal
= mEventloopNestingState
;
137 mEventloopNestingState
= eEventloopXPCOM
;
139 IncrementEventloopNestingLevel();
140 bool result
= ProcessNextNativeEvent(mayWait
);
141 DecrementEventloopNestingLevel();
143 mEventloopNestingState
= prevVal
;
147 //-------------------------------------------------------------------------
148 // nsIAppShell methods:
151 nsBaseAppShell::Run(void)
153 NS_ENSURE_STATE(!mRunning
); // should not call Run twice
156 nsIThread
*thread
= NS_GetCurrentThread();
158 MessageLoop::current()->Run();
160 NS_ProcessPendingEvents(thread
);
167 nsBaseAppShell::Exit(void)
169 if (mRunning
&& !mExiting
) {
170 MessageLoop::current()->Quit();
177 nsBaseAppShell::FavorPerformanceHint(bool favorPerfOverStarvation
,
178 uint32_t starvationDelay
)
180 mStarvationDelay
= PR_MillisecondsToInterval(starvationDelay
);
181 if (favorPerfOverStarvation
) {
185 mSwitchTime
= PR_IntervalNow();
191 nsBaseAppShell::SuspendNative()
193 ++mSuspendNativeCount
;
198 nsBaseAppShell::ResumeNative()
200 --mSuspendNativeCount
;
201 NS_ASSERTION(mSuspendNativeCount
>= 0, "Unbalanced call to nsBaseAppShell::ResumeNative!");
206 nsBaseAppShell::GetEventloopNestingLevel(uint32_t* aNestingLevelResult
)
208 NS_ENSURE_ARG_POINTER(aNestingLevelResult
);
210 *aNestingLevelResult
= mEventloopNestingLevel
;
215 //-------------------------------------------------------------------------
216 // nsIThreadObserver methods:
218 // Called from any thread
220 nsBaseAppShell::OnDispatchedEvent()
222 if (mBlockNativeEvent
)
225 if (mNativeEventPending
.exchange(true))
228 // Returns on the main thread in NativeEventCallback above
229 ScheduleNativeEventCallback();
233 // Called from the main thread
235 nsBaseAppShell::OnProcessNextEvent(nsIThreadInternal
*thr
, bool mayWait
)
237 if (mBlockNativeEvent
) {
240 // Hmm, we're in a nested native event loop and would like to get
241 // back to it ASAP, but it seems a gecko event has caused us to
242 // spin up a nested XPCOM event loop (eg. modal window), so we
243 // really must start processing native events here again.
244 mBlockNativeEvent
= false;
245 if (NS_HasPendingEvents(thr
))
246 OnDispatchedEvent(); // in case we blocked it earlier
249 PRIntervalTime start
= PR_IntervalNow();
250 PRIntervalTime limit
= THREAD_EVENT_STARVATION_LIMIT
;
252 // Unblock outer nested wait loop (below).
254 *mBlockedWait
= false;
256 bool *oldBlockedWait
= mBlockedWait
;
257 mBlockedWait
= &mayWait
;
259 // When mayWait is true, we need to make sure that there is an event in the
260 // thread's event queue before we return. Otherwise, the thread will block
261 // on its event queue waiting for an event.
262 bool needEvent
= mayWait
;
263 // Reset prior to invoking DoProcessNextNativeEvent which might cause
264 // NativeEventCallback to process gecko events.
265 mProcessedGeckoEvents
= false;
267 if (mFavorPerf
<= 0 && start
> mSwitchTime
+ mStarvationDelay
) {
268 // Favor pending native events
269 PRIntervalTime now
= start
;
272 mLastNativeEventTime
= now
;
273 keepGoing
= DoProcessNextNativeEvent(false);
274 } while (keepGoing
&& ((now
= PR_IntervalNow()) - start
) < limit
);
276 // Avoid starving native events completely when in performance mode
277 if (start
- mLastNativeEventTime
> limit
) {
278 mLastNativeEventTime
= start
;
279 DoProcessNextNativeEvent(false);
283 while (!NS_HasPendingEvents(thr
) && !mProcessedGeckoEvents
) {
284 // If we have been asked to exit from Run, then we should not wait for
285 // events to process. Note that an inner nested event loop causes
286 // 'mayWait' to become false too, through 'mBlockedWait'.
290 mLastNativeEventTime
= PR_IntervalNow();
291 if (!DoProcessNextNativeEvent(mayWait
) || !mayWait
)
295 mBlockedWait
= oldBlockedWait
;
297 // Make sure that the thread event queue does not block on its monitor, as
298 // it normally would do if it did not have any pending events. To avoid
299 // that, we simply insert a dummy event into its queue during shutdown.
300 if (needEvent
&& !mExiting
&& !NS_HasPendingEvents(thr
)) {
301 DispatchDummyEvent(thr
);
308 nsBaseAppShell::DispatchDummyEvent(nsIThread
* aTarget
)
310 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
313 mDummyEvent
= new mozilla::Runnable("DummyEvent");
315 return NS_SUCCEEDED(aTarget
->Dispatch(mDummyEvent
, NS_DISPATCH_NORMAL
));
319 nsBaseAppShell::IncrementEventloopNestingLevel()
321 ++mEventloopNestingLevel
;
322 CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel
);
326 nsBaseAppShell::DecrementEventloopNestingLevel()
328 --mEventloopNestingLevel
;
329 CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel
);
332 // Called from the main thread
334 nsBaseAppShell::AfterProcessNextEvent(nsIThreadInternal
*thr
,
335 bool eventWasProcessed
)
341 nsBaseAppShell::Observe(nsISupports
*subject
, const char *topic
,
342 const char16_t
*data
)
344 NS_ASSERTION(!strcmp(topic
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
), "oops");