Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* |
michael@0 | 6 | * Windows widget support for event loop instrumentation. |
michael@0 | 7 | * See toolkit/xre/EventTracer.cpp for more details. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | #include <windows.h> |
michael@0 | 12 | |
michael@0 | 13 | #include "mozilla/WidgetTraceEvent.h" |
michael@0 | 14 | #include "nsAppShellCID.h" |
michael@0 | 15 | #include "nsComponentManagerUtils.h" |
michael@0 | 16 | #include "nsCOMPtr.h" |
michael@0 | 17 | #include "nsIAppShellService.h" |
michael@0 | 18 | #include "nsIBaseWindow.h" |
michael@0 | 19 | #include "nsIDocShell.h" |
michael@0 | 20 | #include "nsISupportsImpl.h" |
michael@0 | 21 | #include "nsIWidget.h" |
michael@0 | 22 | #include "nsIXULWindow.h" |
michael@0 | 23 | #include "nsAutoPtr.h" |
michael@0 | 24 | #include "nsServiceManagerUtils.h" |
michael@0 | 25 | #include "nsThreadUtils.h" |
michael@0 | 26 | #include "nsWindowDefs.h" |
michael@0 | 27 | |
michael@0 | 28 | namespace { |
michael@0 | 29 | |
michael@0 | 30 | // Used for signaling the background thread from the main thread. |
michael@0 | 31 | HANDLE sEventHandle = nullptr; |
michael@0 | 32 | |
michael@0 | 33 | // We need a runnable in order to find the hidden window on the main |
michael@0 | 34 | // thread. |
michael@0 | 35 | class HWNDGetter : public nsRunnable { |
michael@0 | 36 | public: |
michael@0 | 37 | HWNDGetter() : hidden_window_hwnd(nullptr) { |
michael@0 | 38 | MOZ_COUNT_CTOR(HWNDGetter); |
michael@0 | 39 | } |
michael@0 | 40 | ~HWNDGetter() { |
michael@0 | 41 | MOZ_COUNT_DTOR(HWNDGetter); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | HWND hidden_window_hwnd; |
michael@0 | 45 | |
michael@0 | 46 | NS_IMETHOD Run() { |
michael@0 | 47 | // Jump through some hoops to locate the hidden window. |
michael@0 | 48 | nsCOMPtr<nsIAppShellService> appShell(do_GetService(NS_APPSHELLSERVICE_CONTRACTID)); |
michael@0 | 49 | nsCOMPtr<nsIXULWindow> hiddenWindow; |
michael@0 | 50 | |
michael@0 | 51 | nsresult rv = appShell->GetHiddenWindow(getter_AddRefs(hiddenWindow)); |
michael@0 | 52 | if (NS_FAILED(rv)) { |
michael@0 | 53 | return rv; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | nsCOMPtr<nsIDocShell> docShell; |
michael@0 | 57 | rv = hiddenWindow->GetDocShell(getter_AddRefs(docShell)); |
michael@0 | 58 | if (NS_FAILED(rv) || !docShell) { |
michael@0 | 59 | return rv; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(docShell)); |
michael@0 | 63 | |
michael@0 | 64 | if (!baseWindow) |
michael@0 | 65 | return NS_ERROR_FAILURE; |
michael@0 | 66 | |
michael@0 | 67 | nsCOMPtr<nsIWidget> widget; |
michael@0 | 68 | baseWindow->GetMainWidget(getter_AddRefs(widget)); |
michael@0 | 69 | |
michael@0 | 70 | if (!widget) |
michael@0 | 71 | return NS_ERROR_FAILURE; |
michael@0 | 72 | |
michael@0 | 73 | hidden_window_hwnd = (HWND)widget->GetNativeData(NS_NATIVE_WINDOW); |
michael@0 | 74 | |
michael@0 | 75 | return NS_OK; |
michael@0 | 76 | } |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | HWND GetHiddenWindowHWND() |
michael@0 | 80 | { |
michael@0 | 81 | // Need to dispatch this to the main thread because plenty of |
michael@0 | 82 | // the things it wants to access are main-thread-only. |
michael@0 | 83 | nsRefPtr<HWNDGetter> getter = new HWNDGetter(); |
michael@0 | 84 | NS_DispatchToMainThread(getter, NS_DISPATCH_SYNC); |
michael@0 | 85 | return getter->hidden_window_hwnd; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | } // namespace |
michael@0 | 89 | |
michael@0 | 90 | namespace mozilla { |
michael@0 | 91 | |
michael@0 | 92 | bool InitWidgetTracing() |
michael@0 | 93 | { |
michael@0 | 94 | sEventHandle = CreateEvent(nullptr, FALSE, FALSE, nullptr); |
michael@0 | 95 | return sEventHandle != nullptr; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | void CleanUpWidgetTracing() |
michael@0 | 99 | { |
michael@0 | 100 | CloseHandle(sEventHandle); |
michael@0 | 101 | sEventHandle = nullptr; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | // This function is called from the main (UI) thread. |
michael@0 | 105 | void SignalTracerThread() |
michael@0 | 106 | { |
michael@0 | 107 | if (sEventHandle != nullptr) |
michael@0 | 108 | SetEvent(sEventHandle); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | // This function is called from the background tracer thread. |
michael@0 | 112 | bool FireAndWaitForTracerEvent() |
michael@0 | 113 | { |
michael@0 | 114 | NS_ABORT_IF_FALSE(sEventHandle, "Tracing not initialized!"); |
michael@0 | 115 | |
michael@0 | 116 | // First, try to find the hidden window. |
michael@0 | 117 | static HWND hidden_window = nullptr; |
michael@0 | 118 | if (hidden_window == nullptr) { |
michael@0 | 119 | hidden_window = GetHiddenWindowHWND(); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | if (hidden_window == nullptr) |
michael@0 | 123 | return false; |
michael@0 | 124 | |
michael@0 | 125 | // Post the tracer message into the hidden window's message queue, |
michael@0 | 126 | // and then block until it's processed. |
michael@0 | 127 | PostMessage(hidden_window, MOZ_WM_TRACE, 0, 0); |
michael@0 | 128 | WaitForSingleObject(sEventHandle, INFINITE); |
michael@0 | 129 | return true; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | } // namespace mozilla |