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 <objbase.h> | 7 #include <objbase.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/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/json/json_reader.h" | 15 #include "base/json/json_reader.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/string16.h" | 18 #include "base/string16.h" |
19 #include "base/stringize_macros.h" | 19 #include "base/stringize_macros.h" |
20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
22 #include "base/values.h" | 22 #include "base/values.h" |
23 #include "base/win/scoped_bstr.h" | 23 #include "base/win/scoped_bstr.h" |
24 #include "base/win/scoped_comptr.h" | 24 #include "base/win/scoped_comptr.h" |
25 #include "remoting/base/scoped_sc_handle_win.h" | 25 #include "remoting/base/scoped_sc_handle_win.h" |
26 #include "remoting/host/branding.h" | 26 #include "remoting/host/branding.h" |
| 27 #include "remoting/host/daemon_controller_common_win.h" |
27 #include "remoting/host/plugin/daemon_installer_win.h" | 28 #include "remoting/host/plugin/daemon_installer_win.h" |
28 | 29 |
29 // MIDL-generated declarations and definitions. | 30 // MIDL-generated declarations and definitions. |
30 #include "remoting/host/elevated_controller.h" | 31 #include "remoting/host/elevated_controller.h" |
31 | 32 |
32 using base::win::ScopedBstr; | 33 using base::win::ScopedBstr; |
33 using base::win::ScopedComPtr; | 34 using base::win::ScopedComPtr; |
34 | 35 |
35 namespace remoting { | 36 namespace remoting { |
36 | 37 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 // The functions that actually do the work. They should be called in | 99 // The functions that actually do the work. They should be called in |
99 // the context of |worker_thread_|; | 100 // the context of |worker_thread_|; |
100 void DoGetConfig(const GetConfigCallback& callback); | 101 void DoGetConfig(const GetConfigCallback& callback); |
101 void DoInstallAsNeededAndStart(scoped_ptr<base::DictionaryValue> config, | 102 void DoInstallAsNeededAndStart(scoped_ptr<base::DictionaryValue> config, |
102 const CompletionCallback& done_callback); | 103 const CompletionCallback& done_callback); |
103 void DoSetConfigAndStart(scoped_ptr<base::DictionaryValue> config, | 104 void DoSetConfigAndStart(scoped_ptr<base::DictionaryValue> config, |
104 const CompletionCallback& done_callback); | 105 const CompletionCallback& done_callback); |
105 void DoUpdateConfig(scoped_ptr<base::DictionaryValue> config, | 106 void DoUpdateConfig(scoped_ptr<base::DictionaryValue> config, |
106 const CompletionCallback& done_callback); | 107 const CompletionCallback& done_callback); |
107 void DoStop(const CompletionCallback& done_callback); | 108 void DoStop(const CompletionCallback& done_callback); |
| 109 base::DictionaryValue* ReadConfig(); |
108 | 110 |
109 // The worker thread used for servicing long running operations. | 111 // The worker thread used for servicing long running operations. |
110 ComThread worker_thread_; | 112 ComThread worker_thread_; |
111 | 113 |
112 scoped_ptr<DaemonInstallerWin> installer_; | 114 scoped_ptr<DaemonInstallerWin> installer_; |
113 | 115 |
114 DISALLOW_COPY_AND_ASSIGN(DaemonControllerWin); | 116 DISALLOW_COPY_AND_ASSIGN(DaemonControllerWin); |
115 }; | 117 }; |
116 | 118 |
117 ComThread::ComThread(const char* name) : base::Thread(name), control_(NULL) { | 119 ComThread::ComThread(const char* name) : base::Thread(name), control_(NULL) { |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 return error; | 305 return error; |
304 } | 306 } |
305 | 307 |
306 service_out->Set(service.Take()); | 308 service_out->Set(service.Take()); |
307 return ERROR_SUCCESS; | 309 return ERROR_SUCCESS; |
308 } | 310 } |
309 | 311 |
310 void DaemonControllerWin::DoGetConfig(const GetConfigCallback& callback) { | 312 void DaemonControllerWin::DoGetConfig(const GetConfigCallback& callback) { |
311 DCHECK(worker_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 313 DCHECK(worker_thread_.message_loop_proxy()->BelongsToCurrentThread()); |
312 | 314 |
313 IDaemonControl* control = NULL; | 315 callback.Run(scoped_ptr<base::DictionaryValue>(ReadConfig())); |
314 HRESULT hr = worker_thread_.ActivateElevatedController(&control); | |
315 if (FAILED(hr)) { | |
316 callback.Run(scoped_ptr<base::DictionaryValue>()); | |
317 return; | |
318 } | |
319 | |
320 // Get the host configuration. | |
321 ScopedBstr host_config; | |
322 hr = control->GetConfig(host_config.Receive()); | |
323 if (FAILED(hr)) { | |
324 callback.Run(scoped_ptr<base::DictionaryValue>()); | |
325 return; | |
326 } | |
327 | |
328 string16 file_content(static_cast<BSTR>(host_config), host_config.Length()); | |
329 | |
330 // Parse the string into a dictionary. | |
331 scoped_ptr<base::Value> config( | |
332 base::JSONReader::Read(UTF16ToUTF8(file_content), | |
333 base::JSON_ALLOW_TRAILING_COMMAS)); | |
334 | |
335 base::DictionaryValue* dictionary; | |
336 if (config.get() == NULL || !config->GetAsDictionary(&dictionary)) { | |
337 callback.Run(scoped_ptr<base::DictionaryValue>()); | |
338 return; | |
339 } | |
340 | |
341 config.release(); | |
342 callback.Run(scoped_ptr<base::DictionaryValue>(dictionary)); | |
343 } | 316 } |
344 | 317 |
345 void DaemonControllerWin::DoInstallAsNeededAndStart( | 318 void DaemonControllerWin::DoInstallAsNeededAndStart( |
346 scoped_ptr<base::DictionaryValue> config, | 319 scoped_ptr<base::DictionaryValue> config, |
347 const CompletionCallback& done_callback) { | 320 const CompletionCallback& done_callback) { |
348 DCHECK(worker_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 321 DCHECK(worker_thread_.message_loop_proxy()->BelongsToCurrentThread()); |
349 | 322 |
350 IDaemonControl* control = NULL; | 323 IDaemonControl* control = NULL; |
351 HRESULT hr = worker_thread_.ActivateElevatedController(&control); | 324 HRESULT hr = worker_thread_.ActivateElevatedController(&control); |
352 | 325 |
353 // Just configure and start the Daemon Controller if it is installed already. | 326 // Just configure and start the Daemon Controller if it is installed already. |
354 if (SUCCEEDED(hr)) { | 327 if (SUCCEEDED(hr)) { |
355 DoSetConfigAndStart(config.Pass(), done_callback); | 328 DoSetConfigAndStart(config.Pass(), done_callback); |
356 return; | 329 return; |
357 } | 330 } |
358 | 331 |
359 // Otherwise, install it if it's COM registration entry is missing. | 332 // Otherwise, install it if its COM registration entry is missing. |
360 if (hr == CO_E_CLASSSTRING) { | 333 if (hr == CO_E_CLASSSTRING) { |
361 scoped_ptr<DaemonInstallerWin> installer = DaemonInstallerWin::Create( | 334 scoped_ptr<DaemonInstallerWin> installer = DaemonInstallerWin::Create( |
362 base::Bind(&DaemonControllerWin::OnInstallationComplete, | 335 base::Bind(&DaemonControllerWin::OnInstallationComplete, |
363 base::Unretained(this), | 336 base::Unretained(this), |
364 base::Passed(&config), | 337 base::Passed(&config), |
365 done_callback)); | 338 done_callback)); |
366 if (installer.get()) { | 339 if (installer.get()) { |
367 DCHECK(!installer_.get()); | 340 DCHECK(!installer_.get()); |
368 installer_ = installer.Pass(); | 341 installer_ = installer.Pass(); |
369 installer_->Install(); | 342 installer_->Install(); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 HRESULT hr = worker_thread_.ActivateElevatedController(&control); | 416 HRESULT hr = worker_thread_.ActivateElevatedController(&control); |
444 if (FAILED(hr)) { | 417 if (FAILED(hr)) { |
445 done_callback.Run(HResultToAsyncResult(hr)); | 418 done_callback.Run(HResultToAsyncResult(hr)); |
446 return; | 419 return; |
447 } | 420 } |
448 | 421 |
449 hr = control->StopDaemon(); | 422 hr = control->StopDaemon(); |
450 done_callback.Run(HResultToAsyncResult(hr)); | 423 done_callback.Run(HResultToAsyncResult(hr)); |
451 } | 424 } |
452 | 425 |
| 426 // Reads and parses the configuration file up to |kMaxConfigFileSize| in |
| 427 // size. On error, it logs a message and returns NULL. |
| 428 base::DictionaryValue* DaemonControllerWin::ReadConfig() { |
| 429 // Get the name of the configuration file. |
| 430 FilePath dir = remoting::GetConfigDir(); |
| 431 FilePath filename = dir.Append(kUnprivilegedConfigFileName); |
| 432 |
| 433 // Read raw data from the configuration file. |
| 434 base::win::ScopedHandle file( |
| 435 CreateFileW(filename.value().c_str(), |
| 436 GENERIC_READ, |
| 437 FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 438 NULL, |
| 439 OPEN_EXISTING, |
| 440 FILE_FLAG_SEQUENTIAL_SCAN, |
| 441 NULL)); |
| 442 |
| 443 if (!file.IsValid()) { |
| 444 DWORD error = GetLastError(); |
| 445 LOG_GETLASTERROR(ERROR) |
| 446 << "Failed to open '" << filename.value() << "'"; |
| 447 return NULL; |
| 448 } |
| 449 |
| 450 scoped_array<char> buffer(new char[kMaxConfigFileSize]); |
| 451 DWORD size = kMaxConfigFileSize; |
| 452 if (!::ReadFile(file, &buffer[0], size, &size, NULL)) { |
| 453 DWORD error = GetLastError(); |
| 454 LOG_GETLASTERROR(ERROR) |
| 455 << "Failed to read '" << filename.value() << "'"; |
| 456 return NULL; |
| 457 } |
| 458 |
| 459 // Parse the JSON configuration, expecting it to contain a dictionary. |
| 460 std::string file_content(buffer.get(), size); |
| 461 scoped_ptr<base::Value> value( |
| 462 base::JSONReader::Read(file_content, base::JSON_ALLOW_TRAILING_COMMAS)); |
| 463 |
| 464 base::DictionaryValue* dictionary = NULL; |
| 465 if (value.get() == NULL || !value->GetAsDictionary(&dictionary)) { |
| 466 LOG(ERROR) << "Failed to read '" << filename.value() << "'"; |
| 467 return NULL; |
| 468 } |
| 469 // Release value, because dictionary points to the same object. |
| 470 value.release(); |
| 471 return dictionary; |
| 472 } |
| 473 |
453 } // namespace | 474 } // namespace |
454 | 475 |
455 scoped_ptr<DaemonController> remoting::DaemonController::Create() { | 476 scoped_ptr<DaemonController> remoting::DaemonController::Create() { |
456 return scoped_ptr<DaemonController>(new DaemonControllerWin()); | 477 return scoped_ptr<DaemonController>(new DaemonControllerWin()); |
457 } | 478 } |
458 | 479 |
459 } // namespace remoting | 480 } // namespace remoting |
OLD | NEW |