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. 4 5 #include "chrome/installer/util/google_update_settings.h" 6 7 #include <algorithm> 8 #include <string> 9 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/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" 30 31 using base::win::RegKey; 32 using installer::InstallationState; 33 34 namespace { 35 36 const wchar_t kGoogleUpdatePoliciesKey[] = 37 L"SOFTWARE\\Policies\\Google\\Update"; 38 const wchar_t kGoogleUpdateUpdatePolicyValue[] = L"UpdateDefault"; 39 const wchar_t kGoogleUpdateUpdateOverrideValuePrefix[] = L"Update"; 40 const GoogleUpdateSettings::UpdatePolicy kGoogleUpdateDefaultUpdatePolicy = 41 #if defined(GOOGLE_CHROME_BUILD) 42 GoogleUpdateSettings::AUTOMATIC_UPDATES; 43 #else 44 GoogleUpdateSettings::UPDATES_DISABLED; 45 #endif 46 47 bool ReadGoogleUpdateStrKey(const wchar_t* const name, std::wstring* value) { 48 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 49 std::wstring reg_path = dist->GetStateKey(); 50 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ); 51 if (key.ReadValue(name, value) != ERROR_SUCCESS) { 52 RegKey hklm_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_READ); 53 return (hklm_key.ReadValue(name, value) == ERROR_SUCCESS); 54 } 55 return true; 56 } 57 58 // Update a state registry key |name| to be |value| for the given browser 59 // |dist|. If this is a |system_install|, then update the value under 60 // HKLM (istead of HKCU for user-installs) using a group of keys (one 61 // for each OS user) and also include the method to |aggregate| these 62 // values when reporting. 63 bool WriteGoogleUpdateStrKeyInternal(BrowserDistribution* dist, 64 bool system_install, 65 const wchar_t* const name, 66 // presubmit: allow wstring 67 const std::wstring& value, 68 const wchar_t* const aggregate) { 69 DCHECK(dist); 70 71 if (system_install) { 72 DCHECK(aggregate); 73 // Machine installs require each OS user to write a unique key under a 74 // named key in HKLM as well as an "aggregation" function that describes 75 // how the values of multiple users are to be combined. 76 std::wstring uniquename; // presubmit: allow wstring 77 if (!base::win::GetUserSidString(&uniquename)) { 78 NOTREACHED(); 79 return false; 80 } 81 82 // presubmit: allow wstring 83 std::wstring reg_path(dist->GetStateMediumKey()); 84 reg_path.append(L"\\"); 85 reg_path.append(name); 86 RegKey key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_SET_VALUE); 87 key.WriteValue(google_update::kRegAggregateMethod, aggregate); 88 return (key.WriteValue(uniquename.c_str(), value.c_str()) == ERROR_SUCCESS); 89 } else { 90 // User installs are easy: just write the values to HKCU tree. 91 RegKey key(HKEY_CURRENT_USER, dist->GetStateKey().c_str(), KEY_SET_VALUE); 92 return (key.WriteValue(name, value.c_str()) == ERROR_SUCCESS); 93 } 94 } 95 96 bool WriteGoogleUpdateStrKey(const wchar_t* const name, 97 const std::wstring& value) { 98 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 99 return WriteGoogleUpdateStrKeyInternal(dist, false, name, value, NULL); 100 } 101 102 bool WriteGoogleUpdateStrKeyMultiInstall(BrowserDistribution* dist, 103 const wchar_t* const name, 104 const std::wstring& value, 105 bool system_level) { 106 bool result = WriteGoogleUpdateStrKeyInternal(dist, false, name, value, NULL); 107 if (!InstallUtil::IsMultiInstall(dist, system_level)) 108 return result; 109 // It is a multi-install distro. Must write the reg value again. 110 BrowserDistribution* multi_dist = 111 BrowserDistribution::GetSpecificDistribution( 112 BrowserDistribution::CHROME_BINARIES); 113 return 114 WriteGoogleUpdateStrKeyInternal(multi_dist, false, name, value, NULL) && 115 result; 116 } 117 118 bool ClearGoogleUpdateStrKey(const wchar_t* const name) { 119 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 120 std::wstring reg_path = dist->GetStateKey(); 121 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE); 122 std::wstring value; 123 if (key.ReadValue(name, &value) != ERROR_SUCCESS) 124 return false; 125 return (key.WriteValue(name, L"") == ERROR_SUCCESS); 126 } 127 128 bool RemoveGoogleUpdateStrKey(const wchar_t* const name) { 129 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 130 std::wstring reg_path = dist->GetStateKey(); 131 RegKey key(HKEY_CURRENT_USER, reg_path.c_str(), KEY_READ | KEY_WRITE); 132 if (!key.HasValue(name)) 133 return true; 134 return (key.DeleteValue(name) == ERROR_SUCCESS); 135 } 136 137 bool GetChromeChannelInternal(bool system_install, 138 bool add_multi_modifier, 139 string16* channel) { 140 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 141 if (dist->GetChromeChannel(channel)) { 142 return true; 143 } 144 145 HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 146 string16 reg_path = dist->GetStateKey(); 147 RegKey key(root_key, reg_path.c_str(), KEY_READ); 148 149 installer::ChannelInfo channel_info; 150 if (!channel_info.Initialize(key)) { 151 channel->assign(installer::kChromeChannelUnknown); 152 return false; 153 } 154 155 if (!channel_info.GetChannelName(channel)) { 156 channel->assign(installer::kChromeChannelUnknown); 157 } 158 159 // Tag the channel name if this is a multi-install. 160 if (add_multi_modifier && channel_info.IsMultiInstall()) { 161 if (!channel->empty()) { 162 channel->append(1, L'-'); 163 } 164 channel->append(1, L'm'); 165 } 166 167 return true; 168 } 169 170 // Populates |update_policy| with the UpdatePolicy enum value corresponding to a 171 // DWORD read from the registry and returns true if |value| is within range. 172 // If |value| is out of range, returns false without modifying |update_policy|. 173 bool GetUpdatePolicyFromDword( 174 const DWORD value, 175 GoogleUpdateSettings::UpdatePolicy* update_policy) { 176 switch (value) { 177 case GoogleUpdateSettings::UPDATES_DISABLED: 178 case GoogleUpdateSettings::AUTOMATIC_UPDATES: 179 case GoogleUpdateSettings::MANUAL_UPDATES_ONLY: 180 case GoogleUpdateSettings::AUTO_UPDATES_ONLY: 181 *update_policy = static_cast<GoogleUpdateSettings::UpdatePolicy>(value); 182 return true; 183 default: 184 LOG(WARNING) << "Unexpected update policy override value: " << value; 185 } 186 return false; 187 } 188 189 } // namespace 190 191 bool GoogleUpdateSettings::IsSystemInstall() { 192 bool system_install = false; 193 base::FilePath module_dir; 194 if (!PathService::Get(base::DIR_MODULE, &module_dir)) { 195 LOG(WARNING) 196 << "Failed to get directory of module; assuming per-user install."; 197 } else { 198 system_install = !InstallUtil::IsPerUserInstall(module_dir.value().c_str()); 199 } 200 return system_install; 201 } 202 203 bool GoogleUpdateSettings::GetCollectStatsConsent() { 204 return GetCollectStatsConsentAtLevel(IsSystemInstall()); 205 } 206 207 // Older versions of Chrome unconditionally read from HKCU\...\ClientState\... 208 // and then HKLM\...\ClientState\.... This means that system-level Chrome 209 // never checked ClientStateMedium (which has priority according to Google 210 // Update) and gave preference to a value in HKCU (which was never checked by 211 // Google Update). From now on, Chrome follows Google Update's policy. 212 bool GoogleUpdateSettings::GetCollectStatsConsentAtLevel(bool system_install) { 213 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 214 215 // Consent applies to all products in a multi-install package. 216 if (InstallUtil::IsMultiInstall(dist, system_install)) { 217 dist = BrowserDistribution::GetSpecificDistribution( 218 BrowserDistribution::CHROME_BINARIES); 219 } 220 221 RegKey key; 222 DWORD value = 0; 223 bool have_value = false; 224 225 // For system-level installs, try ClientStateMedium first. 226 have_value = 227 system_install && 228 key.Open(HKEY_LOCAL_MACHINE, dist->GetStateMediumKey().c_str(), 229 KEY_QUERY_VALUE) == ERROR_SUCCESS && 230 key.ReadValueDW(google_update::kRegUsageStatsField, 231 &value) == ERROR_SUCCESS; 232 233 // Otherwise, try ClientState. 234 if (!have_value) { 235 have_value = 236 key.Open(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, 237 dist->GetStateKey().c_str(), 238 KEY_QUERY_VALUE) == ERROR_SUCCESS && 239 key.ReadValueDW(google_update::kRegUsageStatsField, 240 &value) == ERROR_SUCCESS; 241 } 242 243 // Google Update specifically checks that the value is 1, so we do the same. 244 return have_value && value == 1; 245 } 246 247 bool GoogleUpdateSettings::SetCollectStatsConsent(bool consented) { 248 return SetCollectStatsConsentAtLevel(IsSystemInstall(), consented); 249 } 250 251 bool GoogleUpdateSettings::SetCollectStatsConsentAtLevel(bool system_install, 252 bool consented) { 253 // Google Update writes and expects 1 for true, 0 for false. 254 DWORD value = consented ? 1 : 0; 255 256 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 257 258 // Consent applies to all products in a multi-install package. 259 if (InstallUtil::IsMultiInstall(dist, system_install)) { 260 dist = BrowserDistribution::GetSpecificDistribution( 261 BrowserDistribution::CHROME_BINARIES); 262 } 263 264 // Write to ClientStateMedium for system-level; ClientState otherwise. 265 HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 266 std::wstring reg_path = 267 system_install ? dist->GetStateMediumKey() : dist->GetStateKey(); 268 RegKey key; 269 LONG result = key.Create(root_key, reg_path.c_str(), KEY_SET_VALUE); 270 if (result != ERROR_SUCCESS) { 271 LOG(ERROR) << "Failed opening key " << reg_path << " to set " 272 << google_update::kRegUsageStatsField << "; result: " << result; 273 } else { 274 result = key.WriteValue(google_update::kRegUsageStatsField, value); 275 LOG_IF(ERROR, result != ERROR_SUCCESS) << "Failed setting " 276 << google_update::kRegUsageStatsField << " in key " << reg_path 277 << "; result: " << result; 278 } 279 return (result == ERROR_SUCCESS); 280 } 281 282 bool GoogleUpdateSettings::GetMetricsId(std::string* metrics_id) { 283 std::wstring metrics_id_w; 284 bool rv = ReadGoogleUpdateStrKey(google_update::kRegMetricsId, &metrics_id_w); 285 *metrics_id = WideToUTF8(metrics_id_w); 286 return rv; 287 } 288 289 bool GoogleUpdateSettings::SetMetricsId(const std::string& metrics_id) { 290 std::wstring metrics_id_w = UTF8ToWide(metrics_id); 291 return WriteGoogleUpdateStrKey(google_update::kRegMetricsId, metrics_id_w); 292 } 293 294 // EULA consent is only relevant for system-level installs. 295 bool GoogleUpdateSettings::SetEULAConsent( 296 const InstallationState& machine_state, 297 BrowserDistribution* dist, 298 bool consented) { 299 DCHECK(dist); 300 const DWORD eula_accepted = consented ? 1 : 0; 301 std::wstring reg_path = dist->GetStateMediumKey(); 302 bool succeeded = true; 303 RegKey key; 304 305 // Write the consent value into the product's ClientStateMedium key. 306 if (key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(), 307 KEY_SET_VALUE) != ERROR_SUCCESS || 308 key.WriteValue(google_update::kRegEULAAceptedField, 309 eula_accepted) != ERROR_SUCCESS) { 310 succeeded = false; 311 } 312 313 // If this is a multi-install, also write it into the binaries' key. 314 // --mutli-install is not provided on the command-line, so deduce it from 315 // the product's state. 316 const installer::ProductState* product_state = 317 machine_state.GetProductState(true, dist->GetType()); 318 if (product_state != NULL && product_state->is_multi_install()) { 319 dist = BrowserDistribution::GetSpecificDistribution( 320 BrowserDistribution::CHROME_BINARIES); 321 reg_path = dist->GetStateMediumKey(); 322 if (key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(), 323 KEY_SET_VALUE) != ERROR_SUCCESS || 324 key.WriteValue(google_update::kRegEULAAceptedField, 325 eula_accepted) != ERROR_SUCCESS) { 326 succeeded = false; 327 } 328 } 329 330 return succeeded; 331 } 332 333 int GoogleUpdateSettings::GetLastRunTime() { 334 std::wstring time_s; 335 if (!ReadGoogleUpdateStrKey(google_update::kRegLastRunTimeField, &time_s)) 336 return -1; 337 int64 time_i; 338 if (!base::StringToInt64(time_s, &time_i)) 339 return -1; 340 base::TimeDelta td = 341 base::Time::NowFromSystemTime() - base::Time::FromInternalValue(time_i); 342 return td.InDays(); 343 } 344 345 bool GoogleUpdateSettings::SetLastRunTime() { 346 int64 time = base::Time::NowFromSystemTime().ToInternalValue(); 347 return WriteGoogleUpdateStrKey(google_update::kRegLastRunTimeField, 348 base::Int64ToString16(time)); 349 } 350 351 bool GoogleUpdateSettings::RemoveLastRunTime() { 352 return RemoveGoogleUpdateStrKey(google_update::kRegLastRunTimeField); 353 } 354 355 bool GoogleUpdateSettings::GetBrowser(std::wstring* browser) { 356 return ReadGoogleUpdateStrKey(google_update::kRegBrowserField, browser); 357 } 358 359 bool GoogleUpdateSettings::GetLanguage(std::wstring* language) { 360 return ReadGoogleUpdateStrKey(google_update::kRegLangField, language); 361 } 362 363 bool GoogleUpdateSettings::GetBrand(std::wstring* brand) { 364 return ReadGoogleUpdateStrKey(google_update::kRegRLZBrandField, brand); 365 } 366 367 bool GoogleUpdateSettings::GetReactivationBrand(std::wstring* brand) { 368 return ReadGoogleUpdateStrKey(google_update::kRegRLZReactivationBrandField, 369 brand); 370 } 371 372 bool GoogleUpdateSettings::GetClient(std::wstring* client) { 373 return ReadGoogleUpdateStrKey(google_update::kRegClientField, client); 374 } 375 376 bool GoogleUpdateSettings::SetClient(const std::wstring& client) { 377 return WriteGoogleUpdateStrKey(google_update::kRegClientField, client); 378 } 379 380 bool GoogleUpdateSettings::GetReferral(std::wstring* referral) { 381 return ReadGoogleUpdateStrKey(google_update::kRegReferralField, referral); 382 } 383 384 bool GoogleUpdateSettings::ClearReferral() { 385 return ClearGoogleUpdateStrKey(google_update::kRegReferralField); 386 } 387 388 bool GoogleUpdateSettings::UpdateDidRunState(bool did_run, 389 bool system_level) { 390 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 391 return UpdateDidRunStateForDistribution(dist, did_run, system_level); 392 } 393 394 bool GoogleUpdateSettings::UpdateDidRunStateForDistribution( 395 BrowserDistribution* dist, 396 bool did_run, 397 bool system_level) { 398 return WriteGoogleUpdateStrKeyMultiInstall(dist, 399 google_update::kRegDidRunField, 400 did_run ? L"1" : L"0", 401 system_level); 402 } 403 404 std::wstring GoogleUpdateSettings::GetChromeChannel(bool system_install) { 405 std::wstring channel; 406 GetChromeChannelInternal(system_install, false, &channel); 407 return channel; 408 } 409 410 bool GoogleUpdateSettings::GetChromeChannelAndModifiers(bool system_install, 411 string16* channel) { 412 return GetChromeChannelInternal(system_install, true, channel); 413 } 414 415 void GoogleUpdateSettings::UpdateInstallStatus(bool system_install, 416 installer::ArchiveType archive_type, int install_return_code, 417 const std::wstring& product_guid) { 418 DCHECK(archive_type != installer::UNKNOWN_ARCHIVE_TYPE || 419 install_return_code != 0); 420 HKEY reg_root = (system_install) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 421 422 RegKey key; 423 installer::ChannelInfo channel_info; 424 std::wstring reg_key(google_update::kRegPathClientState); 425 reg_key.append(L"\\"); 426 reg_key.append(product_guid); 427 LONG result = key.Open(reg_root, reg_key.c_str(), 428 KEY_QUERY_VALUE | KEY_SET_VALUE); 429 if (result == ERROR_SUCCESS) 430 channel_info.Initialize(key); 431 else if (result != ERROR_FILE_NOT_FOUND) 432 LOG(ERROR) << "Failed to open " << reg_key << "; Error: " << result; 433 434 if (UpdateGoogleUpdateApKey(archive_type, install_return_code, 435 &channel_info)) { 436 // We have a modified channel_info value to write. 437 // Create the app's ClientState key if it doesn't already exist. 438 if (!key.Valid()) { 439 result = key.Open(reg_root, google_update::kRegPathClientState, 440 KEY_CREATE_SUB_KEY); 441 if (result == ERROR_SUCCESS) 442 result = key.CreateKey(product_guid.c_str(), KEY_SET_VALUE); 443 444 if (result != ERROR_SUCCESS) { 445 LOG(ERROR) << "Failed to create " << reg_key << "; Error: " << result; 446 return; 447 } 448 } 449 if (!channel_info.Write(&key)) { 450 LOG(ERROR) << "Failed to write to application's ClientState key " 451 << google_update::kRegApField << " = " << channel_info.value(); 452 } 453 } 454 } 455 456 bool GoogleUpdateSettings::UpdateGoogleUpdateApKey( 457 installer::ArchiveType archive_type, int install_return_code, 458 installer::ChannelInfo* value) { 459 DCHECK(archive_type != installer::UNKNOWN_ARCHIVE_TYPE || 460 install_return_code != 0); 461 bool modified = false; 462 463 if (archive_type == installer::FULL_ARCHIVE_TYPE || !install_return_code) { 464 if (value->SetFullSuffix(false)) { 465 VLOG(1) << "Removed incremental installer failure key; " 466 "switching to channel: " 467 << value->value(); 468 modified = true; 469 } 470 } else if (archive_type == installer::INCREMENTAL_ARCHIVE_TYPE) { 471 if (value->SetFullSuffix(true)) { 472 VLOG(1) << "Incremental installer failed; switching to channel: " 473 << value->value(); 474 modified = true; 475 } else { 476 VLOG(1) << "Incremental installer failure; already on channel: " 477 << value->value(); 478 } 479 } else { 480 // It's okay if we don't know the archive type. In this case, leave the 481 // "-full" suffix as we found it. 482 DCHECK_EQ(installer::UNKNOWN_ARCHIVE_TYPE, archive_type); 483 } 484 485 if (value->SetMultiFailSuffix(false)) { 486 VLOG(1) << "Removed multi-install failure key; switching to channel: " 487 << value->value(); 488 modified = true; 489 } 490 491 return modified; 492 } 493 494 void GoogleUpdateSettings::UpdateProfileCounts(int profiles_active, 495 int profiles_signedin) { 496 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 497 bool system_install = IsSystemInstall(); 498 WriteGoogleUpdateStrKeyInternal(dist, system_install, 499 google_update::kRegProfilesActive, 500 base::Int64ToString16(profiles_active), 501 L"sum()"); 502 WriteGoogleUpdateStrKeyInternal(dist, system_install, 503 google_update::kRegProfilesSignedIn, 504 base::Int64ToString16(profiles_signedin), 505 L"sum()"); 506 } 507 508 int GoogleUpdateSettings::DuplicateGoogleUpdateSystemClientKey() { 509 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 510 std::wstring reg_path = dist->GetStateKey(); 511 512 // Minimum access needed is to be able to write to this key. 513 RegKey reg_key(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_SET_VALUE); 514 if (!reg_key.Valid()) 515 return 0; 516 517 HANDLE target_handle = 0; 518 if (!DuplicateHandle(GetCurrentProcess(), reg_key.Handle(), 519 GetCurrentProcess(), &target_handle, KEY_SET_VALUE, 520 TRUE, DUPLICATE_SAME_ACCESS)) { 521 return 0; 522 } 523 return reinterpret_cast<int>(target_handle); 524 } 525 526 bool GoogleUpdateSettings::WriteGoogleUpdateSystemClientKey( 527 int handle, const std::wstring& key, const std::wstring& value) { 528 HKEY reg_key = reinterpret_cast<HKEY>(reinterpret_cast<void*>(handle)); 529 DWORD size = static_cast<DWORD>(value.size()) * sizeof(wchar_t); 530 LSTATUS status = RegSetValueEx(reg_key, key.c_str(), 0, REG_SZ, 531 reinterpret_cast<const BYTE*>(value.c_str()), size); 532 return status == ERROR_SUCCESS; 533 } 534 535 GoogleUpdateSettings::UpdatePolicy GoogleUpdateSettings::GetAppUpdatePolicy( 536 const std::wstring& app_guid, 537 bool* is_overridden) { 538 bool found_override = false; 539 UpdatePolicy update_policy = kGoogleUpdateDefaultUpdatePolicy; 540 541 #if defined(GOOGLE_CHROME_BUILD) 542 DCHECK(!app_guid.empty()); 543 RegKey policy_key; 544 545 // Google Update Group Policy settings are always in HKLM. 546 if (policy_key.Open(HKEY_LOCAL_MACHINE, kGoogleUpdatePoliciesKey, 547 KEY_QUERY_VALUE) == ERROR_SUCCESS) { 548 static const size_t kPrefixLen = 549 arraysize(kGoogleUpdateUpdateOverrideValuePrefix) - 1; 550 DWORD value; 551 std::wstring app_update_override; 552 app_update_override.reserve(kPrefixLen + app_guid.size()); 553 app_update_override.append(kGoogleUpdateUpdateOverrideValuePrefix, 554 kPrefixLen); 555 app_update_override.append(app_guid); 556 // First try to read and comprehend the app-specific override. 557 found_override = (policy_key.ReadValueDW(app_update_override.c_str(), 558 &value) == ERROR_SUCCESS && 559 GetUpdatePolicyFromDword(value, &update_policy)); 560 561 // Failing that, try to read and comprehend the default override. 562 if (!found_override && 563 policy_key.ReadValueDW(kGoogleUpdateUpdatePolicyValue, 564 &value) == ERROR_SUCCESS) { 565 GetUpdatePolicyFromDword(value, &update_policy); 566 } 567 } 568 #endif // defined(GOOGLE_CHROME_BUILD) 569 570 if (is_overridden != NULL) 571 *is_overridden = found_override; 572 573 return update_policy; 574 } 575 576 void GoogleUpdateSettings::RecordChromeUpdatePolicyHistograms() { 577 const bool is_multi_install = InstallUtil::IsMultiInstall( 578 BrowserDistribution::GetDistribution(), IsSystemInstall()); 579 const base::string16 app_guid = 580 BrowserDistribution::GetSpecificDistribution( 581 is_multi_install ? BrowserDistribution::CHROME_BINARIES : 582 BrowserDistribution::CHROME_BROWSER)->GetAppGuid(); 583 584 bool is_overridden = false; 585 const UpdatePolicy update_policy = GetAppUpdatePolicy(app_guid, 586 &is_overridden); 587 UMA_HISTOGRAM_BOOLEAN("GoogleUpdate.UpdatePolicyIsOverridden", is_overridden); 588 UMA_HISTOGRAM_ENUMERATION("GoogleUpdate.EffectivePolicy", update_policy, 589 UPDATE_POLICIES_COUNT); 590 } 591 592 string16 GoogleUpdateSettings::GetUninstallCommandLine(bool system_install) { 593 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 594 string16 cmd_line; 595 RegKey update_key; 596 597 if (update_key.Open(root_key, google_update::kRegPathGoogleUpdate, 598 KEY_QUERY_VALUE) == ERROR_SUCCESS) { 599 update_key.ReadValue(google_update::kRegUninstallCmdLine, &cmd_line); 600 } 601 602 return cmd_line; 603 } 604 605 Version GoogleUpdateSettings::GetGoogleUpdateVersion(bool system_install) { 606 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 607 string16 version; 608 RegKey key; 609 610 if (key.Open(root_key, 611 google_update::kRegPathGoogleUpdate, 612 KEY_QUERY_VALUE) == ERROR_SUCCESS && 613 key.ReadValue(google_update::kRegGoogleUpdateVersion, 614 &version) == ERROR_SUCCESS) { 615 return Version(UTF16ToUTF8(version)); 616 } 617 618 return Version(); 619 } 620 621 base::Time GoogleUpdateSettings::GetGoogleUpdateLastStartedAU( 622 bool system_install) { 623 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 624 RegKey update_key; 625 626 if (update_key.Open(root_key, google_update::kRegPathGoogleUpdate, 627 KEY_QUERY_VALUE) == ERROR_SUCCESS) { 628 DWORD last_start; 629 if (update_key.ReadValueDW(google_update::kRegLastStartedAUField, 630 &last_start) == ERROR_SUCCESS) { 631 return base::Time::FromTimeT(last_start); 632 } 633 } 634 635 return base::Time(); 636 } 637 638 base::Time GoogleUpdateSettings::GetGoogleUpdateLastChecked( 639 bool system_install) { 640 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 641 RegKey update_key; 642 643 if (update_key.Open(root_key, google_update::kRegPathGoogleUpdate, 644 KEY_QUERY_VALUE) == ERROR_SUCCESS) { 645 DWORD last_check; 646 if (update_key.ReadValueDW(google_update::kRegLastCheckedField, 647 &last_check) == ERROR_SUCCESS) { 648 return base::Time::FromTimeT(last_check); 649 } 650 } 651 652 return base::Time(); 653 } 654 655 bool GoogleUpdateSettings::GetUpdateDetailForApp(bool system_install, 656 const wchar_t* app_guid, 657 ProductData* data) { 658 DCHECK(app_guid); 659 DCHECK(data); 660 661 bool product_found = false; 662 663 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 664 string16 clientstate_reg_path(google_update::kRegPathClientState); 665 clientstate_reg_path.append(L"\\"); 666 clientstate_reg_path.append(app_guid); 667 668 RegKey clientstate; 669 if (clientstate.Open(root_key, clientstate_reg_path.c_str(), 670 KEY_QUERY_VALUE) == ERROR_SUCCESS) { 671 string16 version; 672 DWORD dword_value; 673 if ((clientstate.ReadValueDW(google_update::kRegLastCheckSuccessField, 674 &dword_value) == ERROR_SUCCESS) && 675 (clientstate.ReadValue(google_update::kRegVersionField, 676 &version) == ERROR_SUCCESS)) { 677 product_found = true; 678 data->version = WideToASCII(version); 679 data->last_success = base::Time::FromTimeT(dword_value); 680 data->last_result = 0; 681 data->last_error_code = 0; 682 data->last_extra_code = 0; 683 684 if (clientstate.ReadValueDW(google_update::kRegLastInstallerResultField, 685 &dword_value) == ERROR_SUCCESS) { 686 // Google Update convention is that if an installer writes an result 687 // code that is invalid, it is clamped to an exit code result. 688 const DWORD kMaxValidInstallResult = 4; // INSTALLER_RESULT_EXIT_CODE 689 data->last_result = std::min(dword_value, kMaxValidInstallResult); 690 } 691 if (clientstate.ReadValueDW(google_update::kRegLastInstallerErrorField, 692 &dword_value) == ERROR_SUCCESS) { 693 data->last_error_code = dword_value; 694 } 695 if (clientstate.ReadValueDW(google_update::kRegLastInstallerExtraField, 696 &dword_value) == ERROR_SUCCESS) { 697 data->last_extra_code = dword_value; 698 } 699 } 700 } 701 702 return product_found; 703 } 704 705 bool GoogleUpdateSettings::GetUpdateDetailForGoogleUpdate(bool system_install, 706 ProductData* data) { 707 return GetUpdateDetailForApp(system_install, 708 google_update::kGoogleUpdateUpgradeCode, 709 data); 710 } 711 712 bool GoogleUpdateSettings::GetUpdateDetail(bool system_install, 713 ProductData* data) { 714 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 715 return GetUpdateDetailForApp(system_install, 716 dist->GetAppGuid().c_str(), 717 data); 718 } 719 720 bool GoogleUpdateSettings::SetExperimentLabels( 721 bool system_install, 722 const string16& experiment_labels) { 723 HKEY reg_root = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 724 725 // Use the browser distribution and install level to write to the correct 726 // client state/app guid key. 727 bool success = false; 728 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 729 if (dist->ShouldSetExperimentLabels()) { 730 string16 client_state_path( 731 system_install ? dist->GetStateMediumKey() : dist->GetStateKey()); 732 RegKey client_state( 733 reg_root, client_state_path.c_str(), KEY_SET_VALUE); 734 if (experiment_labels.empty()) { 735 success = client_state.DeleteValue(google_update::kExperimentLabels) 736 == ERROR_SUCCESS; 737 } else { 738 success = client_state.WriteValue(google_update::kExperimentLabels, 739 experiment_labels.c_str()) == ERROR_SUCCESS; 740 } 741 } 742 743 return success; 744 } 745 746 bool GoogleUpdateSettings::ReadExperimentLabels( 747 bool system_install, 748 string16* experiment_labels) { 749 HKEY reg_root = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; 750 751 // If this distribution does not set the experiment labels, don't bother 752 // reading. 753 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 754 if (!dist->ShouldSetExperimentLabels()) 755 return false; 756 757 string16 client_state_path( 758 system_install ? dist->GetStateMediumKey() : dist->GetStateKey()); 759 760 RegKey client_state; 761 LONG result = 762 client_state.Open(reg_root, client_state_path.c_str(), KEY_QUERY_VALUE); 763 if (result == ERROR_SUCCESS) { 764 result = client_state.ReadValue(google_update::kExperimentLabels, 765 experiment_labels); 766 } 767 768 // If the key or value was not present, return the empty string. 769 if (result == ERROR_FILE_NOT_FOUND || result == ERROR_PATH_NOT_FOUND) { 770 experiment_labels->clear(); 771 return true; 772 } 773 774 return result == ERROR_SUCCESS; 775 } 776