Add test_runner support for new accessibility event
[chromium-blink-merge.git] / remoting / host / setup / daemon_controller_delegate_linux.cc
blob3d7fa6591f144fac378505e8316da4e67109bc4d
1 // Copyright 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.
5 #include "remoting/host/setup/daemon_controller_delegate_linux.h"
7 #include <unistd.h>
9 #include "base/base_paths.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/environment.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/json/json_writer.h"
18 #include "base/logging.h"
19 #include "base/md5.h"
20 #include "base/path_service.h"
21 #include "base/process/launch.h"
22 #include "base/process/process.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_split.h"
25 #include "base/strings/string_util.h"
26 #include "base/thread_task_runner_handle.h"
27 #include "base/values.h"
28 #include "build/build_config.h"
29 #include "net/base/net_util.h"
30 #include "remoting/host/host_config.h"
31 #include "remoting/host/usage_stats_consent.h"
33 namespace remoting {
35 namespace {
37 const char kDaemonScript[] =
38 "/opt/google/chrome-remote-desktop/chrome-remote-desktop";
40 // Timeout for running daemon script. The script itself sets a timeout when
41 // waiting for the host to come online, so the setting here should be at least
42 // as long.
43 const int64 kDaemonTimeoutMs = 60000;
45 // Timeout for commands that require password prompt - 5 minutes.
46 const int64 kSudoTimeoutSeconds = 5 * 60;
48 std::string GetMd5(const std::string& value) {
49 base::MD5Context ctx;
50 base::MD5Init(&ctx);
51 base::MD5Update(&ctx, value);
52 base::MD5Digest digest;
53 base::MD5Final(&digest, &ctx);
54 return base::StringToLowerASCII(base::HexEncode(digest.a, sizeof(digest.a)));
57 base::FilePath GetConfigPath() {
58 std::string filename = "host#" + GetMd5(net::GetHostName()) + ".json";
59 base::FilePath homedir;
60 PathService::Get(base::DIR_HOME, &homedir);
61 return homedir.Append(".config/chrome-remote-desktop").Append(filename);
64 bool GetScriptPath(base::FilePath* result) {
65 base::FilePath candidate_exe(kDaemonScript);
66 if (access(candidate_exe.value().c_str(), X_OK) == 0) {
67 *result = candidate_exe;
68 return true;
70 return false;
73 bool RunHostScriptWithTimeout(
74 const std::vector<std::string>& args,
75 base::TimeDelta timeout,
76 int* exit_code) {
77 DCHECK(exit_code);
79 // As long as we're relying on running an external binary from the
80 // PATH, don't do it as root.
81 if (getuid() == 0) {
82 LOG(ERROR) << "Refusing to run script as root.";
83 return false;
85 base::FilePath script_path;
86 if (!GetScriptPath(&script_path)) {
87 LOG(ERROR) << "GetScriptPath() failed.";
88 return false;
90 base::CommandLine command_line(script_path);
91 for (unsigned int i = 0; i < args.size(); ++i) {
92 command_line.AppendArg(args[i]);
95 // Redirect the child's stdout to the parent's stderr. In the case where this
96 // parent process is a Native Messaging host, its stdout is used to send
97 // messages to the web-app.
98 base::FileHandleMappingVector fds_to_remap;
99 fds_to_remap.push_back(std::pair<int, int>(STDERR_FILENO, STDOUT_FILENO));
100 base::LaunchOptions options;
101 options.fds_to_remap = &fds_to_remap;
103 #if !defined(OS_CHROMEOS)
104 options.allow_new_privs = true;
105 #endif
107 base::Process process = base::LaunchProcess(command_line, options);
108 if (!process.IsValid()) {
109 LOG(ERROR) << "Failed to run command: "
110 << command_line.GetCommandLineString();
111 return false;
114 if (!process.WaitForExitWithTimeout(timeout, exit_code)) {
115 process.Terminate(0, false);
116 LOG(ERROR) << "Timeout exceeded for command: "
117 << command_line.GetCommandLineString();
118 return false;
121 return true;
124 bool RunHostScript(const std::vector<std::string>& args, int* exit_code) {
125 return RunHostScriptWithTimeout(
126 args, base::TimeDelta::FromMilliseconds(kDaemonTimeoutMs), exit_code);
129 } // namespace
131 DaemonControllerDelegateLinux::DaemonControllerDelegateLinux() {
134 DaemonControllerDelegateLinux::~DaemonControllerDelegateLinux() {
137 DaemonController::State DaemonControllerDelegateLinux::GetState() {
138 base::FilePath script_path;
139 if (!GetScriptPath(&script_path)) {
140 LOG(ERROR) << "GetScriptPath() failed.";
141 return DaemonController::STATE_UNKNOWN;
143 base::CommandLine command_line(script_path);
144 command_line.AppendArg("--get-status");
146 std::string status;
147 int exit_code = 0;
148 if (!base::GetAppOutputWithExitCode(command_line, &status, &exit_code) ||
149 exit_code != 0) {
150 LOG(ERROR) << "Failed to run \"" << command_line.GetCommandLineString()
151 << "\". Exit code: " << exit_code;
152 return DaemonController::STATE_UNKNOWN;
155 base::TrimWhitespaceASCII(status, base::TRIM_ALL, &status);
157 if (status == "STARTED") {
158 return DaemonController::STATE_STARTED;
159 } else if (status == "STOPPED") {
160 return DaemonController::STATE_STOPPED;
161 } else if (status == "NOT_IMPLEMENTED") {
162 // Chrome Remote Desktop is not currently supported on the underlying Linux
163 // Distro.
164 return DaemonController::STATE_NOT_IMPLEMENTED;
165 } else {
166 LOG(ERROR) << "Unknown status string returned from \""
167 << command_line.GetCommandLineString()
168 << "\": " << status;
169 return DaemonController::STATE_UNKNOWN;
173 scoped_ptr<base::DictionaryValue> DaemonControllerDelegateLinux::GetConfig() {
174 scoped_ptr<base::DictionaryValue> config(
175 HostConfigFromJsonFile(GetConfigPath()));
176 if (!config)
177 return nullptr;
179 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
180 std::string value;
181 if (config->GetString(kHostIdConfigPath, &value)) {
182 result->SetString(kHostIdConfigPath, value);
184 if (config->GetString(kXmppLoginConfigPath, &value)) {
185 result->SetString(kXmppLoginConfigPath, value);
187 return result.Pass();
190 void DaemonControllerDelegateLinux::SetConfigAndStart(
191 scoped_ptr<base::DictionaryValue> config,
192 bool consent,
193 const DaemonController::CompletionCallback& done) {
194 // Add the user to chrome-remote-desktop group first.
195 std::vector<std::string> args;
196 args.push_back("--add-user");
197 int exit_code;
198 if (!RunHostScriptWithTimeout(
199 args, base::TimeDelta::FromSeconds(kSudoTimeoutSeconds),
200 &exit_code) ||
201 exit_code != 0) {
202 LOG(ERROR) << "Failed to add user to chrome-remote-desktop group.";
203 done.Run(DaemonController::RESULT_FAILED);
204 return;
207 // Ensure the configuration directory exists.
208 base::FilePath config_dir = GetConfigPath().DirName();
209 if (!base::DirectoryExists(config_dir) &&
210 !base::CreateDirectory(config_dir)) {
211 LOG(ERROR) << "Failed to create config directory " << config_dir.value();
212 done.Run(DaemonController::RESULT_FAILED);
213 return;
216 // Write config.
217 if (!HostConfigToJsonFile(*config, GetConfigPath())) {
218 LOG(ERROR) << "Failed to update config file.";
219 done.Run(DaemonController::RESULT_FAILED);
220 return;
223 // Finally start the host.
224 args.clear();
225 args.push_back("--start");
226 DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
227 if (RunHostScript(args, &exit_code) && (exit_code == 0))
228 result = DaemonController::RESULT_OK;
230 done.Run(result);
233 void DaemonControllerDelegateLinux::UpdateConfig(
234 scoped_ptr<base::DictionaryValue> config,
235 const DaemonController::CompletionCallback& done) {
236 scoped_ptr<base::DictionaryValue> new_config(
237 HostConfigFromJsonFile(GetConfigPath()));
238 if (new_config)
239 new_config->MergeDictionary(config.get());
240 if (!new_config || !HostConfigToJsonFile(*new_config, GetConfigPath())) {
241 LOG(ERROR) << "Failed to update config file.";
242 done.Run(DaemonController::RESULT_FAILED);
243 return;
246 std::vector<std::string> args;
247 args.push_back("--reload");
248 int exit_code = 0;
249 DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
250 if (RunHostScript(args, &exit_code) && (exit_code == 0))
251 result = DaemonController::RESULT_OK;
253 done.Run(result);
256 void DaemonControllerDelegateLinux::Stop(
257 const DaemonController::CompletionCallback& done) {
258 std::vector<std::string> args;
259 args.push_back("--stop");
260 int exit_code = 0;
261 DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
262 if (RunHostScript(args, &exit_code) && (exit_code == 0))
263 result = DaemonController::RESULT_OK;
265 done.Run(result);
268 DaemonController::UsageStatsConsent
269 DaemonControllerDelegateLinux::GetUsageStatsConsent() {
270 // Crash dump collection is not implemented on Linux yet.
271 // http://crbug.com/130678.
272 DaemonController::UsageStatsConsent consent;
273 consent.supported = false;
274 consent.allowed = false;
275 consent.set_by_policy = false;
276 return consent;
279 scoped_refptr<DaemonController> DaemonController::Create() {
280 scoped_ptr<DaemonController::Delegate> delegate(
281 new DaemonControllerDelegateLinux());
282 return new DaemonController(delegate.Pass());
285 } // namespace remoting