widget/xpwidgets/nsBaseAppShell.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/xpwidgets/nsBaseAppShell.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,167 @@
     1.4 +/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsBaseAppShell_h__
    1.10 +#define nsBaseAppShell_h__
    1.11 +
    1.12 +#include "mozilla/Atomics.h"
    1.13 +#include "nsIAppShell.h"
    1.14 +#include "nsIThreadInternal.h"
    1.15 +#include "nsIObserver.h"
    1.16 +#include "nsIRunnable.h"
    1.17 +#include "nsCOMPtr.h"
    1.18 +#include "nsTArray.h"
    1.19 +#include "prinrval.h"
    1.20 +
    1.21 +/**
    1.22 + * A singleton that manages the UI thread's event queue.  Subclass this class
    1.23 + * to enable platform-specific event queue support.
    1.24 + */
    1.25 +class nsBaseAppShell : public nsIAppShell, public nsIThreadObserver,
    1.26 +                       public nsIObserver
    1.27 +{
    1.28 +public:
    1.29 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.30 +  NS_DECL_NSIAPPSHELL
    1.31 +  NS_DECL_NSITHREADOBSERVER
    1.32 +  NS_DECL_NSIOBSERVER
    1.33 +
    1.34 +  nsBaseAppShell();
    1.35 +
    1.36 +protected:
    1.37 +  virtual ~nsBaseAppShell();
    1.38 +
    1.39 +  /**
    1.40 +   * This method is called by subclasses when the app shell singleton is
    1.41 +   * instantiated.
    1.42 +   */
    1.43 +  nsresult Init();
    1.44 +
    1.45 +  /**
    1.46 +   * Called by subclasses from a native event. See ScheduleNativeEventCallback.
    1.47 +   */
    1.48 +  void NativeEventCallback();
    1.49 +
    1.50 +  /**
    1.51 +   * Make a decision as to whether or not NativeEventCallback will
    1.52 +   * trigger gecko event processing when there are pending gecko
    1.53 +   * events.
    1.54 +   */
    1.55 +  virtual void DoProcessMoreGeckoEvents();
    1.56 +
    1.57 +  /**
    1.58 +   * Implemented by subclasses.  Invoke NativeEventCallback from a native
    1.59 +   * event.  This method may be called on any thread.
    1.60 +   */
    1.61 +  virtual void ScheduleNativeEventCallback() = 0;
    1.62 +
    1.63 +  /**
    1.64 +   * Implemented by subclasses.  Process the next native event.  Only wait for
    1.65 +   * the next native event if mayWait is true.  This method is only called on
    1.66 +   * the main application thread.
    1.67 +   *
    1.68 +   * @param mayWait
    1.69 +   *   If "true", then this method may wait if necessary for the next available
    1.70 +   *   native event.  DispatchNativeEvent may be called to unblock a call to
    1.71 +   *   ProcessNextNativeEvent that is waiting.
    1.72 +   * @return
    1.73 +   *   This method returns "true" if a native event was processed.
    1.74 +   */
    1.75 +  virtual bool ProcessNextNativeEvent(bool mayWait) = 0;
    1.76 +
    1.77 +  int32_t mSuspendNativeCount;
    1.78 +  uint32_t mEventloopNestingLevel;
    1.79 +
    1.80 +private:
    1.81 +  bool DoProcessNextNativeEvent(bool mayWait, uint32_t recursionDepth);
    1.82 +
    1.83 +  bool DispatchDummyEvent(nsIThread* target);
    1.84 +
    1.85 +  void IncrementEventloopNestingLevel();
    1.86 +  void DecrementEventloopNestingLevel();
    1.87 +
    1.88 +  /**
    1.89 +   * Runs all synchronous sections which are queued up in mSyncSections.
    1.90 +   */
    1.91 +  void RunSyncSectionsInternal(bool stable, uint32_t threadRecursionLevel);
    1.92 +
    1.93 +  void RunSyncSections(bool stable, uint32_t threadRecursionLevel)
    1.94 +  {
    1.95 +    if (!mSyncSections.IsEmpty()) {
    1.96 +      RunSyncSectionsInternal(stable, threadRecursionLevel);
    1.97 +    }
    1.98 +  }
    1.99 +
   1.100 +  void ScheduleSyncSection(nsIRunnable* runnable, bool stable);
   1.101 +
   1.102 +  struct SyncSection {
   1.103 +    SyncSection()
   1.104 +    : mStable(false), mEventloopNestingLevel(0), mThreadRecursionLevel(0)
   1.105 +    { }
   1.106 +
   1.107 +    void Forget(SyncSection* other) {
   1.108 +      other->mStable = mStable;
   1.109 +      other->mEventloopNestingLevel = mEventloopNestingLevel;
   1.110 +      other->mThreadRecursionLevel = mThreadRecursionLevel;
   1.111 +      other->mRunnable = mRunnable.forget();
   1.112 +    }
   1.113 +
   1.114 +    bool mStable;
   1.115 +    uint32_t mEventloopNestingLevel;
   1.116 +    uint32_t mThreadRecursionLevel;
   1.117 +    nsCOMPtr<nsIRunnable> mRunnable;
   1.118 +  };
   1.119 +
   1.120 +  nsCOMPtr<nsIRunnable> mDummyEvent;
   1.121 +  /**
   1.122 +   * mBlockedWait points back to a slot that controls the wait loop in
   1.123 +   * an outer OnProcessNextEvent invocation.  Nested calls always set
   1.124 +   * it to false to unblock an outer loop, since all events may
   1.125 +   * have been consumed by the inner event loop(s).
   1.126 +   */
   1.127 +  bool *mBlockedWait;
   1.128 +  int32_t mFavorPerf;
   1.129 +  mozilla::Atomic<bool> mNativeEventPending;
   1.130 +  PRIntervalTime mStarvationDelay;
   1.131 +  PRIntervalTime mSwitchTime;
   1.132 +  PRIntervalTime mLastNativeEventTime;
   1.133 +  enum EventloopNestingState {
   1.134 +    eEventloopNone,  // top level thread execution
   1.135 +    eEventloopXPCOM, // innermost native event loop is ProcessNextNativeEvent
   1.136 +    eEventloopOther  // innermost native event loop is a native library/plugin etc
   1.137 +  };
   1.138 +  EventloopNestingState mEventloopNestingState;
   1.139 +  nsTArray<SyncSection> mSyncSections;
   1.140 +  bool mRunning;
   1.141 +  bool mExiting;
   1.142 +  /**
   1.143 +   * mBlockNativeEvent blocks the appshell from processing native events.
   1.144 +   * It is set to true while a nested native event loop (eEventloopOther)
   1.145 +   * is processing gecko events in NativeEventCallback(), thus queuing up
   1.146 +   * native events until we return to that loop (bug 420148).
   1.147 +   * We force mBlockNativeEvent to false in case handling one of the gecko
   1.148 +   * events spins up a nested XPCOM event loop (eg. modal window) which would
   1.149 +   * otherwise lead to a "deadlock" where native events aren't processed at all.
   1.150 +   */
   1.151 +  bool mBlockNativeEvent;
   1.152 +  /**
   1.153 +   * Tracks whether we have processed any gecko events in NativeEventCallback so
   1.154 +   * that we can avoid erroneously entering a blocking loop waiting for gecko
   1.155 +   * events to show up during OnProcessNextEvent.  This is required because on
   1.156 +   * OS X ProcessGeckoEvents may be invoked inside the context of
   1.157 +   * ProcessNextNativeEvent and may result in NativeEventCallback being invoked
   1.158 +   * and in turn invoking NS_ProcessPendingEvents.  Because
   1.159 +   * ProcessNextNativeEvent may be invoked prior to the NS_HasPendingEvents
   1.160 +   * waiting loop, this is the only way to make the loop aware that events may
   1.161 +   * have been processed.
   1.162 +   *
   1.163 +   * This variable is set to false in OnProcessNextEvent prior to the first
   1.164 +   * call to DoProcessNextNativeEvent.  It is set to true by
   1.165 +   * NativeEventCallback after calling NS_ProcessPendingEvents.
   1.166 +   */
   1.167 +  bool mProcessedGeckoEvents;
   1.168 +};
   1.169 +
   1.170 +#endif // nsBaseAppShell_h__

mercurial