Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: remoting/host/setup/daemon_controller_delegate_linux.cc

Issue 23606019: Refactor the daemon controller so that the callbacks are called on the caller thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix the license Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/host/setup/daemon_controller.h" 5 #include "remoting/host/setup/daemon_controller_delegate_linux.h"
6 6
7 #include <unistd.h> 7 #include <unistd.h>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/environment.h" 13 #include "base/environment.h"
14 #include "base/file_util.h" 14 #include "base/file_util.h"
15 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
16 #include "base/json/json_writer.h" 16 #include "base/json/json_writer.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/md5.h" 18 #include "base/md5.h"
19 #include "base/process/kill.h" 19 #include "base/process/kill.h"
20 #include "base/process/launch.h" 20 #include "base/process/launch.h"
21 #include "base/process/process_handle.h" 21 #include "base/process/process_handle.h"
22 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_split.h" 23 #include "base/strings/string_split.h"
24 #include "base/strings/string_util.h" 24 #include "base/strings/string_util.h"
25 #include "base/threading/thread.h" 25 #include "base/thread_task_runner_handle.h"
26 #include "base/values.h" 26 #include "base/values.h"
27 #include "net/base/net_util.h" 27 #include "net/base/net_util.h"
28 #include "remoting/host/host_config.h" 28 #include "remoting/host/host_config.h"
29 #include "remoting/host/json_host_config.h" 29 #include "remoting/host/json_host_config.h"
30 #include "remoting/host/usage_stats_consent.h" 30 #include "remoting/host/usage_stats_consent.h"
31 31
32 namespace remoting { 32 namespace remoting {
33 33
34 namespace { 34 namespace {
35 35
(...skipping 10 matching lines...) Expand all
46 46
47 std::string GetMd5(const std::string& value) { 47 std::string GetMd5(const std::string& value) {
48 base::MD5Context ctx; 48 base::MD5Context ctx;
49 base::MD5Init(&ctx); 49 base::MD5Init(&ctx);
50 base::MD5Update(&ctx, value); 50 base::MD5Update(&ctx, value);
51 base::MD5Digest digest; 51 base::MD5Digest digest;
52 base::MD5Final(&digest, &ctx); 52 base::MD5Final(&digest, &ctx);
53 return StringToLowerASCII(base::HexEncode(digest.a, sizeof(digest.a))); 53 return StringToLowerASCII(base::HexEncode(digest.a, sizeof(digest.a)));
54 } 54 }
55 55
56 class DaemonControllerLinux : public remoting::DaemonController { 56 base::FilePath GetConfigPath() {
57 public: 57 std::string filename = "host#" + GetMd5(net::GetHostName()) + ".json";
58 DaemonControllerLinux(); 58 return file_util::GetHomeDir().
59 59 Append(".config/chrome-remote-desktop").Append(filename);
60 virtual State GetState() OVERRIDE;
61 virtual void GetConfig(const GetConfigCallback& callback) OVERRIDE;
62 virtual void SetConfigAndStart(
63 scoped_ptr<base::DictionaryValue> config,
64 bool consent,
65 const CompletionCallback& done) OVERRIDE;
66 virtual void UpdateConfig(scoped_ptr<base::DictionaryValue> config,
67 const CompletionCallback& done_callback) OVERRIDE;
68 virtual void Stop(const CompletionCallback& done_callback) OVERRIDE;
69 virtual void SetWindow(void* window_handle) OVERRIDE;
70 virtual void GetVersion(const GetVersionCallback& done_callback) OVERRIDE;
71 virtual void GetUsageStatsConsent(
72 const GetUsageStatsConsentCallback& done) OVERRIDE;
73
74 private:
75 base::FilePath GetConfigPath();
76
77 void DoGetConfig(const GetConfigCallback& callback);
78 void DoSetConfigAndStart(scoped_ptr<base::DictionaryValue> config,
79 const CompletionCallback& done);
80 void DoUpdateConfig(scoped_ptr<base::DictionaryValue> config,
81 const CompletionCallback& done_callback);
82 void DoStop(const CompletionCallback& done_callback);
83 void DoGetVersion(const GetVersionCallback& done_callback);
84
85 base::Thread file_io_thread_;
86
87 DISALLOW_COPY_AND_ASSIGN(DaemonControllerLinux);
88 };
89
90 DaemonControllerLinux::DaemonControllerLinux()
91 : file_io_thread_("DaemonControllerFileIO") {
92 file_io_thread_.Start();
93 } 60 }
94 61
95 static bool GetScriptPath(base::FilePath* result) { 62 bool GetScriptPath(base::FilePath* result) {
96 base::FilePath candidate_exe(kDaemonScript); 63 base::FilePath candidate_exe(kDaemonScript);
97 if (access(candidate_exe.value().c_str(), X_OK) == 0) { 64 if (access(candidate_exe.value().c_str(), X_OK) == 0) {
98 *result = candidate_exe; 65 *result = candidate_exe;
99 return true; 66 return true;
100 } 67 }
101 return false; 68 return false;
102 } 69 }
103 70
104 static bool RunHostScriptWithTimeout( 71 bool RunHostScriptWithTimeout(
105 const std::vector<std::string>& args, 72 const std::vector<std::string>& args,
106 base::TimeDelta timeout, 73 base::TimeDelta timeout,
107 int* exit_code) { 74 int* exit_code) {
108 DCHECK(exit_code); 75 DCHECK(exit_code);
109 76
110 // As long as we're relying on running an external binary from the 77 // As long as we're relying on running an external binary from the
111 // PATH, don't do it as root. 78 // PATH, don't do it as root.
112 if (getuid() == 0) { 79 if (getuid() == 0) {
113 LOG(ERROR) << "Refusing to run script as root."; 80 LOG(ERROR) << "Refusing to run script as root.";
114 return false; 81 return false;
(...skipping 25 matching lines...) Expand all
140 if (!base::WaitForExitCodeWithTimeout(process_handle, exit_code, timeout)) { 107 if (!base::WaitForExitCodeWithTimeout(process_handle, exit_code, timeout)) {
141 base::KillProcess(process_handle, 0, false); 108 base::KillProcess(process_handle, 0, false);
142 LOG(ERROR) << "Timeout exceeded for command: " 109 LOG(ERROR) << "Timeout exceeded for command: "
143 << command_line.GetCommandLineString(); 110 << command_line.GetCommandLineString();
144 return false; 111 return false;
145 } 112 }
146 113
147 return true; 114 return true;
148 } 115 }
149 116
150 static bool RunHostScript(const std::vector<std::string>& args, 117 bool RunHostScript(const std::vector<std::string>& args,
151 int* exit_code) { 118 int* exit_code) {
152 return RunHostScriptWithTimeout( 119 return RunHostScriptWithTimeout(
153 args, base::TimeDelta::FromMilliseconds(kDaemonTimeoutMs), exit_code); 120 args, base::TimeDelta::FromMilliseconds(kDaemonTimeoutMs), exit_code);
154 } 121 }
155 122
156 remoting::DaemonController::State DaemonControllerLinux::GetState() { 123 } // namespace
124
125 DaemonControllerDelegateLinux::DaemonControllerDelegateLinux() {
126 }
127
128 DaemonControllerDelegateLinux::~DaemonControllerDelegateLinux() {
129 }
130
131 DaemonController::State DaemonControllerDelegateLinux::GetState() {
157 std::vector<std::string> args; 132 std::vector<std::string> args;
158 args.push_back("--check-running"); 133 args.push_back("--check-running");
159 int exit_code = 0; 134 int exit_code = 0;
160 if (!RunHostScript(args, &exit_code)) { 135 if (!RunHostScript(args, &exit_code)) {
161 // TODO(jamiewalch): When we have a good story for installing, return 136 // TODO(jamiewalch): When we have a good story for installing, return
162 // NOT_INSTALLED rather than NOT_IMPLEMENTED (the former suppresses 137 // NOT_INSTALLED rather than NOT_IMPLEMENTED (the former suppresses
163 // the relevant UI in the web-app). 138 // the relevant UI in the web-app).
164 return remoting::DaemonController::STATE_NOT_IMPLEMENTED; 139 return DaemonController::STATE_NOT_IMPLEMENTED;
165 } 140 }
166 141
167 if (exit_code == 0) { 142 if (exit_code == 0) {
168 return remoting::DaemonController::STATE_STARTED; 143 return DaemonController::STATE_STARTED;
169 } else { 144 } else {
170 return remoting::DaemonController::STATE_STOPPED; 145 return DaemonController::STATE_STOPPED;
171 } 146 }
172 } 147 }
173 148
174 void DaemonControllerLinux::GetConfig(const GetConfigCallback& callback) { 149 scoped_ptr<base::DictionaryValue> DaemonControllerDelegateLinux::GetConfig() {
175 // base::Unretained() is safe because we control lifetime of the thread.
176 file_io_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
177 &DaemonControllerLinux::DoGetConfig, base::Unretained(this), callback));
178 }
179
180 void DaemonControllerLinux::GetUsageStatsConsent(
181 const GetUsageStatsConsentCallback& done) {
182 // Crash dump collection is not implemented on Linux yet.
183 // http://crbug.com/130678.
184 done.Run(false, false, false);
185 }
186
187 void DaemonControllerLinux::SetConfigAndStart(
188 scoped_ptr<base::DictionaryValue> config,
189 bool /* consent */,
190 const CompletionCallback& done) {
191 // base::Unretained() is safe because we control lifetime of the thread.
192 file_io_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
193 &DaemonControllerLinux::DoSetConfigAndStart, base::Unretained(this),
194 base::Passed(&config), done));
195 }
196
197 void DaemonControllerLinux::UpdateConfig(
198 scoped_ptr<base::DictionaryValue> config,
199 const CompletionCallback& done_callback) {
200 file_io_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
201 &DaemonControllerLinux::DoUpdateConfig, base::Unretained(this),
202 base::Passed(&config), done_callback));
203 }
204
205 void DaemonControllerLinux::Stop(const CompletionCallback& done_callback) {
206 file_io_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
207 &DaemonControllerLinux::DoStop, base::Unretained(this),
208 done_callback));
209 }
210
211 void DaemonControllerLinux::SetWindow(void* window_handle) {
212 // noop
213 }
214
215 void DaemonControllerLinux::GetVersion(
216 const GetVersionCallback& done_callback) {
217 file_io_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
218 &DaemonControllerLinux::DoGetVersion, base::Unretained(this),
219 done_callback));
220 }
221
222 base::FilePath DaemonControllerLinux::GetConfigPath() {
223 std::string filename = "host#" + GetMd5(net::GetHostName()) + ".json";
224 return file_util::GetHomeDir().
225 Append(".config/chrome-remote-desktop").Append(filename);
226 }
227
228 void DaemonControllerLinux::DoGetConfig(const GetConfigCallback& callback) {
229 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 150 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
230 151
231 if (GetState() != remoting::DaemonController::STATE_NOT_IMPLEMENTED) { 152 if (GetState() != DaemonController::STATE_NOT_IMPLEMENTED) {
232 JsonHostConfig config(GetConfigPath()); 153 JsonHostConfig config(GetConfigPath());
233 if (config.Read()) { 154 if (config.Read()) {
234 std::string value; 155 std::string value;
235 if (config.GetString(kHostIdConfigPath, &value)) { 156 if (config.GetString(kHostIdConfigPath, &value)) {
236 result->SetString(kHostIdConfigPath, value); 157 result->SetString(kHostIdConfigPath, value);
237 } 158 }
238 if (config.GetString(kXmppLoginConfigPath, &value)) { 159 if (config.GetString(kXmppLoginConfigPath, &value)) {
239 result->SetString(kXmppLoginConfigPath, value); 160 result->SetString(kXmppLoginConfigPath, value);
240 } 161 }
241 } else { 162 } else {
242 result.reset(); // Return NULL in case of error. 163 result.reset(); // Return NULL in case of error.
243 } 164 }
244 } 165 }
245 166
246 callback.Run(result.Pass()); 167 return result.Pass();
247 } 168 }
248 169
249 void DaemonControllerLinux::DoSetConfigAndStart( 170 void DaemonControllerDelegateLinux::SetConfigAndStart(
250 scoped_ptr<base::DictionaryValue> config, 171 scoped_ptr<base::DictionaryValue> config,
251 const CompletionCallback& done_callback) { 172 bool consent,
252 173 const DaemonController::CompletionCallback& done) {
253 // Add the user to chrome-remote-desktop group first. 174 // Add the user to chrome-remote-desktop group first.
254 std::vector<std::string> args; 175 std::vector<std::string> args;
255 args.push_back("--add-user"); 176 args.push_back("--add-user");
256 int exit_code; 177 int exit_code;
257 if (!RunHostScriptWithTimeout( 178 if (!RunHostScriptWithTimeout(
258 args, base::TimeDelta::FromSeconds(kSudoTimeoutSeconds), 179 args, base::TimeDelta::FromSeconds(kSudoTimeoutSeconds),
259 &exit_code) || 180 &exit_code) ||
260 exit_code != 0) { 181 exit_code != 0) {
261 LOG(ERROR) << "Failed to add user to chrome-remote-desktop group."; 182 LOG(ERROR) << "Failed to add user to chrome-remote-desktop group.";
262 done_callback.Run(RESULT_FAILED); 183 done.Run(DaemonController::RESULT_FAILED);
263 return; 184 return;
264 } 185 }
265 186
266 // Ensure the configuration directory exists. 187 // Ensure the configuration directory exists.
267 base::FilePath config_dir = GetConfigPath().DirName(); 188 base::FilePath config_dir = GetConfigPath().DirName();
268 if (!base::DirectoryExists(config_dir) && 189 if (!base::DirectoryExists(config_dir) &&
269 !file_util::CreateDirectory(config_dir)) { 190 !file_util::CreateDirectory(config_dir)) {
270 LOG(ERROR) << "Failed to create config directory " << config_dir.value(); 191 LOG(ERROR) << "Failed to create config directory " << config_dir.value();
271 done_callback.Run(RESULT_FAILED); 192 done.Run(DaemonController::RESULT_FAILED);
272 return; 193 return;
273 } 194 }
274 195
275 // Write config. 196 // Write config.
276 JsonHostConfig config_file(GetConfigPath()); 197 JsonHostConfig config_file(GetConfigPath());
277 if (!config_file.CopyFrom(config.get()) || 198 if (!config_file.CopyFrom(config.get()) ||
278 !config_file.Save()) { 199 !config_file.Save()) {
279 LOG(ERROR) << "Failed to update config file."; 200 LOG(ERROR) << "Failed to update config file.";
280 done_callback.Run(RESULT_FAILED); 201 done.Run(DaemonController::RESULT_FAILED);
281 return; 202 return;
282 } 203 }
283 204
284 // Finally start the host. 205 // Finally start the host.
285 args.clear(); 206 args.clear();
286 args.push_back("--start"); 207 args.push_back("--start");
287 AsyncResult result; 208 DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
288 if (RunHostScript(args, &exit_code)) { 209 if (RunHostScript(args, &exit_code) && (exit_code == 0))
289 result = (exit_code == 0) ? RESULT_OK : RESULT_FAILED; 210 result = DaemonController::RESULT_OK;
290 } else { 211
291 result = RESULT_FAILED; 212 done.Run(result);
292 }
293 done_callback.Run(result);
294 } 213 }
295 214
296 void DaemonControllerLinux::DoUpdateConfig( 215 void DaemonControllerDelegateLinux::UpdateConfig(
297 scoped_ptr<base::DictionaryValue> config, 216 scoped_ptr<base::DictionaryValue> config,
298 const CompletionCallback& done_callback) { 217 const DaemonController::CompletionCallback& done) {
299 JsonHostConfig config_file(GetConfigPath()); 218 JsonHostConfig config_file(GetConfigPath());
300 if (!config_file.Read() || 219 if (!config_file.Read() ||
301 !config_file.CopyFrom(config.get()) || 220 !config_file.CopyFrom(config.get()) ||
302 !config_file.Save()) { 221 !config_file.Save()) {
303 LOG(ERROR) << "Failed to update config file."; 222 LOG(ERROR) << "Failed to update config file.";
304 done_callback.Run(RESULT_FAILED); 223 done.Run(DaemonController::RESULT_FAILED);
305 return; 224 return;
306 } 225 }
307 226
308 std::vector<std::string> args; 227 std::vector<std::string> args;
309 args.push_back("--reload"); 228 args.push_back("--reload");
310 AsyncResult result; 229 int exit_code = 0;
311 int exit_code; 230 DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
312 if (RunHostScript(args, &exit_code)) { 231 if (RunHostScript(args, &exit_code) && (exit_code == 0))
313 result = (exit_code == 0) ? RESULT_OK : RESULT_FAILED; 232 result = DaemonController::RESULT_OK;
314 } else {
315 result = RESULT_FAILED;
316 }
317 233
318 done_callback.Run(result); 234 done.Run(result);
319 } 235 }
320 236
321 void DaemonControllerLinux::DoStop(const CompletionCallback& done_callback) { 237 void DaemonControllerDelegateLinux::Stop(
238 const DaemonController::CompletionCallback& done) {
322 std::vector<std::string> args; 239 std::vector<std::string> args;
323 args.push_back("--stop"); 240 args.push_back("--stop");
324 int exit_code = 0; 241 int exit_code = 0;
325 AsyncResult result; 242 DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
326 if (RunHostScript(args, &exit_code)) { 243 if (RunHostScript(args, &exit_code) && (exit_code == 0))
327 result = (exit_code == 0) ? RESULT_OK : RESULT_FAILED; 244 result = DaemonController::RESULT_OK;
328 } else { 245
329 result = RESULT_FAILED; 246 done.Run(result);
330 }
331 done_callback.Run(result);
332 } 247 }
333 248
334 void DaemonControllerLinux::DoGetVersion( 249 void DaemonControllerDelegateLinux::SetWindow(void* window_handle) {
335 const GetVersionCallback& done_callback) { 250 // noop
251 }
252
253 std::string DaemonControllerDelegateLinux::GetVersion() {
336 base::FilePath script_path; 254 base::FilePath script_path;
337 if (!GetScriptPath(&script_path)) { 255 if (!GetScriptPath(&script_path)) {
338 done_callback.Run(std::string()); 256 return std::string();
339 return;
340 } 257 }
341 CommandLine command_line(script_path); 258 CommandLine command_line(script_path);
342 command_line.AppendArg("--host-version"); 259 command_line.AppendArg("--host-version");
343 260
344 std::string version; 261 std::string version;
345 int exit_code = 0; 262 int exit_code = 0;
346 int result = 263 int result =
347 base::GetAppOutputWithExitCode(command_line, &version, &exit_code); 264 base::GetAppOutputWithExitCode(command_line, &version, &exit_code);
348 if (!result || exit_code != 0) { 265 if (!result || exit_code != 0) {
349 LOG(ERROR) << "Failed to run \"" << command_line.GetCommandLineString() 266 LOG(ERROR) << "Failed to run \"" << command_line.GetCommandLineString()
350 << "\". Exit code: " << exit_code; 267 << "\". Exit code: " << exit_code;
351 done_callback.Run(std::string()); 268 return std::string();
352 return;
353 } 269 }
354 270
355 TrimWhitespaceASCII(version, TRIM_ALL, &version); 271 TrimWhitespaceASCII(version, TRIM_ALL, &version);
356 if (!ContainsOnlyChars(version, "0123456789.")) { 272 if (!ContainsOnlyChars(version, "0123456789.")) {
357 LOG(ERROR) << "Received invalid host version number: " << version; 273 LOG(ERROR) << "Received invalid host version number: " << version;
358 done_callback.Run(std::string()); 274 return std::string();
359 return;
360 } 275 }
361 276
362 done_callback.Run(version); 277 return version;
363 } 278 }
364 279
365 } // namespace 280 DaemonController::UsageStatsConsent
281 DaemonControllerDelegateLinux::GetUsageStatsConsent() {
282 // Crash dump collection is not implemented on Linux yet.
283 // http://crbug.com/130678.
284 DaemonController::UsageStatsConsent consent;
285 consent.supported = false;
286 consent.allowed = false;
287 consent.set_by_policy = false;
288 return consent;
289 }
366 290
367 scoped_ptr<DaemonController> remoting::DaemonController::Create() { 291 scoped_refptr<DaemonController> DaemonController::Create() {
368 return scoped_ptr<DaemonController>(new DaemonControllerLinux()); 292 scoped_ptr<DaemonController::Delegate> delegate(
293 new DaemonControllerDelegateLinux());
294 return new DaemonController(delegate.Pass());
369 } 295 }
370 296
371 } // namespace remoting 297 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/setup/daemon_controller_delegate_linux.h ('k') | remoting/host/setup/daemon_controller_delegate_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698