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
|
// Copyright (c) 2010 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 "chrome/installer/util/product.h"
#include <algorithm>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/process_util.h"
#include "base/win/registry.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/helper.h"
#include "chrome/installer/util/install_util.h"
#include "chrome/installer/util/master_preferences.h"
#include "chrome/installer/util/master_preferences_constants.h"
#include "chrome/installer/util/package_properties.h"
using base::win::RegKey;
using installer::MasterPreferences;
namespace {
class ProductIsOfType {
public:
explicit ProductIsOfType(BrowserDistribution::Type type)
: type_(type) { }
bool operator()(
const scoped_refptr<const installer::Product>& pi) const {
return pi->distribution()->GetType() == type_;
}
private:
BrowserDistribution::Type type_;
}; // class ProductIsOfType
} // end namespace
namespace installer {
const Product* FindProduct(const Products& products,
BrowserDistribution::Type type) {
Products::const_iterator i =
std::find_if(products.begin(), products.end(), ProductIsOfType(type));
return i == products.end() ? NULL : *i;
}
////////////////////////////////////////////////////////////////////////////////
Product::Product(BrowserDistribution* distribution, Package* package)
: distribution_(distribution),
package_(package),
msi_(false),
cache_state_(0) {
package_->AssociateProduct(this);
}
const Package& Product::package() const {
return *package_.get();
}
FilePath Product::GetUserDataPath() const {
return GetChromeUserDataPath(distribution_);
}
bool Product::LaunchChrome() const {
const FilePath& install_package = package_->path();
bool success = !install_package.empty();
if (success) {
CommandLine cmd(install_package.Append(installer::kChromeExe));
success = base::LaunchApp(cmd, false, false, NULL);
}
return success;
}
bool Product::LaunchChromeAndWait(const CommandLine& options,
int32* exit_code) const {
const FilePath& install_package = package_->path();
if (install_package.empty())
return false;
CommandLine cmd(install_package.Append(installer::kChromeExe));
cmd.AppendArguments(options, false);
bool success = false;
STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi = {0};
// Cast away constness of the command_line_string() since CreateProcess
// might modify the string (insert \0 to separate the program from the
// arguments). Since we're not using the cmd variable beyond this point
// we don't care.
if (!::CreateProcess(cmd.GetProgram().value().c_str(),
const_cast<wchar_t*>(cmd.command_line_string().c_str()),
NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL,
&si, &pi)) {
PLOG(ERROR) << "Failed to launch: " << cmd.command_line_string();
} else {
::CloseHandle(pi.hThread);
DWORD ret = ::WaitForSingleObject(pi.hProcess, INFINITE);
DLOG_IF(ERROR, ret != WAIT_OBJECT_0)
<< "Unexpected return value from WaitForSingleObject: " << ret;
if (::GetExitCodeProcess(pi.hProcess, &ret)) {
DCHECK(ret != STILL_ACTIVE);
success = true;
if (exit_code)
*exit_code = ret;
} else {
PLOG(ERROR) << "GetExitCodeProcess failed";
}
::CloseHandle(pi.hProcess);
}
return success;
}
bool Product::IsMsi() const {
if ((cache_state_ & MSI_STATE) == 0) {
msi_ = false; // Covers failure cases below.
const MasterPreferences& prefs =
installer::MasterPreferences::ForCurrentProcess();
bool is_msi = false;
prefs.GetBool(installer::master_preferences::kMsi, &is_msi);
if (!is_msi) {
// We didn't find it in the preferences, try looking in the registry.
HKEY reg_root = system_level() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
RegKey key;
if (key.Open(reg_root, distribution_->GetStateKey().c_str(), KEY_READ)) {
DWORD msi_value;
if (key.ReadValueDW(google_update::kRegMSIField, &msi_value) &&
msi_value != 0) {
msi_ = true;
}
}
} else {
msi_ = true;
}
cache_state_ |= MSI_STATE;
}
return msi_;
}
bool Product::SetMsiMarker(bool set) const {
bool success = false;
HKEY reg_root = system_level() ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
RegKey client_state_key;
if (client_state_key.Open(reg_root, distribution_->GetStateKey().c_str(),
KEY_READ | KEY_WRITE)) {
DWORD msi_value = set ? 1 : 0;
if (client_state_key.WriteValue(google_update::kRegMSIField, msi_value)) {
success = true;
} else {
LOG(ERROR) << "Could not write MSI value to client state key.";
}
} else {
LOG(ERROR) << "SetMsiMarker: Could not open client state key!";
}
return success;
}
const Version* Product::GetInstalledVersion() const {
if ((cache_state_ & VERSION) == 0) {
DCHECK(installed_version_.get() == NULL);
installed_version_.reset(InstallUtil::GetChromeVersion(distribution_,
system_level()));
cache_state_ |= VERSION;
}
return installed_version_.get();
}
bool Product::IsInstalled() const {
return GetInstalledVersion() != NULL;
}
///////////////////////////////////////////////////////////////////////////////
ProductPackageMapping::ProductPackageMapping(bool multi_install,
bool system_level)
#if defined(GOOGLE_CHROME_BUILD)
: package_properties_(new ChromePackageProperties()),
#else
: package_properties_(new ChromiumPackageProperties()),
#endif
multi_install_(multi_install),
system_level_(system_level) {
}
const Packages& ProductPackageMapping::packages() const {
return packages_;
}
const Products& ProductPackageMapping::products() const {
return products_;
}
bool ProductPackageMapping::AddDistribution(BrowserDistribution* distribution) {
DCHECK(distribution);
// Each product type can be added exactly once.
DCHECK(FindProduct(products_, distribution->GetType()) == NULL);
FilePath install_package(GetChromeInstallPath(system_level_, distribution));
scoped_refptr<Package> target_package;
for (size_t i = 0; i < packages_.size(); ++i) {
if (packages_[i]->IsEqual(install_package)) {
// Use an existing Package.
target_package = packages_[i];
break;
}
}
if (!target_package.get()) {
DCHECK(packages_.empty()) << "Multiple packages per run unsupported.";
// create new one and add.
target_package = new Package(multi_install_, system_level_, install_package,
package_properties_.get());
packages_.push_back(target_package);
}
scoped_refptr<Product> product(new Product(distribution, target_package));
#ifndef NDEBUG
for (size_t i = 0; i < products_.size(); ++i) {
DCHECK_EQ(product->IsMsi(), products_[i]->IsMsi());
}
#endif
products_.push_back(product);
return true;
}
bool ProductPackageMapping::AddDistribution(
BrowserDistribution::Type type,
const installer::MasterPreferences& prefs) {
BrowserDistribution* distribution =
BrowserDistribution::GetSpecificDistribution(type, prefs);
if (!distribution) {
NOTREACHED();
return false;
}
return AddDistribution(distribution);
}
} // namespace installer
|