Bug 1943650 - Command-line --help output misformatted after --dbus-service. r=emilio
[gecko.git] / toolkit / components / remote / nsWinRemoteServer.cpp
blob0fa4f5facca5bac115ab76df90657b192b2dcae7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "CmdLineAndEnvUtils.h"
9 #include "nsWinRemoteServer.h"
10 #include "RemoteUtils.h"
11 #include "nsCOMPtr.h"
12 #include "nsXPCOM.h"
13 #include "nsPIDOMWindow.h"
14 #include "nsIWindowMediator.h"
15 #include "nsIBaseWindow.h"
16 #include "nsIWidget.h"
17 #include "nsICommandLineRunner.h"
18 #include "nsICommandLine.h"
19 #include "nsCommandLine.h"
20 #include "nsIDocShell.h"
21 #include "WinRemoteMessage.h"
23 HWND hwndForDOMWindow(mozIDOMWindowProxy* window) {
24 if (!window) {
25 return 0;
27 nsCOMPtr<nsPIDOMWindowOuter> pidomwindow = nsPIDOMWindowOuter::From(window);
29 nsCOMPtr<nsIBaseWindow> ppBaseWindow =
30 do_QueryInterface(pidomwindow->GetDocShell());
31 if (!ppBaseWindow) {
32 return 0;
35 nsCOMPtr<nsIWidget> ppWidget;
36 ppBaseWindow->GetMainWidget(getter_AddRefs(ppWidget));
38 return (HWND)(ppWidget->GetNativeData(NS_NATIVE_WIDGET));
41 static nsresult GetMostRecentWindow(mozIDOMWindowProxy** aWindow) {
42 nsresult rv;
43 nsCOMPtr<nsIWindowMediator> med(
44 do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv));
45 if (NS_FAILED(rv)) return rv;
47 if (med) return med->GetMostRecentWindow(nullptr, aWindow);
49 return NS_ERROR_FAILURE;
52 LRESULT CALLBACK WindowProc(HWND msgWindow, UINT msg, WPARAM wp, LPARAM lp) {
53 if (msg == WM_COPYDATA) {
54 WinRemoteMessageReceiver receiver;
55 if (NS_SUCCEEDED(receiver.Parse(reinterpret_cast<COPYDATASTRUCT*>(lp)))) {
56 receiver.CommandLineRunner()->Run();
57 } else {
58 NS_ERROR("Error initializing command line.");
61 // Get current window and return its window handle.
62 nsCOMPtr<mozIDOMWindowProxy> win;
63 GetMostRecentWindow(getter_AddRefs(win));
64 return win ? (LRESULT)hwndForDOMWindow(win) : 0;
66 return DefWindowProcW(msgWindow, msg, wp, lp);
69 nsresult nsWinRemoteServer::Startup(const char* aAppName,
70 const char* aProfileName) {
71 nsString className;
72 BuildClassName(aAppName, aProfileName, className);
74 WNDCLASSW classStruct = {0, // style
75 &WindowProc, // lpfnWndProc
76 0, // cbClsExtra
77 0, // cbWndExtra
78 0, // hInstance
79 0, // hIcon
80 0, // hCursor
81 0, // hbrBackground
82 0, // lpszMenuName
83 className.get()}; // lpszClassName
85 // Register the window class.
86 NS_ENSURE_TRUE(::RegisterClassW(&classStruct), NS_ERROR_FAILURE);
88 // Create the window.
89 mHandle = ::CreateWindowW(className.get(),
90 0, // title
91 WS_CAPTION, // style
92 0, 0, 0, 0, // x, y, cx, cy
93 0, // parent
94 0, // menu
95 0, // instance
96 0); // create struct
98 return mHandle ? NS_OK : NS_ERROR_FAILURE;
101 void nsWinRemoteServer::Shutdown() {
102 DestroyWindow(mHandle);
103 mHandle = nullptr;