forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsBaseAppShell.cpp
459 lines (382 loc) · 13.8 KB
/
nsBaseAppShell.cpp
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
/* 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/. */
#include "base/message_loop.h"
#include "nsBaseAppShell.h"
#if defined(MOZ_CRASHREPORTER)
#include "nsExceptionHandler.h"
#endif
#include "nsThreadUtils.h"
#include "nsIObserverService.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/Services.h"
// When processing the next thread event, the appshell may process native
// events (if not in performance mode), which can result in suppressing the
// next thread event for at most this many ticks:
#define THREAD_EVENT_STARVATION_LIMIT PR_MillisecondsToInterval(20)
NS_IMPL_ISUPPORTS(nsBaseAppShell, nsIAppShell, nsIThreadObserver, nsIObserver)
nsBaseAppShell::nsBaseAppShell()
: mSuspendNativeCount(0)
, mEventloopNestingLevel(0)
, mBlockedWait(nullptr)
, mFavorPerf(0)
, mNativeEventPending(false)
, mStarvationDelay(0)
, mSwitchTime(0)
, mLastNativeEventTime(0)
, mEventloopNestingState(eEventloopNone)
, mRunningSyncSections(false)
, mRunning(false)
, mExiting(false)
, mBlockNativeEvent(false)
{
}
nsBaseAppShell::~nsBaseAppShell()
{
NS_ASSERTION(mSyncSections.IsEmpty(), "Must have run all sync sections");
}
nsresult
nsBaseAppShell::Init()
{
// Configure ourselves as an observer for the current thread:
nsCOMPtr<nsIThreadInternal> threadInt =
do_QueryInterface(NS_GetCurrentThread());
NS_ENSURE_STATE(threadInt);
threadInt->SetObserver(this);
nsCOMPtr<nsIObserverService> obsSvc =
mozilla::services::GetObserverService();
if (obsSvc)
obsSvc->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
return NS_OK;
}
// Called by nsAppShell's native event callback
void
nsBaseAppShell::NativeEventCallback()
{
if (!mNativeEventPending.exchange(false))
return;
// If DoProcessNextNativeEvent is on the stack, then we assume that we can
// just unwind and let nsThread::ProcessNextEvent process the next event.
// However, if we are called from a nested native event loop (maybe via some
// plug-in or library function), then go ahead and process Gecko events now.
if (mEventloopNestingState == eEventloopXPCOM) {
mEventloopNestingState = eEventloopOther;
// XXX there is a tiny risk we will never get a new NativeEventCallback,
// XXX see discussion in bug 389931.
return;
}
// nsBaseAppShell::Run is not being used to pump events, so this may be
// our only opportunity to process pending gecko events.
nsIThread *thread = NS_GetCurrentThread();
bool prevBlockNativeEvent = mBlockNativeEvent;
if (mEventloopNestingState == eEventloopOther) {
if (!NS_HasPendingEvents(thread))
return;
// We're in a nested native event loop and have some gecko events to
// process. While doing that we block processing native events from the
// appshell - instead, we want to get back to the nested native event
// loop ASAP (bug 420148).
mBlockNativeEvent = true;
}
IncrementEventloopNestingLevel();
EventloopNestingState prevVal = mEventloopNestingState;
NS_ProcessPendingEvents(thread, THREAD_EVENT_STARVATION_LIMIT);
mProcessedGeckoEvents = true;
mEventloopNestingState = prevVal;
mBlockNativeEvent = prevBlockNativeEvent;
// Continue processing pending events later (we don't want to starve the
// embedders event loop).
if (NS_HasPendingEvents(thread))
DoProcessMoreGeckoEvents();
DecrementEventloopNestingLevel();
}
// Note, this is currently overidden on windows, see comments in nsAppShell for
// details.
void
nsBaseAppShell::DoProcessMoreGeckoEvents()
{
OnDispatchedEvent(nullptr);
}
// Main thread via OnProcessNextEvent below
bool
nsBaseAppShell::DoProcessNextNativeEvent(bool mayWait, uint32_t recursionDepth)
{
// The next native event to be processed may trigger our NativeEventCallback,
// in which case we do not want it to process any thread events since we'll
// do that when this function returns.
//
// If the next native event is not our NativeEventCallback, then we may end
// up recursing into this function.
//
// However, if the next native event is not our NativeEventCallback, but it
// results in another native event loop, then our NativeEventCallback could
// fire and it will see mEventloopNestingState as eEventloopOther.
//
EventloopNestingState prevVal = mEventloopNestingState;
mEventloopNestingState = eEventloopXPCOM;
IncrementEventloopNestingLevel();
bool result = ProcessNextNativeEvent(mayWait);
// Make sure that any sync sections registered during this most recent event
// are run now. This is not considered a stable state because we're not back
// to the event loop yet.
RunSyncSections(false, recursionDepth);
DecrementEventloopNestingLevel();
mEventloopNestingState = prevVal;
return result;
}
//-------------------------------------------------------------------------
// nsIAppShell methods:
NS_IMETHODIMP
nsBaseAppShell::Run(void)
{
NS_ENSURE_STATE(!mRunning); // should not call Run twice
mRunning = true;
nsIThread *thread = NS_GetCurrentThread();
MessageLoop::current()->Run();
NS_ProcessPendingEvents(thread);
mRunning = false;
return NS_OK;
}
NS_IMETHODIMP
nsBaseAppShell::Exit(void)
{
if (mRunning && !mExiting) {
MessageLoop::current()->Quit();
}
mExiting = true;
return NS_OK;
}
NS_IMETHODIMP
nsBaseAppShell::FavorPerformanceHint(bool favorPerfOverStarvation,
uint32_t starvationDelay)
{
mStarvationDelay = PR_MillisecondsToInterval(starvationDelay);
if (favorPerfOverStarvation) {
++mFavorPerf;
} else {
--mFavorPerf;
mSwitchTime = PR_IntervalNow();
}
return NS_OK;
}
NS_IMETHODIMP
nsBaseAppShell::SuspendNative()
{
++mSuspendNativeCount;
return NS_OK;
}
NS_IMETHODIMP
nsBaseAppShell::ResumeNative()
{
--mSuspendNativeCount;
NS_ASSERTION(mSuspendNativeCount >= 0, "Unbalanced call to nsBaseAppShell::ResumeNative!");
return NS_OK;
}
NS_IMETHODIMP
nsBaseAppShell::GetEventloopNestingLevel(uint32_t* aNestingLevelResult)
{
NS_ENSURE_ARG_POINTER(aNestingLevelResult);
*aNestingLevelResult = mEventloopNestingLevel;
return NS_OK;
}
//-------------------------------------------------------------------------
// nsIThreadObserver methods:
// Called from any thread
NS_IMETHODIMP
nsBaseAppShell::OnDispatchedEvent(nsIThreadInternal *thr)
{
if (mBlockNativeEvent)
return NS_OK;
if (mNativeEventPending.exchange(true))
return NS_OK;
// Returns on the main thread in NativeEventCallback above
ScheduleNativeEventCallback();
return NS_OK;
}
// Called from the main thread
NS_IMETHODIMP
nsBaseAppShell::OnProcessNextEvent(nsIThreadInternal *thr, bool mayWait,
uint32_t recursionDepth)
{
if (mBlockNativeEvent) {
if (!mayWait)
return NS_OK;
// Hmm, we're in a nested native event loop and would like to get
// back to it ASAP, but it seems a gecko event has caused us to
// spin up a nested XPCOM event loop (eg. modal window), so we
// really must start processing native events here again.
mBlockNativeEvent = false;
if (NS_HasPendingEvents(thr))
OnDispatchedEvent(thr); // in case we blocked it earlier
}
PRIntervalTime start = PR_IntervalNow();
PRIntervalTime limit = THREAD_EVENT_STARVATION_LIMIT;
// Unblock outer nested wait loop (below).
if (mBlockedWait)
*mBlockedWait = false;
bool *oldBlockedWait = mBlockedWait;
mBlockedWait = &mayWait;
// When mayWait is true, we need to make sure that there is an event in the
// thread's event queue before we return. Otherwise, the thread will block
// on its event queue waiting for an event.
bool needEvent = mayWait;
// Reset prior to invoking DoProcessNextNativeEvent which might cause
// NativeEventCallback to process gecko events.
mProcessedGeckoEvents = false;
if (mFavorPerf <= 0 && start > mSwitchTime + mStarvationDelay) {
// Favor pending native events
PRIntervalTime now = start;
bool keepGoing;
do {
mLastNativeEventTime = now;
keepGoing = DoProcessNextNativeEvent(false, recursionDepth);
} while (keepGoing && ((now = PR_IntervalNow()) - start) < limit);
} else {
// Avoid starving native events completely when in performance mode
if (start - mLastNativeEventTime > limit) {
mLastNativeEventTime = start;
DoProcessNextNativeEvent(false, recursionDepth);
}
}
while (!NS_HasPendingEvents(thr) && !mProcessedGeckoEvents) {
// If we have been asked to exit from Run, then we should not wait for
// events to process. Note that an inner nested event loop causes
// 'mayWait' to become false too, through 'mBlockedWait'.
if (mExiting)
mayWait = false;
mLastNativeEventTime = PR_IntervalNow();
if (!DoProcessNextNativeEvent(mayWait, recursionDepth) || !mayWait)
break;
}
mBlockedWait = oldBlockedWait;
// Make sure that the thread event queue does not block on its monitor, as
// it normally would do if it did not have any pending events. To avoid
// that, we simply insert a dummy event into its queue during shutdown.
if (needEvent && !mExiting && !NS_HasPendingEvents(thr)) {
DispatchDummyEvent(thr);
}
// We're about to run an event, so we're in a stable state.
RunSyncSections(true, recursionDepth);
return NS_OK;
}
bool
nsBaseAppShell::DispatchDummyEvent(nsIThread* aTarget)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
if (!mDummyEvent)
mDummyEvent = new nsRunnable();
return NS_SUCCEEDED(aTarget->Dispatch(mDummyEvent, NS_DISPATCH_NORMAL));
}
void
nsBaseAppShell::IncrementEventloopNestingLevel()
{
++mEventloopNestingLevel;
#if defined(MOZ_CRASHREPORTER)
CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel);
#endif
}
void
nsBaseAppShell::DecrementEventloopNestingLevel()
{
--mEventloopNestingLevel;
#if defined(MOZ_CRASHREPORTER)
CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel);
#endif
}
void
nsBaseAppShell::RunSyncSectionsInternal(bool aStable,
uint32_t aThreadRecursionLevel)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
NS_ASSERTION(!mSyncSections.IsEmpty(), "Nothing to do!");
// We don't support re-entering sync sections. This effectively means that
// sync sections may not spin the event loop.
MOZ_RELEASE_ASSERT(!mRunningSyncSections);
mRunningSyncSections = true;
// We've got synchronous sections. Run all of them that are are awaiting a
// stable state if aStable is true (i.e. we really are in a stable state).
// Also run the synchronous sections that are simply waiting for the right
// combination of event loop nesting level and thread recursion level.
// Note that a synchronous section could add another synchronous section, so
// we don't remove elements from mSyncSections until all sections have been
// run, or else we'll screw up our iteration. Any sync sections that are not
// ready to be run are saved for later.
nsTArray<SyncSection> pendingSyncSections;
for (uint32_t i = 0; i < mSyncSections.Length(); i++) {
SyncSection& section = mSyncSections[i];
if ((aStable && section.mStable) ||
(!section.mStable &&
section.mEventloopNestingLevel == mEventloopNestingLevel &&
section.mThreadRecursionLevel == aThreadRecursionLevel)) {
section.mRunnable->Run();
}
else {
// Add to pending list.
SyncSection* pending = pendingSyncSections.AppendElement();
section.Forget(pending);
}
}
mSyncSections.SwapElements(pendingSyncSections);
mRunningSyncSections = false;
}
void
nsBaseAppShell::ScheduleSyncSection(nsIRunnable* aRunnable, bool aStable)
{
NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
nsIThread* thread = NS_GetCurrentThread();
// Add this runnable to our list of synchronous sections.
SyncSection* section = mSyncSections.AppendElement();
section->mStable = aStable;
section->mRunnable = aRunnable;
// If aStable is false then this synchronous section is supposed to run before
// the next event at the current nesting level. Record the event loop nesting
// level and the thread recursion level so that the synchronous section will
// run at the proper time.
if (!aStable) {
section->mEventloopNestingLevel = mEventloopNestingLevel;
nsCOMPtr<nsIThreadInternal> threadInternal = do_QueryInterface(thread);
NS_ASSERTION(threadInternal, "This should never fail!");
uint32_t recursionLevel;
if (NS_FAILED(threadInternal->GetRecursionDepth(&recursionLevel))) {
NS_ERROR("This should never fail!");
}
// Due to the weird way that the thread recursion counter is implemented we
// subtract one from the recursion level if we have one.
section->mThreadRecursionLevel = recursionLevel ? recursionLevel - 1 : 0;
}
// Ensure we've got a pending event, else the callbacks will never run.
if (!NS_HasPendingEvents(thread) && !DispatchDummyEvent(thread)) {
RunSyncSections(true, 0);
}
}
// Called from the main thread
NS_IMETHODIMP
nsBaseAppShell::AfterProcessNextEvent(nsIThreadInternal *thr,
uint32_t recursionDepth,
bool eventWasProcessed)
{
// We've just finished running an event, so we're in a stable state.
RunSyncSections(true, recursionDepth);
return NS_OK;
}
NS_IMETHODIMP
nsBaseAppShell::Observe(nsISupports *subject, const char *topic,
const char16_t *data)
{
NS_ASSERTION(!strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID), "oops");
Exit();
return NS_OK;
}
NS_IMETHODIMP
nsBaseAppShell::RunInStableState(nsIRunnable* aRunnable)
{
ScheduleSyncSection(aRunnable, true);
return NS_OK;
}
NS_IMETHODIMP
nsBaseAppShell::RunBeforeNextEvent(nsIRunnable* aRunnable)
{
ScheduleSyncSection(aRunnable, false);
return NS_OK;
}