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
|
// 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/google_update_settings.h"
#include <algorithm>
#include "base/command_line.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/time.h"
#include "base/win/registry.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/install_util.h"
using base::win::RegKey;
namespace {
bool ReadGoogleUpdateStrKey(const wchar_t* const name, std::wstring* value) {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
std::wstring reg_path = dist->GetStateKey();
RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ);
if (!key.ReadValue(name, value)) {
RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ);
return hklm_key.ReadValue(name, value);
}
return true;
}
bool WriteGoogleUpdateStrKey(const wchar_t* const name,
const std::wstring& value) {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
std::wstring reg_path = dist->GetStateKey();
RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
return key.WriteValue(name, value.c_str());
}
bool ClearGoogleUpdateStrKey(const wchar_t* const name) {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
std::wstring reg_path = dist->GetStateKey();
RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
std::wstring value;
if (!key.ReadValue(name, &value))
return false;
return key.WriteValue(name, L"");
}
bool RemoveGoogleUpdateStrKey(const wchar_t* const name) {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
std::wstring reg_path = dist->GetStateKey();
RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
if (!key.ValueExists(name))
return true;
return key.DeleteValue(name);
}
} // namespace.
bool GoogleUpdateSettings::GetCollectStatsConsent() {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
std::wstring reg_path = dist->GetStateKey();
RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ);
DWORD value;
if (!key.ReadValueDW(google_update::kRegUsageStatsField, &value)) {
RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ);
if (!hklm_key.ReadValueDW(google_update::kRegUsageStatsField, &value))
return false;
}
return (1 == value);
}
bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
DWORD value = consented? 1 : 0;
// Writing to HKLM is only a best effort deal.
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
std::wstring reg_path = dist->GetStateMediumKey();
RegKey key_hklm(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ | KEY_WRITE);
key_hklm.WriteValue(google_update::kRegUsageStatsField, value);
// Writing to HKCU is used both by chrome and by the crash reporter.
reg_path = dist->GetStateKey();
RegKey key_hkcu(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE);
return key_hkcu.WriteValue(google_update::kRegUsageStatsField, value);
}
bool GoogleUpdateSettings::GetMetricsId(std::wstring* metrics_id) {
return ReadGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id);
}
bool GoogleUpdateSettings::SetMetricsId(const std::wstring& metrics_id) {
return WriteGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id);
}
bool GoogleUpdateSettings::SetEULAConsent(bool consented) {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
std::wstring reg_path = dist->GetStateMediumKey();
RegKey key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ | KEY_SET_VALUE);
return key.WriteValue(google_update::kRegEULAAceptedField, consented? 1 : 0);
}
int GoogleUpdateSettings::GetLastRunTime() {
std::wstring time_s;
if (!ReadGoogleUpdateStrKey(google_update::kRegLastRunTimeField, &time_s))
return -1;
int64 time_i;
if (!base::StringToInt64(time_s, &time_i))
return -1;
base::TimeDelta td =
base::Time::NowFromSystemTime() - base::Time::FromInternalValue(time_i);
return td.InDays();
}
bool GoogleUpdateSettings::SetLastRunTime() {
int64 time = base::Time::NowFromSystemTime().ToInternalValue();
return WriteGoogleUpdateStrKey(google_update::kRegLastRunTimeField,
base::Int64ToString16(time));
}
bool GoogleUpdateSettings::RemoveLastRunTime() {
return RemoveGoogleUpdateStrKey(google_update::kRegLastRunTimeField);
}
bool GoogleUpdateSettings::GetBrowser(std::wstring* browser) {
return ReadGoogleUpdateStrKey(google_update::kRegBrowserField, browser);
}
bool GoogleUpdateSettings::GetLanguage(std::wstring* language) {
return ReadGoogleUpdateStrKey(google_update::kRegLangField, language);
}
bool GoogleUpdateSettings::GetBrand(std::wstring* brand) {
return ReadGoogleUpdateStrKey(google_update::kRegRLZBrandField, brand);
}
bool GoogleUpdateSettings::GetClient(std::wstring* client) {
return ReadGoogleUpdateStrKey(google_update::kRegClientField, client);
}
bool GoogleUpdateSettings::SetClient(const std::wstring& client) {
return WriteGoogleUpdateStrKey(google_update::kRegClientField, client);
}
bool GoogleUpdateSettings::GetReferral(std::wstring* referral) {
return ReadGoogleUpdateStrKey(google_update::kRegReferralField, referral);
}
bool GoogleUpdateSettings::ClearReferral() {
return ClearGoogleUpdateStrKey(google_update::kRegReferralField);
}
bool GoogleUpdateSettings::GetChromeChannel(bool system_install,
std::wstring* channel) {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
if (dist->GetChromeChannel(channel))
return true;
HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
std::wstring reg_path = dist->GetStateKey();
RegKey key(root_key, reg_path.c_str(), KEY_READ);
std::wstring update_branch;
if (!key.ReadValue(google_update::kRegApField, &update_branch)) {
*channel = L"unknown";
return false;
}
// Map to something pithy for human consumption. There are no rules as to
// what the ap string can contain, but generally it will contain a number
// followed by a dash followed by the branch name (and then some random
// suffix). We fall back on empty string in case we fail to parse.
// Only ever return "", "unknown", "dev" or "beta".
if (update_branch.find(L"-beta") != std::wstring::npos)
*channel = L"beta";
else if (update_branch.find(L"-dev") != std::wstring::npos)
*channel = L"dev";
else if (update_branch.empty())
*channel = L"";
else
*channel = L"unknown";
return true;
}
void GoogleUpdateSettings::UpdateDiffInstallStatus(bool system_install,
bool incremental_install, int install_return_code,
const std::wstring& product_guid) {
HKEY reg_root = (system_install) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
RegKey key;
std::wstring ap_key_value;
std::wstring reg_key(google_update::kRegPathClientState);
reg_key.append(L"\\");
reg_key.append(product_guid);
if (!key.Open(reg_root, reg_key.c_str(), KEY_ALL_ACCESS) ||
!key.ReadValue(google_update::kRegApField, &ap_key_value)) {
LOG(INFO) << "Application key not found.";
if (!incremental_install || !install_return_code) {
LOG(INFO) << "Returning without changing application key.";
key.Close();
return;
} else if (!key.Valid()) {
reg_key.assign(google_update::kRegPathClientState);
if (!key.Open(reg_root, reg_key.c_str(), KEY_ALL_ACCESS) ||
!key.CreateKey(product_guid.c_str(), KEY_ALL_ACCESS)) {
LOG(ERROR) << "Failed to create application key.";
key.Close();
return;
}
}
}
std::wstring new_value = GetNewGoogleUpdateApKey(
incremental_install, install_return_code, ap_key_value);
if ((new_value.compare(ap_key_value) != 0) &&
!key.WriteValue(google_update::kRegApField, new_value.c_str())) {
LOG(ERROR) << "Failed to write value " << new_value
<< " to the registry field " << google_update::kRegApField;
}
key.Close();
}
std::wstring GoogleUpdateSettings::GetNewGoogleUpdateApKey(
bool diff_install, int install_return_code, const std::wstring& value) {
// Magic suffix that we need to add or remove to "ap" key value.
const std::wstring kMagicSuffix = L"-full";
bool has_magic_string = false;
if ((value.length() >= kMagicSuffix.length()) &&
(value.rfind(kMagicSuffix) == (value.length() - kMagicSuffix.length()))) {
LOG(INFO) << "Incremental installer failure key already set.";
has_magic_string = true;
}
std::wstring new_value(value);
if ((!diff_install || !install_return_code) && has_magic_string) {
LOG(INFO) << "Removing failure key from value " << value;
new_value = value.substr(0, value.length() - kMagicSuffix.length());
} else if ((diff_install && install_return_code) &&
!has_magic_string) {
LOG(INFO) << "Incremental installer failed, setting failure key.";
new_value.append(kMagicSuffix);
}
return new_value;
}
int GoogleUpdateSettings::DuplicateGoogleUpdateSystemClientKey() {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
std::wstring reg_path = dist->GetStateKey();
// Minimum access needed is to be able to write to this key.
RegKey reg_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_SET_VALUE);
if (!reg_key.Valid())
return 0;
HANDLE target_handle = 0;
if (!DuplicateHandle(GetCurrentProcess(), reg_key.Handle(),
GetCurrentProcess(), &target_handle, KEY_SET_VALUE,
TRUE, DUPLICATE_SAME_ACCESS)) {
return 0;
}
return reinterpret_cast<int>(target_handle);
}
bool GoogleUpdateSettings::WriteGoogleUpdateSystemClientKey(
int handle, const std::wstring& key, const std::wstring& value) {
HKEY reg_key = reinterpret_cast<HKEY>(reinterpret_cast<void*>(handle));
DWORD size = static_cast<DWORD>(value.size()) * sizeof(wchar_t);
LSTATUS status = RegSetValueEx(reg_key, key.c_str(), 0, REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()), size);
return status == ERROR_SUCCESS;
}
bool GoogleUpdateSettings::IsOrganic(const std::wstring& brand) {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kOrganicInstall))
return true;
static const wchar_t* kBrands[] = {
L"CHFO", L"CHFT", L"CHHS", L"CHHM", L"CHMA", L"CHMB", L"CHME", L"CHMF",
L"CHMG", L"CHMH", L"CHMI", L"CHMQ", L"CHMV", L"CHNB", L"CHNC", L"CHNG",
L"CHNH", L"CHNI", L"CHOA", L"CHOB", L"CHOC", L"CHON", L"CHOO", L"CHOP",
L"CHOQ", L"CHOR", L"CHOS", L"CHOT", L"CHOU", L"CHOX", L"CHOY", L"CHOZ",
L"CHPD", L"CHPE", L"CHPF", L"CHPG", L"EUBB", L"EUBC", L"GGLA", L"GGLS"
};
const wchar_t** end = &kBrands[arraysize(kBrands)];
const wchar_t** found = std::find(&kBrands[0], end, brand);
if (found != end)
return true;
if (StartsWith(brand, L"EUB", true) || StartsWith(brand, L"EUC", true) ||
StartsWith(brand, L"GGR", true))
return true;
return false;
}
|