Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / host / host_main.cc
blob9a19b46fb69952c624d948d5649f3f3e07bb36b8
1 // Copyright (c) 2013 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.
4 //
5 // This file implements the common entry point shared by all Chromoting Host
6 // processes.
8 #include "remoting/host/host_main.h"
10 #include <string>
12 #include "base/at_exit.h"
13 #include "base/command_line.h"
14 #include "base/files/file_path.h"
15 #include "base/i18n/icu_util.h"
16 #include "base/logging.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringize_macros.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "remoting/base/breakpad.h"
22 #include "remoting/base/resources.h"
23 #include "remoting/host/host_exit_codes.h"
24 #include "remoting/host/logging.h"
25 #include "remoting/host/setup/me2me_native_messaging_host.h"
26 #include "remoting/host/usage_stats_consent.h"
28 #if defined(OS_MACOSX)
29 #include "base/mac/scoped_nsautorelease_pool.h"
30 #endif // defined(OS_MACOSX)
32 #if defined(OS_WIN)
33 #include <commctrl.h>
34 #include <shellapi.h>
35 #endif // defined(OS_WIN)
37 namespace remoting {
39 // Known entry points.
40 int HostProcessMain();
41 #if defined(OS_WIN)
42 int DaemonProcessMain();
43 int DesktopProcessMain();
44 int ElevatedControllerMain();
45 int RdpDesktopSessionMain();
46 #endif // defined(OS_WIN)
48 const char kElevateSwitchName[] = "elevate";
49 const char kProcessTypeSwitchName[] = "type";
51 const char kProcessTypeController[] = "controller";
52 const char kProcessTypeDaemon[] = "daemon";
53 const char kProcessTypeDesktop[] = "desktop";
54 const char kProcessTypeHost[] = "host";
55 const char kProcessTypeRdpDesktopSession[] = "rdp_desktop_session";
57 namespace {
59 typedef int (*MainRoutineFn)();
61 // "--help" or "--?" prints the usage message.
62 const char kHelpSwitchName[] = "help";
63 const char kQuestionSwitchName[] = "?";
65 // The command line switch used to get version of the daemon.
66 const char kVersionSwitchName[] = "version";
68 const char kUsageMessage[] =
69 "Usage: %s [options]\n"
70 "\n"
71 "Options:\n"
72 " --audio-pipe-name=<pipe> - Sets the pipe name to capture audio on Linux.\n"
73 " --console - Runs the daemon interactively.\n"
74 " --daemon-pipe=<pipe> - Specifies the pipe to connect to the daemon.\n"
75 " --elevate=<binary> - Runs <binary> elevated.\n"
76 " --host-config=<config> - Specifies the host configuration.\n"
77 " --help, -? - Print this message.\n"
78 " --type - Specifies process type.\n"
79 " --version - Prints the host version and exits.\n";
81 void Usage(const base::FilePath& program_name) {
82 printf(kUsageMessage, program_name.MaybeAsASCII().c_str());
85 #if defined(OS_WIN)
87 // Runs the binary specified by the command line, elevated.
88 int RunElevated() {
89 const CommandLine::SwitchMap& switches =
90 CommandLine::ForCurrentProcess()->GetSwitches();
91 CommandLine::StringVector args = CommandLine::ForCurrentProcess()->GetArgs();
93 // Create the child process command line by copying switches from the current
94 // command line.
95 CommandLine command_line(CommandLine::NO_PROGRAM);
96 for (CommandLine::SwitchMap::const_iterator i = switches.begin();
97 i != switches.end(); ++i) {
98 if (i->first != kElevateSwitchName)
99 command_line.AppendSwitchNative(i->first, i->second);
101 for (CommandLine::StringVector::const_iterator i = args.begin();
102 i != args.end(); ++i) {
103 command_line.AppendArgNative(*i);
106 // Get the name of the binary to launch.
107 base::FilePath binary =
108 CommandLine::ForCurrentProcess()->GetSwitchValuePath(kElevateSwitchName);
109 CommandLine::StringType parameters = command_line.GetCommandLineString();
111 // Launch the child process requesting elevation.
112 SHELLEXECUTEINFO info;
113 memset(&info, 0, sizeof(info));
114 info.cbSize = sizeof(info);
115 info.lpVerb = L"runas";
116 info.lpFile = binary.value().c_str();
117 info.lpParameters = parameters.c_str();
118 info.nShow = SW_SHOWNORMAL;
120 if (!ShellExecuteEx(&info)) {
121 DWORD exit_code = GetLastError();
122 LOG_GETLASTERROR(ERROR) << "Unable to launch '" << binary.value() << "'";
123 return exit_code;
126 return kSuccessExitCode;
129 #endif // !defined(OS_WIN)
131 // Select the entry point corresponding to the process type.
132 MainRoutineFn SelectMainRoutine(const std::string& process_type) {
133 MainRoutineFn main_routine = NULL;
135 if (process_type == kProcessTypeHost) {
136 main_routine = &HostProcessMain;
137 #if defined(OS_WIN)
138 } else if (process_type == kProcessTypeDaemon) {
139 main_routine = &DaemonProcessMain;
140 } else if (process_type == kProcessTypeDesktop) {
141 main_routine = &DesktopProcessMain;
142 } else if (process_type == kProcessTypeController) {
143 main_routine = &ElevatedControllerMain;
144 } else if (process_type == kProcessTypeRdpDesktopSession) {
145 main_routine = &RdpDesktopSessionMain;
146 #endif // defined(OS_WIN)
149 return main_routine;
152 } // namespace
154 int HostMain(int argc, char** argv) {
155 #if defined(OS_MACOSX)
156 // Needed so we don't leak objects when threads are created.
157 base::mac::ScopedNSAutoreleasePool pool;
158 #endif
160 CommandLine::Init(argc, argv);
162 // Initialize Breakpad as early as possible. On Mac the command-line needs to
163 // be initialized first, so that the preference for crash-reporting can be
164 // looked up in the config file.
165 #if defined(REMOTING_ENABLE_BREAKPAD)
166 if (IsUsageStatsAllowed()) {
167 InitializeCrashReporting();
169 #endif // defined(REMOTING_ENABLE_BREAKPAD)
171 // This object instance is required by Chrome code (for example,
172 // LazyInstance, MessageLoop).
173 base::AtExitManager exit_manager;
175 // Enable debug logs.
176 InitHostLogging();
178 // Register and initialize common controls.
179 #if defined(OS_WIN)
180 INITCOMMONCONTROLSEX info;
181 info.dwSize = sizeof(info);
182 info.dwICC = ICC_STANDARD_CLASSES;
183 InitCommonControlsEx(&info);
184 #endif // defined(OS_WIN)
186 // Parse the command line.
187 const CommandLine* command_line = CommandLine::ForCurrentProcess();
188 if (command_line->HasSwitch(kHelpSwitchName) ||
189 command_line->HasSwitch(kQuestionSwitchName)) {
190 Usage(command_line->GetProgram());
191 return kSuccessExitCode;
194 if (command_line->HasSwitch(kVersionSwitchName)) {
195 printf("%s\n", STRINGIZE(VERSION));
196 return kSuccessExitCode;
199 #if defined(OS_WIN)
200 if (command_line->HasSwitch(kElevateSwitchName)) {
201 return RunElevated();
203 #endif // defined(OS_WIN)
205 // Assume the host process by default.
206 std::string process_type = kProcessTypeHost;
207 if (command_line->HasSwitch(kProcessTypeSwitchName)) {
208 process_type = command_line->GetSwitchValueASCII(kProcessTypeSwitchName);
211 MainRoutineFn main_routine = SelectMainRoutine(process_type);
212 if (!main_routine) {
213 fprintf(stderr, "Unknown process type '%s' specified.",
214 process_type.c_str());
215 Usage(command_line->GetProgram());
216 return kUsageExitCode;
219 // Required to find the ICU data file, used by some file_util routines.
220 base::i18n::InitializeICU();
222 remoting::LoadResources("");
224 // Invoke the entry point.
225 int exit_code = main_routine();
226 if (exit_code == kUsageExitCode) {
227 Usage(command_line->GetProgram());
230 remoting::UnloadResources();
232 return exit_code;
235 } // namespace remoting