Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / installer / util / google_update_settings.cc
blob7b2fbc3a66116aaed2e5d4da2759593284afca18
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"
7 #include <algorithm>
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/path_service.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/time/time.h"
19 #include "base/win/registry.h"
20 #include "base/win/win_util.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/installer/util/app_registration_data.h"
23 #include "chrome/installer/util/browser_distribution.h"
24 #include "chrome/installer/util/channel_info.h"
25 #include "chrome/installer/util/google_update_constants.h"
26 #include "chrome/installer/util/google_update_experiment_util.h"
27 #include "chrome/installer/util/install_util.h"
28 #include "chrome/installer/util/installation_state.h"
29 #include "chrome/installer/util/product.h"
31 using base::win::RegKey;
32 using installer::InstallationState;
34 const wchar_t GoogleUpdateSettings::kPoliciesKey[] =
35 L"SOFTWARE\\Policies\\Google\\Update";
36 const wchar_t GoogleUpdateSettings::kUpdatePolicyValue[] = L"UpdateDefault";
37 const wchar_t GoogleUpdateSettings::kUpdateOverrideValuePrefix[] = L"Update";
38 const wchar_t GoogleUpdateSettings::kCheckPeriodOverrideMinutes[] =
39 L"AutoUpdateCheckPeriodMinutes";
41 // Don't allow update periods longer than six weeks.
42 const int GoogleUpdateSettings::kCheckPeriodOverrideMinutesMax =
43 60 * 24 * 7 * 6;
45 const GoogleUpdateSettings::UpdatePolicy
46 GoogleUpdateSettings::kDefaultUpdatePolicy =
47 #if defined(GOOGLE_CHROME_BUILD)
48 GoogleUpdateSettings::AUTOMATIC_UPDATES;
49 #else
50 GoogleUpdateSettings::UPDATES_DISABLED;
51 #endif
53 namespace {
55 bool ReadGoogleUpdateStrKey(const wchar_t* const name, base::string16* value) {
56 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
57 base::string16 reg_path = dist->GetStateKey();
58 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WOW64_32KEY);
59 if (key.ReadValue(name, value) != ERROR_SUCCESS) {
60 RegKey hklm_key(
61 HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ | KEY_WOW64_32KEY);
62 return (hklm_key.ReadValue(name, value) == ERROR_SUCCESS);
64 return true;
67 // Writes |value| into a user-specific value in the key |name| under
68 // |app_reg_data|'s ClientStateMedium key in HKLM along with the aggregation
69 // method |aggregate|. This function is solely for use by system-level installs.
70 bool WriteGoogleUpdateAggregateNumKeyInternal(
71 const AppRegistrationData& app_reg_data,
72 const wchar_t* const name,
73 int value,
74 const wchar_t* const aggregate) {
75 DCHECK(aggregate);
76 DCHECK(GoogleUpdateSettings::IsSystemInstall());
77 const REGSAM kAccess = KEY_SET_VALUE | KEY_WOW64_32KEY;
79 // Machine installs require each OS user to write a unique key under a
80 // named key in HKLM as well as an "aggregation" function that describes
81 // how the values of multiple users are to be combined.
82 base::string16 uniquename;
83 if (!base::win::GetUserSidString(&uniquename)) {
84 NOTREACHED();
85 return false;
88 base::string16 reg_path(app_reg_data.GetStateMediumKey());
89 reg_path.append(L"\\");
90 reg_path.append(name);
91 RegKey key(HKEY_LOCAL_MACHINE, reg_path.c_str(), kAccess);
92 key.WriteValue(google_update::kRegAggregateMethod, aggregate);
93 return (key.WriteValue(uniquename.c_str(), value) == ERROR_SUCCESS);
96 // Updates a registry key |name| to be |value| for the given |app_reg_data|.
97 bool WriteGoogleUpdateStrKeyInternal(const AppRegistrationData& app_reg_data,
98 const wchar_t* const name,
99 const base::string16& value) {
100 const REGSAM kAccess = KEY_SET_VALUE | KEY_WOW64_32KEY;
101 RegKey key(HKEY_CURRENT_USER, app_reg_data.GetStateKey().c_str(), kAccess);
102 return (key.WriteValue(name, value.c_str()) == ERROR_SUCCESS);
105 bool WriteGoogleUpdateStrKey(const wchar_t* const name,
106 const base::string16& value) {
107 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
108 return WriteGoogleUpdateStrKeyInternal(
109 dist->GetAppRegistrationData(), name, value);
112 bool ClearGoogleUpdateStrKey(const wchar_t* const name) {
113 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
114 base::string16 reg_path = dist->GetStateKey();
115 RegKey key(HKEY_CURRENT_USER,
116 reg_path.c_str(),
117 KEY_READ | KEY_WRITE | KEY_WOW64_32KEY);
118 base::string16 value;
119 if (key.ReadValue(name, &value) != ERROR_SUCCESS)
120 return false;
121 return (key.WriteValue(name, L"") == ERROR_SUCCESS);
124 bool RemoveGoogleUpdateStrKey(const wchar_t* const name) {
125 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
126 base::string16 reg_path = dist->GetStateKey();
127 RegKey key(HKEY_CURRENT_USER,
128 reg_path.c_str(),
129 KEY_READ | KEY_WRITE | KEY_WOW64_32KEY);
130 if (!key.HasValue(name))
131 return true;
132 return (key.DeleteValue(name) == ERROR_SUCCESS);
135 bool GetChromeChannelInternal(bool system_install,
136 bool add_multi_modifier,
137 base::string16* channel) {
138 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
140 // Shortcut in case this distribution knows what channel it is (canary).
141 if (dist->GetChromeChannel(channel))
142 return true;
144 // Determine whether or not chrome is multi-install. If so, updates are
145 // delivered under the binaries' app guid, so that's where the relevant
146 // channel is found.
147 installer::ProductState state;
148 installer::ChannelInfo channel_info;
149 ignore_result(state.Initialize(system_install, dist));
150 if (!state.is_multi_install()) {
151 // Use the channel info that was just read for this single-install chrome.
152 channel_info = state.channel();
153 } else {
154 // Read the channel info from the binaries' state key.
155 HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
156 dist = BrowserDistribution::GetSpecificDistribution(
157 BrowserDistribution::CHROME_BINARIES);
158 RegKey key(root_key, dist->GetStateKey().c_str(),
159 KEY_READ | KEY_WOW64_32KEY);
161 if (!channel_info.Initialize(key)) {
162 channel->assign(installer::kChromeChannelUnknown);
163 return false;
167 if (!channel_info.GetChannelName(channel))
168 channel->assign(installer::kChromeChannelUnknown);
170 // Tag the channel name if this is a multi-install.
171 if (add_multi_modifier && state.is_multi_install()) {
172 if (!channel->empty())
173 channel->push_back(L'-');
174 channel->push_back(L'm');
177 return true;
180 // Populates |update_policy| with the UpdatePolicy enum value corresponding to a
181 // DWORD read from the registry and returns true if |value| is within range.
182 // If |value| is out of range, returns false without modifying |update_policy|.
183 bool GetUpdatePolicyFromDword(
184 const DWORD value,
185 GoogleUpdateSettings::UpdatePolicy* update_policy) {
186 switch (value) {
187 case GoogleUpdateSettings::UPDATES_DISABLED:
188 case GoogleUpdateSettings::AUTOMATIC_UPDATES:
189 case GoogleUpdateSettings::MANUAL_UPDATES_ONLY:
190 case GoogleUpdateSettings::AUTO_UPDATES_ONLY:
191 *update_policy = static_cast<GoogleUpdateSettings::UpdatePolicy>(value);
192 return true;
193 default:
194 LOG(WARNING) << "Unexpected update policy override value: " << value;
196 return false;
199 // Convenience routine: GoogleUpdateSettings::UpdateDidRunStateForApp()
200 // specialized for Chrome Binaries.
201 bool UpdateDidRunStateForBinaries(bool did_run) {
202 BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution(
203 BrowserDistribution::CHROME_BINARIES);
204 return GoogleUpdateSettings::UpdateDidRunStateForApp(
205 dist->GetAppRegistrationData(), did_run);
208 } // namespace
210 bool GoogleUpdateSettings::IsSystemInstall() {
211 bool system_install = false;
212 base::FilePath module_dir;
213 if (!PathService::Get(base::DIR_MODULE, &module_dir)) {
214 LOG(WARNING)
215 << "Failed to get directory of module; assuming per-user install.";
216 } else {
217 system_install = !InstallUtil::IsPerUserInstall(module_dir);
219 return system_install;
222 bool GoogleUpdateSettings::GetCollectStatsConsent() {
223 return GetCollectStatsConsentAtLevel(IsSystemInstall());
226 // Older versions of Chrome unconditionally read from HKCU\...\ClientState\...
227 // and then HKLM\...\ClientState\.... This means that system-level Chrome
228 // never checked ClientStateMedium (which has priority according to Google
229 // Update) and gave preference to a value in HKCU (which was never checked by
230 // Google Update). From now on, Chrome follows Google Update's policy.
231 bool GoogleUpdateSettings::GetCollectStatsConsentAtLevel(bool system_install) {
232 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
234 // Consent applies to all products in a multi-install package.
235 if (InstallUtil::IsMultiInstall(dist, system_install)) {
236 dist = BrowserDistribution::GetSpecificDistribution(
237 BrowserDistribution::CHROME_BINARIES);
240 RegKey key;
241 DWORD value = 0;
242 bool have_value = false;
243 const REGSAM kAccess = KEY_QUERY_VALUE | KEY_WOW64_32KEY;
245 // For system-level installs, try ClientStateMedium first.
246 have_value =
247 system_install &&
248 key.Open(HKEY_LOCAL_MACHINE, dist->GetStateMediumKey().c_str(),
249 kAccess) == ERROR_SUCCESS &&
250 key.ReadValueDW(google_update::kRegUsageStatsField,
251 &value) == ERROR_SUCCESS;
253 // Otherwise, try ClientState.
254 if (!have_value) {
255 have_value =
256 key.Open(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
257 dist->GetStateKey().c_str(),
258 kAccess) == ERROR_SUCCESS &&
259 key.ReadValueDW(google_update::kRegUsageStatsField,
260 &value) == ERROR_SUCCESS;
263 // Google Update specifically checks that the value is 1, so we do the same.
264 return have_value && value == 1;
267 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) {
268 return SetCollectStatsConsentAtLevel(IsSystemInstall(), consented);
271 bool GoogleUpdateSettings::SetCollectStatsConsentAtLevel(bool system_install,
272 bool consented) {
273 // Google Update writes and expects 1 for true, 0 for false.
274 DWORD value = consented ? 1 : 0;
276 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
278 // Consent applies to all products in a multi-install package.
279 if (InstallUtil::IsMultiInstall(dist, system_install)) {
280 dist = BrowserDistribution::GetSpecificDistribution(
281 BrowserDistribution::CHROME_BINARIES);
284 // Write to ClientStateMedium for system-level; ClientState otherwise.
285 HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
286 base::string16 reg_path =
287 system_install ? dist->GetStateMediumKey() : dist->GetStateKey();
288 RegKey key;
289 LONG result = key.Create(
290 root_key, reg_path.c_str(), KEY_SET_VALUE | KEY_WOW64_32KEY);
291 if (result != ERROR_SUCCESS) {
292 LOG(ERROR) << "Failed opening key " << reg_path << " to set "
293 << google_update::kRegUsageStatsField << "; result: " << result;
294 } else {
295 result = key.WriteValue(google_update::kRegUsageStatsField, value);
296 LOG_IF(ERROR, result != ERROR_SUCCESS) << "Failed setting "
297 << google_update::kRegUsageStatsField << " in key " << reg_path
298 << "; result: " << result;
300 return (result == ERROR_SUCCESS);
303 scoped_ptr<metrics::ClientInfo> GoogleUpdateSettings::LoadMetricsClientInfo() {
304 base::string16 client_id_16;
305 if (!ReadGoogleUpdateStrKey(google_update::kRegMetricsId, &client_id_16) ||
306 client_id_16.empty()) {
307 return scoped_ptr<metrics::ClientInfo>();
310 scoped_ptr<metrics::ClientInfo> client_info(new metrics::ClientInfo);
311 client_info->client_id = base::UTF16ToUTF8(client_id_16);
313 base::string16 installation_date_str;
314 if (ReadGoogleUpdateStrKey(google_update::kRegMetricsIdInstallDate,
315 &installation_date_str)) {
316 base::StringToInt64(installation_date_str, &client_info->installation_date);
319 base::string16 reporting_enbaled_date_date_str;
320 if (ReadGoogleUpdateStrKey(google_update::kRegMetricsIdEnabledDate,
321 &reporting_enbaled_date_date_str)) {
322 base::StringToInt64(reporting_enbaled_date_date_str,
323 &client_info->reporting_enabled_date);
326 return client_info.Pass();
329 void GoogleUpdateSettings::StoreMetricsClientInfo(
330 const metrics::ClientInfo& client_info) {
331 // Attempt a best-effort at backing |client_info| in the registry (but don't
332 // handle/report failures).
333 WriteGoogleUpdateStrKey(google_update::kRegMetricsId,
334 base::UTF8ToUTF16(client_info.client_id));
335 WriteGoogleUpdateStrKey(google_update::kRegMetricsIdInstallDate,
336 base::Int64ToString16(client_info.installation_date));
337 WriteGoogleUpdateStrKey(
338 google_update::kRegMetricsIdEnabledDate,
339 base::Int64ToString16(client_info.reporting_enabled_date));
342 // EULA consent is only relevant for system-level installs.
343 bool GoogleUpdateSettings::SetEULAConsent(
344 const InstallationState& machine_state,
345 BrowserDistribution* dist,
346 bool consented) {
347 DCHECK(dist);
348 const DWORD eula_accepted = consented ? 1 : 0;
349 const REGSAM kAccess = KEY_SET_VALUE | KEY_WOW64_32KEY;
350 base::string16 reg_path = dist->GetStateMediumKey();
351 bool succeeded = true;
352 RegKey key;
354 // Write the consent value into the product's ClientStateMedium key.
355 if (key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(),
356 kAccess) != ERROR_SUCCESS ||
357 key.WriteValue(google_update::kRegEULAAceptedField,
358 eula_accepted) != ERROR_SUCCESS) {
359 succeeded = false;
362 // If this is a multi-install, also write it into the binaries' key.
363 // --mutli-install is not provided on the command-line, so deduce it from
364 // the product's state.
365 const installer::ProductState* product_state =
366 machine_state.GetProductState(true, dist->GetType());
367 if (product_state != NULL && product_state->is_multi_install()) {
368 dist = BrowserDistribution::GetSpecificDistribution(
369 BrowserDistribution::CHROME_BINARIES);
370 reg_path = dist->GetStateMediumKey();
371 if (key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(),
372 kAccess) != ERROR_SUCCESS ||
373 key.WriteValue(google_update::kRegEULAAceptedField,
374 eula_accepted) != ERROR_SUCCESS) {
375 succeeded = false;
379 return succeeded;
382 int GoogleUpdateSettings::GetLastRunTime() {
383 base::string16 time_s;
384 if (!ReadGoogleUpdateStrKey(google_update::kRegLastRunTimeField, &time_s))
385 return -1;
386 int64 time_i;
387 if (!base::StringToInt64(time_s, &time_i))
388 return -1;
389 base::TimeDelta td =
390 base::Time::NowFromSystemTime() - base::Time::FromInternalValue(time_i);
391 return td.InDays();
394 bool GoogleUpdateSettings::SetLastRunTime() {
395 int64 time = base::Time::NowFromSystemTime().ToInternalValue();
396 return WriteGoogleUpdateStrKey(google_update::kRegLastRunTimeField,
397 base::Int64ToString16(time));
400 bool GoogleUpdateSettings::RemoveLastRunTime() {
401 return RemoveGoogleUpdateStrKey(google_update::kRegLastRunTimeField);
404 bool GoogleUpdateSettings::GetBrowser(base::string16* browser) {
405 return ReadGoogleUpdateStrKey(google_update::kRegBrowserField, browser);
408 bool GoogleUpdateSettings::GetLanguage(base::string16* language) {
409 return ReadGoogleUpdateStrKey(google_update::kRegLangField, language);
412 bool GoogleUpdateSettings::GetBrand(base::string16* brand) {
413 return ReadGoogleUpdateStrKey(google_update::kRegRLZBrandField, brand);
416 bool GoogleUpdateSettings::GetReactivationBrand(base::string16* brand) {
417 return ReadGoogleUpdateStrKey(google_update::kRegRLZReactivationBrandField,
418 brand);
421 bool GoogleUpdateSettings::GetClient(base::string16* client) {
422 return ReadGoogleUpdateStrKey(google_update::kRegClientField, client);
425 bool GoogleUpdateSettings::SetClient(const base::string16& client) {
426 return WriteGoogleUpdateStrKey(google_update::kRegClientField, client);
429 bool GoogleUpdateSettings::GetReferral(base::string16* referral) {
430 return ReadGoogleUpdateStrKey(google_update::kRegReferralField, referral);
433 bool GoogleUpdateSettings::ClearReferral() {
434 return ClearGoogleUpdateStrKey(google_update::kRegReferralField);
437 bool GoogleUpdateSettings::UpdateDidRunStateForApp(
438 const AppRegistrationData& app_reg_data,
439 bool did_run) {
440 return WriteGoogleUpdateStrKeyInternal(app_reg_data,
441 google_update::kRegDidRunField,
442 did_run ? L"1" : L"0");
445 bool GoogleUpdateSettings::UpdateDidRunState(bool did_run, bool system_level) {
446 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
447 bool result = UpdateDidRunStateForApp(dist->GetAppRegistrationData(),
448 did_run);
449 // Update state for binaries, even if the previous call was unsuccessful.
450 if (InstallUtil::IsMultiInstall(dist, system_level))
451 result = UpdateDidRunStateForBinaries(did_run) && result;
452 return result;
455 base::string16 GoogleUpdateSettings::GetChromeChannel(bool system_install) {
456 base::string16 channel;
457 GetChromeChannelInternal(system_install, false, &channel);
458 return channel;
461 bool GoogleUpdateSettings::GetChromeChannelAndModifiers(
462 bool system_install,
463 base::string16* channel) {
464 return GetChromeChannelInternal(system_install, true, channel);
467 void GoogleUpdateSettings::UpdateInstallStatus(bool system_install,
468 installer::ArchiveType archive_type, int install_return_code,
469 const base::string16& product_guid) {
470 DCHECK(archive_type != installer::UNKNOWN_ARCHIVE_TYPE ||
471 install_return_code != 0);
472 HKEY reg_root = (system_install) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
474 RegKey key;
475 installer::ChannelInfo channel_info;
476 base::string16 reg_key(google_update::kRegPathClientState);
477 reg_key.append(L"\\");
478 reg_key.append(product_guid);
479 LONG result = key.Open(reg_root,
480 reg_key.c_str(),
481 KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_WOW64_32KEY);
482 if (result == ERROR_SUCCESS)
483 channel_info.Initialize(key);
484 else if (result != ERROR_FILE_NOT_FOUND)
485 LOG(ERROR) << "Failed to open " << reg_key << "; Error: " << result;
487 if (UpdateGoogleUpdateApKey(archive_type, install_return_code,
488 &channel_info)) {
489 // We have a modified channel_info value to write.
490 // Create the app's ClientState key if it doesn't already exist.
491 if (!key.Valid()) {
492 result = key.Open(reg_root,
493 google_update::kRegPathClientState,
494 KEY_CREATE_SUB_KEY | KEY_WOW64_32KEY);
495 if (result == ERROR_SUCCESS)
496 result = key.CreateKey(product_guid.c_str(),
497 KEY_SET_VALUE | KEY_WOW64_32KEY);
499 if (result != ERROR_SUCCESS) {
500 LOG(ERROR) << "Failed to create " << reg_key << "; Error: " << result;
501 return;
504 if (!channel_info.Write(&key)) {
505 LOG(ERROR) << "Failed to write to application's ClientState key "
506 << google_update::kRegApField << " = " << channel_info.value();
511 bool GoogleUpdateSettings::UpdateGoogleUpdateApKey(
512 installer::ArchiveType archive_type, int install_return_code,
513 installer::ChannelInfo* value) {
514 DCHECK(archive_type != installer::UNKNOWN_ARCHIVE_TYPE ||
515 install_return_code != 0);
516 bool modified = false;
518 if (archive_type == installer::FULL_ARCHIVE_TYPE || !install_return_code) {
519 if (value->SetFullSuffix(false)) {
520 VLOG(1) << "Removed incremental installer failure key; "
521 "switching to channel: "
522 << value->value();
523 modified = true;
525 } else if (archive_type == installer::INCREMENTAL_ARCHIVE_TYPE) {
526 if (value->SetFullSuffix(true)) {
527 VLOG(1) << "Incremental installer failed; switching to channel: "
528 << value->value();
529 modified = true;
530 } else {
531 VLOG(1) << "Incremental installer failure; already on channel: "
532 << value->value();
534 } else {
535 // It's okay if we don't know the archive type. In this case, leave the
536 // "-full" suffix as we found it.
537 DCHECK_EQ(installer::UNKNOWN_ARCHIVE_TYPE, archive_type);
540 if (value->SetMultiFailSuffix(false)) {
541 VLOG(1) << "Removed multi-install failure key; switching to channel: "
542 << value->value();
543 modified = true;
546 return modified;
549 void GoogleUpdateSettings::UpdateProfileCounts(int profiles_active,
550 int profiles_signedin) {
551 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
552 // System-level installs must write into the ClientStateMedium key shared by
553 // all users. Special treatment is used to aggregate across those users.
554 if (IsSystemInstall()) {
555 // Write the counts as ints that get aggregated across all users via
556 // summation for system-level installs.
557 WriteGoogleUpdateAggregateNumKeyInternal(
558 dist->GetAppRegistrationData(),
559 google_update::kRegProfilesActive,
560 profiles_active,
561 L"sum()");
562 WriteGoogleUpdateAggregateNumKeyInternal(
563 dist->GetAppRegistrationData(),
564 google_update::kRegProfilesSignedIn,
565 profiles_signedin,
566 L"sum()");
567 } else {
568 // Write the counts as strings since no aggregation function is needed for
569 // user-level installs.
570 WriteGoogleUpdateStrKeyInternal(dist->GetAppRegistrationData(),
571 google_update::kRegProfilesActive,
572 base::IntToString16(profiles_active));
573 WriteGoogleUpdateStrKeyInternal(dist->GetAppRegistrationData(),
574 google_update::kRegProfilesSignedIn,
575 base::IntToString16(profiles_signedin));
579 int GoogleUpdateSettings::DuplicateGoogleUpdateSystemClientKey() {
580 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
581 base::string16 reg_path = dist->GetStateKey();
583 // Minimum access needed is to be able to write to this key.
584 RegKey reg_key(
585 HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_SET_VALUE | KEY_WOW64_32KEY);
586 if (!reg_key.Valid())
587 return 0;
589 HANDLE target_handle = 0;
590 if (!DuplicateHandle(GetCurrentProcess(), reg_key.Handle(),
591 GetCurrentProcess(), &target_handle, KEY_SET_VALUE,
592 TRUE, DUPLICATE_SAME_ACCESS)) {
593 return 0;
595 return reinterpret_cast<int>(target_handle);
598 bool GoogleUpdateSettings::WriteGoogleUpdateSystemClientKey(
599 int handle, const base::string16& key, const base::string16& value) {
600 HKEY reg_key = reinterpret_cast<HKEY>(reinterpret_cast<void*>(handle));
601 DWORD size = static_cast<DWORD>(value.size()) * sizeof(wchar_t);
602 LSTATUS status = RegSetValueEx(reg_key, key.c_str(), 0, REG_SZ,
603 reinterpret_cast<const BYTE*>(value.c_str()), size);
604 return status == ERROR_SUCCESS;
607 GoogleUpdateSettings::UpdatePolicy GoogleUpdateSettings::GetAppUpdatePolicy(
608 const base::string16& app_guid,
609 bool* is_overridden) {
610 bool found_override = false;
611 UpdatePolicy update_policy = kDefaultUpdatePolicy;
613 #if defined(GOOGLE_CHROME_BUILD)
614 DCHECK(!app_guid.empty());
615 RegKey policy_key;
617 // Google Update Group Policy settings are always in HKLM.
618 // TODO(wfh): Check if policies should go into Wow6432Node or not.
619 if (policy_key.Open(HKEY_LOCAL_MACHINE, kPoliciesKey, KEY_QUERY_VALUE) ==
620 ERROR_SUCCESS) {
621 DWORD value = 0;
622 base::string16 app_update_override(kUpdateOverrideValuePrefix);
623 app_update_override.append(app_guid);
624 // First try to read and comprehend the app-specific override.
625 found_override = (policy_key.ReadValueDW(app_update_override.c_str(),
626 &value) == ERROR_SUCCESS &&
627 GetUpdatePolicyFromDword(value, &update_policy));
629 // Failing that, try to read and comprehend the default override.
630 if (!found_override &&
631 policy_key.ReadValueDW(kUpdatePolicyValue, &value) == ERROR_SUCCESS) {
632 GetUpdatePolicyFromDword(value, &update_policy);
635 #endif // defined(GOOGLE_CHROME_BUILD)
637 if (is_overridden != NULL)
638 *is_overridden = found_override;
640 return update_policy;
643 // static
644 bool GoogleUpdateSettings::AreAutoupdatesEnabled() {
645 #if defined(GOOGLE_CHROME_BUILD)
646 // Check the auto-update check period override. If it is 0 or exceeds the
647 // maximum timeout, then for all intents and purposes auto updates are
648 // disabled.
649 RegKey policy_key;
650 DWORD value = 0;
651 if (policy_key.Open(HKEY_LOCAL_MACHINE, kPoliciesKey,
652 KEY_QUERY_VALUE) == ERROR_SUCCESS &&
653 policy_key.ReadValueDW(kCheckPeriodOverrideMinutes,
654 &value) == ERROR_SUCCESS &&
655 (value == 0 || value > kCheckPeriodOverrideMinutesMax)) {
656 return false;
659 // Auto updates are subtly broken when Chrome and the binaries have different
660 // overrides in place. If this Chrome cannot possibly be multi-install by
661 // virtue of being a side-by-side installation, simply check Chrome's policy.
662 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
663 UpdatePolicy app_policy = GetAppUpdatePolicy(dist->GetAppGuid(), nullptr);
664 if (InstallUtil::IsChromeSxSProcess())
665 return app_policy == AUTOMATIC_UPDATES || app_policy == AUTO_UPDATES_ONLY;
667 // Otherwise, check for consistency between Chrome and the binaries regardless
668 // of whether or not this Chrome is multi-install since the next update likely
669 // will attempt to migrate it to such.
670 BrowserDistribution* binaries = BrowserDistribution::GetSpecificDistribution(
671 BrowserDistribution::CHROME_BINARIES);
672 return (GetAppUpdatePolicy(binaries->GetAppGuid(), nullptr) == app_policy &&
673 (app_policy == AUTOMATIC_UPDATES || app_policy == AUTO_UPDATES_ONLY));
674 #else // defined(GOOGLE_CHROME_BUILD)
675 // Chromium does not auto update.
676 return false;
677 #endif // !defined(GOOGLE_CHROME_BUILD)
680 // static
681 bool GoogleUpdateSettings::ReenableAutoupdates() {
682 #if defined(GOOGLE_CHROME_BUILD)
683 int needs_reset_count = 0;
684 int did_reset_count = 0;
686 // Reset overrides for Chrome and for the binaries if this Chrome supports
687 // multi-install.
688 std::vector<base::string16> app_guids;
689 app_guids.push_back(BrowserDistribution::GetDistribution()->GetAppGuid());
690 if (!InstallUtil::IsChromeSxSProcess()) {
691 app_guids.push_back(BrowserDistribution::GetSpecificDistribution(
692 BrowserDistribution::CHROME_BINARIES)->GetAppGuid());
695 UpdatePolicy update_policy = kDefaultUpdatePolicy;
696 RegKey policy_key;
697 if (policy_key.Open(HKEY_LOCAL_MACHINE, kPoliciesKey,
698 KEY_SET_VALUE | KEY_QUERY_VALUE) == ERROR_SUCCESS) {
699 // Set to true while app-specific overrides are present that allow automatic
700 // updates. When this is the case, the defaults are irrelevant and don't
701 // need to be checked or reset.
702 bool automatic_updates_allowed_by_overrides = true;
703 DWORD value = 0;
704 for (const base::string16& app_guid : app_guids) {
705 // First check the app-specific override value and reset that if needed.
706 // Note that this intentionally sets the override to AUTOMATIC_UPDATES
707 // even if it was previously AUTO_UPDATES_ONLY. The thinking is that
708 // AUTOMATIC_UPDATES is marginally more likely to let a user update and
709 // this code is only called when a stuck user asks for updates.
710 base::string16 app_update_override(kUpdateOverrideValuePrefix);
711 app_update_override.append(app_guid);
712 if (policy_key.ReadValueDW(app_update_override.c_str(),
713 &value) != ERROR_SUCCESS) {
714 automatic_updates_allowed_by_overrides = false;
715 } else if (!GetUpdatePolicyFromDword(value, &update_policy) ||
716 update_policy != GoogleUpdateSettings::AUTOMATIC_UPDATES) {
717 automatic_updates_allowed_by_overrides = false;
718 ++needs_reset_count;
719 if (policy_key.WriteValue(
720 app_update_override.c_str(),
721 static_cast<DWORD>(GoogleUpdateSettings::AUTOMATIC_UPDATES)) ==
722 ERROR_SUCCESS) {
723 ++did_reset_count;
728 // If there were no app-specific override policies, see if there's a global
729 // policy preventing updates and delete it if so.
730 if (!automatic_updates_allowed_by_overrides &&
731 policy_key.ReadValueDW(kUpdatePolicyValue, &value) == ERROR_SUCCESS &&
732 (!GetUpdatePolicyFromDword(value, &update_policy) ||
733 update_policy != GoogleUpdateSettings::AUTOMATIC_UPDATES)) {
734 ++needs_reset_count;
735 if (policy_key.DeleteValue(kUpdatePolicyValue) == ERROR_SUCCESS)
736 ++did_reset_count;
739 // Check the auto-update check period override. If it is 0 or exceeds
740 // the maximum timeout, delete the override value.
741 if (policy_key.ReadValueDW(kCheckPeriodOverrideMinutes,
742 &value) == ERROR_SUCCESS &&
743 (value == 0 || value > kCheckPeriodOverrideMinutesMax)) {
744 ++needs_reset_count;
745 if (policy_key.DeleteValue(kCheckPeriodOverrideMinutes) == ERROR_SUCCESS)
746 ++did_reset_count;
749 // Return whether the number of successful resets is the same as the
750 // number of things that appeared to need resetting.
751 return (needs_reset_count == did_reset_count);
752 } else {
753 // For some reason we couldn't open the policy key with the desired
754 // permissions to make changes (the most likely reason is that there is no
755 // policy set). Simply return whether or not we think updates are enabled.
756 return AreAutoupdatesEnabled();
759 #endif
760 // Non Google Chrome isn't going to autoupdate.
761 return true;
764 void GoogleUpdateSettings::RecordChromeUpdatePolicyHistograms() {
765 const bool is_multi_install = InstallUtil::IsMultiInstall(
766 BrowserDistribution::GetDistribution(), IsSystemInstall());
767 const base::string16 app_guid =
768 BrowserDistribution::GetSpecificDistribution(
769 is_multi_install ? BrowserDistribution::CHROME_BINARIES :
770 BrowserDistribution::CHROME_BROWSER)->GetAppGuid();
772 bool is_overridden = false;
773 const UpdatePolicy update_policy = GetAppUpdatePolicy(app_guid,
774 &is_overridden);
775 UMA_HISTOGRAM_BOOLEAN("GoogleUpdate.UpdatePolicyIsOverridden", is_overridden);
776 UMA_HISTOGRAM_ENUMERATION("GoogleUpdate.EffectivePolicy", update_policy,
777 UPDATE_POLICIES_COUNT);
780 base::string16 GoogleUpdateSettings::GetUninstallCommandLine(
781 bool system_install) {
782 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
783 base::string16 cmd_line;
784 RegKey update_key;
786 if (update_key.Open(root_key, google_update::kRegPathGoogleUpdate,
787 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
788 update_key.ReadValue(google_update::kRegUninstallCmdLine, &cmd_line);
791 return cmd_line;
794 Version GoogleUpdateSettings::GetGoogleUpdateVersion(bool system_install) {
795 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
796 base::string16 version;
797 RegKey key;
799 if (key.Open(root_key,
800 google_update::kRegPathGoogleUpdate,
801 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS &&
802 key.ReadValue(google_update::kRegGoogleUpdateVersion, &version) ==
803 ERROR_SUCCESS) {
804 return Version(base::UTF16ToUTF8(version));
807 return Version();
810 base::Time GoogleUpdateSettings::GetGoogleUpdateLastStartedAU(
811 bool system_install) {
812 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
813 RegKey update_key;
815 if (update_key.Open(root_key,
816 google_update::kRegPathGoogleUpdate,
817 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
818 DWORD last_start;
819 if (update_key.ReadValueDW(google_update::kRegLastStartedAUField,
820 &last_start) == ERROR_SUCCESS) {
821 return base::Time::FromTimeT(last_start);
825 return base::Time();
828 base::Time GoogleUpdateSettings::GetGoogleUpdateLastChecked(
829 bool system_install) {
830 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
831 RegKey update_key;
833 if (update_key.Open(root_key,
834 google_update::kRegPathGoogleUpdate,
835 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
836 DWORD last_check;
837 if (update_key.ReadValueDW(google_update::kRegLastCheckedField,
838 &last_check) == ERROR_SUCCESS) {
839 return base::Time::FromTimeT(last_check);
843 return base::Time();
846 bool GoogleUpdateSettings::GetUpdateDetailForApp(bool system_install,
847 const wchar_t* app_guid,
848 ProductData* data) {
849 DCHECK(app_guid);
850 DCHECK(data);
852 bool product_found = false;
854 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
855 base::string16 clientstate_reg_path(google_update::kRegPathClientState);
856 clientstate_reg_path.append(L"\\");
857 clientstate_reg_path.append(app_guid);
859 RegKey clientstate;
860 if (clientstate.Open(root_key,
861 clientstate_reg_path.c_str(),
862 KEY_QUERY_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
863 base::string16 version;
864 DWORD dword_value;
865 if ((clientstate.ReadValueDW(google_update::kRegLastCheckSuccessField,
866 &dword_value) == ERROR_SUCCESS) &&
867 (clientstate.ReadValue(google_update::kRegVersionField,
868 &version) == ERROR_SUCCESS)) {
869 product_found = true;
870 data->version = base::UTF16ToASCII(version);
871 data->last_success = base::Time::FromTimeT(dword_value);
872 data->last_result = 0;
873 data->last_error_code = 0;
874 data->last_extra_code = 0;
876 if (clientstate.ReadValueDW(google_update::kRegLastInstallerResultField,
877 &dword_value) == ERROR_SUCCESS) {
878 // Google Update convention is that if an installer writes an result
879 // code that is invalid, it is clamped to an exit code result.
880 const DWORD kMaxValidInstallResult = 4; // INSTALLER_RESULT_EXIT_CODE
881 data->last_result = std::min(dword_value, kMaxValidInstallResult);
883 if (clientstate.ReadValueDW(google_update::kRegLastInstallerErrorField,
884 &dword_value) == ERROR_SUCCESS) {
885 data->last_error_code = dword_value;
887 if (clientstate.ReadValueDW(google_update::kRegLastInstallerExtraField,
888 &dword_value) == ERROR_SUCCESS) {
889 data->last_extra_code = dword_value;
894 return product_found;
897 bool GoogleUpdateSettings::GetUpdateDetailForGoogleUpdate(bool system_install,
898 ProductData* data) {
899 return GetUpdateDetailForApp(system_install,
900 google_update::kGoogleUpdateUpgradeCode,
901 data);
904 bool GoogleUpdateSettings::GetUpdateDetail(bool system_install,
905 ProductData* data) {
906 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
907 return GetUpdateDetailForApp(system_install,
908 dist->GetAppGuid().c_str(),
909 data);
912 bool GoogleUpdateSettings::SetExperimentLabels(
913 bool system_install,
914 const base::string16& experiment_labels) {
915 HKEY reg_root = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
917 // Use the browser distribution and install level to write to the correct
918 // client state/app guid key.
919 bool success = false;
920 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
921 if (dist->ShouldSetExperimentLabels()) {
922 base::string16 client_state_path(
923 system_install ? dist->GetStateMediumKey() : dist->GetStateKey());
924 RegKey client_state(
925 reg_root, client_state_path.c_str(), KEY_SET_VALUE | KEY_WOW64_32KEY);
926 if (experiment_labels.empty()) {
927 success = client_state.DeleteValue(google_update::kExperimentLabels)
928 == ERROR_SUCCESS;
929 } else {
930 success = client_state.WriteValue(google_update::kExperimentLabels,
931 experiment_labels.c_str()) == ERROR_SUCCESS;
935 return success;
938 bool GoogleUpdateSettings::ReadExperimentLabels(
939 bool system_install,
940 base::string16* experiment_labels) {
941 HKEY reg_root = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
943 // If this distribution does not set the experiment labels, don't bother
944 // reading.
945 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
946 if (!dist->ShouldSetExperimentLabels())
947 return false;
949 base::string16 client_state_path(
950 system_install ? dist->GetStateMediumKey() : dist->GetStateKey());
952 RegKey client_state;
953 LONG result = client_state.Open(
954 reg_root, client_state_path.c_str(), KEY_QUERY_VALUE | KEY_WOW64_32KEY);
955 if (result == ERROR_SUCCESS) {
956 result = client_state.ReadValue(google_update::kExperimentLabels,
957 experiment_labels);
960 // If the key or value was not present, return the empty string.
961 if (result == ERROR_FILE_NOT_FOUND || result == ERROR_PATH_NOT_FOUND) {
962 experiment_labels->clear();
963 return true;
966 return result == ERROR_SUCCESS;