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/. */
5 
6 #include "base/message_loop.h"
7 
8 #include "nsBaseAppShell.h"
9 #if defined(MOZ_CRASHREPORTER)
10 #include "nsExceptionHandler.h"
11 #endif
12 #include "nsThreadUtils.h"
13 #include "nsIObserverService.h"
14 #include "nsServiceManagerUtils.h"
15 #include "mozilla/Services.h"
16 
17 // When processing the next thread event, the appshell may process native
18 // events (if not in performance mode), which can result in suppressing the
19 // next thread event for at most this many ticks:
20 #define THREAD_EVENT_STARVATION_LIMIT PR_MillisecondsToInterval(10)
21 
NS_IMPL_ISUPPORTS(nsBaseAppShell,nsIAppShell,nsIThreadObserver,nsIObserver)22 NS_IMPL_ISUPPORTS(nsBaseAppShell, nsIAppShell, nsIThreadObserver, nsIObserver)
23 
24 nsBaseAppShell::nsBaseAppShell()
25   : mSuspendNativeCount(0)
26   , mEventloopNestingLevel(0)
27   , mBlockedWait(nullptr)
28   , mFavorPerf(0)
29   , mNativeEventPending(false)
30   , mStarvationDelay(0)
31   , mSwitchTime(0)
32   , mLastNativeEventTime(0)
33   , mEventloopNestingState(eEventloopNone)
34   , mRunning(false)
35   , mExiting(false)
36   , mBlockNativeEvent(false)
37 {
38 }
39 
~nsBaseAppShell()40 nsBaseAppShell::~nsBaseAppShell()
41 {
42 }
43 
44 nsresult
Init()45 nsBaseAppShell::Init()
46 {
47   // Configure ourselves as an observer for the current thread:
48 
49   nsCOMPtr<nsIThreadInternal> threadInt =
50       do_QueryInterface(NS_GetCurrentThread());
51   NS_ENSURE_STATE(threadInt);
52 
53   threadInt->SetObserver(this);
54 
55   nsCOMPtr<nsIObserverService> obsSvc =
56     mozilla::services::GetObserverService();
57   if (obsSvc)
58     obsSvc->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
59   return NS_OK;
60 }
61 
62 // Called by nsAppShell's native event callback
63 void
NativeEventCallback()64 nsBaseAppShell::NativeEventCallback()
65 {
66   if (!mNativeEventPending.exchange(false))
67     return;
68 
69   // If DoProcessNextNativeEvent is on the stack, then we assume that we can
70   // just unwind and let nsThread::ProcessNextEvent process the next event.
71   // However, if we are called from a nested native event loop (maybe via some
72   // plug-in or library function), then go ahead and process Gecko events now.
73   if (mEventloopNestingState == eEventloopXPCOM) {
74     mEventloopNestingState = eEventloopOther;
75     // XXX there is a tiny risk we will never get a new NativeEventCallback,
76     // XXX see discussion in bug 389931.
77     return;
78   }
79 
80   // nsBaseAppShell::Run is not being used to pump events, so this may be
81   // our only opportunity to process pending gecko events.
82 
83   nsIThread *thread = NS_GetCurrentThread();
84   bool prevBlockNativeEvent = mBlockNativeEvent;
85   if (mEventloopNestingState == eEventloopOther) {
86     if (!NS_HasPendingEvents(thread))
87       return;
88     // We're in a nested native event loop and have some gecko events to
89     // process.  While doing that we block processing native events from the
90     // appshell - instead, we want to get back to the nested native event
91     // loop ASAP (bug 420148).
92     mBlockNativeEvent = true;
93   }
94 
95   IncrementEventloopNestingLevel();
96   EventloopNestingState prevVal = mEventloopNestingState;
97   NS_ProcessPendingEvents(thread, THREAD_EVENT_STARVATION_LIMIT);
98   mProcessedGeckoEvents = true;
99   mEventloopNestingState = prevVal;
100   mBlockNativeEvent = prevBlockNativeEvent;
101 
102   // Continue processing pending events later (we don't want to starve the
103   // embedders event loop).
104   if (NS_HasPendingEvents(thread))
105     DoProcessMoreGeckoEvents();
106 
107   DecrementEventloopNestingLevel();
108 }
109 
110 // Note, this is currently overidden on windows, see comments in nsAppShell for
111 // details.
112 void
DoProcessMoreGeckoEvents()113 nsBaseAppShell::DoProcessMoreGeckoEvents()
114 {
115   OnDispatchedEvent(nullptr);
116 }
117 
118 
119 // Main thread via OnProcessNextEvent below
120 bool
DoProcessNextNativeEvent(bool mayWait)121 nsBaseAppShell::DoProcessNextNativeEvent(bool mayWait)
122 {
123   // The next native event to be processed may trigger our NativeEventCallback,
124   // in which case we do not want it to process any thread events since we'll
125   // do that when this function returns.
126   //
127   // If the next native event is not our NativeEventCallback, then we may end
128   // up recursing into this function.
129   //
130   // However, if the next native event is not our NativeEventCallback, but it
131   // results in another native event loop, then our NativeEventCallback could
132   // fire and it will see mEventloopNestingState as eEventloopOther.
133   //
134   EventloopNestingState prevVal = mEventloopNestingState;
135   mEventloopNestingState = eEventloopXPCOM;
136 
137   IncrementEventloopNestingLevel();
138   bool result = ProcessNextNativeEvent(mayWait);
139   DecrementEventloopNestingLevel();
140 
141   mEventloopNestingState = prevVal;
142   return result;
143 }
144 
145 //-------------------------------------------------------------------------
146 // nsIAppShell methods:
147 
148 NS_IMETHODIMP
Run(void)149 nsBaseAppShell::Run(void)
150 {
151   NS_ENSURE_STATE(!mRunning);  // should not call Run twice
152   mRunning = true;
153 
154   nsIThread *thread = NS_GetCurrentThread();
155 
156   MessageLoop::current()->Run();
157 
158   NS_ProcessPendingEvents(thread);
159 
160   mRunning = false;
161   return NS_OK;
162 }
163 
164 NS_IMETHODIMP
Exit(void)165 nsBaseAppShell::Exit(void)
166 {
167   if (mRunning && !mExiting) {
168     MessageLoop::current()->Quit();
169   }
170   mExiting = true;
171   return NS_OK;
172 }
173 
174 NS_IMETHODIMP
FavorPerformanceHint(bool favorPerfOverStarvation,uint32_t starvationDelay)175 nsBaseAppShell::FavorPerformanceHint(bool favorPerfOverStarvation,
176                                      uint32_t starvationDelay)
177 {
178   mStarvationDelay = PR_MillisecondsToInterval(starvationDelay);
179   if (favorPerfOverStarvation) {
180     ++mFavorPerf;
181   } else {
182     --mFavorPerf;
183     mSwitchTime = PR_IntervalNow();
184   }
185   return NS_OK;
186 }
187 
188 NS_IMETHODIMP
SuspendNative()189 nsBaseAppShell::SuspendNative()
190 {
191   ++mSuspendNativeCount;
192   return NS_OK;
193 }
194 
195 NS_IMETHODIMP
ResumeNative()196 nsBaseAppShell::ResumeNative()
197 {
198   --mSuspendNativeCount;
199   NS_ASSERTION(mSuspendNativeCount >= 0, "Unbalanced call to nsBaseAppShell::ResumeNative!");
200   return NS_OK;
201 }
202 
203 NS_IMETHODIMP
GetEventloopNestingLevel(uint32_t * aNestingLevelResult)204 nsBaseAppShell::GetEventloopNestingLevel(uint32_t* aNestingLevelResult)
205 {
206   NS_ENSURE_ARG_POINTER(aNestingLevelResult);
207 
208   *aNestingLevelResult = mEventloopNestingLevel;
209 
210   return NS_OK;
211 }
212 
213 //-------------------------------------------------------------------------
214 // nsIThreadObserver methods:
215 
216 // Called from any thread
217 NS_IMETHODIMP
OnDispatchedEvent(nsIThreadInternal * thr)218 nsBaseAppShell::OnDispatchedEvent(nsIThreadInternal *thr)
219 {
220   if (mBlockNativeEvent)
221     return NS_OK;
222 
223   if (mNativeEventPending.exchange(true))
224     return NS_OK;
225 
226   // Returns on the main thread in NativeEventCallback above
227   ScheduleNativeEventCallback();
228   return NS_OK;
229 }
230 
231 // Called from the main thread
232 NS_IMETHODIMP
OnProcessNextEvent(nsIThreadInternal * thr,bool mayWait)233 nsBaseAppShell::OnProcessNextEvent(nsIThreadInternal *thr, bool mayWait)
234 {
235   if (mBlockNativeEvent) {
236     if (!mayWait)
237       return NS_OK;
238     // Hmm, we're in a nested native event loop and would like to get
239     // back to it ASAP, but it seems a gecko event has caused us to
240     // spin up a nested XPCOM event loop (eg. modal window), so we
241     // really must start processing native events here again.
242     mBlockNativeEvent = false;
243     if (NS_HasPendingEvents(thr))
244       OnDispatchedEvent(thr); // in case we blocked it earlier
245   }
246 
247   PRIntervalTime start = PR_IntervalNow();
248   PRIntervalTime limit = THREAD_EVENT_STARVATION_LIMIT;
249 
250   // Unblock outer nested wait loop (below).
251   if (mBlockedWait)
252     *mBlockedWait = false;
253 
254   bool *oldBlockedWait = mBlockedWait;
255   mBlockedWait = &mayWait;
256 
257   // When mayWait is true, we need to make sure that there is an event in the
258   // thread's event queue before we return.  Otherwise, the thread will block
259   // on its event queue waiting for an event.
260   bool needEvent = mayWait;
261   // Reset prior to invoking DoProcessNextNativeEvent which might cause
262   // NativeEventCallback to process gecko events.
263   mProcessedGeckoEvents = false;
264 
265   if (mFavorPerf <= 0 && start > mSwitchTime + mStarvationDelay) {
266     // Favor pending native events
267     PRIntervalTime now = start;
268     bool keepGoing;
269     do {
270       mLastNativeEventTime = now;
271       keepGoing = DoProcessNextNativeEvent(false);
272     } while (keepGoing && ((now = PR_IntervalNow()) - start) < limit);
273   } else {
274     // Avoid starving native events completely when in performance mode
275     if (start - mLastNativeEventTime > limit) {
276       mLastNativeEventTime = start;
277       DoProcessNextNativeEvent(false);
278     }
279   }
280 
281   while (!NS_HasPendingEvents(thr) && !mProcessedGeckoEvents) {
282     // If we have been asked to exit from Run, then we should not wait for
283     // events to process.  Note that an inner nested event loop causes
284     // 'mayWait' to become false too, through 'mBlockedWait'.
285     if (mExiting)
286       mayWait = false;
287 
288     mLastNativeEventTime = PR_IntervalNow();
289     if (!DoProcessNextNativeEvent(mayWait) || !mayWait)
290       break;
291   }
292 
293   mBlockedWait = oldBlockedWait;
294 
295   // Make sure that the thread event queue does not block on its monitor, as
296   // it normally would do if it did not have any pending events.  To avoid
297   // that, we simply insert a dummy event into its queue during shutdown.
298   if (needEvent && !mExiting && !NS_HasPendingEvents(thr)) {
299     DispatchDummyEvent(thr);
300   }
301 
302   return NS_OK;
303 }
304 
305 bool
DispatchDummyEvent(nsIThread * aTarget)306 nsBaseAppShell::DispatchDummyEvent(nsIThread* aTarget)
307 {
308   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
309 
310   if (!mDummyEvent)
311     mDummyEvent = new mozilla::Runnable();
312 
313   return NS_SUCCEEDED(aTarget->Dispatch(mDummyEvent, NS_DISPATCH_NORMAL));
314 }
315 
316 void
IncrementEventloopNestingLevel()317 nsBaseAppShell::IncrementEventloopNestingLevel()
318 {
319   ++mEventloopNestingLevel;
320 #if defined(MOZ_CRASHREPORTER)
321   CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel);
322 #endif
323 }
324 
325 void
DecrementEventloopNestingLevel()326 nsBaseAppShell::DecrementEventloopNestingLevel()
327 {
328   --mEventloopNestingLevel;
329 #if defined(MOZ_CRASHREPORTER)
330   CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel);
331 #endif
332 }
333 
334 // Called from the main thread
335 NS_IMETHODIMP
AfterProcessNextEvent(nsIThreadInternal * thr,bool eventWasProcessed)336 nsBaseAppShell::AfterProcessNextEvent(nsIThreadInternal *thr,
337                                       bool eventWasProcessed)
338 {
339   return NS_OK;
340 }
341 
342 NS_IMETHODIMP
Observe(nsISupports * subject,const char * topic,const char16_t * data)343 nsBaseAppShell::Observe(nsISupports *subject, const char *topic,
344                         const char16_t *data)
345 {
346   NS_ASSERTION(!strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID), "oops");
347   Exit();
348   return NS_OK;
349 }
350