2013-02-12 12:51:25 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "MetroAppShell.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "mozilla/widget/AudioSession.h"
|
|
|
|
#include "MetroUtils.h"
|
|
|
|
#include "MetroApp.h"
|
2013-09-06 06:11:14 -07:00
|
|
|
#include "FrameworkView.h"
|
2013-02-12 12:51:25 -08:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-06 06:11:14 -07:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2013-12-20 07:51:13 -08:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-03-17 21:41:24 -07:00
|
|
|
#include "WinUtils.h"
|
2013-09-10 07:24:56 -07:00
|
|
|
#include "nsIAppStartup.h"
|
|
|
|
#include "nsToolkitCompsCID.h"
|
2013-11-19 09:47:23 -08:00
|
|
|
#include <shellapi.h>
|
2014-01-15 16:11:36 -08:00
|
|
|
#include "nsIDOMWakeLockListener.h"
|
|
|
|
#include "nsIPowerManagerService.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include <windows.system.display.h>
|
2013-02-12 12:51:25 -08:00
|
|
|
|
2013-09-06 06:11:14 -07:00
|
|
|
using namespace mozilla;
|
2013-03-17 21:41:24 -07:00
|
|
|
using namespace mozilla::widget;
|
2013-02-12 12:51:25 -08:00
|
|
|
using namespace mozilla::widget::winrt;
|
|
|
|
using namespace Microsoft::WRL;
|
|
|
|
using namespace Microsoft::WRL::Wrappers;
|
|
|
|
using namespace ABI::Windows::UI::Core;
|
|
|
|
using namespace ABI::Windows::Foundation;
|
|
|
|
|
2013-09-06 06:11:14 -07:00
|
|
|
// ProcessNextNativeEvent message wait timeout, see bug 907410.
|
|
|
|
#define MSG_WAIT_TIMEOUT 250
|
2013-12-17 13:13:33 -08:00
|
|
|
// MetroInput will occasionally ask us to flush all input so that the dom is
|
|
|
|
// up to date. This is the maximum amount of time we'll agree to spend in
|
|
|
|
// NS_ProcessPendingEvents.
|
|
|
|
#define PURGE_MAX_TIMEOUT 50
|
2013-09-06 06:11:14 -07:00
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
namespace winrt {
|
|
|
|
extern ComPtr<MetroApp> sMetroApp;
|
2013-09-06 06:11:14 -07:00
|
|
|
extern ComPtr<FrameworkView> sFrameworkView;
|
2013-02-12 12:51:25 -08:00
|
|
|
} } }
|
|
|
|
|
2013-08-07 07:55:12 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace widget {
|
|
|
|
// pulled from win32 app shell
|
|
|
|
extern UINT sAppShellGeckoMsgId;
|
|
|
|
} }
|
|
|
|
|
2014-01-15 16:11:36 -08:00
|
|
|
class WakeLockListener : public nsIDOMMozWakeLockListener {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ComPtr<ABI::Windows::System::Display::IDisplayRequest> mDisplayRequest;
|
|
|
|
|
|
|
|
NS_IMETHOD Callback(const nsAString& aTopic, const nsAString& aState) {
|
|
|
|
if (!mDisplayRequest) {
|
|
|
|
if (FAILED(ActivateGenericInstance(RuntimeClass_Windows_System_Display_DisplayRequest, mDisplayRequest))) {
|
|
|
|
NS_WARNING("Failed to instantiate IDisplayRequest, wakelocks will be broken!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aState.Equals(NS_LITERAL_STRING("locked-foreground"))) {
|
|
|
|
mDisplayRequest->RequestActive();
|
|
|
|
} else {
|
|
|
|
mDisplayRequest->RequestRelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(WakeLockListener, nsIDOMMozWakeLockListener)
|
|
|
|
StaticRefPtr<WakeLockListener> sWakeLockListener;
|
2013-02-12 12:51:25 -08:00
|
|
|
static ComPtr<ICoreWindowStatic> sCoreStatic;
|
2013-09-06 06:11:14 -07:00
|
|
|
static bool sIsDispatching = false;
|
2013-12-17 13:13:33 -08:00
|
|
|
static bool sShouldPurgeThreadQueue = false;
|
|
|
|
static bool sBlockNativeEvents = false;
|
2013-12-20 07:51:13 -08:00
|
|
|
static TimeStamp sPurgeThreadQueueStart;
|
2013-02-12 12:51:25 -08:00
|
|
|
|
|
|
|
MetroAppShell::~MetroAppShell()
|
|
|
|
{
|
|
|
|
if (mEventWnd) {
|
|
|
|
SendMessage(mEventWnd, WM_CLOSE, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MetroAppShell::Init()
|
|
|
|
{
|
|
|
|
LogFunction();
|
|
|
|
|
|
|
|
WNDCLASSW wc;
|
2013-10-08 11:48:34 -07:00
|
|
|
HINSTANCE module = GetModuleHandle(nullptr);
|
2013-02-12 12:51:25 -08:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *const kWindowClass = L"nsAppShell:EventWindowClass";
|
2013-02-12 12:51:25 -08:00
|
|
|
if (!GetClassInfoW(module, kWindowClass, &wc)) {
|
|
|
|
wc.style = 0;
|
|
|
|
wc.lpfnWndProc = EventWindowProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = module;
|
2013-10-08 11:48:34 -07:00
|
|
|
wc.hIcon = nullptr;
|
|
|
|
wc.hCursor = nullptr;
|
|
|
|
wc.hbrBackground = (HBRUSH) nullptr;
|
|
|
|
wc.lpszMenuName = (LPCWSTR) nullptr;
|
2013-02-12 12:51:25 -08:00
|
|
|
wc.lpszClassName = kWindowClass;
|
|
|
|
RegisterClassW(&wc);
|
|
|
|
}
|
|
|
|
|
|
|
|
mEventWnd = CreateWindowW(kWindowClass, L"nsAppShell:EventWindow",
|
2013-10-08 11:48:34 -07:00
|
|
|
0, 0, 0, 10, 10, nullptr, nullptr, module, nullptr);
|
2013-02-12 12:51:25 -08:00
|
|
|
NS_ENSURE_STATE(mEventWnd);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
observerService->AddObserver(this, "dl-start", false);
|
|
|
|
observerService->AddObserver(this, "dl-done", false);
|
|
|
|
observerService->AddObserver(this, "dl-cancel", false);
|
|
|
|
observerService->AddObserver(this, "dl-failed", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsBaseAppShell::Init();
|
|
|
|
}
|
|
|
|
|
2013-09-10 07:24:56 -07:00
|
|
|
HRESULT SHCreateShellItemArrayFromShellItemDynamic(IShellItem *psi, REFIID riid, void **ppv)
|
|
|
|
{
|
|
|
|
HMODULE shell32DLL = LoadLibraryW(L"shell32.dll");
|
|
|
|
if (!shell32DLL) {
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef BOOL (WINAPI* SHFn)(IShellItem *psi, REFIID riid, void **ppv);
|
|
|
|
|
|
|
|
HRESULT hr = E_FAIL;
|
|
|
|
SHFn SHCreateShellItemArrayFromShellItemDynamicPtr =
|
|
|
|
(SHFn)GetProcAddress(shell32DLL, "SHCreateShellItemArrayFromShellItem");
|
|
|
|
FreeLibrary(shell32DLL);
|
|
|
|
if (SHCreateShellItemArrayFromShellItemDynamicPtr) {
|
|
|
|
hr = SHCreateShellItemArrayFromShellItemDynamicPtr(psi, riid, ppv);
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeLibrary(shell32DLL);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2013-12-13 07:51:21 -08:00
|
|
|
HRESULT
|
|
|
|
WinLaunchDeferredMetroFirefox()
|
2013-09-10 07:24:56 -07:00
|
|
|
{
|
|
|
|
// Create an instance of the Firefox Metro DEH which is used to launch the browser
|
|
|
|
const CLSID CLSID_FirefoxMetroDEH = {0x5100FEC1,0x212B, 0x4BF5 ,{0x9B,0xF8, 0x3E,0x65, 0x0F,0xD7,0x94,0xA3}};
|
|
|
|
|
|
|
|
nsRefPtr<IExecuteCommand> executeCommand;
|
|
|
|
HRESULT hr = CoCreateInstance(CLSID_FirefoxMetroDEH,
|
2013-10-08 11:48:34 -07:00
|
|
|
nullptr,
|
2013-09-10 07:24:56 -07:00
|
|
|
CLSCTX_LOCAL_SERVER,
|
|
|
|
IID_IExecuteCommand,
|
|
|
|
getter_AddRefs(executeCommand));
|
|
|
|
if (FAILED(hr))
|
2013-12-13 07:51:21 -08:00
|
|
|
return hr;
|
2013-09-10 07:24:56 -07:00
|
|
|
|
|
|
|
// Get the currently running exe path
|
|
|
|
WCHAR exePath[MAX_PATH + 1] = { L'\0' };
|
|
|
|
if (!::GetModuleFileNameW(0, exePath, MAX_PATH))
|
2013-12-13 07:51:21 -08:00
|
|
|
return hr;
|
2013-09-10 07:24:56 -07:00
|
|
|
|
|
|
|
// Convert the path to a long path since GetModuleFileNameW returns the path
|
|
|
|
// that was used to launch Firefox which is not necessarily a long path.
|
|
|
|
if (!::GetLongPathNameW(exePath, exePath, MAX_PATH))
|
2013-12-13 07:51:21 -08:00
|
|
|
return hr;
|
2013-09-10 07:24:56 -07:00
|
|
|
|
|
|
|
// Create an IShellItem for the current browser path
|
|
|
|
nsRefPtr<IShellItem> shellItem;
|
2013-10-08 11:48:34 -07:00
|
|
|
hr = WinUtils::SHCreateItemFromParsingName(exePath, nullptr, IID_IShellItem,
|
|
|
|
getter_AddRefs(shellItem));
|
2013-09-10 07:24:56 -07:00
|
|
|
if (FAILED(hr))
|
2013-12-13 07:51:21 -08:00
|
|
|
return hr;
|
2013-09-10 07:24:56 -07:00
|
|
|
|
|
|
|
// Convert to an IShellItemArray which is used for the path to launch
|
|
|
|
nsRefPtr<IShellItemArray> shellItemArray;
|
|
|
|
hr = SHCreateShellItemArrayFromShellItemDynamic(shellItem, IID_IShellItemArray, getter_AddRefs(shellItemArray));
|
|
|
|
if (FAILED(hr))
|
2013-12-13 07:51:21 -08:00
|
|
|
return hr;
|
2013-09-10 07:24:56 -07:00
|
|
|
|
|
|
|
// Set the path to launch and parameters needed
|
|
|
|
nsRefPtr<IObjectWithSelection> selection;
|
|
|
|
hr = executeCommand->QueryInterface(IID_IObjectWithSelection, getter_AddRefs(selection));
|
|
|
|
if (FAILED(hr))
|
2013-12-13 07:51:21 -08:00
|
|
|
return hr;
|
2013-09-10 07:24:56 -07:00
|
|
|
hr = selection->SetSelection(shellItemArray);
|
|
|
|
if (FAILED(hr))
|
2013-12-13 07:51:21 -08:00
|
|
|
return hr;
|
2013-09-10 07:24:56 -07:00
|
|
|
|
2013-12-13 07:51:21 -08:00
|
|
|
hr = executeCommand->SetParameters(L"--metro-restart");
|
2013-09-10 07:24:56 -07:00
|
|
|
if (FAILED(hr))
|
2013-12-13 07:51:21 -08:00
|
|
|
return hr;
|
2013-09-10 07:24:56 -07:00
|
|
|
|
|
|
|
// Run the default browser through the DEH
|
2013-12-13 07:51:21 -08:00
|
|
|
return executeCommand->Execute();
|
2013-09-10 07:24:56 -07:00
|
|
|
}
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
// Called by appstartup->run in xre, which is initiated by a call to
|
|
|
|
// XRE_metroStartup in MetroApp. This call is on the metro main thread.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MetroAppShell::Run(void)
|
|
|
|
{
|
|
|
|
LogFunction();
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
switch(XRE_GetProcessType()) {
|
|
|
|
case GeckoProcessType_Content:
|
|
|
|
case GeckoProcessType_IPDLUnitTest:
|
|
|
|
mozilla::widget::StartAudioSession();
|
|
|
|
rv = nsBaseAppShell::Run();
|
|
|
|
mozilla::widget::StopAudioSession();
|
|
|
|
break;
|
|
|
|
case GeckoProcessType_Plugin:
|
|
|
|
NS_WARNING("We don't support plugins currently.");
|
|
|
|
// Just exit
|
|
|
|
rv = NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
break;
|
2013-09-10 07:24:56 -07:00
|
|
|
case GeckoProcessType_Default: {
|
2014-01-15 16:11:36 -08:00
|
|
|
nsCOMPtr<nsIPowerManagerService> sPowerManagerService = do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
|
|
|
if (sPowerManagerService) {
|
|
|
|
sWakeLockListener = new WakeLockListener();
|
|
|
|
sPowerManagerService->AddWakeLockListener(sWakeLockListener);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("Failed to retrieve PowerManagerService, wakelocks will be broken!");
|
|
|
|
}
|
|
|
|
|
2013-09-06 06:11:14 -07:00
|
|
|
mozilla::widget::StartAudioSession();
|
|
|
|
sFrameworkView->ActivateView();
|
|
|
|
rv = nsBaseAppShell::Run();
|
|
|
|
mozilla::widget::StopAudioSession();
|
2013-09-10 07:24:56 -07:00
|
|
|
|
2014-01-15 16:11:36 -08:00
|
|
|
if (sPowerManagerService) {
|
|
|
|
sPowerManagerService->RemoveWakeLockListener(sWakeLockListener);
|
|
|
|
|
|
|
|
sPowerManagerService = nullptr;
|
|
|
|
sWakeLockListener = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-10 07:24:56 -07:00
|
|
|
nsCOMPtr<nsIAppStartup> appStartup (do_GetService(NS_APPSTARTUP_CONTRACTID));
|
2013-12-13 07:51:21 -08:00
|
|
|
bool restartingInMetro = false, restartingInDesktop = false;
|
2013-11-19 09:47:23 -08:00
|
|
|
|
2013-12-13 07:51:21 -08:00
|
|
|
if (!appStartup || NS_FAILED(appStartup->GetRestarting(&restartingInDesktop))) {
|
|
|
|
WinUtils::Log("appStartup->GetRestarting() unsuccessful");
|
2013-11-19 09:47:23 -08:00
|
|
|
}
|
|
|
|
|
2013-12-13 07:51:21 -08:00
|
|
|
if (appStartup && NS_SUCCEEDED(appStartup->GetRestartingTouchEnvironment(&restartingInMetro)) &&
|
|
|
|
restartingInMetro) {
|
|
|
|
restartingInDesktop = false;
|
2013-09-10 07:24:56 -07:00
|
|
|
}
|
|
|
|
|
2013-12-13 07:51:21 -08:00
|
|
|
// This calls XRE_metroShutdown() in xre. Shuts down gecko, including
|
|
|
|
// releasing the profile, and destroys MessagePump.
|
2013-09-06 06:11:16 -07:00
|
|
|
sMetroApp->ShutdownXPCOM();
|
2013-09-10 07:24:56 -07:00
|
|
|
|
2013-12-13 07:51:21 -08:00
|
|
|
// Handle update restart or browser switch requests
|
|
|
|
if (restartingInDesktop) {
|
|
|
|
WinUtils::Log("Relaunching desktop browser");
|
2013-11-19 09:47:23 -08:00
|
|
|
SHELLEXECUTEINFOW sinfo;
|
|
|
|
memset(&sinfo, 0, sizeof(SHELLEXECUTEINFOW));
|
|
|
|
sinfo.cbSize = sizeof(SHELLEXECUTEINFOW);
|
|
|
|
// Per the Metro style enabled desktop browser, for some reason,
|
|
|
|
// SEE_MASK_FLAG_LOG_USAGE is needed to change from immersive mode
|
|
|
|
// to desktop.
|
|
|
|
sinfo.fMask = SEE_MASK_FLAG_LOG_USAGE;
|
|
|
|
sinfo.lpFile = L"http://-desktop";
|
|
|
|
sinfo.lpVerb = L"open";
|
2013-11-19 10:14:40 -08:00
|
|
|
sinfo.lpParameters = L"--desktop-restart";
|
2013-11-19 09:47:23 -08:00
|
|
|
sinfo.nShow = SW_SHOWNORMAL;
|
|
|
|
ShellExecuteEx(&sinfo);
|
2013-12-13 07:51:21 -08:00
|
|
|
} else if (restartingInMetro) {
|
|
|
|
HRESULT hresult = WinLaunchDeferredMetroFirefox();
|
|
|
|
WinUtils::Log("Relaunching metro browser (hr=%X)", hresult);
|
2013-11-19 09:47:23 -08:00
|
|
|
}
|
|
|
|
|
2013-09-06 06:11:16 -07:00
|
|
|
// This will free the real main thread in CoreApplication::Run()
|
|
|
|
// once winrt cleans up this thread.
|
|
|
|
sMetroApp->CoreExit();
|
2013-09-10 07:24:56 -07:00
|
|
|
}
|
2013-02-12 12:51:25 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-09-13 03:34:24 -07:00
|
|
|
// Called in certain cases where we have async input events in the thread
|
|
|
|
// queue and need to make sure they get dispatched before the stack unwinds.
|
|
|
|
void // static
|
|
|
|
MetroAppShell::MarkEventQueueForPurge()
|
|
|
|
{
|
2013-12-17 13:13:33 -08:00
|
|
|
sShouldPurgeThreadQueue = true;
|
2013-09-13 03:34:24 -07:00
|
|
|
|
|
|
|
// If we're dispatching native events, wait until the dispatcher is
|
|
|
|
// off the stack.
|
|
|
|
if (sIsDispatching) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safe to process pending events now
|
|
|
|
DispatchAllGeckoEvents();
|
|
|
|
}
|
|
|
|
|
2013-12-17 13:13:33 -08:00
|
|
|
// Notification from MetroInput that all events it wanted delivered
|
|
|
|
// have been dispatched. It is safe to start processing windowing
|
|
|
|
// events.
|
|
|
|
void // static
|
|
|
|
MetroAppShell::InputEventsDispatched()
|
|
|
|
{
|
|
|
|
sBlockNativeEvents = false;
|
|
|
|
}
|
|
|
|
|
2013-09-13 03:34:24 -07:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
MetroAppShell::DispatchAllGeckoEvents()
|
|
|
|
{
|
2014-01-27 07:09:33 -08:00
|
|
|
// Only do this if requested and when we're not shutting down
|
|
|
|
if (!sShouldPurgeThreadQueue || MetroApp::sGeckoShuttingDown) {
|
2013-09-13 03:34:24 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-25 12:38:02 -08:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "DispatchAllGeckoEvents should be called on the main thread");
|
2013-09-13 03:34:24 -07:00
|
|
|
|
2013-12-17 13:13:33 -08:00
|
|
|
sShouldPurgeThreadQueue = false;
|
2013-12-20 07:51:13 -08:00
|
|
|
sPurgeThreadQueueStart = TimeStamp::Now();
|
2013-12-17 13:13:33 -08:00
|
|
|
|
|
|
|
sBlockNativeEvents = true;
|
2013-09-13 03:34:24 -07:00
|
|
|
nsIThread *thread = NS_GetCurrentThread();
|
2013-12-17 13:13:33 -08:00
|
|
|
NS_ProcessPendingEvents(thread, PURGE_MAX_TIMEOUT);
|
|
|
|
sBlockNativeEvents = false;
|
2013-09-13 03:34:24 -07:00
|
|
|
}
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
static void
|
|
|
|
ProcessNativeEvents(CoreProcessEventsOption eventOption)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
if (!sCoreStatic) {
|
|
|
|
hr = GetActivationFactory(HStringReference(L"Windows.UI.Core.CoreWindow").Get(), sCoreStatic.GetAddressOf());
|
|
|
|
NS_ASSERTION(SUCCEEDED(hr), "GetActivationFactory failed?");
|
|
|
|
AssertHRESULT(hr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ComPtr<ICoreWindow> window;
|
|
|
|
AssertHRESULT(sCoreStatic->GetForCurrentThread(window.GetAddressOf()));
|
|
|
|
ComPtr<ICoreDispatcher> dispatcher;
|
|
|
|
hr = window->get_Dispatcher(&dispatcher);
|
|
|
|
NS_ASSERTION(SUCCEEDED(hr), "get_Dispatcher failed?");
|
|
|
|
AssertHRESULT(hr);
|
|
|
|
dispatcher->ProcessEvents(eventOption);
|
|
|
|
}
|
|
|
|
|
2013-08-03 14:18:45 -07:00
|
|
|
// static
|
2013-09-06 06:11:14 -07:00
|
|
|
bool
|
2013-02-12 12:51:25 -08:00
|
|
|
MetroAppShell::ProcessOneNativeEventIfPresent()
|
|
|
|
{
|
2013-09-06 06:11:14 -07:00
|
|
|
if (sIsDispatching) {
|
2013-09-13 03:34:24 -07:00
|
|
|
// Calling into ProcessNativeEvents is harmless, but won't actually process any
|
|
|
|
// native events. So we log here so we can spot this and get a handle on the
|
|
|
|
// corner cases where this can happen.
|
2013-11-01 03:09:32 -07:00
|
|
|
WinUtils::Log("WARNING: Reentrant call into process events detected, returning early.");
|
2013-09-13 03:34:24 -07:00
|
|
|
return false;
|
2013-09-06 06:11:14 -07:00
|
|
|
}
|
2013-09-13 03:34:24 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
AutoRestore<bool> dispatching(sIsDispatching);
|
|
|
|
sIsDispatching = true;
|
|
|
|
ProcessNativeEvents(CoreProcessEventsOption::CoreProcessEventsOption_ProcessOneIfPresent);
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchAllGeckoEvents();
|
|
|
|
|
2013-09-06 06:11:14 -07:00
|
|
|
return !!HIWORD(::GetQueueStatus(MOZ_QS_ALLEVENT));
|
2013-02-12 12:51:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MetroAppShell::ProcessNextNativeEvent(bool mayWait)
|
|
|
|
{
|
2013-12-20 07:51:13 -08:00
|
|
|
// NS_ProcessPendingEvents will process thread events *and* call
|
|
|
|
// nsBaseAppShell::OnProcessNextEvent to process native events. However
|
|
|
|
// we do not want native events getting dispatched while we are trying
|
2013-12-17 13:13:33 -08:00
|
|
|
// to dispatch pending input in DispatchAllGeckoEvents since a native
|
|
|
|
// event may be a UIA Automation call coming in to check focus.
|
|
|
|
if (sBlockNativeEvents) {
|
2013-12-20 07:51:13 -08:00
|
|
|
if ((TimeStamp::Now() - sPurgeThreadQueueStart).ToMilliseconds()
|
|
|
|
< PURGE_MAX_TIMEOUT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sBlockNativeEvents = false;
|
2013-12-17 13:13:33 -08:00
|
|
|
}
|
|
|
|
|
2013-09-06 06:11:14 -07:00
|
|
|
if (ProcessOneNativeEventIfPresent()) {
|
2013-02-12 12:51:25 -08:00
|
|
|
return true;
|
|
|
|
}
|
2013-09-06 06:11:14 -07:00
|
|
|
if (mayWait) {
|
2013-10-08 11:48:34 -07:00
|
|
|
DWORD result = ::MsgWaitForMultipleObjectsEx(0, nullptr, MSG_WAIT_TIMEOUT,
|
|
|
|
MOZ_QS_ALLEVENT,
|
2013-09-06 06:11:14 -07:00
|
|
|
MWMO_INPUTAVAILABLE|MWMO_ALERTABLE);
|
|
|
|
NS_WARN_IF_FALSE(result != WAIT_FAILED, "Wait failed");
|
2013-02-12 12:51:25 -08:00
|
|
|
}
|
2013-09-06 06:11:14 -07:00
|
|
|
return ProcessOneNativeEventIfPresent();
|
2013-02-12 12:51:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MetroAppShell::NativeCallback()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Native callbacks must be on the metro main thread");
|
2014-01-27 07:09:33 -08:00
|
|
|
|
|
|
|
// We shouldn't process native events during xpcom shutdown - this can
|
|
|
|
// trigger unexpected xpcom event dispatching for the main thread when
|
|
|
|
// the thread manager is in the process of shutting down non-main threads,
|
|
|
|
// resulting in shutdown hangs.
|
|
|
|
if (MetroApp::sGeckoShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-12 12:51:25 -08:00
|
|
|
NativeEventCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
LRESULT CALLBACK
|
|
|
|
MetroAppShell::EventWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2013-08-07 07:55:12 -07:00
|
|
|
if (uMsg == sAppShellGeckoMsgId) {
|
2013-02-12 12:51:25 -08:00
|
|
|
MetroAppShell *as = reinterpret_cast<MetroAppShell *>(lParam);
|
|
|
|
as->NativeCallback();
|
|
|
|
NS_RELEASE(as);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MetroAppShell::ScheduleNativeEventCallback()
|
|
|
|
{
|
|
|
|
NS_ADDREF_THIS();
|
2013-08-07 07:55:12 -07:00
|
|
|
PostMessage(mEventWnd, sAppShellGeckoMsgId, 0, reinterpret_cast<LPARAM>(this));
|
2013-02-12 12:51:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MetroAppShell::DoProcessMoreGeckoEvents()
|
|
|
|
{
|
|
|
|
ScheduleNativeEventCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
static HANDLE
|
|
|
|
PowerCreateRequestDyn(REASON_CONTEXT *context)
|
|
|
|
{
|
|
|
|
typedef HANDLE (WINAPI * PowerCreateRequestPtr)(REASON_CONTEXT *context);
|
|
|
|
static HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
|
|
|
|
static PowerCreateRequestPtr powerCreateRequest =
|
|
|
|
(PowerCreateRequestPtr)GetProcAddress(kernel32, "PowerCreateRequest");
|
|
|
|
if (!powerCreateRequest)
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
return powerCreateRequest(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
PowerClearRequestDyn(HANDLE powerRequest, POWER_REQUEST_TYPE requestType)
|
|
|
|
{
|
|
|
|
typedef BOOL (WINAPI * PowerClearRequestPtr)(HANDLE powerRequest, POWER_REQUEST_TYPE requestType);
|
|
|
|
static HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
|
|
|
|
static PowerClearRequestPtr powerClearRequest =
|
|
|
|
(PowerClearRequestPtr)GetProcAddress(kernel32, "PowerClearRequest");
|
|
|
|
if (!powerClearRequest)
|
|
|
|
return FALSE;
|
|
|
|
return powerClearRequest(powerRequest, requestType);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
PowerSetRequestDyn(HANDLE powerRequest, POWER_REQUEST_TYPE requestType)
|
|
|
|
{
|
|
|
|
typedef BOOL (WINAPI * PowerSetRequestPtr)(HANDLE powerRequest, POWER_REQUEST_TYPE requestType);
|
|
|
|
static HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
|
|
|
|
static PowerSetRequestPtr powerSetRequest =
|
|
|
|
(PowerSetRequestPtr)GetProcAddress(kernel32, "PowerSetRequest");
|
|
|
|
if (!powerSetRequest)
|
|
|
|
return FALSE;
|
|
|
|
return powerSetRequest(powerRequest, requestType);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MetroAppShell::Observe(nsISupports *subject, const char *topic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *data)
|
2013-02-12 12:51:25 -08:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(topic);
|
|
|
|
if (!strcmp(topic, "dl-start")) {
|
|
|
|
if (mPowerRequestCount++ == 0) {
|
2013-11-01 03:09:32 -07:00
|
|
|
WinUtils::Log("Download started - Disallowing suspend");
|
2013-02-12 12:51:25 -08:00
|
|
|
REASON_CONTEXT context;
|
|
|
|
context.Version = POWER_REQUEST_CONTEXT_VERSION;
|
|
|
|
context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
|
|
|
|
context.Reason.SimpleReasonString = L"downloading";
|
|
|
|
mPowerRequest.own(PowerCreateRequestDyn(&context));
|
|
|
|
PowerSetRequestDyn(mPowerRequest, PowerRequestExecutionRequired);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
} else if (!strcmp(topic, "dl-done") ||
|
|
|
|
!strcmp(topic, "dl-cancel") ||
|
|
|
|
!strcmp(topic, "dl-failed")) {
|
|
|
|
if (--mPowerRequestCount == 0 && mPowerRequest) {
|
2013-11-01 03:09:32 -07:00
|
|
|
WinUtils::Log("All downloads ended - Allowing suspend");
|
2013-02-12 12:51:25 -08:00
|
|
|
PowerClearRequestDyn(mPowerRequest, PowerRequestExecutionRequired);
|
|
|
|
mPowerRequest.reset();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsBaseAppShell::Observe(subject, topic, data);
|
|
|
|
}
|