/* -*- 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 "mozilla/ipc/MessageChannel.h" #include "nsAppShell.h" #include "nsToolkit.h" #include "nsThreadUtils.h" #include "WinUtils.h" #include "WinTaskbar.h" #include "WinMouseScrollHandler.h" #include "nsWindowDefs.h" #include "nsString.h" #include "WinIMEHandler.h" #include "mozilla/widget/AudioSession.h" #include "mozilla/HangMonitor.h" #include "nsIDOMWakeLockListener.h" #include "nsIPowerManagerService.h" #include "mozilla/StaticPtr.h" #include "nsTHashtable.h" #include "nsHashKeys.h" #include "GeckoProfiler.h" using namespace mozilla; using namespace mozilla::widget; // 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) { bool isLocked = mLockedTopics.Contains(aTopic); bool shouldLock = aState.Equals(NS_LITERAL_STRING("locked-foreground")); if (isLocked == shouldLock) { return NS_OK; } if (shouldLock) { if (!mLockedTopics.Count()) { // This is the first topic to request the screen saver be disabled. // Prevent screen saver. SetThreadExecutionState(ES_DISPLAY_REQUIRED|ES_CONTINUOUS); } mLockedTopics.PutEntry(aTopic); } else { mLockedTopics.RemoveEntry(aTopic); if (!mLockedTopics.Count()) { // No other outstanding topics have requested screen saver be disabled. // Re-enable screen saver. SetThreadExecutionState(ES_CONTINUOUS); } } return NS_OK; } // Keep track of all the topics that have requested a wake lock. When the // number of topics in the hashtable reaches zero, we can uninhibit the // screensaver again. nsTHashtable mLockedTopics; }; NS_IMPL_ISUPPORTS(WinWakeLockListener, nsIDOMMozWakeLockListener) StaticRefPtr sWakeLockListener; static void AddScreenWakeLockListener() { nsCOMPtr 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 sPowerManagerService = do_GetService(POWERMANAGERSERVICE_CONTRACTID); if (sPowerManagerService) { sPowerManagerService->RemoveWakeLockListener(sWakeLockListener); sPowerManagerService = nullptr; sWakeLockListener = nullptr; } } namespace mozilla { namespace widget { // Native event callback message. UINT sAppShellGeckoMsgId = RegisterWindowMessageW(L"nsAppShell:EventID"); } } const wchar_t* kTaskbarButtonEventId = L"TaskbarButtonCreated"; UINT sTaskbarButtonCreatedMsg; /* static */ UINT nsAppShell::GetTaskbarButtonCreatedMessage() { return sTaskbarButtonCreatedMsg; } namespace mozilla { namespace crashreporter { void LSPAnnotate(); } // namespace crashreporter } // namespace mozilla using mozilla::crashreporter::LSPAnnotate; //------------------------------------------------------------------------- /*static*/ LRESULT CALLBACK nsAppShell::EventWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == sAppShellGeckoMsgId) { nsAppShell *as = reinterpret_cast(lParam); 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() { #ifdef MOZ_CRASHREPORTER LSPAnnotate(); #endif mLastNativeEventScheduled = TimeStamp::NowLoRes(); sTaskbarButtonCreatedMsg = ::RegisterWindowMessageW(kTaskbarButtonEventId); NS_ASSERTION(sTaskbarButtonCreatedMsg, "Could not register taskbar button creation message"); WNDCLASSW wc; HINSTANCE module = GetModuleHandle(nullptr); const wchar_t *const kWindowClass = L"nsAppShell:EventWindowClass"; if (!GetClassInfoW(module, kWindowClass, &wc)) { wc.style = 0; wc.lpfnWndProc = EventWindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = module; wc.hIcon = nullptr; wc.hCursor = nullptr; wc.hbrBackground = (HBRUSH) nullptr; wc.lpszMenuName = (LPCWSTR) nullptr; wc.lpszClassName = kWindowClass; RegisterClassW(&wc); } mEventWnd = CreateWindowW(kWindowClass, L"nsAppShell:EventWindow", 0, 0, 0, 10, 10, nullptr, nullptr, module, nullptr); NS_ENSURE_STATE(mEventWnd); return nsBaseAppShell::Init(); } NS_IMETHODIMP nsAppShell::Run(void) { // Ignore failure; failing to start the application is not exactly an // appropriate response to failing to start an audio session. mozilla::widget::StartAudioSession(); // Add an observer that disables the screen saver when requested by Gecko. // For example when we're playing video in the foreground tab. AddScreenWakeLockListener(); nsresult rv = nsBaseAppShell::Run(); RemoveScreenWakeLockListener(); mozilla::widget::StopAudioSession(); return rv; } NS_IMETHODIMP nsAppShell::Exit(void) { return nsBaseAppShell::Exit(); } 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. // 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) { OnDispatchedEvent(nullptr); mNativeCallbackPending = false; } else { mNativeCallbackPending = true; } } void nsAppShell::ScheduleNativeEventCallback() { // Post a message to the hidden message window NS_ADDREF_THIS(); // will be released when the event is processed { 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(); } ::PostMessage(mEventWnd, sAppShellGeckoMsgId, 0, reinterpret_cast(this)); } bool nsAppShell::ProcessNextNativeEvent(bool mayWait) { // Notify ipc we are spinning a (possibly nested) gecko event loop. mozilla::ipc::MessageChannel::NotifyGeckoEventDispatch(); bool gotMessage = false; do { MSG msg; 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()) { gotMessage = WinUtils::PeekMessage(&msg, nullptr, MOZ_WM_MOUSEWHEEL_FIRST, MOZ_WM_MOUSEWHEEL_LAST, PM_REMOVE); NS_ASSERTION(gotMessage, "waiting internal wheel message, but it has not come"); uiMessage = gotMessage; } if (!gotMessage) { gotMessage = WinUtils::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE); 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) { if (msg.message == WM_QUIT) { ::PostQuitMessage(msg.wParam); Exit(); } else { // 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); if (msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST && IMEHandler::ProcessRawKeyMessage(msg)) { continue; // the message is consumed. } ::TranslateMessage(&msg); ::DispatchMessageW(&msg); } } else if (mayWait) { // Block and wait for any posted application message mozilla::HangMonitor::Suspend(); { GeckoProfilerSleepRAII profiler_sleep; ::WaitMessage(); } } } while (!gotMessage && mayWait); // See DoProcessNextNativeEvent, mEventloopNestingLevel will be // one when a modal loop unwinds. if (mNativeCallbackPending && mEventloopNestingLevel == 1) DoProcessMoreGeckoEvents(); // Check for starved native callbacks. If we haven't processed one // of these events in NATIVE_EVENT_STARVATION_LIMIT, fire one off. static const mozilla::TimeDuration nativeEventStarvationLimit = mozilla::TimeDuration::FromSeconds(NATIVE_EVENT_STARVATION_LIMIT); TimeDuration timeSinceLastNativeEventScheduled; { MutexAutoLock lock(mLastNativeEventScheduledMutex); timeSinceLastNativeEventScheduled = TimeStamp::NowLoRes() - mLastNativeEventScheduled; } if (timeSinceLastNativeEventScheduled > nativeEventStarvationLimit) { ScheduleNativeEventCallback(); } return gotMessage; }