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
3 // found in the LICENSE file.
5 #include "chrome/installer/util/google_update_settings.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_restrictions.h"
19 #include "base/time/time.h"
20 #include "base/win/registry.h"
21 #include "base/win/win_util.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/installer/util/app_registration_data.h"
24 #include "chrome/installer/util/browser_distribution.h"
25 #include "chrome/installer/util/channel_info.h"
26 #include "chrome/installer/util/google_update_constants.h"
27 #include "chrome/installer/util/google_update_experiment_util.h"
28 #include "chrome/installer/util/install_util.h"
29 #include "chrome/installer/util/installation_state.h"
30 #include "chrome/installer/util/product.h"
32 using base::win::RegKey
;
33 using installer::InstallationState
;
35 const wchar_t GoogleUpdateSettings::kPoliciesKey
[] =
36 L
"SOFTWARE\\Policies\\Google\\Update";
37 const wchar_t GoogleUpdateSettings::kUpdatePolicyValue
[] = L
"UpdateDefault";
38 const wchar_t GoogleUpdateSettings::kUpdateOverrideValuePrefix
[] = L
"Update";
39 const wchar_t GoogleUpdateSettings::kCheckPeriodOverrideMinutes
[] =
40 L
"AutoUpdateCheckPeriodMinutes";
42 // Don't allow update periods longer than six weeks.
43 const int GoogleUpdateSettings::kCheckPeriodOverrideMinutesMax
=
46 const GoogleUpdateSettings::UpdatePolicy
47 GoogleUpdateSettings::kDefaultUpdatePolicy
=
48 #if defined(GOOGLE_CHROME_BUILD)
49 GoogleUpdateSettings::AUTOMATIC_UPDATES
;
51 GoogleUpdateSettings::UPDATES_DISABLED
;
56 bool ReadGoogleUpdateStrKey(const wchar_t* const name
, base::string16
* value
) {
57 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
58 base::string16 reg_path
= dist
->GetStateKey();
59 RegKey
key(HKEY_CURRENT_USER
, reg_path
.c_str(), KEY_READ
| KEY_WOW64_32KEY
);
60 if (key
.ReadValue(name
, value
) != ERROR_SUCCESS
) {
62 HKEY_LOCAL_MACHINE
, reg_path
.c_str(), KEY_READ
| KEY_WOW64_32KEY
);
63 return (hklm_key
.ReadValue(name
, value
) == ERROR_SUCCESS
);
68 // Writes |value| into a user-specific value in the key |name| under
69 // |app_reg_data|'s ClientStateMedium key in HKLM along with the aggregation
70 // method |aggregate|. This function is solely for use by system-level installs.
71 bool WriteGoogleUpdateAggregateNumKeyInternal(
72 const AppRegistrationData
& app_reg_data
,
73 const wchar_t* const name
,
75 const wchar_t* const aggregate
) {
77 DCHECK(GoogleUpdateSettings::IsSystemInstall());
78 const REGSAM kAccess
= KEY_SET_VALUE
| KEY_WOW64_32KEY
;
80 // Machine installs require each OS user to write a unique key under a
81 // named key in HKLM as well as an "aggregation" function that describes
82 // how the values of multiple users are to be combined.
83 base::string16 uniquename
;
84 if (!base::win::GetUserSidString(&uniquename
)) {
89 base::string16
reg_path(app_reg_data
.GetStateMediumKey());
90 reg_path
.append(L
"\\");
91 reg_path
.append(name
);
92 RegKey
key(HKEY_LOCAL_MACHINE
, reg_path
.c_str(), kAccess
);
93 key
.WriteValue(google_update::kRegAggregateMethod
, aggregate
);
95 DWORD dword_value
= (value
> std::numeric_limits
<DWORD
>::max() ?
96 std::numeric_limits
<DWORD
>::max() :
97 static_cast<DWORD
>(value
));
98 return (key
.WriteValue(uniquename
.c_str(), dword_value
) == ERROR_SUCCESS
);
101 // Updates a registry key |name| to be |value| for the given |app_reg_data|.
102 bool WriteGoogleUpdateStrKeyInternal(const AppRegistrationData
& app_reg_data
,
103 const wchar_t* const name
,
104 const base::string16
& value
) {
105 const REGSAM kAccess
= KEY_SET_VALUE
| KEY_WOW64_32KEY
;
106 RegKey
key(HKEY_CURRENT_USER
, app_reg_data
.GetStateKey().c_str(), kAccess
);
107 return (key
.WriteValue(name
, value
.c_str()) == ERROR_SUCCESS
);
110 bool WriteGoogleUpdateStrKey(const wchar_t* const name
,
111 const base::string16
& value
) {
112 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
113 return WriteGoogleUpdateStrKeyInternal(
114 dist
->GetAppRegistrationData(), name
, value
);
117 bool ClearGoogleUpdateStrKey(const wchar_t* const name
) {
118 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
119 base::string16 reg_path
= dist
->GetStateKey();
120 RegKey
key(HKEY_CURRENT_USER
,
122 KEY_READ
| KEY_WRITE
| KEY_WOW64_32KEY
);
123 base::string16 value
;
124 if (key
.ReadValue(name
, &value
) != ERROR_SUCCESS
)
126 return (key
.WriteValue(name
, L
"") == ERROR_SUCCESS
);
129 bool RemoveGoogleUpdateStrKey(const wchar_t* const name
) {
130 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
131 base::string16 reg_path
= dist
->GetStateKey();
132 RegKey
key(HKEY_CURRENT_USER
,
134 KEY_READ
| KEY_WRITE
| KEY_WOW64_32KEY
);
135 if (!key
.HasValue(name
))
137 return (key
.DeleteValue(name
) == ERROR_SUCCESS
);
140 bool GetChromeChannelInternal(bool system_install
,
141 bool add_multi_modifier
,
142 base::string16
* channel
) {
143 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
145 // Shortcut in case this distribution knows what channel it is (canary).
146 if (dist
->GetChromeChannel(channel
))
149 // Determine whether or not chrome is multi-install. If so, updates are
150 // delivered under the binaries' app guid, so that's where the relevant
152 installer::ProductState state
;
153 installer::ChannelInfo channel_info
;
154 ignore_result(state
.Initialize(system_install
, dist
));
155 if (!state
.is_multi_install()) {
156 // Use the channel info that was just read for this single-install chrome.
157 channel_info
= state
.channel();
159 // Read the channel info from the binaries' state key.
160 HKEY root_key
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
161 dist
= BrowserDistribution::GetSpecificDistribution(
162 BrowserDistribution::CHROME_BINARIES
);
163 RegKey
key(root_key
, dist
->GetStateKey().c_str(),
164 KEY_READ
| KEY_WOW64_32KEY
);
166 if (!channel_info
.Initialize(key
)) {
167 channel
->assign(installer::kChromeChannelUnknown
);
172 if (!channel_info
.GetChannelName(channel
))
173 channel
->assign(installer::kChromeChannelUnknown
);
175 // Tag the channel name if this is a multi-install.
176 if (add_multi_modifier
&& state
.is_multi_install()) {
177 if (!channel
->empty())
178 channel
->push_back(L
'-');
179 channel
->push_back(L
'm');
185 // Populates |update_policy| with the UpdatePolicy enum value corresponding to a
186 // DWORD read from the registry and returns true if |value| is within range.
187 // If |value| is out of range, returns false without modifying |update_policy|.
188 bool GetUpdatePolicyFromDword(
190 GoogleUpdateSettings::UpdatePolicy
* update_policy
) {
192 case GoogleUpdateSettings::UPDATES_DISABLED
:
193 case GoogleUpdateSettings::AUTOMATIC_UPDATES
:
194 case GoogleUpdateSettings::MANUAL_UPDATES_ONLY
:
195 case GoogleUpdateSettings::AUTO_UPDATES_ONLY
:
196 *update_policy
= static_cast<GoogleUpdateSettings::UpdatePolicy
>(value
);
199 LOG(WARNING
) << "Unexpected update policy override value: " << value
;
204 // Convenience routine: GoogleUpdateSettings::UpdateDidRunStateForApp()
205 // specialized for Chrome Binaries.
206 bool UpdateDidRunStateForBinaries(bool did_run
) {
207 BrowserDistribution
* dist
= BrowserDistribution::GetSpecificDistribution(
208 BrowserDistribution::CHROME_BINARIES
);
209 return GoogleUpdateSettings::UpdateDidRunStateForApp(
210 dist
->GetAppRegistrationData(), did_run
);
215 bool GoogleUpdateSettings::IsSystemInstall() {
216 bool system_install
= false;
217 base::FilePath module_dir
;
218 if (!PathService::Get(base::DIR_MODULE
, &module_dir
)) {
220 << "Failed to get directory of module; assuming per-user install.";
222 system_install
= !InstallUtil::IsPerUserInstall(module_dir
);
224 return system_install
;
227 bool GoogleUpdateSettings::GetCollectStatsConsent() {
228 return GetCollectStatsConsentAtLevel(IsSystemInstall());
231 // Older versions of Chrome unconditionally read from HKCU\...\ClientState\...
232 // and then HKLM\...\ClientState\.... This means that system-level Chrome
233 // never checked ClientStateMedium (which has priority according to Google
234 // Update) and gave preference to a value in HKCU (which was never checked by
235 // Google Update). From now on, Chrome follows Google Update's policy.
236 bool GoogleUpdateSettings::GetCollectStatsConsentAtLevel(bool system_install
) {
237 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
239 // Consent applies to all products in a multi-install package.
240 if (InstallUtil::IsMultiInstall(dist
, system_install
)) {
241 dist
= BrowserDistribution::GetSpecificDistribution(
242 BrowserDistribution::CHROME_BINARIES
);
247 bool have_value
= false;
248 const REGSAM kAccess
= KEY_QUERY_VALUE
| KEY_WOW64_32KEY
;
250 // For system-level installs, try ClientStateMedium first.
253 key
.Open(HKEY_LOCAL_MACHINE
, dist
->GetStateMediumKey().c_str(),
254 kAccess
) == ERROR_SUCCESS
&&
255 key
.ReadValueDW(google_update::kRegUsageStatsField
,
256 &value
) == ERROR_SUCCESS
;
258 // Otherwise, try ClientState.
261 key
.Open(system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
,
262 dist
->GetStateKey().c_str(),
263 kAccess
) == ERROR_SUCCESS
&&
264 key
.ReadValueDW(google_update::kRegUsageStatsField
,
265 &value
) == ERROR_SUCCESS
;
268 // Google Update specifically checks that the value is 1, so we do the same.
269 return have_value
&& value
== 1;
272 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented
) {
273 return SetCollectStatsConsentAtLevel(IsSystemInstall(), consented
);
276 bool GoogleUpdateSettings::SetCollectStatsConsentAtLevel(bool system_install
,
278 // Google Update writes and expects 1 for true, 0 for false.
279 DWORD value
= consented
? 1 : 0;
281 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
283 // Consent applies to all products in a multi-install package.
284 if (InstallUtil::IsMultiInstall(dist
, system_install
)) {
285 dist
= BrowserDistribution::GetSpecificDistribution(
286 BrowserDistribution::CHROME_BINARIES
);
289 // Write to ClientStateMedium for system-level; ClientState otherwise.
290 HKEY root_key
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
291 base::string16 reg_path
=
292 system_install
? dist
->GetStateMediumKey() : dist
->GetStateKey();
294 LONG result
= key
.Create(
295 root_key
, reg_path
.c_str(), KEY_SET_VALUE
| KEY_WOW64_32KEY
);
296 if (result
!= ERROR_SUCCESS
) {
297 LOG(ERROR
) << "Failed opening key " << reg_path
<< " to set "
298 << google_update::kRegUsageStatsField
<< "; result: " << result
;
300 result
= key
.WriteValue(google_update::kRegUsageStatsField
, value
);
301 LOG_IF(ERROR
, result
!= ERROR_SUCCESS
) << "Failed setting "
302 << google_update::kRegUsageStatsField
<< " in key " << reg_path
303 << "; result: " << result
;
305 return (result
== ERROR_SUCCESS
);
308 scoped_ptr
<metrics::ClientInfo
> GoogleUpdateSettings::LoadMetricsClientInfo() {
309 base::string16 client_id_16
;
310 if (!ReadGoogleUpdateStrKey(google_update::kRegMetricsId
, &client_id_16
) ||
311 client_id_16
.empty()) {
312 return scoped_ptr
<metrics::ClientInfo
>();
315 scoped_ptr
<metrics::ClientInfo
> client_info(new metrics::ClientInfo
);
316 client_info
->client_id
= base::UTF16ToUTF8(client_id_16
);
318 base::string16 installation_date_str
;
319 if (ReadGoogleUpdateStrKey(google_update::kRegMetricsIdInstallDate
,
320 &installation_date_str
)) {
321 base::StringToInt64(installation_date_str
, &client_info
->installation_date
);
324 base::string16 reporting_enbaled_date_date_str
;
325 if (ReadGoogleUpdateStrKey(google_update::kRegMetricsIdEnabledDate
,
326 &reporting_enbaled_date_date_str
)) {
327 base::StringToInt64(reporting_enbaled_date_date_str
,
328 &client_info
->reporting_enabled_date
);
331 return client_info
.Pass();
334 void GoogleUpdateSettings::StoreMetricsClientInfo(
335 const metrics::ClientInfo
& client_info
) {
336 // Attempt a best-effort at backing |client_info| in the registry (but don't
337 // handle/report failures).
338 WriteGoogleUpdateStrKey(google_update::kRegMetricsId
,
339 base::UTF8ToUTF16(client_info
.client_id
));
340 WriteGoogleUpdateStrKey(google_update::kRegMetricsIdInstallDate
,
341 base::Int64ToString16(client_info
.installation_date
));
342 WriteGoogleUpdateStrKey(
343 google_update::kRegMetricsIdEnabledDate
,
344 base::Int64ToString16(client_info
.reporting_enabled_date
));
347 // EULA consent is only relevant for system-level installs.
348 bool GoogleUpdateSettings::SetEULAConsent(
349 const InstallationState
& machine_state
,
350 BrowserDistribution
* dist
,
353 const DWORD eula_accepted
= consented
? 1 : 0;
354 const REGSAM kAccess
= KEY_SET_VALUE
| KEY_WOW64_32KEY
;
355 base::string16 reg_path
= dist
->GetStateMediumKey();
356 bool succeeded
= true;
359 // Write the consent value into the product's ClientStateMedium key.
360 if (key
.Create(HKEY_LOCAL_MACHINE
, reg_path
.c_str(),
361 kAccess
) != ERROR_SUCCESS
||
362 key
.WriteValue(google_update::kRegEULAAceptedField
,
363 eula_accepted
) != ERROR_SUCCESS
) {
367 // If this is a multi-install, also write it into the binaries' key.
368 // --mutli-install is not provided on the command-line, so deduce it from
369 // the product's state.
370 const installer::ProductState
* product_state
=
371 machine_state
.GetProductState(true, dist
->GetType());
372 if (product_state
!= NULL
&& product_state
->is_multi_install()) {
373 dist
= BrowserDistribution::GetSpecificDistribution(
374 BrowserDistribution::CHROME_BINARIES
);
375 reg_path
= dist
->GetStateMediumKey();
376 if (key
.Create(HKEY_LOCAL_MACHINE
, reg_path
.c_str(),
377 kAccess
) != ERROR_SUCCESS
||
378 key
.WriteValue(google_update::kRegEULAAceptedField
,
379 eula_accepted
) != ERROR_SUCCESS
) {
387 int GoogleUpdateSettings::GetLastRunTime() {
388 base::string16 time_s
;
389 if (!ReadGoogleUpdateStrKey(google_update::kRegLastRunTimeField
, &time_s
))
392 if (!base::StringToInt64(time_s
, &time_i
))
395 base::Time::NowFromSystemTime() - base::Time::FromInternalValue(time_i
);
399 bool GoogleUpdateSettings::SetLastRunTime() {
400 int64 time
= base::Time::NowFromSystemTime().ToInternalValue();
401 return WriteGoogleUpdateStrKey(google_update::kRegLastRunTimeField
,
402 base::Int64ToString16(time
));
405 bool GoogleUpdateSettings::RemoveLastRunTime() {
406 return RemoveGoogleUpdateStrKey(google_update::kRegLastRunTimeField
);
409 bool GoogleUpdateSettings::GetBrowser(base::string16
* browser
) {
410 return ReadGoogleUpdateStrKey(google_update::kRegBrowserField
, browser
);
413 bool GoogleUpdateSettings::GetLanguage(base::string16
* language
) {
414 return ReadGoogleUpdateStrKey(google_update::kRegLangField
, language
);
417 bool GoogleUpdateSettings::GetBrand(base::string16
* brand
) {
418 return ReadGoogleUpdateStrKey(google_update::kRegRLZBrandField
, brand
);
421 bool GoogleUpdateSettings::GetReactivationBrand(base::string16
* brand
) {
422 return ReadGoogleUpdateStrKey(google_update::kRegRLZReactivationBrandField
,
426 bool GoogleUpdateSettings::GetClient(base::string16
* client
) {
427 return ReadGoogleUpdateStrKey(google_update::kRegClientField
, client
);
430 bool GoogleUpdateSettings::SetClient(const base::string16
& client
) {
431 return WriteGoogleUpdateStrKey(google_update::kRegClientField
, client
);
434 bool GoogleUpdateSettings::GetReferral(base::string16
* referral
) {
435 return ReadGoogleUpdateStrKey(google_update::kRegReferralField
, referral
);
438 bool GoogleUpdateSettings::ClearReferral() {
439 return ClearGoogleUpdateStrKey(google_update::kRegReferralField
);
442 bool GoogleUpdateSettings::UpdateDidRunStateForApp(
443 const AppRegistrationData
& app_reg_data
,
445 return WriteGoogleUpdateStrKeyInternal(app_reg_data
,
446 google_update::kRegDidRunField
,
447 did_run
? L
"1" : L
"0");
450 bool GoogleUpdateSettings::UpdateDidRunState(bool did_run
, bool system_level
) {
451 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
452 bool result
= UpdateDidRunStateForApp(dist
->GetAppRegistrationData(),
454 // Update state for binaries, even if the previous call was unsuccessful.
455 if (InstallUtil::IsMultiInstall(dist
, system_level
))
456 result
= UpdateDidRunStateForBinaries(did_run
) && result
;
460 base::string16
GoogleUpdateSettings::GetChromeChannel(bool system_install
) {
461 base::string16 channel
;
462 GetChromeChannelInternal(system_install
, false, &channel
);
466 bool GoogleUpdateSettings::GetChromeChannelAndModifiers(
468 base::string16
* channel
) {
469 return GetChromeChannelInternal(system_install
, true, channel
);
472 void GoogleUpdateSettings::UpdateInstallStatus(bool system_install
,
473 installer::ArchiveType archive_type
, int install_return_code
,
474 const base::string16
& product_guid
) {
475 DCHECK(archive_type
!= installer::UNKNOWN_ARCHIVE_TYPE
||
476 install_return_code
!= 0);
477 HKEY reg_root
= (system_install
) ? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
480 installer::ChannelInfo channel_info
;
481 base::string16
reg_key(google_update::kRegPathClientState
);
482 reg_key
.append(L
"\\");
483 reg_key
.append(product_guid
);
484 LONG result
= key
.Open(reg_root
,
486 KEY_QUERY_VALUE
| KEY_SET_VALUE
| KEY_WOW64_32KEY
);
487 if (result
== ERROR_SUCCESS
)
488 channel_info
.Initialize(key
);
489 else if (result
!= ERROR_FILE_NOT_FOUND
)
490 LOG(ERROR
) << "Failed to open " << reg_key
<< "; Error: " << result
;
492 if (UpdateGoogleUpdateApKey(archive_type
, install_return_code
,
494 // We have a modified channel_info value to write.
495 // Create the app's ClientState key if it doesn't already exist.
497 result
= key
.Open(reg_root
,
498 google_update::kRegPathClientState
,
499 KEY_CREATE_SUB_KEY
| KEY_WOW64_32KEY
);
500 if (result
== ERROR_SUCCESS
)
501 result
= key
.CreateKey(product_guid
.c_str(),
502 KEY_SET_VALUE
| KEY_WOW64_32KEY
);
504 if (result
!= ERROR_SUCCESS
) {
505 LOG(ERROR
) << "Failed to create " << reg_key
<< "; Error: " << result
;
509 if (!channel_info
.Write(&key
)) {
510 LOG(ERROR
) << "Failed to write to application's ClientState key "
511 << google_update::kRegApField
<< " = " << channel_info
.value();
516 bool GoogleUpdateSettings::UpdateGoogleUpdateApKey(
517 installer::ArchiveType archive_type
, int install_return_code
,
518 installer::ChannelInfo
* value
) {
519 DCHECK(archive_type
!= installer::UNKNOWN_ARCHIVE_TYPE
||
520 install_return_code
!= 0);
521 bool modified
= false;
523 if (archive_type
== installer::FULL_ARCHIVE_TYPE
|| !install_return_code
) {
524 if (value
->SetFullSuffix(false)) {
525 VLOG(1) << "Removed incremental installer failure key; "
526 "switching to channel: "
530 } else if (archive_type
== installer::INCREMENTAL_ARCHIVE_TYPE
) {
531 if (value
->SetFullSuffix(true)) {
532 VLOG(1) << "Incremental installer failed; switching to channel: "
536 VLOG(1) << "Incremental installer failure; already on channel: "
540 // It's okay if we don't know the archive type. In this case, leave the
541 // "-full" suffix as we found it.
542 DCHECK_EQ(installer::UNKNOWN_ARCHIVE_TYPE
, archive_type
);
545 if (value
->SetMultiFailSuffix(false)) {
546 VLOG(1) << "Removed multi-install failure key; switching to channel: "
554 void GoogleUpdateSettings::UpdateProfileCounts(size_t profiles_active
,
555 size_t profiles_signedin
) {
556 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
557 // System-level installs must write into the ClientStateMedium key shared by
558 // all users. Special treatment is used to aggregate across those users.
559 if (IsSystemInstall()) {
560 // Write the counts as ints that get aggregated across all users via
561 // summation for system-level installs.
562 WriteGoogleUpdateAggregateNumKeyInternal(
563 dist
->GetAppRegistrationData(),
564 google_update::kRegProfilesActive
,
567 WriteGoogleUpdateAggregateNumKeyInternal(
568 dist
->GetAppRegistrationData(),
569 google_update::kRegProfilesSignedIn
,
573 // Write the counts as strings since no aggregation function is needed for
574 // user-level installs.
575 WriteGoogleUpdateStrKeyInternal(dist
->GetAppRegistrationData(),
576 google_update::kRegProfilesActive
,
577 base::SizeTToString16(profiles_active
));
578 WriteGoogleUpdateStrKeyInternal(dist
->GetAppRegistrationData(),
579 google_update::kRegProfilesSignedIn
,
580 base::SizeTToString16(profiles_signedin
));
584 int GoogleUpdateSettings::DuplicateGoogleUpdateSystemClientKey() {
585 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
586 base::string16 reg_path
= dist
->GetStateKey();
588 // Minimum access needed is to be able to write to this key.
590 HKEY_LOCAL_MACHINE
, reg_path
.c_str(), KEY_SET_VALUE
| KEY_WOW64_32KEY
);
591 if (!reg_key
.Valid())
594 HANDLE target_handle
= 0;
595 if (!DuplicateHandle(GetCurrentProcess(), reg_key
.Handle(),
596 GetCurrentProcess(), &target_handle
, KEY_SET_VALUE
,
597 TRUE
, DUPLICATE_SAME_ACCESS
)) {
600 return static_cast<int>(reinterpret_cast<uintptr_t>(target_handle
));
603 bool GoogleUpdateSettings::WriteGoogleUpdateSystemClientKey(
604 int handle
, const base::string16
& key
, const base::string16
& value
) {
605 HKEY reg_key
= reinterpret_cast<HKEY
>(
606 reinterpret_cast<void*>(static_cast<uintptr_t>(handle
)));
607 DWORD size
= static_cast<DWORD
>(value
.size()) * sizeof(wchar_t);
608 LSTATUS status
= RegSetValueEx(reg_key
, key
.c_str(), 0, REG_SZ
,
609 reinterpret_cast<const BYTE
*>(value
.c_str()), size
);
610 return status
== ERROR_SUCCESS
;
613 GoogleUpdateSettings::UpdatePolicy
GoogleUpdateSettings::GetAppUpdatePolicy(
614 const base::string16
& app_guid
,
615 bool* is_overridden
) {
616 bool found_override
= false;
617 UpdatePolicy update_policy
= kDefaultUpdatePolicy
;
619 #if defined(GOOGLE_CHROME_BUILD)
620 DCHECK(!app_guid
.empty());
623 // Google Update Group Policy settings are always in HKLM.
624 // TODO(wfh): Check if policies should go into Wow6432Node or not.
625 if (policy_key
.Open(HKEY_LOCAL_MACHINE
, kPoliciesKey
, KEY_QUERY_VALUE
) ==
628 base::string16
app_update_override(kUpdateOverrideValuePrefix
);
629 app_update_override
.append(app_guid
);
630 // First try to read and comprehend the app-specific override.
631 found_override
= (policy_key
.ReadValueDW(app_update_override
.c_str(),
632 &value
) == ERROR_SUCCESS
&&
633 GetUpdatePolicyFromDword(value
, &update_policy
));
635 // Failing that, try to read and comprehend the default override.
636 if (!found_override
&&
637 policy_key
.ReadValueDW(kUpdatePolicyValue
, &value
) == ERROR_SUCCESS
) {
638 GetUpdatePolicyFromDword(value
, &update_policy
);
641 #endif // defined(GOOGLE_CHROME_BUILD)
643 if (is_overridden
!= NULL
)
644 *is_overridden
= found_override
;
646 return update_policy
;
650 bool GoogleUpdateSettings::AreAutoupdatesEnabled() {
651 #if defined(GOOGLE_CHROME_BUILD)
652 // Check the auto-update check period override. If it is 0 or exceeds the
653 // maximum timeout, then for all intents and purposes auto updates are
657 if (policy_key
.Open(HKEY_LOCAL_MACHINE
, kPoliciesKey
,
658 KEY_QUERY_VALUE
) == ERROR_SUCCESS
&&
659 policy_key
.ReadValueDW(kCheckPeriodOverrideMinutes
,
660 &value
) == ERROR_SUCCESS
&&
661 (value
== 0 || value
> kCheckPeriodOverrideMinutesMax
)) {
665 // Auto updates are subtly broken when Chrome and the binaries have different
666 // overrides in place. If this Chrome cannot possibly be multi-install by
667 // virtue of being a side-by-side installation, simply check Chrome's policy.
668 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
669 UpdatePolicy app_policy
= GetAppUpdatePolicy(dist
->GetAppGuid(), nullptr);
670 if (InstallUtil::IsChromeSxSProcess())
671 return app_policy
== AUTOMATIC_UPDATES
|| app_policy
== AUTO_UPDATES_ONLY
;
673 // Otherwise, check for consistency between Chrome and the binaries regardless
674 // of whether or not this Chrome is multi-install since the next update likely
675 // will attempt to migrate it to such.
676 BrowserDistribution
* binaries
= BrowserDistribution::GetSpecificDistribution(
677 BrowserDistribution::CHROME_BINARIES
);
678 return (GetAppUpdatePolicy(binaries
->GetAppGuid(), nullptr) == app_policy
&&
679 (app_policy
== AUTOMATIC_UPDATES
|| app_policy
== AUTO_UPDATES_ONLY
));
680 #else // defined(GOOGLE_CHROME_BUILD)
681 // Chromium does not auto update.
683 #endif // !defined(GOOGLE_CHROME_BUILD)
687 bool GoogleUpdateSettings::ReenableAutoupdates() {
688 #if defined(GOOGLE_CHROME_BUILD)
689 int needs_reset_count
= 0;
690 int did_reset_count
= 0;
692 // Reset overrides for Chrome and for the binaries if this Chrome supports
694 std::vector
<base::string16
> app_guids
;
695 app_guids
.push_back(BrowserDistribution::GetDistribution()->GetAppGuid());
696 if (!InstallUtil::IsChromeSxSProcess()) {
697 app_guids
.push_back(BrowserDistribution::GetSpecificDistribution(
698 BrowserDistribution::CHROME_BINARIES
)->GetAppGuid());
701 UpdatePolicy update_policy
= kDefaultUpdatePolicy
;
703 if (policy_key
.Open(HKEY_LOCAL_MACHINE
, kPoliciesKey
,
704 KEY_SET_VALUE
| KEY_QUERY_VALUE
) == ERROR_SUCCESS
) {
705 // Set to true while app-specific overrides are present that allow automatic
706 // updates. When this is the case, the defaults are irrelevant and don't
707 // need to be checked or reset.
708 bool automatic_updates_allowed_by_overrides
= true;
710 for (const base::string16
& app_guid
: app_guids
) {
711 // First check the app-specific override value and reset that if needed.
712 // Note that this intentionally sets the override to AUTOMATIC_UPDATES
713 // even if it was previously AUTO_UPDATES_ONLY. The thinking is that
714 // AUTOMATIC_UPDATES is marginally more likely to let a user update and
715 // this code is only called when a stuck user asks for updates.
716 base::string16
app_update_override(kUpdateOverrideValuePrefix
);
717 app_update_override
.append(app_guid
);
718 if (policy_key
.ReadValueDW(app_update_override
.c_str(),
719 &value
) != ERROR_SUCCESS
) {
720 automatic_updates_allowed_by_overrides
= false;
721 } else if (!GetUpdatePolicyFromDword(value
, &update_policy
) ||
722 update_policy
!= GoogleUpdateSettings::AUTOMATIC_UPDATES
) {
723 automatic_updates_allowed_by_overrides
= false;
725 if (policy_key
.WriteValue(
726 app_update_override
.c_str(),
727 static_cast<DWORD
>(GoogleUpdateSettings::AUTOMATIC_UPDATES
)) ==
734 // If there were no app-specific override policies, see if there's a global
735 // policy preventing updates and delete it if so.
736 if (!automatic_updates_allowed_by_overrides
&&
737 policy_key
.ReadValueDW(kUpdatePolicyValue
, &value
) == ERROR_SUCCESS
&&
738 (!GetUpdatePolicyFromDword(value
, &update_policy
) ||
739 update_policy
!= GoogleUpdateSettings::AUTOMATIC_UPDATES
)) {
741 if (policy_key
.DeleteValue(kUpdatePolicyValue
) == ERROR_SUCCESS
)
745 // Check the auto-update check period override. If it is 0 or exceeds
746 // the maximum timeout, delete the override value.
747 if (policy_key
.ReadValueDW(kCheckPeriodOverrideMinutes
,
748 &value
) == ERROR_SUCCESS
&&
749 (value
== 0 || value
> kCheckPeriodOverrideMinutesMax
)) {
751 if (policy_key
.DeleteValue(kCheckPeriodOverrideMinutes
) == ERROR_SUCCESS
)
755 // Return whether the number of successful resets is the same as the
756 // number of things that appeared to need resetting.
757 return (needs_reset_count
== did_reset_count
);
759 // For some reason we couldn't open the policy key with the desired
760 // permissions to make changes (the most likely reason is that there is no
761 // policy set). Simply return whether or not we think updates are enabled.
762 return AreAutoupdatesEnabled();
766 // Non Google Chrome isn't going to autoupdate.
770 void GoogleUpdateSettings::RecordChromeUpdatePolicyHistograms() {
771 const bool is_multi_install
= InstallUtil::IsMultiInstall(
772 BrowserDistribution::GetDistribution(), IsSystemInstall());
773 const base::string16 app_guid
=
774 BrowserDistribution::GetSpecificDistribution(
775 is_multi_install
? BrowserDistribution::CHROME_BINARIES
:
776 BrowserDistribution::CHROME_BROWSER
)->GetAppGuid();
778 bool is_overridden
= false;
779 const UpdatePolicy update_policy
= GetAppUpdatePolicy(app_guid
,
781 UMA_HISTOGRAM_BOOLEAN("GoogleUpdate.UpdatePolicyIsOverridden", is_overridden
);
782 UMA_HISTOGRAM_ENUMERATION("GoogleUpdate.EffectivePolicy", update_policy
,
783 UPDATE_POLICIES_COUNT
);
786 base::string16
GoogleUpdateSettings::GetUninstallCommandLine(
787 bool system_install
) {
788 const HKEY root_key
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
789 base::string16 cmd_line
;
792 if (update_key
.Open(root_key
, google_update::kRegPathGoogleUpdate
,
793 KEY_QUERY_VALUE
| KEY_WOW64_32KEY
) == ERROR_SUCCESS
) {
794 update_key
.ReadValue(google_update::kRegUninstallCmdLine
, &cmd_line
);
800 Version
GoogleUpdateSettings::GetGoogleUpdateVersion(bool system_install
) {
801 const HKEY root_key
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
802 base::string16 version
;
805 if (key
.Open(root_key
,
806 google_update::kRegPathGoogleUpdate
,
807 KEY_QUERY_VALUE
| KEY_WOW64_32KEY
) == ERROR_SUCCESS
&&
808 key
.ReadValue(google_update::kRegGoogleUpdateVersion
, &version
) ==
810 return Version(base::UTF16ToUTF8(version
));
816 base::Time
GoogleUpdateSettings::GetGoogleUpdateLastStartedAU(
817 bool system_install
) {
818 const HKEY root_key
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
821 if (update_key
.Open(root_key
,
822 google_update::kRegPathGoogleUpdate
,
823 KEY_QUERY_VALUE
| KEY_WOW64_32KEY
) == ERROR_SUCCESS
) {
825 if (update_key
.ReadValueDW(google_update::kRegLastStartedAUField
,
826 &last_start
) == ERROR_SUCCESS
) {
827 return base::Time::FromTimeT(last_start
);
834 base::Time
GoogleUpdateSettings::GetGoogleUpdateLastChecked(
835 bool system_install
) {
836 const HKEY root_key
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
839 if (update_key
.Open(root_key
,
840 google_update::kRegPathGoogleUpdate
,
841 KEY_QUERY_VALUE
| KEY_WOW64_32KEY
) == ERROR_SUCCESS
) {
843 if (update_key
.ReadValueDW(google_update::kRegLastCheckedField
,
844 &last_check
) == ERROR_SUCCESS
) {
845 return base::Time::FromTimeT(last_check
);
852 bool GoogleUpdateSettings::GetUpdateDetailForApp(bool system_install
,
853 const wchar_t* app_guid
,
858 bool product_found
= false;
860 const HKEY root_key
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
861 base::string16
clientstate_reg_path(google_update::kRegPathClientState
);
862 clientstate_reg_path
.append(L
"\\");
863 clientstate_reg_path
.append(app_guid
);
866 if (clientstate
.Open(root_key
,
867 clientstate_reg_path
.c_str(),
868 KEY_QUERY_VALUE
| KEY_WOW64_32KEY
) == ERROR_SUCCESS
) {
869 base::string16 version
;
871 if ((clientstate
.ReadValueDW(google_update::kRegLastCheckSuccessField
,
872 &dword_value
) == ERROR_SUCCESS
) &&
873 (clientstate
.ReadValue(google_update::kRegVersionField
,
874 &version
) == ERROR_SUCCESS
)) {
875 product_found
= true;
876 data
->version
= base::UTF16ToASCII(version
);
877 data
->last_success
= base::Time::FromTimeT(dword_value
);
878 data
->last_result
= 0;
879 data
->last_error_code
= 0;
880 data
->last_extra_code
= 0;
882 if (clientstate
.ReadValueDW(google_update::kRegLastInstallerResultField
,
883 &dword_value
) == ERROR_SUCCESS
) {
884 // Google Update convention is that if an installer writes an result
885 // code that is invalid, it is clamped to an exit code result.
886 const DWORD kMaxValidInstallResult
= 4; // INSTALLER_RESULT_EXIT_CODE
887 data
->last_result
= std::min(dword_value
, kMaxValidInstallResult
);
889 if (clientstate
.ReadValueDW(google_update::kRegLastInstallerErrorField
,
890 &dword_value
) == ERROR_SUCCESS
) {
891 data
->last_error_code
= dword_value
;
893 if (clientstate
.ReadValueDW(google_update::kRegLastInstallerExtraField
,
894 &dword_value
) == ERROR_SUCCESS
) {
895 data
->last_extra_code
= dword_value
;
900 return product_found
;
903 bool GoogleUpdateSettings::GetUpdateDetailForGoogleUpdate(bool system_install
,
905 return GetUpdateDetailForApp(system_install
,
906 google_update::kGoogleUpdateUpgradeCode
,
910 bool GoogleUpdateSettings::GetUpdateDetail(bool system_install
,
912 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
913 return GetUpdateDetailForApp(system_install
,
914 dist
->GetAppGuid().c_str(),
918 bool GoogleUpdateSettings::SetExperimentLabels(
920 const base::string16
& experiment_labels
) {
921 HKEY reg_root
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
923 // Use the browser distribution and install level to write to the correct
924 // client state/app guid key.
925 bool success
= false;
926 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
927 if (dist
->ShouldSetExperimentLabels()) {
928 base::string16
client_state_path(
929 system_install
? dist
->GetStateMediumKey() : dist
->GetStateKey());
931 reg_root
, client_state_path
.c_str(), KEY_SET_VALUE
| KEY_WOW64_32KEY
);
932 if (experiment_labels
.empty()) {
933 success
= client_state
.DeleteValue(google_update::kExperimentLabels
)
936 success
= client_state
.WriteValue(google_update::kExperimentLabels
,
937 experiment_labels
.c_str()) == ERROR_SUCCESS
;
944 bool GoogleUpdateSettings::ReadExperimentLabels(
946 base::string16
* experiment_labels
) {
947 HKEY reg_root
= system_install
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
949 // If this distribution does not set the experiment labels, don't bother
951 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
952 if (!dist
->ShouldSetExperimentLabels())
955 base::string16
client_state_path(
956 system_install
? dist
->GetStateMediumKey() : dist
->GetStateKey());
959 LONG result
= client_state
.Open(
960 reg_root
, client_state_path
.c_str(), KEY_QUERY_VALUE
| KEY_WOW64_32KEY
);
961 if (result
== ERROR_SUCCESS
) {
962 result
= client_state
.ReadValue(google_update::kExperimentLabels
,
966 // If the key or value was not present, return the empty string.
967 if (result
== ERROR_FILE_NOT_FOUND
|| result
== ERROR_PATH_NOT_FOUND
) {
968 experiment_labels
->clear();
972 return result
== ERROR_SUCCESS
;