1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/installer/util/beacons.h"
#include <stdint.h>
#include <string_view>
#include <tuple>
#include "base/notreached.h"
#include "base/win/registry.h"
#include "base/win/win_util.h"
#include "chrome/install_static/install_details.h"
#include "chrome/install_static/install_util.h"
void UpdateDefaultBrowserBeaconForPath(const base::FilePath& chrome_exe) {
// Getting Chrome's default state causes the beacon to be updated via a call
// to UpdateDefaultBrowserBeaconWithState below.
std::ignore = ShellUtil::GetChromeDefaultStateFromPath(chrome_exe);
}
void UpdateDefaultBrowserBeaconWithState(
ShellUtil::DefaultState default_state) {
switch (default_state) {
case ShellUtil::UNKNOWN_DEFAULT:
break;
case ShellUtil::NOT_DEFAULT:
installer_util::MakeFirstNotDefaultBeacon()->Update();
break;
case ShellUtil::IS_DEFAULT:
case ShellUtil::OTHER_MODE_IS_DEFAULT:
installer_util::MakeLastWasDefaultBeacon()->Update();
installer_util::MakeFirstNotDefaultBeacon()->Remove();
break;
}
}
void UpdateOsUpgradeBeacon() {
installer_util::MakeLastOsUpgradeBeacon()->Update();
}
namespace installer_util {
std::unique_ptr<Beacon> MakeLastOsUpgradeBeacon() {
return std::make_unique<Beacon>(L"LastOsUpgrade", Beacon::BeaconType::LAST,
Beacon::BeaconScope::PER_INSTALL);
}
std::unique_ptr<Beacon> MakeLastWasDefaultBeacon() {
return std::make_unique<Beacon>(L"LastWasDefault", Beacon::BeaconType::LAST,
Beacon::BeaconScope::PER_USER);
}
std::unique_ptr<Beacon> MakeFirstNotDefaultBeacon() {
return std::make_unique<Beacon>(L"FirstNotDefault", Beacon::BeaconType::FIRST,
Beacon::BeaconScope::PER_USER);
}
// Beacon ----------------------------------------------------------------------
Beacon::Beacon(std::wstring_view name, BeaconType type, BeaconScope scope)
: type_(type),
root_(install_static::IsSystemInstall() ? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER),
scope_(scope) {
Initialize(name);
}
Beacon::~Beacon() = default;
void Beacon::Update() {
const REGSAM kAccess = KEY_WOW64_32KEY | KEY_QUERY_VALUE | KEY_SET_VALUE;
base::win::RegKey key;
// Nothing to update if the key couldn't be created. This should only be the
// case for a developer build.
if (key.Create(root_, key_path_.c_str(), kAccess) != ERROR_SUCCESS)
return;
// Nothing to do if this beacon is tracking the first occurrence of an event
// that has already been recorded.
if (type_ == BeaconType::FIRST && key.HasValue(value_name_.c_str()))
return;
int64_t now(base::Time::Now().ToInternalValue());
key.WriteValue(value_name_.c_str(), &now, sizeof(now), REG_QWORD);
}
void Beacon::Remove() {
const REGSAM kAccess = KEY_WOW64_32KEY | KEY_SET_VALUE;
base::win::RegKey key;
if (key.Open(root_, key_path_.c_str(), kAccess) == ERROR_SUCCESS)
key.DeleteValue(value_name_.c_str());
}
base::Time Beacon::Get() {
const REGSAM kAccess = KEY_WOW64_32KEY | KEY_QUERY_VALUE;
base::win::RegKey key;
int64_t now;
if (key.Open(root_, key_path_.c_str(), kAccess) != ERROR_SUCCESS ||
key.ReadInt64(value_name_.c_str(), &now) != ERROR_SUCCESS) {
return base::Time();
}
return base::Time::FromInternalValue(now);
}
void Beacon::Initialize(std::wstring_view name) {
const install_static::InstallDetails& install_details =
install_static::InstallDetails::Get();
// When possible, beacons are located in the app's ClientState key. Per-user
// beacons for a per-machine install are located in a beacon-specific sub-key
// of the app's ClientStateMedium key.
if (scope_ == BeaconScope::PER_INSTALL ||
!install_static::IsSystemInstall()) {
key_path_ = install_details.GetClientStateKeyPath();
value_name_.assign(name);
} else {
key_path_ = install_details.GetClientStateMediumKeyPath();
key_path_.push_back(L'\\');
key_path_.append(name);
// This should never fail. If it does, the beacon will be written in the
// key's default value, which is okay since the majority case is likely a
// machine with a single user.
if (!base::win::GetUserSidString(&value_name_)) {
NOTREACHED();
}
}
}
} // namespace installer_util
|