1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/setup/daemon_installer_win.h"
#include <windows.h>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/process/launch.h"
#include "base/strings/string16.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/win/object_watcher.h"
#include "base/win/registry.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_comptr.h"
#include "base/win/scoped_handle.h"
#include "base/win/scoped_variant.h"
#include "base/win/windows_version.h"
#include "google_update/google_update_idl.h"
#include "remoting/base/dispatch_win.h"
#include "remoting/host/win/omaha.h"
using base::win::ScopedBstr;
using base::win::ScopedComPtr;
using base::win::ScopedVariant;
namespace {
// ProgID of the per-machine Omaha COM server.
const wchar_t kGoogleUpdate[] = L"GoogleUpdate.Update3WebMachine";
// The COM elevation moniker for the per-machine Omaha COM server.
const wchar_t kGoogleUpdateElevationMoniker[] =
L"Elevation:Administrator!new:GoogleUpdate.Update3WebMachine";
// The registry key where the configuration of Omaha is stored.
const wchar_t kOmahaUpdateKeyName[] = L"Software\\Google\\Update";
// The name of the value where the full path to GoogleUpdate.exe is stored.
const wchar_t kOmahaPathValueName[] = L"path";
// The command line format string for GoogleUpdate.exe
const wchar_t kGoogleUpdateCommandLineFormat[] =
L"\"%ls\" /install \"bundlename=Chromoting%%20Host&appguid=%ls&"
L"appname=Chromoting%%20Host&needsadmin=True&lang=%ls\"";
// TODO(alexeypa): Get the desired laungage from the web app.
const wchar_t kOmahaLanguage[] = L"en";
// An empty string for optional parameters.
const wchar_t kOmahaEmpty[] = L"";
// The installation status polling interval.
const int kOmahaPollIntervalMs = 500;
} // namespace
namespace remoting {
// This class implements on-demand installation of the Chromoting Host via
// per-machine Omaha instance.
class DaemonComInstallerWin : public DaemonInstallerWin {
public:
DaemonComInstallerWin(const ScopedComPtr<IDispatch>& update3,
const CompletionCallback& done);
// DaemonInstallerWin implementation.
virtual void Install() OVERRIDE;
private:
// Polls the installation status performing state-specific actions (such as
// starting installation once download has finished).
void PollInstallationStatus();
// Omaha interfaces.
ScopedVariant app_;
ScopedVariant bundle_;
ScopedComPtr<IDispatch> update3_;
base::Timer polling_timer_;
};
// This class implements on-demand installation of the Chromoting Host by
// launching a per-user instance of Omaha and requesting elevation.
class DaemonCommandLineInstallerWin
: public DaemonInstallerWin,
public base::win::ObjectWatcher::Delegate {
public:
DaemonCommandLineInstallerWin(const CompletionCallback& done);
~DaemonCommandLineInstallerWin();
// DaemonInstallerWin implementation.
virtual void Install() OVERRIDE;
// base::win::ObjectWatcher::Delegate implementation.
virtual void OnObjectSignaled(HANDLE object) OVERRIDE;
private:
// Handle of the launched process.
base::win::ScopedHandle process_;
// Used to determine when the launched process terminates.
base::win::ObjectWatcher process_watcher_;
};
DaemonComInstallerWin::DaemonComInstallerWin(
const ScopedComPtr<IDispatch>& update3,
const CompletionCallback& done)
: DaemonInstallerWin(done),
update3_(update3),
polling_timer_(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kOmahaPollIntervalMs),
base::Bind(&DaemonComInstallerWin::PollInstallationStatus,
base::Unretained(this)),
false) {
}
void DaemonComInstallerWin::Install() {
// Create an app bundle.
HRESULT hr = dispatch::Invoke(update3_.get(), L"createAppBundleWeb",
DISPATCH_METHOD, bundle_.Receive());
if (FAILED(hr)) {
Done(hr);
return;
}
if (bundle_.type() != VT_DISPATCH) {
Done(DISP_E_TYPEMISMATCH);
return;
}
hr = dispatch::Invoke(V_DISPATCH(&bundle_), L"initialize", DISPATCH_METHOD,
NULL);
if (FAILED(hr)) {
Done(hr);
return;
}
// Add Chromoting Host to the bundle.
ScopedVariant appid(kHostOmahaAppid);
ScopedVariant empty(kOmahaEmpty);
ScopedVariant language(kOmahaLanguage);
hr = dispatch::Invoke(V_DISPATCH(&bundle_), L"createApp", DISPATCH_METHOD,
appid, empty, language, empty, NULL);
if (FAILED(hr)) {
Done(hr);
return;
}
hr = dispatch::Invoke(V_DISPATCH(&bundle_), L"checkForUpdate",
DISPATCH_METHOD, NULL);
if (FAILED(hr)) {
Done(hr);
return;
}
hr = dispatch::Invoke(V_DISPATCH(&bundle_), L"appWeb",
DISPATCH_PROPERTYGET, ScopedVariant(0), app_.Receive());
if (FAILED(hr)) {
Done(hr);
return;
}
if (app_.type() != VT_DISPATCH) {
Done(DISP_E_TYPEMISMATCH);
return;
}
// Now poll for the installation status.
PollInstallationStatus();
}
void DaemonComInstallerWin::PollInstallationStatus() {
// Get the current application installation state.
// N.B. The object underlying the ICurrentState interface has static data that
// does not get updated as the server state changes. To get the most "current"
// state, the currentState property needs to be queried again.
ScopedVariant current_state;
HRESULT hr = dispatch::Invoke(V_DISPATCH(&app_), L"currentState",
DISPATCH_PROPERTYGET, current_state.Receive());
if (FAILED(hr)) {
Done(hr);
return;
}
if (current_state.type() != VT_DISPATCH) {
Done(DISP_E_TYPEMISMATCH);
return;
}
ScopedVariant state;
hr = dispatch::Invoke(V_DISPATCH(¤t_state), L"stateValue",
DISPATCH_PROPERTYGET, state.Receive());
if (state.type() != VT_I4) {
Done(DISP_E_TYPEMISMATCH);
return;
}
// Perform state-specific actions.
switch (V_I4(&state)) {
case STATE_INIT:
case STATE_WAITING_TO_CHECK_FOR_UPDATE:
case STATE_CHECKING_FOR_UPDATE:
case STATE_WAITING_TO_DOWNLOAD:
case STATE_RETRYING_DOWNLOAD:
case STATE_DOWNLOADING:
case STATE_WAITING_TO_INSTALL:
case STATE_INSTALLING:
case STATE_PAUSED:
break;
case STATE_UPDATE_AVAILABLE:
hr = dispatch::Invoke(V_DISPATCH(&bundle_), L"download",
DISPATCH_METHOD, NULL);
if (FAILED(hr)) {
Done(hr);
return;
}
break;
case STATE_DOWNLOAD_COMPLETE:
case STATE_EXTRACTING:
case STATE_APPLYING_DIFFERENTIAL_PATCH:
case STATE_READY_TO_INSTALL:
hr = dispatch::Invoke(V_DISPATCH(&bundle_), L"install",
DISPATCH_METHOD, NULL);
if (FAILED(hr)) {
Done(hr);
return;
}
break;
case STATE_INSTALL_COMPLETE:
case STATE_NO_UPDATE:
// Installation complete or not required. Report success.
Done(S_OK);
return;
case STATE_ERROR: {
ScopedVariant error_code;
hr = dispatch::Invoke(V_DISPATCH(¤t_state), L"errorCode",
DISPATCH_PROPERTYGET, error_code.Receive());
if (FAILED(hr)) {
Done(hr);
return;
}
if (error_code.type() != VT_UI4) {
Done(DISP_E_TYPEMISMATCH);
return;
}
Done(V_UI4(&error_code));
return;
}
default:
LOG(ERROR) << "Unknown bundle state: " << V_I4(&state) << ".";
Done(E_FAIL);
return;
}
// Keep polling.
polling_timer_.Reset();
}
DaemonCommandLineInstallerWin::DaemonCommandLineInstallerWin(
const CompletionCallback& done) : DaemonInstallerWin(done) {
}
DaemonCommandLineInstallerWin::~DaemonCommandLineInstallerWin() {
process_watcher_.StopWatching();
}
void DaemonCommandLineInstallerWin::Install() {
// Get the full path to GoogleUpdate.exe from the registry.
base::win::RegKey update_key;
LONG result = update_key.Open(HKEY_CURRENT_USER,
kOmahaUpdateKeyName,
KEY_READ);
if (result != ERROR_SUCCESS) {
Done(HRESULT_FROM_WIN32(result));
return;
}
// presubmit: allow wstring
std::wstring google_update;
result = update_key.ReadValue(kOmahaPathValueName, &google_update);
if (result != ERROR_SUCCESS) {
Done(HRESULT_FROM_WIN32(result));
return;
}
// Launch the updater process and wait for its termination.
base::string16 command_line = base::WideToUTF16(
base::StringPrintf(kGoogleUpdateCommandLineFormat,
google_update.c_str(),
kHostOmahaAppid,
kOmahaLanguage));
base::LaunchOptions options;
if (!base::LaunchProcess(command_line, options, &process_)) {
result = GetLastError();
Done(HRESULT_FROM_WIN32(result));
return;
}
if (!process_watcher_.StartWatching(process_.Get(), this)) {
result = GetLastError();
Done(HRESULT_FROM_WIN32(result));
return;
}
}
void DaemonCommandLineInstallerWin::OnObjectSignaled(HANDLE object) {
// Check if the updater process returned success.
DWORD exit_code;
if (GetExitCodeProcess(process_.Get(), &exit_code) && exit_code == 0) {
Done(S_OK);
} else {
Done(E_FAIL);
}
}
DaemonInstallerWin::DaemonInstallerWin(const CompletionCallback& done)
: done_(done) {
}
DaemonInstallerWin::~DaemonInstallerWin() {
}
void DaemonInstallerWin::Done(HRESULT result) {
CompletionCallback done = done_;
done_.Reset();
done.Run(result);
}
// static
scoped_ptr<DaemonInstallerWin> DaemonInstallerWin::Create(
HWND window_handle,
CompletionCallback done) {
HRESULT result = E_FAIL;
ScopedComPtr<IDispatch> update3;
// Check if the machine instance of Omaha is available. The COM elevation is
// supported on Vista+, so on XP/W2K3 we assume that we are running under
// a privileged user and get ACCESS_DENIED later if we are not.
if (base::win::GetVersion() < base::win::VERSION_VISTA) {
CLSID class_id;
result = CLSIDFromProgID(kGoogleUpdate, &class_id);
if (SUCCEEDED(result)) {
result = CoCreateInstance(class_id,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IDispatch,
update3.ReceiveVoid());
}
} else {
BIND_OPTS3 bind_options;
memset(&bind_options, 0, sizeof(bind_options));
bind_options.cbStruct = sizeof(bind_options);
bind_options.hwnd = GetTopLevelWindow(window_handle);
bind_options.dwClassContext = CLSCTX_LOCAL_SERVER;
result = CoGetObject(kGoogleUpdateElevationMoniker,
&bind_options,
IID_IDispatch,
update3.ReceiveVoid());
}
if (SUCCEEDED(result)) {
// The machine instance of Omaha is available and we successfully passed
// the UAC prompt.
return scoped_ptr<DaemonInstallerWin>(
new DaemonComInstallerWin(update3, done));
} else if (result == CO_E_CLASSSTRING) {
// The machine instance of Omaha is not available so we will have to run
// GoogleUpdate.exe manually passing "needsadmin=True". This will cause
// Omaha to install the machine instance first and then install Chromoting
// Host.
return scoped_ptr<DaemonInstallerWin>(
new DaemonCommandLineInstallerWin(done));
} else {
// The user declined the UAC prompt or some other error occured.
done.Run(result);
return scoped_ptr<DaemonInstallerWin>();
}
}
HWND GetTopLevelWindow(HWND window) {
if (window == NULL) {
return NULL;
}
for (;;) {
LONG style = GetWindowLong(window, GWL_STYLE);
if ((style & WS_OVERLAPPEDWINDOW) == WS_OVERLAPPEDWINDOW ||
(style & WS_POPUP) == WS_POPUP) {
return window;
}
HWND parent = GetAncestor(window, GA_PARENT);
if (parent == NULL) {
return window;
}
window = parent;
}
}
} // namespace remoting
|