widget/windows/WidgetTraceEvent.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/WidgetTraceEvent.h"
    14 #include "nsAppShellCID.h"
    15 #include "nsComponentManagerUtils.h"
    16 #include "nsCOMPtr.h"
    17 #include "nsIAppShellService.h"
    18 #include "nsIBaseWindow.h"
    19 #include "nsIDocShell.h"
    20 #include "nsISupportsImpl.h"
    21 #include "nsIWidget.h"
    22 #include "nsIXULWindow.h"
    23 #include "nsAutoPtr.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 nsRunnable {
    36 public:
    37   HWNDGetter() : hidden_window_hwnd(nullptr) {
    38     MOZ_COUNT_CTOR(HWNDGetter);
    39   }
    40   ~HWNDGetter() {
    41     MOZ_COUNT_DTOR(HWNDGetter);
    42   }
    44   HWND hidden_window_hwnd;
    46   NS_IMETHOD Run() {
    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));
    52     if (NS_FAILED(rv)) {
    53       return rv;
    54     }
    56     nsCOMPtr<nsIDocShell> docShell;
    57     rv = hiddenWindow->GetDocShell(getter_AddRefs(docShell));
    58     if (NS_FAILED(rv) || !docShell) {
    59       return rv;
    60     }
    62     nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(docShell));
    64     if (!baseWindow)
    65       return NS_ERROR_FAILURE;
    67     nsCOMPtr<nsIWidget> widget;
    68     baseWindow->GetMainWidget(getter_AddRefs(widget));
    70     if (!widget)
    71       return NS_ERROR_FAILURE;
    73     hidden_window_hwnd = (HWND)widget->GetNativeData(NS_NATIVE_WINDOW);
    75     return NS_OK;
    76   }
    77 };
    79 HWND GetHiddenWindowHWND()
    80 {
    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;
    86 }
    88 } // namespace
    90 namespace mozilla {
    92 bool InitWidgetTracing()
    93 {
    94   sEventHandle = CreateEvent(nullptr, FALSE, FALSE, nullptr);
    95   return sEventHandle != nullptr;
    96 }
    98 void CleanUpWidgetTracing()
    99 {
   100   CloseHandle(sEventHandle);
   101   sEventHandle = nullptr;
   102 }
   104 // This function is called from the main (UI) thread.
   105 void SignalTracerThread()
   106 {
   107   if (sEventHandle != nullptr)
   108     SetEvent(sEventHandle);
   109 }
   111 // This function is called from the background tracer thread.
   112 bool FireAndWaitForTracerEvent()
   113 {
   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();
   120   }
   122   if (hidden_window == nullptr)
   123     return false;
   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);
   129   return true;
   130 }
   132 }  // namespace mozilla

mercurial