blob: 74defd4ebfe741ac0034c57509a86aabb287801b [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
9#include "base/bind.h"
10#include "base/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"
Noah Rose Ledesmafc9b3652022-11-12 02:10:4923#include "chrome/updater/util/util.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2424#include "components/prefs/pref_service.h"
Joshua Pawlickie0cf78e22021-03-15 13:12:2425#include "components/update_client/update_client.h"
26
27namespace updater {
Joshua Pawlickidca31c92021-08-03 19:16:3928namespace {
29
Sorin Jianuf5425c322022-01-29 01:14:2730bool ShouldSkipCheck(scoped_refptr<Configurator> config,
31 scoped_refptr<updater::PersistedData> persisted_data) {
Joshua Pawlicki8bc230f2022-06-23 21:07:2832 // 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 Pawlickidca31c92021-08-03 19:16:3936 // Skip if periodic updates are disabled altogether.
Joshua Pawlicki8bc230f2022-06-23 21:07:2837 if (check_delay.is_zero()) {
Joshua Pawlickidca31c92021-08-03 19:16:3938 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 Jianuf5425c322022-01-29 01:14:2744 base::Time::NowFromSystemTime() - persisted_data->GetLastChecked();
Joshua Pawlicki8bc230f2022-06-23 21:07:2845 if (time_since_update.is_positive() && time_since_update < check_delay) {
Joshua Pawlickidca31c92021-08-03 19:16:3946 VLOG(0) << "Skipping checking for updates: last update was "
S. Ganesh6cd1f262022-08-15 18:30:2947 << time_since_update.InMinutes()
48 << " minutes ago. check_delay == " << check_delay.InMinutes();
Joshua Pawlickidca31c92021-08-03 19:16:3949 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 Pawlickie0cf78e22021-03-15 13:12:2469
70CheckForUpdatesTask::CheckForUpdatesTask(
Joshua Pawlickidca31c92021-08-03 19:16:3971 scoped_refptr<Configurator> config,
Joshua Pawlicki03b6c8e2022-02-10 19:07:5472 base::OnceCallback<void(UpdateService::Callback)> update_checker)
Joshua Pawlickie0cf78e22021-03-15 13:12:2473 : config_(config),
Joshua Pawlicki32084542021-03-19 18:46:2074 update_checker_(std::move(update_checker)),
Joshua Pawlickie0cf78e22021-03-15 13:12:2475 persisted_data_(
76 base::MakeRefCounted<PersistedData>(config_->GetPrefService())),
Joshua Pawlicki03b6c8e2022-02-10 19:07:5477 update_client_(update_client::UpdateClientFactory(config_)) {}
Joshua Pawlickie0cf78e22021-03-15 13:12:2478
79CheckForUpdatesTask::~CheckForUpdatesTask() = default;
80
Joshua Pawlicki03b6c8e2022-02-10 19:07:5481void CheckForUpdatesTask::Run(base::OnceClosure callback) {
Joshua Pawlickie0cf78e22021-03-15 13:12:2482 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Joshua Pawlickie0cf78e22021-03-15 13:12:2483
Sorin Jianuf5425c322022-01-29 01:14:2784 if (ShouldSkipCheck(config_, persisted_data_)) {
Sorin Jianu7bd356a2022-10-22 00:59:5885 base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
86 FROM_HERE, std::move(callback));
Joshua Pawlickie0cf78e22021-03-15 13:12:2487 return;
88 }
89
Sorin Jianu7bd356a2022-10-22 00:59:5890 base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
Joshua Pawlickie0cf78e22021-03-15 13:12:2491 FROM_HERE,
92 base::BindOnce(
Joshua Pawlicki32084542021-03-19 18:46:2093 std::move(update_checker_),
Joshua Pawlickie0cf78e22021-03-15 13:12:2494 base::BindOnce(
Sorin Jianuf5425c322022-01-29 01:14:2795 [](base::OnceClosure closure, UpdateService::Result result) {
96 VLOG(0) << "Check for update task complete: " << result;
Joshua Pawlickie0cf78e22021-03-15 13:12:2497 std::move(closure).Run();
98 },
Joshua Pawlicki03b6c8e2022-02-10 19:07:5499 std::move(callback))),
Peter Kastinge5a38ed2021-10-02 03:06:35100 base::Seconds(config_->InitialDelay()));
Joshua Pawlickie0cf78e22021-03-15 13:12:24101}
102
Joshua Pawlickie0cf78e22021-03-15 13:12:24103} // namespace updater