| OLD | NEW |
| 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/plugin/daemon_controller.h" | 5 #include "remoting/host/plugin/daemon_controller.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" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 std::string GetMd5(const std::string& value) { | 37 std::string GetMd5(const std::string& value) { |
| 38 base::MD5Context ctx; | 38 base::MD5Context ctx; |
| 39 base::MD5Init(&ctx); | 39 base::MD5Init(&ctx); |
| 40 base::MD5Update(&ctx, value); | 40 base::MD5Update(&ctx, value); |
| 41 base::MD5Digest digest; | 41 base::MD5Digest digest; |
| 42 base::MD5Final(&digest, &ctx); | 42 base::MD5Final(&digest, &ctx); |
| 43 return StringToLowerASCII(base::HexEncode(digest.a, sizeof(digest.a))); | 43 return StringToLowerASCII(base::HexEncode(digest.a, sizeof(digest.a))); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // TODO(sergeyu): This is a very hacky implementation of | |
| 47 // DaemonController interface for linux. Current version works, but | |
| 48 // there are sevaral problems with it: | |
| 49 // * All calls are executed synchronously, even though this API is | |
| 50 // supposed to be asynchronous. | |
| 51 // * The host is configured by passing configuration data as CL | |
| 52 // argument - this is obviously not secure. | |
| 53 // Rewrite this code to solve these two problems. | |
| 54 // http://crbug.com/120950 . | |
| 55 class DaemonControllerLinux : public remoting::DaemonController { | 46 class DaemonControllerLinux : public remoting::DaemonController { |
| 56 public: | 47 public: |
| 57 DaemonControllerLinux(); | 48 DaemonControllerLinux(); |
| 58 | 49 |
| 59 virtual State GetState() OVERRIDE; | 50 virtual State GetState() OVERRIDE; |
| 60 virtual void GetConfig(const GetConfigCallback& callback) OVERRIDE; | 51 virtual void GetConfig(const GetConfigCallback& callback) OVERRIDE; |
| 61 virtual void SetConfigAndStart( | 52 virtual void SetConfigAndStart( |
| 62 scoped_ptr<base::DictionaryValue> config, | 53 scoped_ptr<base::DictionaryValue> config, |
| 63 bool consent, | 54 bool consent, |
| 64 const CompletionCallback& done) OVERRIDE; | 55 const CompletionCallback& done) OVERRIDE; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 219 } |
| 229 } | 220 } |
| 230 | 221 |
| 231 callback.Run(result.Pass()); | 222 callback.Run(result.Pass()); |
| 232 } | 223 } |
| 233 | 224 |
| 234 void DaemonControllerLinux::DoSetConfigAndStart( | 225 void DaemonControllerLinux::DoSetConfigAndStart( |
| 235 scoped_ptr<base::DictionaryValue> config, | 226 scoped_ptr<base::DictionaryValue> config, |
| 236 const CompletionCallback& done_callback) { | 227 const CompletionCallback& done_callback) { |
| 237 JsonHostConfig config_file(GetConfigPath()); | 228 JsonHostConfig config_file(GetConfigPath()); |
| 238 for (DictionaryValue::key_iterator key(config->begin_keys()); | 229 if (!config_file.CopyFrom(config.get()) || |
| 239 key != config->end_keys(); ++key) { | 230 !config_file.Save()) { |
| 240 std::string value; | 231 LOG(ERROR) << "Failed to update config file."; |
| 241 if (!config->GetString(*key, &value)) { | |
| 242 LOG(ERROR) << *key << " is not a string."; | |
| 243 done_callback.Run(RESULT_FAILED); | |
| 244 return; | |
| 245 } | |
| 246 config_file.SetString(*key, value); | |
| 247 } | |
| 248 | |
| 249 bool success = config_file.Save(); | |
| 250 if (!success) { | |
| 251 done_callback.Run(RESULT_FAILED); | 232 done_callback.Run(RESULT_FAILED); |
| 252 return; | 233 return; |
| 253 } | 234 } |
| 254 | 235 |
| 255 std::vector<std::string> args; | 236 std::vector<std::string> args; |
| 256 args.push_back("--silent"); | 237 args.push_back("--silent"); |
| 257 AsyncResult result; | 238 AsyncResult result; |
| 258 int exit_code; | 239 int exit_code; |
| 259 if (RunScript(args, &exit_code)) { | 240 if (RunScript(args, &exit_code)) { |
| 260 result = (exit_code == 0) ? RESULT_OK : RESULT_FAILED; | 241 result = (exit_code == 0) ? RESULT_OK : RESULT_FAILED; |
| 261 } else { | 242 } else { |
| 262 result = RESULT_FAILED; | 243 result = RESULT_FAILED; |
| 263 } | 244 } |
| 264 done_callback.Run(result); | 245 done_callback.Run(result); |
| 265 } | 246 } |
| 266 | 247 |
| 267 void DaemonControllerLinux::DoUpdateConfig( | 248 void DaemonControllerLinux::DoUpdateConfig( |
| 268 scoped_ptr<base::DictionaryValue> config, | 249 scoped_ptr<base::DictionaryValue> config, |
| 269 const CompletionCallback& done_callback) { | 250 const CompletionCallback& done_callback) { |
| 270 JsonHostConfig config_file(GetConfigPath()); | 251 JsonHostConfig config_file(GetConfigPath()); |
| 271 if (!config_file.Read()) { | 252 if (!config_file.Read() || |
| 253 !config_file.CopyFrom(config.get()) || |
| 254 !config_file.Save()) { |
| 255 LOG(ERROR) << "Failed to update config file."; |
| 272 done_callback.Run(RESULT_FAILED); | 256 done_callback.Run(RESULT_FAILED); |
| 257 return; |
| 273 } | 258 } |
| 274 | 259 |
| 275 for (DictionaryValue::key_iterator key(config->begin_keys()); | 260 std::vector<std::string> args; |
| 276 key != config->end_keys(); ++key) { | 261 args.push_back("--reload"); |
| 277 std::string value; | 262 AsyncResult result; |
| 278 if (!config->GetString(*key, &value)) { | 263 int exit_code; |
| 279 LOG(ERROR) << *key << " is not a string."; | 264 if (RunScript(args, &exit_code)) { |
| 280 done_callback.Run(RESULT_FAILED); | 265 result = (exit_code == 0) ? RESULT_OK : RESULT_FAILED; |
| 281 return; | 266 } else { |
| 282 } | 267 result = RESULT_FAILED; |
| 283 config_file.SetString(*key, value); | |
| 284 } | 268 } |
| 285 bool success = config_file.Save(); | 269 |
| 286 done_callback.Run(success ? RESULT_OK : RESULT_FAILED); | 270 done_callback.Run(result); |
| 287 // TODO(sergeyu): Send signal to the daemon to restart the host. | |
| 288 } | 271 } |
| 289 | 272 |
| 290 void DaemonControllerLinux::DoStop(const CompletionCallback& done_callback) { | 273 void DaemonControllerLinux::DoStop(const CompletionCallback& done_callback) { |
| 291 std::vector<std::string> args; | 274 std::vector<std::string> args; |
| 292 args.push_back("--stop"); | 275 args.push_back("--stop"); |
| 293 int exit_code = 0; | 276 int exit_code = 0; |
| 294 AsyncResult result; | 277 AsyncResult result; |
| 295 if (RunScript(args, &exit_code)) { | 278 if (RunScript(args, &exit_code)) { |
| 296 result = (exit_code == 0) ? RESULT_OK : RESULT_FAILED; | 279 result = (exit_code == 0) ? RESULT_OK : RESULT_FAILED; |
| 297 } else { | 280 } else { |
| 298 result = RESULT_FAILED; | 281 result = RESULT_FAILED; |
| 299 } | 282 } |
| 300 done_callback.Run(result); | 283 done_callback.Run(result); |
| 301 } | 284 } |
| 302 | 285 |
| 303 } // namespace | 286 } // namespace |
| 304 | 287 |
| 305 scoped_ptr<DaemonController> remoting::DaemonController::Create() { | 288 scoped_ptr<DaemonController> remoting::DaemonController::Create() { |
| 306 return scoped_ptr<DaemonController>(new DaemonControllerLinux()); | 289 return scoped_ptr<DaemonController>(new DaemonControllerLinux()); |
| 307 } | 290 } |
| 308 | 291 |
| 309 } // namespace remoting | 292 } // namespace remoting |
| OLD | NEW |