1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Widget code.
17 * The Initial Developer of the Original Code is Google Inc.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
22 * Darin Fisher <darin@meer.net> (original author)
23 * Mats Palmgren <mats.palmgren@bredband.net>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsBaseAppShell.h"
40 #include "nsThreadUtils.h"
41 #include "nsIObserverService.h"
42 #include "nsServiceManagerUtils.h"
44 // When processing the next thread event, the appshell may process native
45 // events (if not in performance mode), which can result in suppressing the
46 // next thread event for at most this many ticks:
47 #define THREAD_EVENT_STARVATION_LIMIT PR_MillisecondsToInterval(20)
49 NS_IMPL_THREADSAFE_ISUPPORTS3(nsBaseAppShell
, nsIAppShell
, nsIThreadObserver
,
52 nsBaseAppShell::nsBaseAppShell()
53 : mSuspendNativeCount(0)
54 , mBlockedWait(nsnull
)
56 , mNativeEventPending(0)
57 , mEventloopNestingLevel(0)
60 , mLastNativeEventTime(0)
61 , mEventloopNestingState(eEventloopNone
)
62 , mRunWasCalled(PR_FALSE
)
64 , mBlockNativeEvent(PR_FALSE
)
69 nsBaseAppShell::Init()
71 // Configure ourselves as an observer for the current thread:
73 nsCOMPtr
<nsIThreadInternal
> threadInt
=
74 do_QueryInterface(NS_GetCurrentThread());
75 NS_ENSURE_STATE(threadInt
);
77 threadInt
->SetObserver(this);
79 nsCOMPtr
<nsIObserverService
> obsSvc
=
80 do_GetService("@mozilla.org/observer-service;1");
82 obsSvc
->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID
, PR_FALSE
);
87 nsBaseAppShell::NativeEventCallback()
89 PRInt32 hasPending
= PR_AtomicSet(&mNativeEventPending
, 0);
93 // If DoProcessNextNativeEvent is on the stack, then we assume that we can
94 // just unwind and let nsThread::ProcessNextEvent process the next event.
95 // However, if we are called from a nested native event loop (maybe via some
96 // plug-in or library function), then go ahead and process Gecko events now.
97 if (mEventloopNestingState
== eEventloopXPCOM
) {
98 mEventloopNestingState
= eEventloopOther
;
99 // XXX there is a tiny risk we will never get a new NativeEventCallback,
100 // XXX see discussion in bug 389931.
104 // nsBaseAppShell::Run is not being used to pump events, so this may be
105 // our only opportunity to process pending gecko events.
107 nsIThread
*thread
= NS_GetCurrentThread();
108 PRBool prevBlockNativeEvent
= mBlockNativeEvent
;
109 if (mEventloopNestingState
== eEventloopOther
) {
110 if (!NS_HasPendingEvents(thread
))
112 // We're in a nested native event loop and have some gecko events to
113 // process. While doing that we block processing native events from the
114 // appshell - instead, we want to get back to the nested native event
115 // loop ASAP (bug 420148).
116 mBlockNativeEvent
= PR_TRUE
;
119 ++mEventloopNestingLevel
;
120 EventloopNestingState prevVal
= mEventloopNestingState
;
121 NS_ProcessPendingEvents(thread
, THREAD_EVENT_STARVATION_LIMIT
);
122 mEventloopNestingState
= prevVal
;
123 mBlockNativeEvent
= prevBlockNativeEvent
;
125 // Continue processing pending events later (we don't want to starve the
126 // embedders event loop).
127 if (NS_HasPendingEvents(thread
))
128 OnDispatchedEvent(nsnull
);
130 --mEventloopNestingLevel
;
134 nsBaseAppShell::DoProcessNextNativeEvent(PRBool mayWait
)
136 // The next native event to be processed may trigger our NativeEventCallback,
137 // in which case we do not want it to process any thread events since we'll
138 // do that when this function returns.
140 // If the next native event is not our NativeEventCallback, then we may end
141 // up recursing into this function.
143 // However, if the next native event is not our NativeEventCallback, but it
144 // results in another native event loop, then our NativeEventCallback could
145 // fire and it will see mEventloopNestingState as eEventloopOther.
147 EventloopNestingState prevVal
= mEventloopNestingState
;
148 mEventloopNestingState
= eEventloopXPCOM
;
150 ++mEventloopNestingLevel
;
151 PRBool result
= ProcessNextNativeEvent(mayWait
);
152 --mEventloopNestingLevel
;
154 mEventloopNestingState
= prevVal
;
158 //-------------------------------------------------------------------------
159 // nsIAppShell methods:
162 nsBaseAppShell::Run(void)
164 nsIThread
*thread
= NS_GetCurrentThread();
166 NS_ENSURE_STATE(!mRunWasCalled
); // should not call Run twice
167 mRunWasCalled
= PR_TRUE
;
170 NS_ProcessNextEvent(thread
);
172 NS_ProcessPendingEvents(thread
);
177 nsBaseAppShell::Exit(void)
184 nsBaseAppShell::FavorPerformanceHint(PRBool favorPerfOverStarvation
,
185 PRUint32 starvationDelay
)
187 mStarvationDelay
= PR_MillisecondsToInterval(starvationDelay
);
188 if (favorPerfOverStarvation
) {
192 mSwitchTime
= PR_IntervalNow();
198 nsBaseAppShell::SuspendNative()
200 ++mSuspendNativeCount
;
205 nsBaseAppShell::ResumeNative()
207 --mSuspendNativeCount
;
208 NS_ASSERTION(mSuspendNativeCount
>= 0, "Unbalanced call to nsBaseAppShell::ResumeNative!");
213 nsBaseAppShell::GetEventloopNestingLevel(PRUint32
* aNestingLevelResult
)
215 NS_ENSURE_ARG_POINTER(aNestingLevelResult
);
217 *aNestingLevelResult
= mEventloopNestingLevel
;
222 //-------------------------------------------------------------------------
223 // nsIThreadObserver methods:
225 // Called from any thread
227 nsBaseAppShell::OnDispatchedEvent(nsIThreadInternal
*thr
)
229 if (mBlockNativeEvent
)
232 PRInt32 lastVal
= PR_AtomicSet(&mNativeEventPending
, 1);
236 ScheduleNativeEventCallback();
240 // Called from the main thread
242 nsBaseAppShell::OnProcessNextEvent(nsIThreadInternal
*thr
, PRBool mayWait
,
243 PRUint32 recursionDepth
)
245 if (mBlockNativeEvent
) {
248 // Hmm, we're in a nested native event loop and would like to get
249 // back to it ASAP, but it seems a gecko event has caused us to
250 // spin up a nested XPCOM event loop (eg. modal window), so we
251 // really must start processing native events here again.
252 mBlockNativeEvent
= PR_FALSE
;
253 if (NS_HasPendingEvents(thr
))
254 OnDispatchedEvent(thr
); // in case we blocked it earlier
257 PRIntervalTime start
= PR_IntervalNow();
258 PRIntervalTime limit
= THREAD_EVENT_STARVATION_LIMIT
;
260 // Unblock outer nested wait loop (below).
262 *mBlockedWait
= PR_FALSE
;
264 PRBool
*oldBlockedWait
= mBlockedWait
;
265 mBlockedWait
= &mayWait
;
267 // When mayWait is true, we need to make sure that there is an event in the
268 // thread's event queue before we return. Otherwise, the thread will block
269 // on its event queue waiting for an event.
270 PRBool needEvent
= mayWait
;
272 if (mFavorPerf
<= 0 && start
> mSwitchTime
+ mStarvationDelay
) {
273 // Favor pending native events
274 PRIntervalTime now
= start
;
277 mLastNativeEventTime
= now
;
278 keepGoing
= DoProcessNextNativeEvent(PR_FALSE
);
279 } while (keepGoing
&& ((now
= PR_IntervalNow()) - start
) < limit
);
281 // Avoid starving native events completely when in performance mode
282 if (start
- mLastNativeEventTime
> limit
) {
283 mLastNativeEventTime
= start
;
284 DoProcessNextNativeEvent(PR_FALSE
);
288 while (!NS_HasPendingEvents(thr
)) {
289 // If we have been asked to exit from Run, then we should not wait for
290 // events to process. Note that an inner nested event loop causes
291 // 'mayWait' to become false too, through 'mBlockedWait'.
295 mLastNativeEventTime
= PR_IntervalNow();
296 if (!DoProcessNextNativeEvent(mayWait
) || !mayWait
)
300 mBlockedWait
= oldBlockedWait
;
302 // Make sure that the thread event queue does not block on its monitor, as
303 // it normally would do if it did not have any pending events. To avoid
304 // that, we simply insert a dummy event into its queue during shutdown.
305 if (needEvent
&& !NS_HasPendingEvents(thr
)) {
307 mDummyEvent
= new nsRunnable();
308 thr
->Dispatch(mDummyEvent
, NS_DISPATCH_NORMAL
);
314 // Called from the main thread
316 nsBaseAppShell::AfterProcessNextEvent(nsIThreadInternal
*thr
,
317 PRUint32 recursionDepth
)
323 nsBaseAppShell::Observe(nsISupports
*subject
, const char *topic
,
324 const PRUnichar
*data
)
326 NS_ASSERTION(!strcmp(topic
, NS_XPCOM_SHUTDOWN_OBSERVER_ID
), "oops");