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 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 return error; | 304 return error; |
304 } | 305 } |
305 | 306 |
306 service_out->Set(service.Take()); | 307 service_out->Set(service.Take()); |
307 return ERROR_SUCCESS; | 308 return ERROR_SUCCESS; |
308 } | 309 } |
309 | 310 |
310 void DaemonControllerWin::DoGetConfig(const GetConfigCallback& callback) { | 311 void DaemonControllerWin::DoGetConfig(const GetConfigCallback& callback) { |
311 DCHECK(worker_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 312 DCHECK(worker_thread_.message_loop_proxy()->BelongsToCurrentThread()); |
312 | 313 |
313 IDaemonControl* control = NULL; | 314 scoped_ptr<base::DictionaryValue> dictionary_null(NULL); |
314 HRESULT hr = worker_thread_.ActivateElevatedController(&control); | 315 // Get the name of the configuration file. |
315 if (FAILED(hr)) { | 316 FilePath dir = remoting::GetConfigDir(); |
316 callback.Run(scoped_ptr<base::DictionaryValue>()); | 317 FilePath filename = dir.Append(kUnprivilegedConfigFileName); |
| 318 |
| 319 // Read raw data from the configuration file. |
| 320 base::win::ScopedHandle file( |
| 321 CreateFileW(filename.value().c_str(), |
| 322 GENERIC_READ, |
| 323 FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 324 NULL, |
| 325 OPEN_EXISTING, |
| 326 FILE_FLAG_SEQUENTIAL_SCAN, |
| 327 NULL)); |
| 328 |
| 329 if (!file.IsValid()) { |
| 330 DWORD error = GetLastError(); |
| 331 LOG_GETLASTERROR(ERROR) |
| 332 << "Failed to open '" << filename.value() << "'"; |
| 333 callback.Run(dictionary_null.Pass()); |
317 return; | 334 return; |
318 } | 335 } |
319 | 336 |
320 // Get the host configuration. | 337 scoped_array<char> buffer(new char[kMaxConfigFileSize]); |
321 ScopedBstr host_config; | 338 DWORD size = kMaxConfigFileSize; |
322 hr = control->GetConfig(host_config.Receive()); | 339 if (!::ReadFile(file, &buffer[0], size, &size, NULL)) { |
323 if (FAILED(hr)) { | 340 DWORD error = GetLastError(); |
324 callback.Run(scoped_ptr<base::DictionaryValue>()); | 341 LOG_GETLASTERROR(ERROR) |
| 342 << "Failed to read '" << filename.value() << "'"; |
| 343 callback.Run(dictionary_null.Pass()); |
325 return; | 344 return; |
326 } | 345 } |
327 | 346 |
328 string16 file_content(static_cast<BSTR>(host_config), host_config.Length()); | 347 // Parse the JSON configuration, expecting it to contain a dictionary. |
| 348 std::string file_content(buffer.get(), size); |
| 349 scoped_ptr<base::Value> value( |
| 350 base::JSONReader::Read(file_content, base::JSON_ALLOW_TRAILING_COMMAS)); |
329 | 351 |
330 // Parse the string into a dictionary. | 352 base::DictionaryValue* dictionary = NULL; |
331 scoped_ptr<base::Value> config( | 353 if (value.get() == NULL || !value->GetAsDictionary(&dictionary)) { |
332 base::JSONReader::Read(UTF16ToUTF8(file_content), | 354 LOG(ERROR) << "Failed to read '" << filename.value() << "'"; |
333 base::JSON_ALLOW_TRAILING_COMMAS)); | 355 callback.Run(dictionary_null.Pass()); |
334 | |
335 base::DictionaryValue* dictionary; | |
336 if (config.get() == NULL || !config->GetAsDictionary(&dictionary)) { | |
337 callback.Run(scoped_ptr<base::DictionaryValue>()); | |
338 return; | 356 return; |
339 } | 357 } |
| 358 // Release value, because dictionary points to the same object. |
| 359 value.release(); |
340 | 360 |
341 config.release(); | |
342 callback.Run(scoped_ptr<base::DictionaryValue>(dictionary)); | 361 callback.Run(scoped_ptr<base::DictionaryValue>(dictionary)); |
343 } | 362 } |
344 | 363 |
345 void DaemonControllerWin::DoInstallAsNeededAndStart( | 364 void DaemonControllerWin::DoInstallAsNeededAndStart( |
346 scoped_ptr<base::DictionaryValue> config, | 365 scoped_ptr<base::DictionaryValue> config, |
347 const CompletionCallback& done_callback) { | 366 const CompletionCallback& done_callback) { |
348 DCHECK(worker_thread_.message_loop_proxy()->BelongsToCurrentThread()); | 367 DCHECK(worker_thread_.message_loop_proxy()->BelongsToCurrentThread()); |
349 | 368 |
350 IDaemonControl* control = NULL; | 369 IDaemonControl* control = NULL; |
351 HRESULT hr = worker_thread_.ActivateElevatedController(&control); | 370 HRESULT hr = worker_thread_.ActivateElevatedController(&control); |
352 | 371 |
353 // Just configure and start the Daemon Controller if it is installed already. | 372 // Just configure and start the Daemon Controller if it is installed already. |
354 if (SUCCEEDED(hr)) { | 373 if (SUCCEEDED(hr)) { |
355 DoSetConfigAndStart(config.Pass(), done_callback); | 374 DoSetConfigAndStart(config.Pass(), done_callback); |
356 return; | 375 return; |
357 } | 376 } |
358 | 377 |
359 // Otherwise, install it if it's COM registration entry is missing. | 378 // Otherwise, install it if its COM registration entry is missing. |
360 if (hr == CO_E_CLASSSTRING) { | 379 if (hr == CO_E_CLASSSTRING) { |
361 scoped_ptr<DaemonInstallerWin> installer = DaemonInstallerWin::Create( | 380 scoped_ptr<DaemonInstallerWin> installer = DaemonInstallerWin::Create( |
362 base::Bind(&DaemonControllerWin::OnInstallationComplete, | 381 base::Bind(&DaemonControllerWin::OnInstallationComplete, |
363 base::Unretained(this), | 382 base::Unretained(this), |
364 base::Passed(&config), | 383 base::Passed(&config), |
365 done_callback)); | 384 done_callback)); |
366 if (installer.get()) { | 385 if (installer.get()) { |
367 DCHECK(!installer_.get()); | 386 DCHECK(!installer_.get()); |
368 installer_ = installer.Pass(); | 387 installer_ = installer.Pass(); |
369 installer_->Install(); | 388 installer_->Install(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 done_callback.Run(HResultToAsyncResult(hr)); | 469 done_callback.Run(HResultToAsyncResult(hr)); |
451 } | 470 } |
452 | 471 |
453 } // namespace | 472 } // namespace |
454 | 473 |
455 scoped_ptr<DaemonController> remoting::DaemonController::Create() { | 474 scoped_ptr<DaemonController> remoting::DaemonController::Create() { |
456 return scoped_ptr<DaemonController>(new DaemonControllerWin()); | 475 return scoped_ptr<DaemonController>(new DaemonControllerWin()); |
457 } | 476 } |
458 | 477 |
459 } // namespace remoting | 478 } // namespace remoting |
OLD | NEW |