[go: nahoru, domu]

blob: 9cea6c84de1d148b7fe5cd7108bef3092bc5c249 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2021 The Chromium Authors
Sanja Perisic24cabca2021-07-14 10:01:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Yeunjoo Choid33fdb62021-07-21 06:57:425#ifndef CHROME_BROWSER_ASH_POLICY_SCHEDULED_TASK_HANDLER_DEVICE_SCHEDULED_REBOOT_HANDLER_H_
6#define CHROME_BROWSER_ASH_POLICY_SCHEDULED_TASK_HANDLER_DEVICE_SCHEDULED_REBOOT_HANDLER_H_
Sanja Perisic24cabca2021-07-14 10:01:127
8#include <memory>
9
Artem Sumaneev9604473c2023-02-22 12:24:1710#include "base/memory/raw_ptr.h"
Sanja Perisic5258dd202022-08-22 21:05:4311#include "base/scoped_observation.h"
Gabriel Charetted87f10f2022-03-31 00:44:2212#include "base/time/time.h"
Sanja Perisic660ac412022-03-02 12:07:0513#include "chrome/browser/ash/policy/scheduled_task_handler/reboot_notifications_scheduler.h"
Yeunjoo Choid33fdb62021-07-21 06:57:4214#include "chrome/browser/ash/policy/scheduled_task_handler/scheduled_task_executor.h"
15#include "chrome/browser/ash/policy/scheduled_task_handler/scoped_wake_lock.h"
Sanja Perisic24cabca2021-07-14 10:01:1216#include "chrome/browser/ash/settings/cros_settings.h"
Henrique Ferreiro1eaedc42022-10-05 15:33:4717#include "chromeos/ash/components/settings/timezone_settings.h"
Sanja Perisic5258dd202022-08-22 21:05:4318#include "chromeos/dbus/power/power_manager_client.h"
Sanja Perisic24cabca2021-07-14 10:01:1219#include "services/device/public/mojom/wake_lock.mojom-forward.h"
20#include "third_party/abseil-cpp/absl/types/optional.h"
21#include "third_party/icu/source/i18n/unicode/timezone.h"
22
23namespace policy {
24
25// This class listens for changes in the scheduled reboot policy and then
Sanja Perisic660ac412022-03-02 12:07:0526// manages recurring reboots based on the policy.
Sanja Perisic24cabca2021-07-14 10:01:1227class DeviceScheduledRebootHandler
Sanja Perisic5258dd202022-08-22 21:05:4328 : public ash::system::TimezoneSettings::Observer,
29 chromeos::PowerManagerClient::Observer {
Sanja Perisic24cabca2021-07-14 10:01:1230 public:
Sanja Perisic660ac412022-03-02 12:07:0531 DeviceScheduledRebootHandler(
Sanja Perisic24cabca2021-07-14 10:01:1232 ash::CrosSettings* cros_settings,
Sanja Perisic660ac412022-03-02 12:07:0533 std::unique_ptr<ScheduledTaskExecutor> scheduled_task_executor,
Artem Sumaneeva6dcac22023-02-28 13:25:1534 RebootNotificationsScheduler* notifications_scheduler);
Sanja Perisic24cabca2021-07-14 10:01:1235 DeviceScheduledRebootHandler(const DeviceScheduledRebootHandler&) = delete;
36 DeviceScheduledRebootHandler& operator=(const DeviceScheduledRebootHandler&) =
37 delete;
38 ~DeviceScheduledRebootHandler() override;
39
40 // TODO(https://crbug.com/1228718): Move Timezone observation to
Yeunjoo Choif6e9d4a2021-11-10 02:50:2841 // ScheduledTaskExecutor. ash::system::TimezoneSettings::Observer
Sanja Perisic24cabca2021-07-14 10:01:1242 // implementation.
43 void TimezoneChanged(const icu::TimeZone& time_zone) override;
44
Sanja Perisic5258dd202022-08-22 21:05:4345 // Called when the power manager service becomes available. Reboot timer can
46 // only be started after this moment.
47 // chromeos::PowerManagerClient::Observer overrides:
48 void PowerManagerBecameAvailable(bool available) override;
49
Sanja Perisic24cabca2021-07-14 10:01:1250 // The tag associated to register |scheduled_task_executor_|.
51 static constexpr char kRebootTimerTag[] = "DeviceScheduledRebootHandler";
52
Sanja Perisic345b52b62022-03-02 10:52:0653 // Sets reboot delay for testing.
54 void SetRebootDelayForTest(const base::TimeDelta& reboot_delay);
55
Sanja Perisic3e1624a2022-08-22 14:22:0056 // Returns value of |scheduled_reboot_data_|.
57 absl::optional<ScheduledTaskExecutor::ScheduledTaskData>
58 GetScheduledRebootDataForTest() const;
59
60 // Returns value of |skip_reboot_|.
61 bool IsRebootSkippedForTest() const;
62
Sanja Perisic24cabca2021-07-14 10:01:1263 protected:
Artem Sumaneev9604473c2023-02-22 12:24:1764 using GetBootTimeCallback = base::RepeatingCallback<base::Time()>;
65
Artem Sumaneeva6dcac22023-02-28 13:25:1566 // Extended constructor for testing purposes. `cros_settings` and
67 // `notifications_scheduler` must outlive the handler.
Artem Sumaneev9604473c2023-02-22 12:24:1768 DeviceScheduledRebootHandler(
69 ash::CrosSettings* cros_settings,
70 std::unique_ptr<ScheduledTaskExecutor> scheduled_task_executor,
Artem Sumaneeva6dcac22023-02-28 13:25:1571 RebootNotificationsScheduler* notifications_scheduler,
Artem Sumaneev9604473c2023-02-22 12:24:1772 GetBootTimeCallback get_boot_time_callback);
73
Sanja Perisic24cabca2021-07-14 10:01:1274 // Called when scheduled timer fires. Triggers a reboot and
75 // schedules the next reboot based on |scheduled_reboot_data_|.
76 virtual void OnRebootTimerExpired();
77
Sanja Perisic660ac412022-03-02 12:07:0578 // Called on button click on the reboot notification or dialog. Executes
79 // reboot instantly.
80 virtual void OnRebootButtonClicked();
81
Sanja Perisic24cabca2021-07-14 10:01:1282 // Callback triggered when scheduled reboot setting has changed.
Sanja Perisic5258dd202022-08-22 21:05:4383 virtual void OnScheduledRebootDataChanged();
Sanja Perisic24cabca2021-07-14 10:01:1284
Sanja Perisic5258dd202022-08-22 21:05:4385 private:
Sanja Perisic24cabca2021-07-14 10:01:1286 // Calls |scheduled_task_executor_| to start the timer. Requires
87 // |scheduled_update_check_data_| to be set.
88 void StartRebootTimer();
89
90 // Called upon starting reboot timer. Indicates whether or not the
91 // timer was started successfully.
92 void OnRebootTimerStartResult(ScopedWakeLock scoped_wake_lock, bool result);
93
94 // Reset all state and cancel all pending tasks
95 void ResetState();
96
Sanja Perisic345b52b62022-03-02 10:52:0697 // Returns random delay between 0 and maximum reboot delay set by
98 // ash::features::kDeviceForceScheduledRebootMaxDelay feature or
99 // |reboot_delay_for_testing_| if set.
100 const base::TimeDelta GetExternalDelay() const;
101
Sanja Perisic3c33f772022-03-04 14:18:54102 void RebootDevice(const std::string& reboot_description) const;
103
Sanja Perisic24cabca2021-07-14 10:01:12104 // Used to retrieve Chrome OS settings. Not owned.
Artem Sumaneev9604473c2023-02-22 12:24:17105 const base::raw_ptr<ash::CrosSettings> cros_settings_;
Sanja Perisic24cabca2021-07-14 10:01:12106
107 // Subscription for callback when settings change.
108 base::CallbackListSubscription cros_settings_subscription_;
109
110 // Currently active scheduled reboot policy.
111 absl::optional<ScheduledTaskExecutor::ScheduledTaskData>
112 scheduled_reboot_data_;
113
114 // Timer that is scheduled to check for updates.
115 std::unique_ptr<ScheduledTaskExecutor> scheduled_task_executor_;
Sanja Perisic345b52b62022-03-02 10:52:06116
117 // Delay added to scheduled reboot time, used for testing.
118 absl::optional<base::TimeDelta> reboot_delay_for_testing_;
Sanja Perisic660ac412022-03-02 12:07:05119
Artem Sumaneeva6dcac22023-02-28 13:25:15120 // Scheduler for reboot notification and dialog. Unowned.
121 base::raw_ptr<RebootNotificationsScheduler> notifications_scheduler_;
Sanja Perisic660ac412022-03-02 12:07:05122
123 // Indicating if the reboot should be skipped.
124 bool skip_reboot_ = false;
Sanja Perisic5258dd202022-08-22 21:05:43125
Artem Sumaneev9604473c2023-02-22 12:24:17126 // Returns device's boot timestamp. The functor is used because the boot time
127 // is not constant and can change at runtime, e.g. because of the time
128 // sync.
129 GetBootTimeCallback get_boot_time_callback_;
130
Sanja Perisic5258dd202022-08-22 21:05:43131 // Observation of chromeos::PowerManagerClient.
132 base::ScopedObservation<chromeos::PowerManagerClient,
133 chromeos::PowerManagerClient::Observer>
134 observation_{this};
Sanja Perisic24cabca2021-07-14 10:01:12135};
136
137} // namespace policy
138
Yeunjoo Choid33fdb62021-07-21 06:57:42139#endif // CHROME_BROWSER_ASH_POLICY_SCHEDULED_TASK_HANDLER_DEVICE_SCHEDULED_REBOOT_HANDLER_H_