Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / system / automatic_reboot_manager.h
blob2dbc98ee83861b899e853c44c5a30a44b5dfaa99
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_AUTOMATIC_REBOOT_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_SYSTEM_AUTOMATIC_REBOOT_MANAGER_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "chrome/browser/chromeos/system/automatic_reboot_manager_observer.h"
17 #include "chromeos/dbus/power_manager_client.h"
18 #include "chromeos/dbus/update_engine_client.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "ui/base/user_activity/user_activity_observer.h"
23 class PrefRegistrySimple;
25 namespace base {
26 class TickClock;
29 namespace chromeos {
30 namespace system {
32 // Schedules and executes automatic reboots.
34 // Automatic reboots may be scheduled for any number of reasons. Currently, the
35 // following are implemented:
36 // * When Chrome OS has applied a system update, a reboot may become necessary
37 // to complete the update process. If the policy to automatically reboot after
38 // an update is enabled, a reboot is scheduled at that point.
39 // * If an uptime limit is set through policy, a reboot is scheduled when the
40 // device's uptime reaches the limit. Time spent sleeping counts as uptime as
41 // well.
43 // When the time of the earliest scheduled reboot is reached, the reboot is
44 // requested. The reboot is performed immediately unless one of the following
45 // reasons inhibits it:
46 // * If the login screen is being shown: Reboots are inhibited while the user is
47 // interacting with the screen (determined by checking whether there has been
48 // any user activity in the past 60 seconds).
49 // * If a session is in progress: Reboots are inhibited until the session ends,
50 // the browser is restarted or the device is suspended.
52 // If reboots are inhibited, a 24 hour grace period is started. The reboot
53 // request is carried out the moment none of the inhibiting criteria apply
54 // anymore (e.g. the user becomes idle on the login screen, the user logs exits
55 // a session, the user suspends the device). If reboots remain inhibited for the
56 // entire grace period, a reboot is unconditionally performed at its end.
58 // Note: Currently, automatic reboots are only enabled while the login screen is
59 // being shown or a kiosk app session is in progress. This will change in the
60 // future and the policy will always apply, regardless of whether a session of
61 // any particular type is in progress or not. http://crbug.com/244972
63 // Reboots may be scheduled and canceled at any time. This causes the time at
64 // which a reboot should be requested and the grace period that follows it to
65 // be recalculated.
67 // Reboots are scheduled in terms of device uptime. The current uptime is read
68 // from /proc/uptime. The time at which a reboot became necessary to finish
69 // applying an update is stored in /var/run/chrome/update_reboot_needed_uptime,
70 // making it persist across browser restarts and crashes. Placing the file under
71 // /var/run ensures that it gets cleared automatically on every boot.
72 class AutomaticRebootManager : public PowerManagerClient::Observer,
73 public UpdateEngineClient::Observer,
74 public ui::UserActivityObserver,
75 public content::NotificationObserver {
76 public:
77 // The current uptime and the uptime at which an update was applied and a
78 // reboot became necessary (if any). Used to pass this information from the
79 // blocking thread pool to the UI thread.
80 struct SystemEventTimes {
81 SystemEventTimes();
82 SystemEventTimes(const base::TimeDelta& uptime,
83 const base::TimeDelta& update_reboot_needed_uptime);
85 bool has_boot_time;
86 base::TimeTicks boot_time;
88 bool has_update_reboot_needed_time;
89 base::TimeTicks update_reboot_needed_time;
92 explicit AutomaticRebootManager(scoped_ptr<base::TickClock> clock);
93 ~AutomaticRebootManager() override;
95 AutomaticRebootManagerObserver::Reason reboot_reason() const {
96 return reboot_reason_;
98 bool reboot_requested() const { return reboot_requested_; }
100 void AddObserver(AutomaticRebootManagerObserver* observer);
101 void RemoveObserver(AutomaticRebootManagerObserver* observer);
103 // PowerManagerClient::Observer:
104 void SuspendDone(const base::TimeDelta& sleep_duration) override;
106 // UpdateEngineClient::Observer:
107 void UpdateStatusChanged(const UpdateEngineClient::Status& status) override;
109 // ui::UserActivityObserver:
110 void OnUserActivity(const ui::Event* event) override;
112 // content::NotificationObserver:
113 void Observe(int type,
114 const content::NotificationSource& source,
115 const content::NotificationDetails& details) override;
117 static void RegisterPrefs(PrefRegistrySimple* registry);
119 private:
120 friend class AutomaticRebootManagerBasicTest;
122 // Finishes initialization. Called after the |system_event_times| have been
123 // loaded in the blocking thread pool.
124 void Init(const SystemEventTimes& system_event_times);
126 // Reschedules the reboot request, start and end of the grace period. Reboots
127 // immediately if the end of the grace period has already passed.
128 void Reschedule();
130 // Requests a reboot.
131 void RequestReboot();
133 // Called whenever the status of the criteria inhibiting reboots may have
134 // changed. Reboots immediately if a reboot has actually been requested and
135 // none of the criteria inhibiting it apply anymore. Otherwise, does nothing.
136 // If |ignore_session|, a session in progress does not inhibit reboots.
137 void MaybeReboot(bool ignore_session);
139 // Reboots immediately.
140 void Reboot();
142 // A clock that can be mocked in tests to fast-forward time.
143 scoped_ptr<base::TickClock> clock_;
145 PrefChangeRegistrar local_state_registrar_;
147 content::NotificationRegistrar notification_registrar_;
149 // Fires when the user has been idle on the login screen for a set amount of
150 // time.
151 scoped_ptr<base::OneShotTimer<AutomaticRebootManager> >
152 login_screen_idle_timer_;
154 // The time at which the device was booted, in |clock_| ticks.
155 bool have_boot_time_;
156 base::TimeTicks boot_time_;
158 // The time at which an update was applied and a reboot became necessary to
159 // complete the update process, in |clock_| ticks.
160 bool have_update_reboot_needed_time_;
161 base::TimeTicks update_reboot_needed_time_;
163 // The reason for the reboot request. Updated whenever a reboot is scheduled.
164 AutomaticRebootManagerObserver::Reason reboot_reason_;
166 // Whether a reboot has been requested.
167 bool reboot_requested_;
169 // Timers that start and end the grace period.
170 scoped_ptr<base::OneShotTimer<AutomaticRebootManager> > grace_start_timer_;
171 scoped_ptr<base::OneShotTimer<AutomaticRebootManager> > grace_end_timer_;
173 base::ObserverList<AutomaticRebootManagerObserver, true> observers_;
175 base::WeakPtrFactory<AutomaticRebootManager> weak_ptr_factory_;
177 DISALLOW_COPY_AND_ASSIGN(AutomaticRebootManager);
180 } // namespace system
181 } // namespace chromeos
183 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_AUTOMATIC_REBOOT_MANAGER_H_