Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Greg Thompson | 3022543 | 2020-10-15 11:20:29 | [diff] [blame] | 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/browser/upgrade_detector/registry_monitor.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
| 11 | #include "base/functional/callback.h" |
Greg Thompson | 3022543 | 2020-10-15 11:20:29 | [diff] [blame] | 12 | #include "base/location.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 13 | #include "base/task/sequenced_task_runner.h" |
Greg Thompson | 3022543 | 2020-10-15 11:20:29 | [diff] [blame] | 14 | #include "base/win/registry.h" |
| 15 | #include "chrome/browser/upgrade_detector/installed_version_monitor.h" |
| 16 | #include "chrome/install_static/install_util.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | base::win::RegKey GetDefaultMonitorLocation() { |
| 21 | return base::win::RegKey(install_static::IsSystemInstall() |
| 22 | ? HKEY_LOCAL_MACHINE |
| 23 | : HKEY_CURRENT_USER, |
| 24 | install_static::GetClientsKeyPath().c_str(), |
| 25 | KEY_NOTIFY | KEY_WOW64_32KEY); |
| 26 | } |
| 27 | |
| 28 | } // namespace |
| 29 | |
| 30 | RegistryMonitor::RegistryMonitor(base::win::RegKey key) |
| 31 | : clients_key_(std::move(key)) {} |
| 32 | |
| 33 | RegistryMonitor::~RegistryMonitor() = default; |
| 34 | |
| 35 | void RegistryMonitor::Start(Callback on_change_callback) { |
| 36 | DCHECK(on_change_callback); |
| 37 | DCHECK(!on_change_callback_); |
| 38 | on_change_callback_ = std::move(on_change_callback); |
| 39 | StartWatching(); |
| 40 | } |
| 41 | |
| 42 | void RegistryMonitor::StartWatching() { |
| 43 | // base::Unretained is safe because RegistryMonitor owns the RegKey. |
| 44 | // Destruction of this instance will cancel the watch and disable any |
| 45 | // outstanding notifications. |
| 46 | if (!clients_key_.Valid() || |
| 47 | !clients_key_.StartWatching(base::BindOnce( |
| 48 | &RegistryMonitor::OnClientsKeyChanged, base::Unretained(this)))) { |
| 49 | // Starting the watch failed. Report this back to the poller via a delayed |
| 50 | // task. |
Sean Maher | 5e3ca35 | 2022-11-07 20:08:49 | [diff] [blame] | 51 | base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
Greg Thompson | 3022543 | 2020-10-15 11:20:29 | [diff] [blame] | 52 | FROM_HERE, base::BindOnce(on_change_callback_, /*error=*/true)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void RegistryMonitor::OnClientsKeyChanged() { |
| 57 | on_change_callback_.Run(/*error=*/false); |
| 58 | StartWatching(); |
| 59 | } |
| 60 | |
| 61 | // static |
| 62 | std::unique_ptr<InstalledVersionMonitor> InstalledVersionMonitor::Create() { |
| 63 | return std::make_unique<RegistryMonitor>(GetDefaultMonitorLocation()); |
| 64 | } |