1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file implements the Windows service controlling Me2Me host processes
6 // running within user sessions.
8 #include "base/at_exit.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/file_path.h"
13 #include "base/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/scoped_native_library.h"
16 #include "base/stringprintf.h"
17 #include "base/utf_string_conversions.h"
18 #include "base/win/windows_version.h"
19 #include "remoting/base/auto_thread_task_runner.h"
20 #include "remoting/base/breakpad.h"
21 #include "remoting/host/desktop_process.h"
22 #include "remoting/host/host_exit_codes.h"
23 #include "remoting/host/logging.h"
24 #include "remoting/host/usage_stats_consent.h"
26 #if defined(OS_MACOSX)
27 #include "base/mac/scoped_nsautorelease_pool.h"
28 #endif // defined(OS_MACOSX)
32 #endif // defined(OS_WIN)
36 // The command line switch specifying the name of the daemon IPC endpoint.
37 const char kDaemonIpcSwitchName
[] = "daemon-pipe";
39 // "--help" or "--?" prints the usage message.
40 const char kHelpSwitchName
[] = "help";
41 const char kQuestionSwitchName
[] = "?";
43 const char kUsageMessage
[] =
45 "Usage: %s [options]\n"
48 " --help, --? - Print this message.\n";
50 void Usage(const FilePath
& program_name
) {
51 std::string display_name
= UTF16ToUTF8(program_name
.LossyDisplayName());
52 LOG(INFO
) << StringPrintf(kUsageMessage
, display_name
.c_str());
57 int main(int argc
, char** argv
) {
58 #if defined(OS_MACOSX)
59 // Needed so we don't leak objects when threads are created.
60 base::mac::ScopedNSAutoreleasePool pool
;
63 CommandLine::Init(argc
, argv
);
65 // This object instance is required by Chrome code (for example,
66 // LazyInstance, MessageLoop).
67 base::AtExitManager exit_manager
;
69 remoting::InitHostLogging();
71 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
72 if (command_line
->HasSwitch(kHelpSwitchName
) ||
73 command_line
->HasSwitch(kQuestionSwitchName
)) {
74 Usage(command_line
->GetProgram());
75 return remoting::kSuccessExitCode
;
78 std::string channel_name
=
79 command_line
->GetSwitchValueASCII(kDaemonIpcSwitchName
);
81 if (channel_name
.empty()) {
82 Usage(command_line
->GetProgram());
83 return remoting::kUsageExitCode
;
86 MessageLoop
message_loop(MessageLoop::TYPE_UI
);
87 base::RunLoop run_loop
;
88 base::Closure quit_ui_task_runner
= base::Bind(
89 base::IgnoreResult(&base::SingleThreadTaskRunner::PostTask
),
90 message_loop
.message_loop_proxy(),
91 FROM_HERE
, run_loop
.QuitClosure());
92 scoped_refptr
<remoting::AutoThreadTaskRunner
> ui_task_runner
=
93 new remoting::AutoThreadTaskRunner(message_loop
.message_loop_proxy(),
96 remoting::DesktopProcess
desktop_process(ui_task_runner
, channel_name
);
97 if (!desktop_process
.Start())
98 return remoting::kInitializationFailed
;
100 // Run the UI message loop.
101 ui_task_runner
= NULL
;
104 return remoting::kSuccessExitCode
;
109 int CALLBACK
WinMain(HINSTANCE instance
,
110 HINSTANCE previous_instance
,
111 LPSTR raw_command_line
,
113 #ifdef OFFICIAL_BUILD
114 if (remoting::IsUsageStatsAllowed()) {
115 remoting::InitializeCrashReporting();
117 #endif // OFFICIAL_BUILD
119 // Register and initialize common controls.
120 INITCOMMONCONTROLSEX info
;
121 info
.dwSize
= sizeof(info
);
122 info
.dwICC
= ICC_STANDARD_CLASSES
;
123 InitCommonControlsEx(&info
);
125 // Mark the process as DPI-aware, so Windows won't scale coordinates in APIs.
126 // N.B. This API exists on Vista and above.
127 if (base::win::GetVersion() >= base::win::VERSION_VISTA
) {
128 FilePath
path(base::GetNativeLibraryName(UTF8ToUTF16("user32")));
129 base::ScopedNativeLibrary
user32(path
);
130 CHECK(user32
.is_valid());
132 typedef BOOL (WINAPI
* SetProcessDPIAwareFn
)();
133 SetProcessDPIAwareFn set_process_dpi_aware
=
134 static_cast<SetProcessDPIAwareFn
>(
135 user32
.GetFunctionPointer("SetProcessDPIAware"));
136 set_process_dpi_aware();
139 // CommandLine::Init() ignores the passed |argc| and |argv| on Windows getting
140 // the command line from GetCommandLineW(), so we can safely pass NULL here.
141 return main(0, NULL
);
144 #endif // defined(OS_WIN)