2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-09-27 18:42:08 -07:00
|
|
|
#include "mozilla/ipc/MessageChannel.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAppShell.h"
|
|
|
|
#include "nsToolkit.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-03-17 21:41:24 -07:00
|
|
|
#include "WinUtils.h"
|
2009-10-06 14:42:45 -07:00
|
|
|
#include "WinTaskbar.h"
|
2012-03-21 17:59:12 -07:00
|
|
|
#include "WinMouseScrollHandler.h"
|
|
|
|
#include "nsWindowDefs.h"
|
2009-12-17 21:54:02 -08:00
|
|
|
#include "nsString.h"
|
2013-02-24 20:00:07 -08:00
|
|
|
#include "WinIMEHandler.h"
|
2011-06-24 07:20:03 -07:00
|
|
|
#include "mozilla/widget/AudioSession.h"
|
2011-10-12 10:52:26 -07:00
|
|
|
#include "mozilla/HangMonitor.h"
|
2014-02-06 16:27:46 -08:00
|
|
|
#include "nsIDOMWakeLockListener.h"
|
|
|
|
#include "nsIPowerManagerService.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2009-12-17 21:54:02 -08:00
|
|
|
|
2013-09-18 21:24:34 -07:00
|
|
|
using namespace mozilla;
|
2013-03-17 21:41:24 -07:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
2014-02-06 16:27:46 -08:00
|
|
|
// A wake lock listener that disables screen saver when requested by
|
|
|
|
// Gecko. For example when we're playing video in a foreground tab we
|
|
|
|
// don't want the screen saver to turn on.
|
|
|
|
class WinWakeLockListener : public nsIDOMMozWakeLockListener {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS;
|
|
|
|
|
|
|
|
private:
|
|
|
|
NS_IMETHOD Callback(const nsAString& aTopic, const nsAString& aState) {
|
|
|
|
if (aState.Equals(NS_LITERAL_STRING("locked-foreground"))) {
|
|
|
|
// Prevent screen saver.
|
|
|
|
SetThreadExecutionState(ES_DISPLAY_REQUIRED|ES_CONTINUOUS);
|
|
|
|
} else {
|
|
|
|
// Re-enable screen saver.
|
|
|
|
SetThreadExecutionState(ES_CONTINUOUS);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(WinWakeLockListener, nsIDOMMozWakeLockListener)
|
|
|
|
StaticRefPtr<WinWakeLockListener> sWakeLockListener;
|
|
|
|
|
|
|
|
static void
|
|
|
|
AddScreenWakeLockListener()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPowerManagerService> sPowerManagerService = do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
|
|
|
if (sPowerManagerService) {
|
|
|
|
sWakeLockListener = new WinWakeLockListener();
|
|
|
|
sPowerManagerService->AddWakeLockListener(sWakeLockListener);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to retrieve PowerManagerService, wakelocks will be broken!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
RemoveScreenWakeLockListener()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPowerManagerService> sPowerManagerService = do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
|
|
|
if (sPowerManagerService) {
|
|
|
|
sPowerManagerService->RemoveWakeLockListener(sWakeLockListener);
|
|
|
|
sPowerManagerService = nullptr;
|
|
|
|
sWakeLockListener = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 07:55:12 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
// Native event callback message.
|
|
|
|
UINT sAppShellGeckoMsgId = RegisterWindowMessageW(L"nsAppShell:EventID");
|
|
|
|
} }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-03 07:12:57 -08:00
|
|
|
const wchar_t* kTaskbarButtonEventId = L"TaskbarButtonCreated";
|
2012-11-02 04:54:44 -07:00
|
|
|
UINT sTaskbarButtonCreatedMsg;
|
2009-10-05 19:26:54 -07:00
|
|
|
|
|
|
|
/* static */
|
|
|
|
UINT nsAppShell::GetTaskbarButtonCreatedMessage() {
|
|
|
|
return sTaskbarButtonCreatedMsg;
|
|
|
|
}
|
|
|
|
|
2010-11-21 16:29:47 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace crashreporter {
|
|
|
|
void LSPAnnotate();
|
|
|
|
} // namespace crashreporter
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
using mozilla::crashreporter::LSPAnnotate;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/*static*/ LRESULT CALLBACK
|
|
|
|
nsAppShell::EventWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2013-08-07 07:55:12 -07:00
|
|
|
if (uMsg == sAppShellGeckoMsgId) {
|
2007-07-08 00:08:04 -07:00
|
|
|
nsAppShell *as = reinterpret_cast<nsAppShell *>(lParam);
|
2007-03-22 10:30:00 -07:00
|
|
|
as->NativeEventCallback();
|
|
|
|
NS_RELEASE(as);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAppShell::~nsAppShell()
|
|
|
|
{
|
|
|
|
if (mEventWnd) {
|
|
|
|
// DestroyWindow doesn't do anything when called from a non UI thread.
|
|
|
|
// Since mEventWnd was created on the UI thread, it must be destroyed on
|
|
|
|
// the UI thread.
|
|
|
|
SendMessage(mEventWnd, WM_CLOSE, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsAppShell::Init()
|
|
|
|
{
|
2010-11-21 16:29:47 -08:00
|
|
|
#ifdef MOZ_CRASHREPORTER
|
|
|
|
LSPAnnotate();
|
|
|
|
#endif
|
|
|
|
|
2013-02-11 13:56:59 -08:00
|
|
|
mLastNativeEventScheduled = TimeStamp::NowLoRes();
|
2011-08-09 07:48:10 -07:00
|
|
|
|
2010-11-29 08:25:16 -08:00
|
|
|
sTaskbarButtonCreatedMsg = ::RegisterWindowMessageW(kTaskbarButtonEventId);
|
2009-10-05 19:26:54 -07:00
|
|
|
NS_ASSERTION(sTaskbarButtonCreatedMsg, "Could not register taskbar button creation message");
|
|
|
|
|
2008-08-29 14:26:56 -07:00
|
|
|
WNDCLASSW wc;
|
2013-10-08 11:48:02 -07:00
|
|
|
HINSTANCE module = GetModuleHandle(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-03 07:12:57 -08:00
|
|
|
const wchar_t *const kWindowClass = L"nsAppShell:EventWindowClass";
|
2008-08-29 14:26:56 -07:00
|
|
|
if (!GetClassInfoW(module, kWindowClass, &wc)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
wc.style = 0;
|
|
|
|
wc.lpfnWndProc = EventWindowProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = module;
|
2013-10-08 11:48:02 -07:00
|
|
|
wc.hIcon = nullptr;
|
|
|
|
wc.hCursor = nullptr;
|
|
|
|
wc.hbrBackground = (HBRUSH) nullptr;
|
|
|
|
wc.lpszMenuName = (LPCWSTR) nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
wc.lpszClassName = kWindowClass;
|
2008-08-29 14:26:56 -07:00
|
|
|
RegisterClassW(&wc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-08-29 14:26:56 -07:00
|
|
|
mEventWnd = CreateWindowW(kWindowClass, L"nsAppShell:EventWindow",
|
2013-10-08 11:48:02 -07:00
|
|
|
0, 0, 0, 10, 10, nullptr, nullptr, module, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_STATE(mEventWnd);
|
|
|
|
|
|
|
|
return nsBaseAppShell::Init();
|
|
|
|
}
|
|
|
|
|
2009-12-17 21:54:02 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAppShell::Run(void)
|
|
|
|
{
|
2011-06-24 07:20:03 -07:00
|
|
|
// Ignore failure; failing to start the application is not exactly an
|
|
|
|
// appropriate response to failing to start an audio session.
|
|
|
|
mozilla::widget::StartAudioSession();
|
2009-12-17 21:54:02 -08:00
|
|
|
|
2014-02-06 16:27:46 -08:00
|
|
|
// Add an observer that disables the screen saver when requested by Gecko.
|
|
|
|
// For example when we're playing video in the foreground tab.
|
|
|
|
AddScreenWakeLockListener();
|
|
|
|
|
2010-02-23 17:39:14 -08:00
|
|
|
nsresult rv = nsBaseAppShell::Run();
|
|
|
|
|
2014-02-06 16:27:46 -08:00
|
|
|
RemoveScreenWakeLockListener();
|
|
|
|
|
2011-06-24 07:20:03 -07:00
|
|
|
mozilla::widget::StopAudioSession();
|
|
|
|
|
2010-02-23 17:39:14 -08:00
|
|
|
return rv;
|
2009-12-17 21:54:02 -08:00
|
|
|
}
|
|
|
|
|
2012-11-02 04:54:44 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAppShell::Exit(void)
|
|
|
|
{
|
|
|
|
return nsBaseAppShell::Exit();
|
|
|
|
}
|
|
|
|
|
2010-11-29 08:25:16 -08:00
|
|
|
void
|
|
|
|
nsAppShell::DoProcessMoreGeckoEvents()
|
|
|
|
{
|
|
|
|
// Called by nsBaseAppShell's NativeEventCallback() after it has finished
|
|
|
|
// processing pending gecko events and there are still gecko events pending
|
|
|
|
// for the thread. (This can happen if NS_ProcessPendingEvents reached it's
|
|
|
|
// starvation timeout limit.) The default behavior in nsBaseAppShell is to
|
|
|
|
// call ScheduleNativeEventCallback to post a follow up native event callback
|
|
|
|
// message. This triggers an additional call to NativeEventCallback for more
|
|
|
|
// gecko event processing.
|
|
|
|
|
|
|
|
// There's a deadlock risk here with certain internal Windows modal loops. In
|
|
|
|
// our dispatch code, we prioritize messages so that input is handled first.
|
|
|
|
// However Windows modal dispatch loops often prioritize posted messages. If
|
|
|
|
// we find ourselves in a tight gecko timer loop where NS_ProcessPendingEvents
|
|
|
|
// takes longer than the timer duration, NS_HasPendingEvents(thread) will
|
|
|
|
// always be true. ScheduleNativeEventCallback will be called on every
|
|
|
|
// NativeEventCallback callback, and in a Windows modal dispatch loop, the
|
|
|
|
// callback message will be processed first -> input gets starved, dead lock.
|
2014-02-06 16:27:46 -08:00
|
|
|
|
2010-11-29 08:25:16 -08:00
|
|
|
// To avoid, don't post native callback messages from NativeEventCallback
|
|
|
|
// when we're in a modal loop. This gets us back into the Windows modal
|
|
|
|
// dispatch loop dispatching input messages. Once we drop out of the modal
|
|
|
|
// loop, we use mNativeCallbackPending to fire off a final NativeEventCallback
|
|
|
|
// if we need it, which insures NS_ProcessPendingEvents gets called and all
|
|
|
|
// gecko events get processed.
|
|
|
|
if (mEventloopNestingLevel < 2) {
|
2012-07-30 07:20:58 -07:00
|
|
|
OnDispatchedEvent(nullptr);
|
2011-10-01 19:16:19 -07:00
|
|
|
mNativeCallbackPending = false;
|
2010-11-29 08:25:16 -08:00
|
|
|
} else {
|
2011-10-01 19:16:19 -07:00
|
|
|
mNativeCallbackPending = true;
|
2010-11-29 08:25:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsAppShell::ScheduleNativeEventCallback()
|
|
|
|
{
|
2010-11-29 08:25:16 -08:00
|
|
|
// Post a message to the hidden message window
|
|
|
|
NS_ADDREF_THIS(); // will be released when the event is processed
|
2013-09-18 21:24:34 -07:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLastNativeEventScheduledMutex);
|
|
|
|
// Time stamp this event so we can detect cases where the event gets
|
|
|
|
// dropping in sub classes / modal loops we do not control.
|
|
|
|
mLastNativeEventScheduled = TimeStamp::NowLoRes();
|
|
|
|
}
|
2013-08-07 07:55:12 -07:00
|
|
|
::PostMessage(mEventWnd, sAppShellGeckoMsgId, 0, reinterpret_cast<LPARAM>(this));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
|
|
|
nsAppShell::ProcessNextNativeEvent(bool mayWait)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-05-18 04:57:08 -07:00
|
|
|
// Notify ipc we are spinning a (possibly nested) gecko event loop.
|
2013-09-27 18:42:08 -07:00
|
|
|
mozilla::ipc::MessageChannel::NotifyGeckoEventDispatch();
|
2011-05-18 04:57:08 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool gotMessage = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
MSG msg;
|
2013-03-23 07:18:52 -07:00
|
|
|
bool uiMessage = false;
|
|
|
|
|
|
|
|
// For avoiding deadlock between our process and plugin process by
|
|
|
|
// mouse wheel messages, we're handling actually when we receive one of
|
|
|
|
// following internal messages which is posted by native mouse wheel
|
|
|
|
// message handler. Any other events, especially native modifier key
|
|
|
|
// events, should not be handled between native message and posted
|
|
|
|
// internal message because it may make different modifier key state or
|
|
|
|
// mouse cursor position between them.
|
|
|
|
if (mozilla::widget::MouseScrollHandler::IsWaitingInternalMessage()) {
|
2013-10-08 11:48:02 -07:00
|
|
|
gotMessage = WinUtils::PeekMessage(&msg, nullptr, MOZ_WM_MOUSEWHEEL_FIRST,
|
2013-03-23 07:18:52 -07:00
|
|
|
MOZ_WM_MOUSEWHEEL_LAST, PM_REMOVE);
|
|
|
|
NS_ASSERTION(gotMessage,
|
|
|
|
"waiting internal wheel message, but it has not come");
|
|
|
|
uiMessage = gotMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gotMessage) {
|
2013-10-08 11:48:02 -07:00
|
|
|
gotMessage = WinUtils::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE);
|
2013-03-23 07:18:52 -07:00
|
|
|
uiMessage =
|
|
|
|
(msg.message >= WM_KEYFIRST && msg.message <= WM_IME_KEYLAST) ||
|
|
|
|
(msg.message >= NS_WM_IMEFIRST && msg.message <= NS_WM_IMELAST) ||
|
|
|
|
(msg.message >= WM_MOUSEFIRST && msg.message <= WM_MOUSELAST);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gotMessage) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (msg.message == WM_QUIT) {
|
2008-08-09 11:49:42 -07:00
|
|
|
::PostQuitMessage(msg.wParam);
|
2007-03-22 10:30:00 -07:00
|
|
|
Exit();
|
|
|
|
} else {
|
2012-05-25 06:22:19 -07:00
|
|
|
// If we had UI activity we would be processing it now so we know we
|
|
|
|
// have either kUIActivity or kActivityNoUIAVail.
|
|
|
|
mozilla::HangMonitor::NotifyActivity(
|
|
|
|
uiMessage ? mozilla::HangMonitor::kUIActivity :
|
|
|
|
mozilla::HangMonitor::kActivityNoUIAVail);
|
2013-03-17 21:41:24 -07:00
|
|
|
|
|
|
|
if (msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST &&
|
|
|
|
IMEHandler::ProcessRawKeyMessage(msg)) {
|
|
|
|
continue; // the message is consumed.
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
::TranslateMessage(&msg);
|
|
|
|
::DispatchMessageW(&msg);
|
|
|
|
}
|
|
|
|
} else if (mayWait) {
|
|
|
|
// Block and wait for any posted application message
|
2011-10-12 10:52:26 -07:00
|
|
|
mozilla::HangMonitor::Suspend();
|
2013-11-11 10:59:45 -08:00
|
|
|
::WaitMessage();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} while (!gotMessage && mayWait);
|
|
|
|
|
2010-11-29 08:25:16 -08:00
|
|
|
// See DoProcessNextNativeEvent, mEventloopNestingLevel will be
|
|
|
|
// one when a modal loop unwinds.
|
|
|
|
if (mNativeCallbackPending && mEventloopNestingLevel == 1)
|
|
|
|
DoProcessMoreGeckoEvents();
|
|
|
|
|
2011-08-09 07:48:10 -07:00
|
|
|
// Check for starved native callbacks. If we haven't processed one
|
|
|
|
// of these events in NATIVE_EVENT_STARVATION_LIMIT, fire one off.
|
2013-02-11 13:56:59 -08:00
|
|
|
static const mozilla::TimeDuration nativeEventStarvationLimit =
|
|
|
|
mozilla::TimeDuration::FromSeconds(NATIVE_EVENT_STARVATION_LIMIT);
|
|
|
|
|
2013-09-18 21:24:34 -07:00
|
|
|
TimeDuration timeSinceLastNativeEventScheduled;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLastNativeEventScheduledMutex);
|
|
|
|
timeSinceLastNativeEventScheduled =
|
|
|
|
TimeStamp::NowLoRes() - mLastNativeEventScheduled;
|
|
|
|
}
|
|
|
|
if (timeSinceLastNativeEventScheduled > nativeEventStarvationLimit) {
|
2011-08-09 07:48:10 -07:00
|
|
|
ScheduleNativeEventCallback();
|
|
|
|
}
|
2014-02-06 16:27:46 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return gotMessage;
|
|
|
|
}
|