Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/beacons.h" | |
| 6 | |
| 7 #include "base/win/registry.h" | |
| 8 #include "base/win/win_util.h" | |
| 9 #include "chrome/installer/util/app_registration_data.h" | |
| 10 #include "chrome/installer/util/browser_distribution.h" | |
| 11 #include "chrome/installer/util/install_util.h" | |
| 12 #include "chrome/installer/util/shell_util.h" | |
| 13 | |
| 14 void UpdateDefaultBrowserBeaconForPath(const base::FilePath& chrome_exe) { | |
| 15 ignore_result(ShellUtil::GetChromeDefaultStateFromPath(chrome_exe)); | |
|
gab
2015/06/01 18:43:12
Add a comment that GetChromeDefaultStateFromPath()
grt (UTC plus 2)
2015/06/01 19:49:42
Done.
| |
| 16 } | |
| 17 | |
| 18 void UpdateDefaultBrowserBeaconWithState( | |
| 19 const base::FilePath& chrome_exe, | |
| 20 BrowserDistribution* distribution, | |
| 21 ShellUtil::DefaultState default_state) { | |
| 22 const bool system_install = !InstallUtil::IsPerUserInstall(chrome_exe); | |
| 23 const AppRegistrationData& registration_data = | |
| 24 distribution->GetAppRegistrationData(); | |
| 25 switch (default_state) { | |
| 26 case ShellUtil::NOT_DEFAULT: | |
| 27 installer_util::MakeFirstNotDefaultBeacon(system_install, | |
| 28 registration_data)->Update(); | |
| 29 break; | |
| 30 case ShellUtil::IS_DEFAULT: | |
| 31 installer_util::MakeLastWasDefaultBeacon(system_install, | |
| 32 registration_data)->Update(); | |
| 33 installer_util::MakeFirstNotDefaultBeacon(system_install, | |
| 34 registration_data)->Remove(); | |
| 35 break; | |
| 36 case ShellUtil::UNKNOWN_DEFAULT: | |
| 37 break; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 void UpdateOsUpgradeBeacon(bool system_install, | |
| 42 BrowserDistribution* distribution) { | |
| 43 installer_util::MakeLastOsUpgradeBeacon( | |
| 44 system_install, distribution->GetAppRegistrationData())->Update(); | |
| 45 } | |
| 46 | |
| 47 namespace installer_util { | |
| 48 | |
| 49 scoped_ptr<Beacon> MakeLastOsUpgradeBeacon( | |
| 50 bool system_install, | |
| 51 const AppRegistrationData& registration_data) { | |
| 52 return make_scoped_ptr(new Beacon(L"LastOsUpgrade", Beacon::BeaconType::LAST, | |
| 53 Beacon::BeaconScope::PER_INSTALL, | |
| 54 system_install, registration_data)); | |
| 55 } | |
| 56 | |
| 57 scoped_ptr<Beacon> MakeLastWasDefaultBeacon( | |
| 58 bool system_install, | |
| 59 const AppRegistrationData& registration_data) { | |
| 60 return make_scoped_ptr(new Beacon(L"LastWasDefault", Beacon::BeaconType::LAST, | |
| 61 Beacon::BeaconScope::PER_USER, | |
| 62 system_install, registration_data)); | |
| 63 } | |
| 64 | |
| 65 scoped_ptr<Beacon> MakeFirstNotDefaultBeacon( | |
| 66 bool system_install, | |
| 67 const AppRegistrationData& registration_data) { | |
| 68 return make_scoped_ptr(new Beacon( | |
| 69 L"FirstNotDefault", Beacon::BeaconType::FIRST, | |
| 70 Beacon::BeaconScope::PER_USER, system_install, registration_data)); | |
| 71 } | |
| 72 | |
| 73 // Beacon ---------------------------------------------------------------------- | |
| 74 | |
| 75 Beacon::Beacon(base::StringPiece16 name, | |
| 76 BeaconType type, | |
| 77 BeaconScope scope, | |
| 78 bool system_install, | |
| 79 const AppRegistrationData& registration_data) | |
| 80 : type_(type), | |
| 81 root_(system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER), | |
| 82 scope_(scope) { | |
| 83 Initialize(name, system_install, registration_data); | |
| 84 } | |
| 85 | |
| 86 Beacon::~Beacon() { | |
| 87 } | |
| 88 | |
| 89 void Beacon::Update() { | |
| 90 const REGSAM kAccess = KEY_WOW64_32KEY | KEY_QUERY_VALUE | KEY_SET_VALUE; | |
| 91 base::win::RegKey key; | |
| 92 | |
| 93 // Nothing to update if the key couldn't be created. This should only be the | |
| 94 // case for a developer build. | |
| 95 if (key.Create(root_, key_path_.c_str(), kAccess) != ERROR_SUCCESS) | |
| 96 return; | |
| 97 | |
| 98 // Nothing to do if this beacon is tracking the first occurrence of an event | |
| 99 // that has already been recorded. | |
| 100 if (type_ == BeaconType::FIRST && key.HasValue(value_name_.c_str())) | |
| 101 return; | |
| 102 | |
| 103 int64_t now(base::Time::Now().ToInternalValue()); | |
| 104 key.WriteValue(value_name_.c_str(), &now, sizeof(now), REG_QWORD); | |
| 105 } | |
| 106 | |
| 107 void Beacon::Remove() { | |
| 108 const REGSAM kAccess = KEY_WOW64_32KEY | KEY_SET_VALUE; | |
| 109 base::win::RegKey key; | |
| 110 | |
| 111 if (key.Open(root_, key_path_.c_str(), kAccess) == ERROR_SUCCESS) | |
| 112 key.DeleteValue(value_name_.c_str()); | |
| 113 } | |
| 114 | |
| 115 base::Time Beacon::Get() { | |
| 116 const REGSAM kAccess = KEY_WOW64_32KEY | KEY_QUERY_VALUE; | |
| 117 base::win::RegKey key; | |
| 118 int64_t now; | |
| 119 | |
| 120 if (key.Open(root_, key_path_.c_str(), kAccess) != ERROR_SUCCESS || | |
| 121 key.ReadInt64(value_name_.c_str(), &now) != ERROR_SUCCESS) { | |
| 122 return base::Time(); | |
| 123 } | |
| 124 | |
| 125 return base::Time::FromInternalValue(now); | |
| 126 } | |
| 127 | |
| 128 void Beacon::Initialize(base::StringPiece16 name, | |
| 129 bool system_install, | |
| 130 const AppRegistrationData& registration_data) { | |
| 131 // When possible, beacons are located in the app's ClientState key. Per-user | |
| 132 // beacons for a per-machine install are located in a beacon-specific sub-key | |
| 133 // of the app's ClientStateMedium key. | |
| 134 if (!system_install || scope_ == BeaconScope::PER_INSTALL) { | |
| 135 key_path_ = registration_data.GetStateKey(); | |
| 136 value_name_ = name.as_string(); | |
| 137 } else { | |
| 138 key_path_ = registration_data.GetStateMediumKey(); | |
| 139 key_path_.push_back(L'\\'); | |
| 140 key_path_.append(name.data(), name.size()); | |
| 141 // This should never fail. If it does, the beacon will be written in the | |
| 142 // key's default value, which is okay since the majority case is likely a | |
| 143 // machine with a single user. | |
| 144 if (!base::win::GetUserSidString(&value_name_)) | |
| 145 NOTREACHED(); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 } // namespace installer_util | |
| OLD | NEW |