2012-05-21 15:12:37 +04: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/. */
|
2011-03-03 19:20:02 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Windows widget support for event loop instrumentation.
|
|
|
|
* See toolkit/xre/EventTracer.cpp for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
2016-04-19 23:51:25 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2011-03-03 19:20:02 +03:00
|
|
|
#include "mozilla/WidgetTraceEvent.h"
|
|
|
|
#include "nsAppShellCID.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIAppShellService.h"
|
|
|
|
#include "nsIBaseWindow.h"
|
|
|
|
#include "nsIDocShell.h"
|
2014-02-27 01:36:35 +04:00
|
|
|
#include "nsISupportsImpl.h"
|
2011-03-03 19:20:02 +03:00
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsIXULWindow.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsWindowDefs.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Used for signaling the background thread from the main thread.
|
2013-10-08 22:48:20 +04:00
|
|
|
HANDLE sEventHandle = nullptr;
|
2011-03-03 19:20:02 +03:00
|
|
|
|
|
|
|
// We need a runnable in order to find the hidden window on the main
|
|
|
|
// thread.
|
2016-04-26 03:23:21 +03:00
|
|
|
class HWNDGetter : public mozilla::Runnable {
|
2011-03-03 19:20:02 +03:00
|
|
|
public:
|
2017-06-21 14:59:26 +03:00
|
|
|
HWNDGetter() : hidden_window_hwnd(nullptr) {}
|
2011-03-03 19:20:02 +03:00
|
|
|
|
|
|
|
HWND hidden_window_hwnd;
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2011-03-03 19:20:02 +03:00
|
|
|
// Jump through some hoops to locate the hidden window.
|
|
|
|
nsCOMPtr<nsIAppShellService> appShell(do_GetService(NS_APPSHELLSERVICE_CONTRACTID));
|
|
|
|
nsCOMPtr<nsIXULWindow> hiddenWindow;
|
|
|
|
|
|
|
|
nsresult rv = appShell->GetHiddenWindow(getter_AddRefs(hiddenWindow));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> docShell;
|
|
|
|
rv = hiddenWindow->GetDocShell(getter_AddRefs(docShell));
|
|
|
|
if (NS_FAILED(rv) || !docShell) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(docShell));
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2011-03-03 19:20:02 +03:00
|
|
|
if (!baseWindow)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWidget> widget;
|
|
|
|
baseWindow->GetMainWidget(getter_AddRefs(widget));
|
|
|
|
|
|
|
|
if (!widget)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
hidden_window_hwnd = (HWND)widget->GetNativeData(NS_NATIVE_WINDOW);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
HWND GetHiddenWindowHWND()
|
|
|
|
{
|
|
|
|
// Need to dispatch this to the main thread because plenty of
|
|
|
|
// the things it wants to access are main-thread-only.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<HWNDGetter> getter = new HWNDGetter();
|
2011-03-03 19:20:02 +03:00
|
|
|
NS_DispatchToMainThread(getter, NS_DISPATCH_SYNC);
|
|
|
|
return getter->hidden_window_hwnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
bool InitWidgetTracing()
|
|
|
|
{
|
2015-01-03 05:13:39 +03:00
|
|
|
sEventHandle = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
2013-10-08 22:48:20 +04:00
|
|
|
return sEventHandle != nullptr;
|
2011-03-03 19:20:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CleanUpWidgetTracing()
|
|
|
|
{
|
|
|
|
CloseHandle(sEventHandle);
|
2013-10-08 22:48:20 +04:00
|
|
|
sEventHandle = nullptr;
|
2011-03-03 19:20:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is called from the main (UI) thread.
|
|
|
|
void SignalTracerThread()
|
|
|
|
{
|
2013-10-08 22:48:20 +04:00
|
|
|
if (sEventHandle != nullptr)
|
2011-03-03 19:20:02 +03:00
|
|
|
SetEvent(sEventHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is called from the background tracer thread.
|
|
|
|
bool FireAndWaitForTracerEvent()
|
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(sEventHandle, "Tracing not initialized!");
|
2011-03-03 19:20:02 +03:00
|
|
|
|
|
|
|
// First, try to find the hidden window.
|
2013-10-08 22:48:20 +04:00
|
|
|
static HWND hidden_window = nullptr;
|
|
|
|
if (hidden_window == nullptr) {
|
2011-03-03 19:20:02 +03:00
|
|
|
hidden_window = GetHiddenWindowHWND();
|
|
|
|
}
|
|
|
|
|
2013-10-08 22:48:20 +04:00
|
|
|
if (hidden_window == nullptr)
|
2011-03-03 19:20:02 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Post the tracer message into the hidden window's message queue,
|
|
|
|
// and then block until it's processed.
|
|
|
|
PostMessage(hidden_window, MOZ_WM_TRACE, 0, 0);
|
|
|
|
WaitForSingleObject(sEventHandle, INFINITE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|