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_win.h"
7 #include "base/basictypes.h"
9 #include "base/bind_helpers.h"
10 #include "base/compiler_specific.h"
11 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h"
13 #include "base/logging.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
19 #include "base/values.h"
20 #include "base/win/scoped_bstr.h"
21 #include "base/win/scoped_comptr.h"
22 #include "base/win/windows_version.h"
23 #include "remoting/base/scoped_sc_handle_win.h"
24 #include "remoting/host/branding.h"
25 // chromoting_lib.h contains MIDL-generated declarations.
26 #include "remoting/host/chromoting_lib.h"
27 #include "remoting/host/usage_stats_consent.h"
29 using base::win::ScopedBstr
;
30 using base::win::ScopedComPtr
;
36 // ProgID of the daemon controller.
37 const wchar_t kDaemonController
[] =
38 L
"ChromotingElevatedController.ElevatedController";
40 // The COM elevation moniker for the Elevated Controller.
41 const wchar_t kDaemonControllerElevationMoniker
[] =
42 L
"Elevation:Administrator!new:"
43 L
"ChromotingElevatedController.ElevatedController";
45 // The maximum duration of keeping a reference to a privileged instance of
46 // the Daemon Controller. This effectively reduces number of UAC prompts a user
48 const int kPrivilegedTimeoutSec
= 5 * 60;
50 // The maximum duration of keeping a reference to an unprivileged instance of
51 // the Daemon Controller. This interval should not be too long. If upgrade
52 // happens while there is a live reference to a Daemon Controller instance
53 // the old binary still can be used. So dropping the references often makes sure
54 // that the old binary will go away sooner.
55 const int kUnprivilegedTimeoutSec
= 60;
57 void ConfigToString(const base::DictionaryValue
& config
, ScopedBstr
* out
) {
58 std::string config_str
;
59 base::JSONWriter::Write(&config
, &config_str
);
60 ScopedBstr
config_scoped_bstr(base::UTF8ToUTF16(config_str
).c_str());
61 out
->Swap(config_scoped_bstr
);
64 DaemonController::State
ConvertToDaemonState(DWORD service_state
) {
65 switch (service_state
) {
67 return DaemonController::STATE_STARTED
;
69 case SERVICE_CONTINUE_PENDING
:
70 case SERVICE_START_PENDING
:
71 return DaemonController::STATE_STARTING
;
74 case SERVICE_PAUSE_PENDING
:
75 case SERVICE_STOP_PENDING
:
76 return DaemonController::STATE_STOPPING
;
81 return DaemonController::STATE_STOPPED
;
86 return DaemonController::STATE_UNKNOWN
;
90 DWORD
OpenService(ScopedScHandle
* service_out
) {
91 // Open the service and query its current state.
92 ScopedScHandle
scmanager(
93 ::OpenSCManagerW(NULL
, SERVICES_ACTIVE_DATABASE
,
94 SC_MANAGER_CONNECT
| SC_MANAGER_ENUMERATE_SERVICE
));
95 if (!scmanager
.IsValid()) {
96 DWORD error
= GetLastError();
97 PLOG(ERROR
) << "Failed to connect to the service control manager";
101 ScopedScHandle
service(
102 ::OpenServiceW(scmanager
, kWindowsServiceName
, SERVICE_QUERY_STATUS
));
103 if (!service
.IsValid()) {
104 DWORD error
= GetLastError();
105 if (error
!= ERROR_SERVICE_DOES_NOT_EXIST
) {
106 PLOG(ERROR
) << "Failed to open to the '" << kWindowsServiceName
112 service_out
->Set(service
.Take());
113 return ERROR_SUCCESS
;
116 DaemonController::AsyncResult
HResultToAsyncResult(
119 return DaemonController::RESULT_OK
;
120 } else if (hr
== HRESULT_FROM_WIN32(ERROR_CANCELLED
)) {
121 return DaemonController::RESULT_CANCELLED
;
123 // TODO(sergeyu): Report other errors to the webapp once it knows
124 // how to handle them.
125 return DaemonController::RESULT_FAILED
;
129 void InvokeCompletionCallback(
130 const DaemonController::CompletionCallback
& done
, HRESULT hr
) {
131 done
.Run(HResultToAsyncResult(hr
));
136 DaemonControllerDelegateWin::DaemonControllerDelegateWin()
137 : control_is_elevated_(false),
138 window_handle_(NULL
) {
141 DaemonControllerDelegateWin::~DaemonControllerDelegateWin() {
144 DaemonController::State
DaemonControllerDelegateWin::GetState() {
145 if (base::win::GetVersion() < base::win::VERSION_XP
) {
146 return DaemonController::STATE_NOT_IMPLEMENTED
;
148 // TODO(alexeypa): Make the thread alertable, so we can switch to APC
149 // notifications rather than polling.
150 ScopedScHandle service
;
151 DWORD error
= OpenService(&service
);
154 case ERROR_SUCCESS
: {
155 SERVICE_STATUS status
;
156 if (::QueryServiceStatus(service
, &status
)) {
157 return ConvertToDaemonState(status
.dwCurrentState
);
159 PLOG(ERROR
) << "Failed to query the state of the '"
160 << kWindowsServiceName
<< "' service";
161 return DaemonController::STATE_UNKNOWN
;
165 case ERROR_SERVICE_DOES_NOT_EXIST
:
166 return DaemonController::STATE_NOT_INSTALLED
;
168 return DaemonController::STATE_UNKNOWN
;
172 scoped_ptr
<base::DictionaryValue
> DaemonControllerDelegateWin::GetConfig() {
173 // Configure and start the Daemon Controller if it is installed already.
174 HRESULT hr
= ActivateController();
176 return scoped_ptr
<base::DictionaryValue
>();
178 // Get the host configuration.
179 ScopedBstr host_config
;
180 hr
= control_
->GetConfig(host_config
.Receive());
182 return scoped_ptr
<base::DictionaryValue
>();
184 // Parse the string into a dictionary.
185 base::string16
file_content(
186 static_cast<BSTR
>(host_config
), host_config
.Length());
187 scoped_ptr
<base::Value
> config(
188 base::JSONReader::Read(base::UTF16ToUTF8(file_content
),
189 base::JSON_ALLOW_TRAILING_COMMAS
));
191 if (!config
|| config
->GetType() != base::Value::TYPE_DICTIONARY
)
192 return scoped_ptr
<base::DictionaryValue
>();
194 return scoped_ptr
<base::DictionaryValue
>(
195 static_cast<base::DictionaryValue
*>(config
.release()));
198 void DaemonControllerDelegateWin::InstallHost(
199 const DaemonController::CompletionCallback
& done
) {
200 DoInstallHost(base::Bind(&InvokeCompletionCallback
, done
));
203 void DaemonControllerDelegateWin::SetConfigAndStart(
204 scoped_ptr
<base::DictionaryValue
> config
,
206 const DaemonController::CompletionCallback
& done
) {
208 base::Bind(&DaemonControllerDelegateWin::StartHostWithConfig
,
209 base::Unretained(this), base::Passed(&config
), consent
, done
));
212 void DaemonControllerDelegateWin::DoInstallHost(
213 const DaemonInstallerWin::CompletionCallback
& done
) {
214 // Configure and start the Daemon Controller if it is installed already.
215 HRESULT hr
= ActivateElevatedController();
221 // Otherwise, install it if its COM registration entry is missing.
222 if (hr
== CO_E_CLASSSTRING
) {
225 installer_
= DaemonInstallerWin::Create(
226 GetTopLevelWindow(window_handle_
), done
);
227 installer_
->Install();
231 LOG(ERROR
) << "Failed to initiate the Chromoting Host installation "
232 << "(error: 0x" << std::hex
<< hr
<< std::dec
<< ").";
236 void DaemonControllerDelegateWin::UpdateConfig(
237 scoped_ptr
<base::DictionaryValue
> config
,
238 const DaemonController::CompletionCallback
& done
) {
239 HRESULT hr
= ActivateElevatedController();
241 InvokeCompletionCallback(done
, hr
);
245 // Update the configuration.
246 ScopedBstr
config_str(NULL
);
247 ConfigToString(*config
, &config_str
);
248 if (config_str
== NULL
) {
249 InvokeCompletionCallback(done
, E_OUTOFMEMORY
);
253 // Make sure that the PIN confirmation dialog is focused properly.
254 hr
= control_
->SetOwnerWindow(
255 reinterpret_cast<LONG_PTR
>(GetTopLevelWindow(window_handle_
)));
257 InvokeCompletionCallback(done
, hr
);
261 hr
= control_
->UpdateConfig(config_str
);
262 InvokeCompletionCallback(done
, hr
);
265 void DaemonControllerDelegateWin::Stop(
266 const DaemonController::CompletionCallback
& done
) {
267 HRESULT hr
= ActivateElevatedController();
269 hr
= control_
->StopDaemon();
271 InvokeCompletionCallback(done
, hr
);
274 void DaemonControllerDelegateWin::SetWindow(void* window_handle
) {
275 window_handle_
= reinterpret_cast<HWND
>(window_handle
);
278 std::string
DaemonControllerDelegateWin::GetVersion() {
279 // Configure and start the Daemon Controller if it is installed already.
280 HRESULT hr
= ActivateController();
282 return std::string();
284 // Get the version string.
286 hr
= control_
->GetVersion(version
.Receive());
288 return std::string();
290 return base::UTF16ToUTF8(
291 base::string16(static_cast<BSTR
>(version
), version
.Length()));
294 DaemonController::UsageStatsConsent
295 DaemonControllerDelegateWin::GetUsageStatsConsent() {
296 DaemonController::UsageStatsConsent consent
;
297 consent
.supported
= true;
298 consent
.allowed
= false;
299 consent
.set_by_policy
= false;
301 // Activate the Daemon Controller and see if it supports |IDaemonControl2|.
302 HRESULT hr
= ActivateController();
304 // The host is not installed yet. Assume that the user didn't consent to
305 // collecting crash dumps.
309 if (control2_
.get() == NULL
) {
310 // The host is installed and does not support crash dump reporting.
314 // Get the recorded user's consent.
317 hr
= control2_
->GetUsageStatsConsent(&allowed
, &set_by_policy
);
319 // If the user's consent is not recorded yet, assume that the user didn't
320 // consent to collecting crash dumps.
324 consent
.allowed
= !!allowed
;
325 consent
.set_by_policy
= !!set_by_policy
;
329 HRESULT
DaemonControllerDelegateWin::ActivateController() {
332 HRESULT hr
= CLSIDFromProgID(kDaemonController
, &class_id
);
337 hr
= CoCreateInstance(class_id
, NULL
, CLSCTX_LOCAL_SERVER
,
338 IID_IDaemonControl
, control_
.ReceiveVoid());
343 // Ignore the error. IID_IDaemonControl2 is optional.
344 control_
.QueryInterface(IID_IDaemonControl2
, control2_
.ReceiveVoid());
346 // Release |control_| upon expiration of the timeout.
347 release_timer_
.reset(new base::OneShotTimer
<DaemonControllerDelegateWin
>());
348 release_timer_
->Start(FROM_HERE
,
349 base::TimeDelta::FromSeconds(kUnprivilegedTimeoutSec
),
351 &DaemonControllerDelegateWin::ReleaseController
);
357 HRESULT
DaemonControllerDelegateWin::ActivateElevatedController() {
358 // The COM elevation is supported on Vista and above.
359 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
360 return ActivateController();
362 // Release an unprivileged instance of the daemon controller if any.
363 if (!control_is_elevated_
)
367 BIND_OPTS3 bind_options
;
368 memset(&bind_options
, 0, sizeof(bind_options
));
369 bind_options
.cbStruct
= sizeof(bind_options
);
370 bind_options
.hwnd
= GetTopLevelWindow(window_handle_
);
371 bind_options
.dwClassContext
= CLSCTX_LOCAL_SERVER
;
373 HRESULT hr
= ::CoGetObject(
374 kDaemonControllerElevationMoniker
,
377 control_
.ReceiveVoid());
382 // Ignore the error. IID_IDaemonControl2 is optional.
383 control_
.QueryInterface(IID_IDaemonControl2
, control2_
.ReceiveVoid());
385 // Note that we hold a reference to an elevated instance now.
386 control_is_elevated_
= true;
388 // Release |control_| upon expiration of the timeout.
389 release_timer_
.reset(new base::OneShotTimer
<DaemonControllerDelegateWin
>());
390 release_timer_
->Start(FROM_HERE
,
391 base::TimeDelta::FromSeconds(kPrivilegedTimeoutSec
),
393 &DaemonControllerDelegateWin::ReleaseController
);
399 void DaemonControllerDelegateWin::ReleaseController() {
402 release_timer_
.reset();
403 control_is_elevated_
= false;
406 void DaemonControllerDelegateWin::StartHostWithConfig(
407 scoped_ptr
<base::DictionaryValue
> config
,
409 const DaemonController::CompletionCallback
& done
,
414 LOG(ERROR
) << "Failed to install the Chromoting Host "
415 << "(error: 0x" << std::hex
<< hr
<< std::dec
<< ").";
416 InvokeCompletionCallback(done
, hr
);
420 hr
= ActivateElevatedController();
422 InvokeCompletionCallback(done
, hr
);
426 // Record the user's consent.
428 hr
= control2_
->SetUsageStatsConsent(consent
);
430 InvokeCompletionCallback(done
, hr
);
435 // Set the configuration.
436 ScopedBstr
config_str(NULL
);
437 ConfigToString(*config
, &config_str
);
438 if (config_str
== NULL
) {
439 InvokeCompletionCallback(done
, E_OUTOFMEMORY
);
443 hr
= control_
->SetOwnerWindow(
444 reinterpret_cast<LONG_PTR
>(GetTopLevelWindow(window_handle_
)));
446 InvokeCompletionCallback(done
, hr
);
450 hr
= control_
->SetConfig(config_str
);
452 InvokeCompletionCallback(done
, hr
);
457 hr
= control_
->StartDaemon();
458 InvokeCompletionCallback(done
, hr
);
461 scoped_refptr
<DaemonController
> DaemonController::Create() {
462 scoped_ptr
<DaemonController::Delegate
> delegate(
463 new DaemonControllerDelegateWin());
464 return new DaemonController(delegate
.Pass());
467 } // namespace remoting