1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * Windows widget support for event loop instrumentation.
7 * See toolkit/xre/EventTracer.cpp for more details.
13 #include "mozilla/WidgetTraceEvent.h"
14 #include "nsAppShellCID.h"
15 #include "nsComponentManagerUtils.h"
17 #include "nsIAppShellService.h"
18 #include "nsIBaseWindow.h"
19 #include "nsIDocShell.h"
20 #include "nsIWidget.h"
21 #include "nsIXULWindow.h"
22 #include "nsAutoPtr.h"
23 #include "nsServiceManagerUtils.h"
24 #include "nsThreadUtils.h"
25 #include "nsTraceRefcnt.h"
26 #include "nsWindowDefs.h"
30 // Used for signaling the background thread from the main thread.
31 HANDLE sEventHandle
= nullptr;
33 // We need a runnable in order to find the hidden window on the main
35 class HWNDGetter
: public nsRunnable
{
37 HWNDGetter() : hidden_window_hwnd(nullptr) {
38 MOZ_COUNT_CTOR(HWNDGetter
);
41 MOZ_COUNT_DTOR(HWNDGetter
);
44 HWND hidden_window_hwnd
;
47 // Jump through some hoops to locate the hidden window.
48 nsCOMPtr
<nsIAppShellService
> appShell(do_GetService(NS_APPSHELLSERVICE_CONTRACTID
));
49 nsCOMPtr
<nsIXULWindow
> hiddenWindow
;
51 nsresult rv
= appShell
->GetHiddenWindow(getter_AddRefs(hiddenWindow
));
56 nsCOMPtr
<nsIDocShell
> docShell
;
57 rv
= hiddenWindow
->GetDocShell(getter_AddRefs(docShell
));
58 if (NS_FAILED(rv
) || !docShell
) {
62 nsCOMPtr
<nsIBaseWindow
> baseWindow(do_QueryInterface(docShell
));
65 return NS_ERROR_FAILURE
;
67 nsCOMPtr
<nsIWidget
> widget
;
68 baseWindow
->GetMainWidget(getter_AddRefs(widget
));
71 return NS_ERROR_FAILURE
;
73 hidden_window_hwnd
= (HWND
)widget
->GetNativeData(NS_NATIVE_WINDOW
);
79 HWND
GetHiddenWindowHWND()
81 // Need to dispatch this to the main thread because plenty of
82 // the things it wants to access are main-thread-only.
83 nsRefPtr
<HWNDGetter
> getter
= new HWNDGetter();
84 NS_DispatchToMainThread(getter
, NS_DISPATCH_SYNC
);
85 return getter
->hidden_window_hwnd
;
92 bool InitWidgetTracing()
94 sEventHandle
= CreateEvent(nullptr, FALSE
, FALSE
, nullptr);
95 return sEventHandle
!= nullptr;
98 void CleanUpWidgetTracing()
100 CloseHandle(sEventHandle
);
101 sEventHandle
= nullptr;
104 // This function is called from the main (UI) thread.
105 void SignalTracerThread()
107 if (sEventHandle
!= nullptr)
108 SetEvent(sEventHandle
);
111 // This function is called from the background tracer thread.
112 bool FireAndWaitForTracerEvent()
114 NS_ABORT_IF_FALSE(sEventHandle
, "Tracing not initialized!");
116 // First, try to find the hidden window.
117 static HWND hidden_window
= nullptr;
118 if (hidden_window
== nullptr) {
119 hidden_window
= GetHiddenWindowHWND();
122 if (hidden_window
== nullptr)
125 // Post the tracer message into the hidden window's message queue,
126 // and then block until it's processed.
127 PostMessage(hidden_window
, MOZ_WM_TRACE
, 0, 0);
128 WaitForSingleObject(sEventHandle
, INFINITE
);
132 } // namespace mozilla