widget/xpwidgets/nsBaseAppShell.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "base/message_loop.h"
michael@0 7
michael@0 8 #include "nsBaseAppShell.h"
michael@0 9 #if defined(MOZ_CRASHREPORTER)
michael@0 10 #include "nsExceptionHandler.h"
michael@0 11 #endif
michael@0 12 #include "nsThreadUtils.h"
michael@0 13 #include "nsIObserverService.h"
michael@0 14 #include "nsServiceManagerUtils.h"
michael@0 15 #include "mozilla/Services.h"
michael@0 16
michael@0 17 // When processing the next thread event, the appshell may process native
michael@0 18 // events (if not in performance mode), which can result in suppressing the
michael@0 19 // next thread event for at most this many ticks:
michael@0 20 #define THREAD_EVENT_STARVATION_LIMIT PR_MillisecondsToInterval(20)
michael@0 21
michael@0 22 NS_IMPL_ISUPPORTS(nsBaseAppShell, nsIAppShell, nsIThreadObserver, nsIObserver)
michael@0 23
michael@0 24 nsBaseAppShell::nsBaseAppShell()
michael@0 25 : mSuspendNativeCount(0)
michael@0 26 , mEventloopNestingLevel(0)
michael@0 27 , mBlockedWait(nullptr)
michael@0 28 , mFavorPerf(0)
michael@0 29 , mNativeEventPending(false)
michael@0 30 , mStarvationDelay(0)
michael@0 31 , mSwitchTime(0)
michael@0 32 , mLastNativeEventTime(0)
michael@0 33 , mEventloopNestingState(eEventloopNone)
michael@0 34 , mRunning(false)
michael@0 35 , mExiting(false)
michael@0 36 , mBlockNativeEvent(false)
michael@0 37 {
michael@0 38 }
michael@0 39
michael@0 40 nsBaseAppShell::~nsBaseAppShell()
michael@0 41 {
michael@0 42 NS_ASSERTION(mSyncSections.IsEmpty(), "Must have run all sync sections");
michael@0 43 }
michael@0 44
michael@0 45 nsresult
michael@0 46 nsBaseAppShell::Init()
michael@0 47 {
michael@0 48 // Configure ourselves as an observer for the current thread:
michael@0 49
michael@0 50 nsCOMPtr<nsIThreadInternal> threadInt =
michael@0 51 do_QueryInterface(NS_GetCurrentThread());
michael@0 52 NS_ENSURE_STATE(threadInt);
michael@0 53
michael@0 54 threadInt->SetObserver(this);
michael@0 55
michael@0 56 nsCOMPtr<nsIObserverService> obsSvc =
michael@0 57 mozilla::services::GetObserverService();
michael@0 58 if (obsSvc)
michael@0 59 obsSvc->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
michael@0 60 return NS_OK;
michael@0 61 }
michael@0 62
michael@0 63 // Called by nsAppShell's native event callback
michael@0 64 void
michael@0 65 nsBaseAppShell::NativeEventCallback()
michael@0 66 {
michael@0 67 if (!mNativeEventPending.exchange(false))
michael@0 68 return;
michael@0 69
michael@0 70 // If DoProcessNextNativeEvent is on the stack, then we assume that we can
michael@0 71 // just unwind and let nsThread::ProcessNextEvent process the next event.
michael@0 72 // However, if we are called from a nested native event loop (maybe via some
michael@0 73 // plug-in or library function), then go ahead and process Gecko events now.
michael@0 74 if (mEventloopNestingState == eEventloopXPCOM) {
michael@0 75 mEventloopNestingState = eEventloopOther;
michael@0 76 // XXX there is a tiny risk we will never get a new NativeEventCallback,
michael@0 77 // XXX see discussion in bug 389931.
michael@0 78 return;
michael@0 79 }
michael@0 80
michael@0 81 // nsBaseAppShell::Run is not being used to pump events, so this may be
michael@0 82 // our only opportunity to process pending gecko events.
michael@0 83
michael@0 84 nsIThread *thread = NS_GetCurrentThread();
michael@0 85 bool prevBlockNativeEvent = mBlockNativeEvent;
michael@0 86 if (mEventloopNestingState == eEventloopOther) {
michael@0 87 if (!NS_HasPendingEvents(thread))
michael@0 88 return;
michael@0 89 // We're in a nested native event loop and have some gecko events to
michael@0 90 // process. While doing that we block processing native events from the
michael@0 91 // appshell - instead, we want to get back to the nested native event
michael@0 92 // loop ASAP (bug 420148).
michael@0 93 mBlockNativeEvent = true;
michael@0 94 }
michael@0 95
michael@0 96 IncrementEventloopNestingLevel();
michael@0 97 EventloopNestingState prevVal = mEventloopNestingState;
michael@0 98 NS_ProcessPendingEvents(thread, THREAD_EVENT_STARVATION_LIMIT);
michael@0 99 mProcessedGeckoEvents = true;
michael@0 100 mEventloopNestingState = prevVal;
michael@0 101 mBlockNativeEvent = prevBlockNativeEvent;
michael@0 102
michael@0 103 // Continue processing pending events later (we don't want to starve the
michael@0 104 // embedders event loop).
michael@0 105 if (NS_HasPendingEvents(thread))
michael@0 106 DoProcessMoreGeckoEvents();
michael@0 107
michael@0 108 DecrementEventloopNestingLevel();
michael@0 109 }
michael@0 110
michael@0 111 // Note, this is currently overidden on windows, see comments in nsAppShell for
michael@0 112 // details.
michael@0 113 void
michael@0 114 nsBaseAppShell::DoProcessMoreGeckoEvents()
michael@0 115 {
michael@0 116 OnDispatchedEvent(nullptr);
michael@0 117 }
michael@0 118
michael@0 119
michael@0 120 // Main thread via OnProcessNextEvent below
michael@0 121 bool
michael@0 122 nsBaseAppShell::DoProcessNextNativeEvent(bool mayWait, uint32_t recursionDepth)
michael@0 123 {
michael@0 124 // The next native event to be processed may trigger our NativeEventCallback,
michael@0 125 // in which case we do not want it to process any thread events since we'll
michael@0 126 // do that when this function returns.
michael@0 127 //
michael@0 128 // If the next native event is not our NativeEventCallback, then we may end
michael@0 129 // up recursing into this function.
michael@0 130 //
michael@0 131 // However, if the next native event is not our NativeEventCallback, but it
michael@0 132 // results in another native event loop, then our NativeEventCallback could
michael@0 133 // fire and it will see mEventloopNestingState as eEventloopOther.
michael@0 134 //
michael@0 135 EventloopNestingState prevVal = mEventloopNestingState;
michael@0 136 mEventloopNestingState = eEventloopXPCOM;
michael@0 137
michael@0 138 IncrementEventloopNestingLevel();
michael@0 139
michael@0 140 bool result = ProcessNextNativeEvent(mayWait);
michael@0 141
michael@0 142 // Make sure that any sync sections registered during this most recent event
michael@0 143 // are run now. This is not considered a stable state because we're not back
michael@0 144 // to the event loop yet.
michael@0 145 RunSyncSections(false, recursionDepth);
michael@0 146
michael@0 147 DecrementEventloopNestingLevel();
michael@0 148
michael@0 149 mEventloopNestingState = prevVal;
michael@0 150 return result;
michael@0 151 }
michael@0 152
michael@0 153 //-------------------------------------------------------------------------
michael@0 154 // nsIAppShell methods:
michael@0 155
michael@0 156 NS_IMETHODIMP
michael@0 157 nsBaseAppShell::Run(void)
michael@0 158 {
michael@0 159 NS_ENSURE_STATE(!mRunning); // should not call Run twice
michael@0 160 mRunning = true;
michael@0 161
michael@0 162 nsIThread *thread = NS_GetCurrentThread();
michael@0 163
michael@0 164 MessageLoop::current()->Run();
michael@0 165
michael@0 166 NS_ProcessPendingEvents(thread);
michael@0 167
michael@0 168 mRunning = false;
michael@0 169 return NS_OK;
michael@0 170 }
michael@0 171
michael@0 172 NS_IMETHODIMP
michael@0 173 nsBaseAppShell::Exit(void)
michael@0 174 {
michael@0 175 if (mRunning && !mExiting) {
michael@0 176 MessageLoop::current()->Quit();
michael@0 177 }
michael@0 178 mExiting = true;
michael@0 179 return NS_OK;
michael@0 180 }
michael@0 181
michael@0 182 NS_IMETHODIMP
michael@0 183 nsBaseAppShell::FavorPerformanceHint(bool favorPerfOverStarvation,
michael@0 184 uint32_t starvationDelay)
michael@0 185 {
michael@0 186 mStarvationDelay = PR_MillisecondsToInterval(starvationDelay);
michael@0 187 if (favorPerfOverStarvation) {
michael@0 188 ++mFavorPerf;
michael@0 189 } else {
michael@0 190 --mFavorPerf;
michael@0 191 mSwitchTime = PR_IntervalNow();
michael@0 192 }
michael@0 193 return NS_OK;
michael@0 194 }
michael@0 195
michael@0 196 NS_IMETHODIMP
michael@0 197 nsBaseAppShell::SuspendNative()
michael@0 198 {
michael@0 199 ++mSuspendNativeCount;
michael@0 200 return NS_OK;
michael@0 201 }
michael@0 202
michael@0 203 NS_IMETHODIMP
michael@0 204 nsBaseAppShell::ResumeNative()
michael@0 205 {
michael@0 206 --mSuspendNativeCount;
michael@0 207 NS_ASSERTION(mSuspendNativeCount >= 0, "Unbalanced call to nsBaseAppShell::ResumeNative!");
michael@0 208 return NS_OK;
michael@0 209 }
michael@0 210
michael@0 211 NS_IMETHODIMP
michael@0 212 nsBaseAppShell::GetEventloopNestingLevel(uint32_t* aNestingLevelResult)
michael@0 213 {
michael@0 214 NS_ENSURE_ARG_POINTER(aNestingLevelResult);
michael@0 215
michael@0 216 *aNestingLevelResult = mEventloopNestingLevel;
michael@0 217
michael@0 218 return NS_OK;
michael@0 219 }
michael@0 220
michael@0 221 //-------------------------------------------------------------------------
michael@0 222 // nsIThreadObserver methods:
michael@0 223
michael@0 224 // Called from any thread
michael@0 225 NS_IMETHODIMP
michael@0 226 nsBaseAppShell::OnDispatchedEvent(nsIThreadInternal *thr)
michael@0 227 {
michael@0 228 if (mBlockNativeEvent)
michael@0 229 return NS_OK;
michael@0 230
michael@0 231 if (mNativeEventPending.exchange(true))
michael@0 232 return NS_OK;
michael@0 233
michael@0 234 // Returns on the main thread in NativeEventCallback above
michael@0 235 ScheduleNativeEventCallback();
michael@0 236 return NS_OK;
michael@0 237 }
michael@0 238
michael@0 239 // Called from the main thread
michael@0 240 NS_IMETHODIMP
michael@0 241 nsBaseAppShell::OnProcessNextEvent(nsIThreadInternal *thr, bool mayWait,
michael@0 242 uint32_t recursionDepth)
michael@0 243 {
michael@0 244 if (mBlockNativeEvent) {
michael@0 245 if (!mayWait)
michael@0 246 return NS_OK;
michael@0 247 // Hmm, we're in a nested native event loop and would like to get
michael@0 248 // back to it ASAP, but it seems a gecko event has caused us to
michael@0 249 // spin up a nested XPCOM event loop (eg. modal window), so we
michael@0 250 // really must start processing native events here again.
michael@0 251 mBlockNativeEvent = false;
michael@0 252 if (NS_HasPendingEvents(thr))
michael@0 253 OnDispatchedEvent(thr); // in case we blocked it earlier
michael@0 254 }
michael@0 255
michael@0 256 PRIntervalTime start = PR_IntervalNow();
michael@0 257 PRIntervalTime limit = THREAD_EVENT_STARVATION_LIMIT;
michael@0 258
michael@0 259 // Unblock outer nested wait loop (below).
michael@0 260 if (mBlockedWait)
michael@0 261 *mBlockedWait = false;
michael@0 262
michael@0 263 bool *oldBlockedWait = mBlockedWait;
michael@0 264 mBlockedWait = &mayWait;
michael@0 265
michael@0 266 // When mayWait is true, we need to make sure that there is an event in the
michael@0 267 // thread's event queue before we return. Otherwise, the thread will block
michael@0 268 // on its event queue waiting for an event.
michael@0 269 bool needEvent = mayWait;
michael@0 270 // Reset prior to invoking DoProcessNextNativeEvent which might cause
michael@0 271 // NativeEventCallback to process gecko events.
michael@0 272 mProcessedGeckoEvents = false;
michael@0 273
michael@0 274 if (mFavorPerf <= 0 && start > mSwitchTime + mStarvationDelay) {
michael@0 275 // Favor pending native events
michael@0 276 PRIntervalTime now = start;
michael@0 277 bool keepGoing;
michael@0 278 do {
michael@0 279 mLastNativeEventTime = now;
michael@0 280 keepGoing = DoProcessNextNativeEvent(false, recursionDepth);
michael@0 281 } while (keepGoing && ((now = PR_IntervalNow()) - start) < limit);
michael@0 282 } else {
michael@0 283 // Avoid starving native events completely when in performance mode
michael@0 284 if (start - mLastNativeEventTime > limit) {
michael@0 285 mLastNativeEventTime = start;
michael@0 286 DoProcessNextNativeEvent(false, recursionDepth);
michael@0 287 }
michael@0 288 }
michael@0 289
michael@0 290 while (!NS_HasPendingEvents(thr) && !mProcessedGeckoEvents) {
michael@0 291 // If we have been asked to exit from Run, then we should not wait for
michael@0 292 // events to process. Note that an inner nested event loop causes
michael@0 293 // 'mayWait' to become false too, through 'mBlockedWait'.
michael@0 294 if (mExiting)
michael@0 295 mayWait = false;
michael@0 296
michael@0 297 mLastNativeEventTime = PR_IntervalNow();
michael@0 298 if (!DoProcessNextNativeEvent(mayWait, recursionDepth) || !mayWait)
michael@0 299 break;
michael@0 300 }
michael@0 301
michael@0 302 mBlockedWait = oldBlockedWait;
michael@0 303
michael@0 304 // Make sure that the thread event queue does not block on its monitor, as
michael@0 305 // it normally would do if it did not have any pending events. To avoid
michael@0 306 // that, we simply insert a dummy event into its queue during shutdown.
michael@0 307 if (needEvent && !mExiting && !NS_HasPendingEvents(thr)) {
michael@0 308 DispatchDummyEvent(thr);
michael@0 309 }
michael@0 310
michael@0 311 // We're about to run an event, so we're in a stable state.
michael@0 312 RunSyncSections(true, recursionDepth);
michael@0 313
michael@0 314 return NS_OK;
michael@0 315 }
michael@0 316
michael@0 317 bool
michael@0 318 nsBaseAppShell::DispatchDummyEvent(nsIThread* aTarget)
michael@0 319 {
michael@0 320 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 321
michael@0 322 if (!mDummyEvent)
michael@0 323 mDummyEvent = new nsRunnable();
michael@0 324
michael@0 325 return NS_SUCCEEDED(aTarget->Dispatch(mDummyEvent, NS_DISPATCH_NORMAL));
michael@0 326 }
michael@0 327
michael@0 328 void
michael@0 329 nsBaseAppShell::IncrementEventloopNestingLevel()
michael@0 330 {
michael@0 331 ++mEventloopNestingLevel;
michael@0 332 #if defined(MOZ_CRASHREPORTER)
michael@0 333 CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel);
michael@0 334 #endif
michael@0 335 }
michael@0 336
michael@0 337 void
michael@0 338 nsBaseAppShell::DecrementEventloopNestingLevel()
michael@0 339 {
michael@0 340 --mEventloopNestingLevel;
michael@0 341 #if defined(MOZ_CRASHREPORTER)
michael@0 342 CrashReporter::SetEventloopNestingLevel(mEventloopNestingLevel);
michael@0 343 #endif
michael@0 344 }
michael@0 345
michael@0 346 void
michael@0 347 nsBaseAppShell::RunSyncSectionsInternal(bool aStable,
michael@0 348 uint32_t aThreadRecursionLevel)
michael@0 349 {
michael@0 350 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
michael@0 351 NS_ASSERTION(!mSyncSections.IsEmpty(), "Nothing to do!");
michael@0 352
michael@0 353 // We've got synchronous sections. Run all of them that are are awaiting a
michael@0 354 // stable state if aStable is true (i.e. we really are in a stable state).
michael@0 355 // Also run the synchronous sections that are simply waiting for the right
michael@0 356 // combination of event loop nesting level and thread recursion level.
michael@0 357 // Note that a synchronous section could add another synchronous section, so
michael@0 358 // we don't remove elements from mSyncSections until all sections have been
michael@0 359 // run, or else we'll screw up our iteration. Any sync sections that are not
michael@0 360 // ready to be run are saved for later.
michael@0 361
michael@0 362 nsTArray<SyncSection> pendingSyncSections;
michael@0 363
michael@0 364 for (uint32_t i = 0; i < mSyncSections.Length(); i++) {
michael@0 365 SyncSection& section = mSyncSections[i];
michael@0 366 if ((aStable && section.mStable) ||
michael@0 367 (!section.mStable &&
michael@0 368 section.mEventloopNestingLevel == mEventloopNestingLevel &&
michael@0 369 section.mThreadRecursionLevel == aThreadRecursionLevel)) {
michael@0 370 section.mRunnable->Run();
michael@0 371 }
michael@0 372 else {
michael@0 373 // Add to pending list.
michael@0 374 SyncSection* pending = pendingSyncSections.AppendElement();
michael@0 375 section.Forget(pending);
michael@0 376 }
michael@0 377 }
michael@0 378
michael@0 379 mSyncSections.SwapElements(pendingSyncSections);
michael@0 380 }
michael@0 381
michael@0 382 void
michael@0 383 nsBaseAppShell::ScheduleSyncSection(nsIRunnable* aRunnable, bool aStable)
michael@0 384 {
michael@0 385 NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
michael@0 386
michael@0 387 nsIThread* thread = NS_GetCurrentThread();
michael@0 388
michael@0 389 // Add this runnable to our list of synchronous sections.
michael@0 390 SyncSection* section = mSyncSections.AppendElement();
michael@0 391 section->mStable = aStable;
michael@0 392 section->mRunnable = aRunnable;
michael@0 393
michael@0 394 // If aStable is false then this synchronous section is supposed to run before
michael@0 395 // the next event at the current nesting level. Record the event loop nesting
michael@0 396 // level and the thread recursion level so that the synchronous section will
michael@0 397 // run at the proper time.
michael@0 398 if (!aStable) {
michael@0 399 section->mEventloopNestingLevel = mEventloopNestingLevel;
michael@0 400
michael@0 401 nsCOMPtr<nsIThreadInternal> threadInternal = do_QueryInterface(thread);
michael@0 402 NS_ASSERTION(threadInternal, "This should never fail!");
michael@0 403
michael@0 404 uint32_t recursionLevel;
michael@0 405 if (NS_FAILED(threadInternal->GetRecursionDepth(&recursionLevel))) {
michael@0 406 NS_ERROR("This should never fail!");
michael@0 407 }
michael@0 408
michael@0 409 // Due to the weird way that the thread recursion counter is implemented we
michael@0 410 // subtract one from the recursion level if we have one.
michael@0 411 section->mThreadRecursionLevel = recursionLevel ? recursionLevel - 1 : 0;
michael@0 412 }
michael@0 413
michael@0 414 // Ensure we've got a pending event, else the callbacks will never run.
michael@0 415 if (!NS_HasPendingEvents(thread) && !DispatchDummyEvent(thread)) {
michael@0 416 RunSyncSections(true, 0);
michael@0 417 }
michael@0 418 }
michael@0 419
michael@0 420 // Called from the main thread
michael@0 421 NS_IMETHODIMP
michael@0 422 nsBaseAppShell::AfterProcessNextEvent(nsIThreadInternal *thr,
michael@0 423 uint32_t recursionDepth,
michael@0 424 bool eventWasProcessed)
michael@0 425 {
michael@0 426 // We've just finished running an event, so we're in a stable state.
michael@0 427 RunSyncSections(true, recursionDepth);
michael@0 428 return NS_OK;
michael@0 429 }
michael@0 430
michael@0 431 NS_IMETHODIMP
michael@0 432 nsBaseAppShell::Observe(nsISupports *subject, const char *topic,
michael@0 433 const char16_t *data)
michael@0 434 {
michael@0 435 NS_ASSERTION(!strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID), "oops");
michael@0 436 Exit();
michael@0 437 return NS_OK;
michael@0 438 }
michael@0 439
michael@0 440 NS_IMETHODIMP
michael@0 441 nsBaseAppShell::RunInStableState(nsIRunnable* aRunnable)
michael@0 442 {
michael@0 443 ScheduleSyncSection(aRunnable, true);
michael@0 444 return NS_OK;
michael@0 445 }
michael@0 446
michael@0 447 NS_IMETHODIMP
michael@0 448 nsBaseAppShell::RunBeforeNextEvent(nsIRunnable* aRunnable)
michael@0 449 {
michael@0 450 ScheduleSyncSection(aRunnable, false);
michael@0 451 return NS_OK;
michael@0 452 }

mercurial