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 "nsJSUtils.h"
11 #include "nsThreadUtils.h"
12 #include "nsIAppShell.h"
13 #include "nsIObserverService.h"
14 #include "nsServiceManagerUtils.h"
15 #include "mozilla/Services.h"
16 #include "nsXULAppAPI.h"
18 // When processing the next thread event, the appshell may process native
19 // events (if not in performance mode), which can result in suppressing the
20 // next thread event for at most this many ticks:
21 #define THREAD_EVENT_STARVATION_LIMIT PR_MillisecondsToInterval(10)
23 NS_IMPL_ISUPPORTS(nsBaseAppShell
, nsIAppShell
, nsIThreadObserver
, nsIObserver
)
25 nsBaseAppShell::nsBaseAppShell()
26 : mSuspendNativeCount(0),
27 mEventloopNestingLevel(0),
28 mBlockedWait(nullptr),
30 mNativeEventPending(false),
33 mLastNativeEventTime(0),
34 mEventloopNestingState(eEventloopNone
),
37 mBlockNativeEvent(false),
38 mProcessedGeckoEvents(false) {}
40 nsBaseAppShell::~nsBaseAppShell() = default;
42 nsresult
nsBaseAppShell::Init() {
43 // Configure ourselves as an observer for the current thread:
45 if (XRE_UseNativeEventProcessing()) {
46 nsCOMPtr
<nsIThreadInternal
> threadInt
=
47 do_QueryInterface(NS_GetCurrentThread());
48 NS_ENSURE_STATE(threadInt
);
50 threadInt
->SetObserver(this);
53 nsCOMPtr
<nsIObserverService
> obsSvc
= mozilla::services::GetObserverService();
54 if (obsSvc
) obsSvc
->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
, false);
58 // Called by nsAppShell's native event callback
59 void nsBaseAppShell::NativeEventCallback() {
60 if (!mNativeEventPending
.exchange(false)) return;
62 // If DoProcessNextNativeEvent is on the stack, then we assume that we can
63 // just unwind and let nsThread::ProcessNextEvent process the next event.
64 // However, if we are called from a nested native event loop (maybe via some
65 // plug-in or library function), then go ahead and process Gecko events now.
66 if (mEventloopNestingState
== eEventloopXPCOM
) {
67 mEventloopNestingState
= eEventloopOther
;
68 // XXX there is a tiny risk we will never get a new NativeEventCallback,
69 // XXX see discussion in bug 389931.
73 // nsBaseAppShell::Run is not being used to pump events, so this may be
74 // our only opportunity to process pending gecko events.
76 nsIThread
* thread
= NS_GetCurrentThread();
77 bool prevBlockNativeEvent
= mBlockNativeEvent
;
78 if (mEventloopNestingState
== eEventloopOther
) {
79 if (!NS_HasPendingEvents(thread
)) return;
80 // We're in a nested native event loop and have some gecko events to
81 // process. While doing that we block processing native events from the
82 // appshell - instead, we want to get back to the nested native event
83 // loop ASAP (bug 420148).
84 mBlockNativeEvent
= true;
87 IncrementEventloopNestingLevel();
88 EventloopNestingState prevVal
= mEventloopNestingState
;
89 NS_ProcessPendingEvents(thread
, THREAD_EVENT_STARVATION_LIMIT
);
90 mProcessedGeckoEvents
= true;
91 mEventloopNestingState
= prevVal
;
92 mBlockNativeEvent
= prevBlockNativeEvent
;
94 // Continue processing pending events later (we don't want to starve the
95 // embedders event loop).
96 if (NS_HasPendingEvents(thread
)) DoProcessMoreGeckoEvents();
98 DecrementEventloopNestingLevel();
101 void nsBaseAppShell::OnSystemTimezoneChange() {
102 nsJSUtils::ResetTimeZone();
104 nsCOMPtr
<nsIObserverService
> obsSvc
= mozilla::services::GetObserverService();
106 // Timezone changed notification
107 obsSvc
->NotifyObservers(nullptr, DEFAULT_TIMEZONE_CHANGED_OBSERVER_TOPIC
,
112 // Note, this is currently overidden on windows, see comments in nsAppShell for
114 void nsBaseAppShell::DoProcessMoreGeckoEvents() { OnDispatchedEvent(); }
116 // Main thread via OnProcessNextEvent below
117 bool nsBaseAppShell::DoProcessNextNativeEvent(bool mayWait
) {
118 // The next native event to be processed may trigger our NativeEventCallback,
119 // in which case we do not want it to process any thread events since we'll
120 // do that when this function returns.
122 // If the next native event is not our NativeEventCallback, then we may end
123 // up recursing into this function.
125 // However, if the next native event is not our NativeEventCallback, but it
126 // results in another native event loop, then our NativeEventCallback could
127 // fire and it will see mEventloopNestingState as eEventloopOther.
129 EventloopNestingState prevVal
= mEventloopNestingState
;
130 mEventloopNestingState
= eEventloopXPCOM
;
132 IncrementEventloopNestingLevel();
133 bool result
= ProcessNextNativeEvent(mayWait
);
134 DecrementEventloopNestingLevel();
136 mEventloopNestingState
= prevVal
;
140 //-------------------------------------------------------------------------
141 // nsIAppShell methods:
144 nsBaseAppShell::Run(void) {
145 NS_ENSURE_STATE(!mRunning
); // should not call Run twice
148 nsIThread
* thread
= NS_GetCurrentThread();
150 MessageLoop::current()->Run();
152 NS_ProcessPendingEvents(thread
);
159 nsBaseAppShell::Exit(void) {
160 if (mRunning
&& !mExiting
) {
161 MessageLoop::current()->Quit();
168 nsBaseAppShell::FavorPerformanceHint(bool favorPerfOverStarvation
,
169 uint32_t starvationDelay
) {
170 mStarvationDelay
= PR_MillisecondsToInterval(starvationDelay
);
171 if (favorPerfOverStarvation
) {
175 mSwitchTime
= PR_IntervalNow();
181 nsBaseAppShell::SuspendNative() {
182 ++mSuspendNativeCount
;
187 nsBaseAppShell::ResumeNative() {
188 --mSuspendNativeCount
;
189 NS_ASSERTION(mSuspendNativeCount
>= 0,
190 "Unbalanced call to nsBaseAppShell::ResumeNative!");
195 nsBaseAppShell::GetEventloopNestingLevel(uint32_t* aNestingLevelResult
) {
196 NS_ENSURE_ARG_POINTER(aNestingLevelResult
);
198 *aNestingLevelResult
= mEventloopNestingLevel
;
203 //-------------------------------------------------------------------------
204 // nsIThreadObserver methods:
206 // Called from any thread
208 nsBaseAppShell::OnDispatchedEvent() {
209 if (mBlockNativeEvent
) return NS_OK
;
211 if (mNativeEventPending
.exchange(true)) return NS_OK
;
213 // Returns on the main thread in NativeEventCallback above
214 ScheduleNativeEventCallback();
218 // Called from the main thread
220 nsBaseAppShell::OnProcessNextEvent(nsIThreadInternal
* thr
, bool mayWait
) {
221 if (mBlockNativeEvent
) {
222 if (!mayWait
) return NS_OK
;
223 // Hmm, we're in a nested native event loop and would like to get
224 // back to it ASAP, but it seems a gecko event has caused us to
225 // spin up a nested XPCOM event loop (eg. modal window), so we
226 // really must start processing native events here again.
227 mBlockNativeEvent
= false;
228 if (NS_HasPendingEvents(thr
))
229 OnDispatchedEvent(); // in case we blocked it earlier
232 PRIntervalTime start
= PR_IntervalNow();
233 PRIntervalTime limit
= THREAD_EVENT_STARVATION_LIMIT
;
235 // Unblock outer nested wait loop (below).
236 if (mBlockedWait
) *mBlockedWait
= false;
238 bool* oldBlockedWait
= mBlockedWait
;
239 mBlockedWait
= &mayWait
;
241 // When mayWait is true, we need to make sure that there is an event in the
242 // thread's event queue before we return. Otherwise, the thread will block
243 // on its event queue waiting for an event.
244 bool needEvent
= mayWait
;
245 // Reset prior to invoking DoProcessNextNativeEvent which might cause
246 // NativeEventCallback to process gecko events.
247 mProcessedGeckoEvents
= false;
249 if (mFavorPerf
<= 0 && start
> mSwitchTime
+ mStarvationDelay
) {
250 // Favor pending native events
251 PRIntervalTime now
= start
;
254 mLastNativeEventTime
= now
;
255 keepGoing
= DoProcessNextNativeEvent(false);
256 } while (keepGoing
&& ((now
= PR_IntervalNow()) - start
) < limit
);
258 // Avoid starving native events completely when in performance mode
259 if (start
- mLastNativeEventTime
> limit
) {
260 mLastNativeEventTime
= start
;
261 DoProcessNextNativeEvent(false);
265 while (!NS_HasPendingEvents(thr
) && !mProcessedGeckoEvents
) {
266 // If we have been asked to exit from Run, then we should not wait for
267 // events to process. Note that an inner nested event loop causes
268 // 'mayWait' to become false too, through 'mBlockedWait'.
269 if (mExiting
) mayWait
= false;
271 mLastNativeEventTime
= PR_IntervalNow();
272 if (!DoProcessNextNativeEvent(mayWait
) || !mayWait
) break;
275 mBlockedWait
= oldBlockedWait
;
277 // Make sure that the thread event queue does not block on its monitor, as
278 // it normally would do if it did not have any pending events. To avoid
279 // that, we simply insert a dummy event into its queue during shutdown.
280 if (needEvent
&& !mExiting
&& !NS_HasPendingEvents(thr
)) {
281 DispatchDummyEvent(thr
);
287 bool nsBaseAppShell::DispatchDummyEvent(nsIThread
* aTarget
) {
288 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
290 if (!mDummyEvent
) mDummyEvent
= new mozilla::Runnable("DummyEvent");
292 return NS_SUCCEEDED(aTarget
->Dispatch(mDummyEvent
, NS_DISPATCH_NORMAL
));
295 void nsBaseAppShell::IncrementEventloopNestingLevel() {
296 ++mEventloopNestingLevel
;
297 CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel
);
300 void nsBaseAppShell::DecrementEventloopNestingLevel() {
301 --mEventloopNestingLevel
;
302 CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel
);
305 // Called from the main thread
307 nsBaseAppShell::AfterProcessNextEvent(nsIThreadInternal
* thr
,
308 bool eventWasProcessed
) {
313 nsBaseAppShell::Observe(nsISupports
* subject
, const char* topic
,
314 const char16_t
* data
) {
315 NS_ASSERTION(!strcmp(topic
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
), "oops");