| Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [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/updater/check_for_updates_task.h" |
| 6 | |
| Joshua Pawlicki | 03b6c8e | 2022-02-10 19:07:54 | [diff] [blame] | 7 | #include <utility> |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 8 | |
| Avi Drissman | 69ba0cc | 2023-01-09 20:56:03 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
| 10 | #include "base/functional/callback.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/memory/scoped_refptr.h" |
| Joshua Pawlicki | 8bc230f | 2022-06-23 21:07:28 | [diff] [blame] | 13 | #include "base/rand_util.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
| Sorin Jianu | 7bd356a | 2022-10-22 00:59:58 | [diff] [blame] | 15 | #include "base/task/sequenced_task_runner.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 16 | #include "base/time/time.h" |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 17 | #include "chrome/updater/configurator.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 18 | #include "chrome/updater/constants.h" |
| 19 | #include "chrome/updater/persisted_data.h" |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 20 | #include "chrome/updater/policy/manager.h" |
| 21 | #include "chrome/updater/policy/service.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 22 | #include "chrome/updater/update_service_impl.h" |
| Xiaoling Bao | 9d44237f | 2023-01-18 21:00:27 | [diff] [blame] | 23 | #include "chrome/updater/updater_scope.h" |
| Noah Rose Ledesma | fc9b365 | 2022-11-12 02:10:49 | [diff] [blame] | 24 | #include "chrome/updater/util/util.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 25 | #include "components/prefs/pref_service.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 26 | #include "components/update_client/update_client.h" |
| 27 | |
| 28 | namespace updater { |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 29 | namespace { |
| 30 | |
| S. Ganesh | d3a4014 | 2024-10-02 05:01:02 | [diff] [blame] | 31 | bool ShouldSkipCheck(scoped_refptr<Configurator> config, |
| 32 | const std::string& task_name) { |
| Joshua Pawlicki | 8bc230f | 2022-06-23 21:07:28 | [diff] [blame] | 33 | // To spread out synchronized load, sometimes use a higher delay. |
| Noah Rose Ledesma | d391ac33 | 2022-12-13 21:09:06 | [diff] [blame] | 34 | const base::TimeDelta check_delay = |
| tomerni-island | 72d3dc9 | 2025-06-11 08:19:55 | [diff] [blame] | 35 | config->NextCheckDelay() * |
| 36 | (base::RandDouble() < kProbabilityOfIncreasedDelay |
| 37 | ? kUpdateCheckMaxDelayFactor |
| 38 | : kUpdateCheckMinDelayFactor); |
| Joshua Pawlicki | 8bc230f | 2022-06-23 21:07:28 | [diff] [blame] | 39 | |
| S. Ganesh | 51b3729 | 2024-10-07 19:35:46 | [diff] [blame] | 40 | // Skip if periodic updates are disabled altogether, for instance, by an admin |
| 41 | // setting `AutoUpdateCheckPeriodMinutes` to zero. |
| Joshua Pawlicki | 8bc230f | 2022-06-23 21:07:28 | [diff] [blame] | 42 | if (check_delay.is_zero()) { |
| S. Ganesh | d3a4014 | 2024-10-02 05:01:02 | [diff] [blame] | 43 | VLOG(0) << "Skipping " << task_name << ": NextCheckDelay is 0."; |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // Skip if the most recent check was too recent (and not in the future). |
| 48 | const base::TimeDelta time_since_update = |
| Joshua Pawlicki | 072744fba | 2023-12-06 00:57:12 | [diff] [blame] | 49 | base::Time::NowFromSystemTime() - |
| 50 | config->GetUpdaterPersistedData()->GetLastChecked(); |
| Joshua Pawlicki | 8bc230f | 2022-06-23 21:07:28 | [diff] [blame] | 51 | if (time_since_update.is_positive() && time_since_update < check_delay) { |
| S. Ganesh | d3a4014 | 2024-10-02 05:01:02 | [diff] [blame] | 52 | VLOG(0) << "Skipping " << task_name << ": last update was " |
| S. Ganesh | 6cd1f26 | 2022-08-15 18:30:29 | [diff] [blame] | 53 | << time_since_update.InMinutes() |
| 54 | << " minutes ago. check_delay == " << check_delay.InMinutes(); |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
| 58 | // Skip if the updater is in the update suppression period. |
| S. Ganesh | a092407 | 2023-07-18 01:38:56 | [diff] [blame] | 59 | return config->GetPolicyService()->AreUpdatesSuppressedNow(); |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | } // namespace |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 63 | |
| Sorin Jianu | 27e5ad2 | 2024-08-26 18:30:24 | [diff] [blame] | 64 | CheckForUpdatesTask::CheckForUpdatesTask(scoped_refptr<Configurator> config, |
| 65 | UpdaterScope scope, |
| S. Ganesh | d3a4014 | 2024-10-02 05:01:02 | [diff] [blame] | 66 | const std::string& task_name, |
| Sorin Jianu | 27e5ad2 | 2024-08-26 18:30:24 | [diff] [blame] | 67 | UpdateChecker update_checker) |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 68 | : config_(config), |
| S. Ganesh | d3a4014 | 2024-10-02 05:01:02 | [diff] [blame] | 69 | task_name_(task_name), |
| Joshua Pawlicki | 3208454 | 2021-03-19 18:46:20 | [diff] [blame] | 70 | update_checker_(std::move(update_checker)), |
| Joshua Pawlicki | 03b6c8e | 2022-02-10 19:07:54 | [diff] [blame] | 71 | update_client_(update_client::UpdateClientFactory(config_)) {} |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 72 | |
| 73 | CheckForUpdatesTask::~CheckForUpdatesTask() = default; |
| 74 | |
| Joshua Pawlicki | 03b6c8e | 2022-02-10 19:07:54 | [diff] [blame] | 75 | void CheckForUpdatesTask::Run(base::OnceClosure callback) { |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 76 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 77 | |
| S. Ganesh | d3a4014 | 2024-10-02 05:01:02 | [diff] [blame] | 78 | if (ShouldSkipCheck(config_, task_name_)) { |
| Sorin Jianu | 7bd356a | 2022-10-22 00:59:58 | [diff] [blame] | 79 | base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
| 80 | FROM_HERE, std::move(callback)); |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
| S. Ganesh | 9483f2f | 2024-11-11 14:28:26 | [diff] [blame] | 84 | base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 85 | FROM_HERE, |
| 86 | base::BindOnce( |
| Joshua Pawlicki | 3208454 | 2021-03-19 18:46:20 | [diff] [blame] | 87 | std::move(update_checker_), |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 88 | base::BindOnce( |
| S. Ganesh | d3a4014 | 2024-10-02 05:01:02 | [diff] [blame] | 89 | [](base::OnceClosure closure, const std::string& task_name, |
| 90 | UpdateService::Result result) { |
| 91 | VLOG(0) << task_name << " task complete: " << result; |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 92 | std::move(closure).Run(); |
| 93 | }, |
| S. Ganesh | 9483f2f | 2024-11-11 14:28:26 | [diff] [blame] | 94 | std::move(callback), task_name_))); |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 95 | } |
| 96 | |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 97 | } // namespace updater |