1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
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"
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
) {
27 nsCOMPtr
<nsPIDOMWindowOuter
> pidomwindow
= nsPIDOMWindowOuter::From(window
);
29 nsCOMPtr
<nsIBaseWindow
> ppBaseWindow
=
30 do_QueryInterface(pidomwindow
->GetDocShell());
35 nsCOMPtr
<nsIWidget
> ppWidget
;
36 ppBaseWindow
->GetMainWidget(getter_AddRefs(ppWidget
));
38 return (HWND
)(ppWidget
->GetNativeData(NS_NATIVE_WIDGET
));
41 static nsresult
GetMostRecentWindow(mozIDOMWindowProxy
** aWindow
) {
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();
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
) {
72 BuildClassName(aAppName
, aProfileName
, className
);
74 WNDCLASSW classStruct
= {0, // style
75 &WindowProc
, // lpfnWndProc
83 className
.get()}; // lpszClassName
85 // Register the window class.
86 NS_ENSURE_TRUE(::RegisterClassW(&classStruct
), NS_ERROR_FAILURE
);
89 mHandle
= ::CreateWindowW(className
.get(),
92 0, 0, 0, 0, // x, y, cx, cy
98 return mHandle
? NS_OK
: NS_ERROR_FAILURE
;
101 void nsWinRemoteServer::Shutdown() {
102 DestroyWindow(mHandle
);