blob: 06ec4175e3fedcf3a3a70f5e51b02efad585e945 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2021 The Chromium Authors
Joshua Pawlickie0cf78e22021-03-15 13:12:242// 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 Pawlicki03b6c8e2022-02-10 19:07:547#include <utility>
Joshua Pawlickie0cf78e22021-03-15 13:12:248
Avi Drissman69ba0cc2023-01-09 20:56:039#include "base/functional/bind.h"
10#include "base/functional/callback.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2411#include "base/logging.h"
12#include "base/memory/scoped_refptr.h"
Joshua Pawlicki8bc230f2022-06-23 21:07:2813#include "base/rand_util.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2414#include "base/sequence_checker.h"
Sorin Jianu7bd356a2022-10-22 00:59:5815#include "base/task/sequenced_task_runner.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2416#include "base/time/time.h"
Joshua Pawlickidca31c92021-08-03 19:16:3917#include "chrome/updater/configurator.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2418#include "chrome/updater/constants.h"
19#include "chrome/updater/persisted_data.h"
Joshua Pawlickidca31c92021-08-03 19:16:3920#include "chrome/updater/policy/manager.h"
21#include "chrome/updater/policy/service.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2422#include "chrome/updater/update_service_impl.h"
Xiaoling Bao9d44237f2023-01-18 21:00:2723#include "chrome/updater/updater_scope.h"
Noah Rose Ledesmafc9b3652022-11-12 02:10:4924#include "chrome/updater/util/util.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2425#include "components/prefs/pref_service.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2426#include "components/update_client/update_client.h"
27
28namespace updater {
Joshua Pawlickidca31c92021-08-03 19:16:3929namespace {
30
S. Ganeshd3a40142024-10-02 05:01:0231bool ShouldSkipCheck(scoped_refptr<Configurator> config,
32 const std::string& task_name) {
Joshua Pawlicki8bc230f2022-06-23 21:07:2833 // To spread out synchronized load, sometimes use a higher delay.
Noah Rose Ledesmad391ac332022-12-13 21:09:0634 const base::TimeDelta check_delay =
tomerni-island72d3dc92025-06-11 08:19:5535 config->NextCheckDelay() *
36 (base::RandDouble() < kProbabilityOfIncreasedDelay
37 ? kUpdateCheckMaxDelayFactor
38 : kUpdateCheckMinDelayFactor);
Joshua Pawlicki8bc230f2022-06-23 21:07:2839
S. Ganesh51b37292024-10-07 19:35:4640 // Skip if periodic updates are disabled altogether, for instance, by an admin
41 // setting `AutoUpdateCheckPeriodMinutes` to zero.
Joshua Pawlicki8bc230f2022-06-23 21:07:2842 if (check_delay.is_zero()) {
S. Ganeshd3a40142024-10-02 05:01:0243 VLOG(0) << "Skipping " << task_name << ": NextCheckDelay is 0.";
Joshua Pawlickidca31c92021-08-03 19:16:3944 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 Pawlicki072744fba2023-12-06 00:57:1249 base::Time::NowFromSystemTime() -
50 config->GetUpdaterPersistedData()->GetLastChecked();
Joshua Pawlicki8bc230f2022-06-23 21:07:2851 if (time_since_update.is_positive() && time_since_update < check_delay) {
S. Ganeshd3a40142024-10-02 05:01:0252 VLOG(0) << "Skipping " << task_name << ": last update was "
S. Ganesh6cd1f262022-08-15 18:30:2953 << time_since_update.InMinutes()
54 << " minutes ago. check_delay == " << check_delay.InMinutes();
Joshua Pawlickidca31c92021-08-03 19:16:3955 return true;
56 }
57
58 // Skip if the updater is in the update suppression period.
S. Ganesha0924072023-07-18 01:38:5659 return config->GetPolicyService()->AreUpdatesSuppressedNow();
Joshua Pawlickidca31c92021-08-03 19:16:3960}
61
62} // namespace
Joshua Pawlickie0cf78e22021-03-15 13:12:2463
Sorin Jianu27e5ad22024-08-26 18:30:2464CheckForUpdatesTask::CheckForUpdatesTask(scoped_refptr<Configurator> config,
65 UpdaterScope scope,
S. Ganeshd3a40142024-10-02 05:01:0266 const std::string& task_name,
Sorin Jianu27e5ad22024-08-26 18:30:2467 UpdateChecker update_checker)
Joshua Pawlickie0cf78e22021-03-15 13:12:2468 : config_(config),
S. Ganeshd3a40142024-10-02 05:01:0269 task_name_(task_name),
Joshua Pawlicki32084542021-03-19 18:46:2070 update_checker_(std::move(update_checker)),
Joshua Pawlicki03b6c8e2022-02-10 19:07:5471 update_client_(update_client::UpdateClientFactory(config_)) {}
Joshua Pawlickie0cf78e22021-03-15 13:12:2472
73CheckForUpdatesTask::~CheckForUpdatesTask() = default;
74
Joshua Pawlicki03b6c8e2022-02-10 19:07:5475void CheckForUpdatesTask::Run(base::OnceClosure callback) {
Joshua Pawlickie0cf78e22021-03-15 13:12:2476 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Joshua Pawlickie0cf78e22021-03-15 13:12:2477
S. Ganeshd3a40142024-10-02 05:01:0278 if (ShouldSkipCheck(config_, task_name_)) {
Sorin Jianu7bd356a2022-10-22 00:59:5879 base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
80 FROM_HERE, std::move(callback));
Joshua Pawlickie0cf78e22021-03-15 13:12:2481 return;
82 }
83
S. Ganesh9483f2f2024-11-11 14:28:2684 base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
Joshua Pawlickie0cf78e22021-03-15 13:12:2485 FROM_HERE,
86 base::BindOnce(
Joshua Pawlicki32084542021-03-19 18:46:2087 std::move(update_checker_),
Joshua Pawlickie0cf78e22021-03-15 13:12:2488 base::BindOnce(
S. Ganeshd3a40142024-10-02 05:01:0289 [](base::OnceClosure closure, const std::string& task_name,
90 UpdateService::Result result) {
91 VLOG(0) << task_name << " task complete: " << result;
Joshua Pawlickie0cf78e22021-03-15 13:12:2492 std::move(closure).Run();
93 },
S. Ganesh9483f2f2024-11-11 14:28:2694 std::move(callback), task_name_)));
Joshua Pawlickie0cf78e22021-03-15 13:12:2495}
96
Joshua Pawlickie0cf78e22021-03-15 13:12:2497} // namespace updater