Bug 1793629 - Implement attention indicator for the unified extensions button, r...
[gecko.git] / widget / windows / WidgetTraceEvent.cpp
blob4509791d5979d3724b8da9160447646530ee9da9
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/. */
5 /*
6 * Windows widget support for event loop instrumentation.
7 * See toolkit/xre/EventTracer.cpp for more details.
8 */
10 #include <stdio.h>
11 #include <windows.h>
13 #include "mozilla/RefPtr.h"
14 #include "mozilla/WidgetTraceEvent.h"
15 #include "nsAppShellCID.h"
16 #include "nsComponentManagerUtils.h"
17 #include "nsCOMPtr.h"
18 #include "nsIAppShellService.h"
19 #include "nsIBaseWindow.h"
20 #include "nsIDocShell.h"
21 #include "nsISupportsImpl.h"
22 #include "nsIWidget.h"
23 #include "nsIAppWindow.h"
24 #include "nsServiceManagerUtils.h"
25 #include "nsThreadUtils.h"
26 #include "nsWindowDefs.h"
28 namespace {
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
34 // thread.
35 class HWNDGetter : public mozilla::Runnable {
36 public:
37 HWNDGetter() : Runnable("HWNDGetter"), hidden_window_hwnd(nullptr) {}
39 HWND hidden_window_hwnd;
41 NS_IMETHOD Run() override {
42 // Jump through some hoops to locate the hidden window.
43 nsCOMPtr<nsIAppShellService> appShell(
44 do_GetService(NS_APPSHELLSERVICE_CONTRACTID));
45 nsCOMPtr<nsIAppWindow> hiddenWindow;
47 nsresult rv = appShell->GetHiddenWindow(getter_AddRefs(hiddenWindow));
48 if (NS_FAILED(rv)) {
49 return rv;
52 nsCOMPtr<nsIDocShell> docShell;
53 rv = hiddenWindow->GetDocShell(getter_AddRefs(docShell));
54 if (NS_FAILED(rv) || !docShell) {
55 return rv;
58 nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(docShell));
60 if (!baseWindow) return NS_ERROR_FAILURE;
62 nsCOMPtr<nsIWidget> widget;
63 baseWindow->GetMainWidget(getter_AddRefs(widget));
65 if (!widget) return NS_ERROR_FAILURE;
67 hidden_window_hwnd = (HWND)widget->GetNativeData(NS_NATIVE_WINDOW);
69 return NS_OK;
73 HWND GetHiddenWindowHWND() {
74 // Need to dispatch this to the main thread because plenty of
75 // the things it wants to access are main-thread-only.
76 RefPtr<HWNDGetter> getter = new HWNDGetter();
77 NS_DispatchToMainThread(getter, NS_DISPATCH_SYNC);
78 return getter->hidden_window_hwnd;
81 } // namespace
83 namespace mozilla {
85 bool InitWidgetTracing() {
86 sEventHandle = CreateEventW(nullptr, FALSE, FALSE, nullptr);
87 return sEventHandle != nullptr;
90 void CleanUpWidgetTracing() {
91 CloseHandle(sEventHandle);
92 sEventHandle = nullptr;
95 // This function is called from the main (UI) thread.
96 void SignalTracerThread() {
97 if (sEventHandle != nullptr) SetEvent(sEventHandle);
100 // This function is called from the background tracer thread.
101 bool FireAndWaitForTracerEvent() {
102 MOZ_ASSERT(sEventHandle, "Tracing not initialized!");
104 // First, try to find the hidden window.
105 static HWND hidden_window = nullptr;
106 if (hidden_window == nullptr) {
107 hidden_window = GetHiddenWindowHWND();
110 if (hidden_window == nullptr) return false;
112 // Post the tracer message into the hidden window's message queue,
113 // and then block until it's processed.
114 PostMessage(hidden_window, MOZ_WM_TRACE, 0, 0);
115 WaitForSingleObject(sEventHandle, INFINITE);
116 return true;
119 } // namespace mozilla