| 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 | |
| 9 | #include "base/bind.h" |
| 10 | #include "base/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" |
| Noah Rose Ledesma | fc9b365 | 2022-11-12 02:10:49 | [diff] [blame^] | 23 | #include "chrome/updater/util/util.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 24 | #include "components/prefs/pref_service.h" |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 25 | #include "components/update_client/update_client.h" |
| 26 | |
| 27 | namespace updater { |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 28 | namespace { |
| 29 | |
| Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 30 | bool ShouldSkipCheck(scoped_refptr<Configurator> config, |
| 31 | scoped_refptr<updater::PersistedData> persisted_data) { |
| Joshua Pawlicki | 8bc230f | 2022-06-23 21:07:28 | [diff] [blame] | 32 | // To spread out synchronized load, sometimes use a higher delay. |
| 33 | const base::TimeDelta check_delay = base::Seconds( |
| 34 | config->NextCheckDelay() * (base::RandDouble() < 0.1 ? 1.2 : 1)); |
| 35 | |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 36 | // Skip if periodic updates are disabled altogether. |
| Joshua Pawlicki | 8bc230f | 2022-06-23 21:07:28 | [diff] [blame] | 37 | if (check_delay.is_zero()) { |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 38 | VLOG(0) << "Skipping checking for updates: NextCheckDelay is 0."; |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | // Skip if the most recent check was too recent (and not in the future). |
| 43 | const base::TimeDelta time_since_update = |
| Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 44 | base::Time::NowFromSystemTime() - persisted_data->GetLastChecked(); |
| Joshua Pawlicki | 8bc230f | 2022-06-23 21:07:28 | [diff] [blame] | 45 | if (time_since_update.is_positive() && time_since_update < check_delay) { |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 46 | VLOG(0) << "Skipping checking for updates: last update was " |
| S. Ganesh | 6cd1f26 | 2022-08-15 18:30:29 | [diff] [blame] | 47 | << time_since_update.InMinutes() |
| 48 | << " minutes ago. check_delay == " << check_delay.InMinutes(); |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 49 | return true; |
| 50 | } |
| 51 | |
| 52 | // Skip if the updater is in the update suppression period. |
| 53 | UpdatesSuppressedTimes suppression; |
| 54 | if (config->GetPolicyService()->GetUpdatesSuppressedTimes(nullptr, |
| 55 | &suppression) && |
| 56 | suppression.valid()) { |
| 57 | base::Time::Exploded now; |
| 58 | base::Time::Now().LocalExplode(&now); |
| 59 | if (suppression.contains(now.hour, now.minute)) { |
| 60 | VLOG(0) << "Skipping checking for updates: in update suppression period."; |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 69 | |
| 70 | CheckForUpdatesTask::CheckForUpdatesTask( |
| Joshua Pawlicki | dca31c9 | 2021-08-03 19:16:39 | [diff] [blame] | 71 | scoped_refptr<Configurator> config, |
| Joshua Pawlicki | 03b6c8e | 2022-02-10 19:07:54 | [diff] [blame] | 72 | base::OnceCallback<void(UpdateService::Callback)> update_checker) |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 73 | : config_(config), |
| Joshua Pawlicki | 3208454 | 2021-03-19 18:46:20 | [diff] [blame] | 74 | update_checker_(std::move(update_checker)), |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 75 | persisted_data_( |
| 76 | base::MakeRefCounted<PersistedData>(config_->GetPrefService())), |
| Joshua Pawlicki | 03b6c8e | 2022-02-10 19:07:54 | [diff] [blame] | 77 | update_client_(update_client::UpdateClientFactory(config_)) {} |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 78 | |
| 79 | CheckForUpdatesTask::~CheckForUpdatesTask() = default; |
| 80 | |
| Joshua Pawlicki | 03b6c8e | 2022-02-10 19:07:54 | [diff] [blame] | 81 | void CheckForUpdatesTask::Run(base::OnceClosure callback) { |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 82 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 83 | |
| Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 84 | if (ShouldSkipCheck(config_, persisted_data_)) { |
| Sorin Jianu | 7bd356a | 2022-10-22 00:59:58 | [diff] [blame] | 85 | base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
| 86 | FROM_HERE, std::move(callback)); |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
| Sorin Jianu | 7bd356a | 2022-10-22 00:59:58 | [diff] [blame] | 90 | base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask( |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 91 | FROM_HERE, |
| 92 | base::BindOnce( |
| Joshua Pawlicki | 3208454 | 2021-03-19 18:46:20 | [diff] [blame] | 93 | std::move(update_checker_), |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 94 | base::BindOnce( |
| Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 95 | [](base::OnceClosure closure, UpdateService::Result result) { |
| 96 | VLOG(0) << "Check for update task complete: " << result; |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 97 | std::move(closure).Run(); |
| 98 | }, |
| Joshua Pawlicki | 03b6c8e | 2022-02-10 19:07:54 | [diff] [blame] | 99 | std::move(callback))), |
| Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 100 | base::Seconds(config_->InitialDelay())); |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 101 | } |
| 102 | |
| Joshua Pawlicki | e0cf78e2 | 2021-03-15 13:12:24 | [diff] [blame] | 103 | } // namespace updater |