blob: cd3ad870c050e587b6822725460a5570a35840b2 [file] [log] [blame]
Darin Petkov1023a602010-08-30 13:47:51 -07001// Copyright (c) 2010 The Chromium OS 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.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_SCHEDULER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_SCHEDULER_H__
7
8#include <base/basictypes.h>
9#include <glib.h>
10#include <gtest/gtest_prod.h> // for FRIEND_TEST
11
12#include "update_engine/update_attempter.h"
13
14namespace chromeos_update_engine {
15
16// UpdateCheckScheduler manages the periodic background update checks. This is
17// the basic update check cycle:
18//
19// Run
20// |
21// v
22// /->ScheduleCheck
23// | |
24// | v
25// | StaticCheck (invoked through a GLib timeout source)
26// | |
27// | v
28// | UpdateAttempter::Update
29// | |
30// | v
31// | SetUpdateStatus (invoked by UpdateAttempter on state transitions)
32// | |
33// | v
34// | ScheduleNextCheck (invoked when UpdateAttempter becomes idle)
35// \---/
36class UpdateCheckScheduler {
37 public:
Gilad Arnold1ebd8132012-03-05 10:19:29 -080038 static const int kTimeoutInitialInterval;
39 static const int kTimeoutPeriodicInterval;
40 static const int kTimeoutQuickInterval;
Darin Petkov1023a602010-08-30 13:47:51 -070041 static const int kTimeoutRegularFuzz;
Gilad Arnold1ebd8132012-03-05 10:19:29 -080042 static const int kTimeoutMaxBackoffInterval;
Darin Petkov1023a602010-08-30 13:47:51 -070043
Gilad Arnold4d740eb2012-05-15 08:48:13 -070044 UpdateCheckScheduler(UpdateAttempter* update_attempter,
45 GpioHandler* gpio_handler);
Darin Petkov1023a602010-08-30 13:47:51 -070046 virtual ~UpdateCheckScheduler();
47
48 // Initiates the periodic update checks, if necessary.
49 void Run();
50
Gilad Arnold1ebd8132012-03-05 10:19:29 -080051 // Sets the new update status. This is invoked by UpdateAttempter. |notice|
52 // is used for passing supplemental information about recent update events,
53 // which may influence scheduling decisions.
54 void SetUpdateStatus(UpdateStatus status, UpdateNotice notice);
Darin Petkov1023a602010-08-30 13:47:51 -070055
Darin Petkov85ced132010-09-01 10:20:56 -070056 void set_poll_interval(int interval) { poll_interval_ = interval; }
Darin Petkov1b003102010-11-30 10:18:36 -080057 int poll_interval() const { return poll_interval_; }
Darin Petkov85ced132010-09-01 10:20:56 -070058
Darin Petkov1023a602010-08-30 13:47:51 -070059 private:
60 friend class UpdateCheckSchedulerTest;
61 FRIEND_TEST(UpdateCheckSchedulerTest, CanScheduleTest);
62 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzBackoffTest);
Darin Petkov85ced132010-09-01 10:20:56 -070063 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPollTest);
64 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPriorityTest);
Darin Petkov1023a602010-08-30 13:47:51 -070065 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzTest);
66 FRIEND_TEST(UpdateCheckSchedulerTest, GTimeoutAddSecondsTest);
67 FRIEND_TEST(UpdateCheckSchedulerTest, IsBootDeviceRemovableTest);
Darin Petkov2a0e6332010-09-24 14:43:41 -070068 FRIEND_TEST(UpdateCheckSchedulerTest, IsOOBECompleteTest);
Darin Petkov1023a602010-08-30 13:47:51 -070069 FRIEND_TEST(UpdateCheckSchedulerTest, IsOfficialBuildTest);
70 FRIEND_TEST(UpdateCheckSchedulerTest, RunBootDeviceRemovableTest);
71 FRIEND_TEST(UpdateCheckSchedulerTest, RunNonOfficialBuildTest);
72 FRIEND_TEST(UpdateCheckSchedulerTest, RunTest);
73 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckDisabledTest);
74 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckEnabledTest);
75 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckNegativeIntervalTest);
76 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleNextCheckDisabledTest);
77 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleNextCheckEnabledTest);
78 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusIdleDisabledTest);
79 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusIdleEnabledTest);
80 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusNonIdleTest);
Darin Petkov2a0e6332010-09-24 14:43:41 -070081 FRIEND_TEST(UpdateCheckSchedulerTest, StaticCheckOOBECompleteTest);
82 FRIEND_TEST(UpdateCheckSchedulerTest, StaticCheckOOBENotCompleteTest);
Thieu Le116fda32011-04-19 11:01:54 -070083 FRIEND_TEST(UpdateAttempterTest, PingOmahaTest);
Darin Petkov1023a602010-08-30 13:47:51 -070084
85 // Wraps GLib's g_timeout_add_seconds so that it can be mocked in tests.
86 virtual guint GTimeoutAddSeconds(guint interval, GSourceFunc function);
87
88 // Wrappers for utils functions so that they can be mocked in tests.
89 virtual bool IsBootDeviceRemovable();
Darin Petkov2a0e6332010-09-24 14:43:41 -070090 virtual bool IsOOBEComplete();
Darin Petkov1023a602010-08-30 13:47:51 -070091 virtual bool IsOfficialBuild();
92
93 // Returns true if an update check can be scheduled. An update check should
94 // not be scheduled if periodic update checks are disabled or if one is
95 // already scheduled.
96 bool CanSchedule() { return enabled_ && !scheduled_; }
97
98 // Schedules the next periodic update check |interval| seconds from now
99 // randomized by +/- |fuzz|/2.
100 void ScheduleCheck(int interval, int fuzz);
101
102 // GLib timeout source callback. Initiates an update check through the update
103 // attempter.
104 static gboolean StaticCheck(void* scheduler);
105
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800106 // Schedules the next update check by setting up a timeout source;
107 // |is_force_quick| will enforce a quick subsequent update check interval.
108 void ScheduleNextCheck(bool is_force_quick);
Darin Petkov1023a602010-08-30 13:47:51 -0700109
110 // Computes the timeout interval along with its random fuzz range for the next
111 // update check by taking into account the last timeout interval as well as
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800112 // the last update status. A nonzero |forced_interval|, however, will override
113 // all other considerations.
114 void ComputeNextIntervalAndFuzz(const int forced_interval,
115 int* next_interval, int* next_fuzz);
Darin Petkov1023a602010-08-30 13:47:51 -0700116
117 // The UpdateAttempter to use for update checks.
118 UpdateAttempter* update_attempter_;
119
120 // True if automatic update checks should be scheduled, false otherwise.
121 bool enabled_;
122
123 // True if there's an update check scheduled already, false otherwise.
124 bool scheduled_;
125
126 // The timeout interval (before fuzzing) for the last update check.
127 int last_interval_;
128
Darin Petkov85ced132010-09-01 10:20:56 -0700129 // Server dictated poll interval in seconds, if positive.
130 int poll_interval_;
131
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700132 // A flag indicating whether a test update cycle was already attempted.
133 bool is_test_update_attempted_;
134
135 // GPIO handler object.
136 GpioHandler* gpio_handler_;
137
Darin Petkov1023a602010-08-30 13:47:51 -0700138 DISALLOW_COPY_AND_ASSIGN(UpdateCheckScheduler);
139};
140
141} // namespace chromeos_update_engine
142
143#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_CHECK_SCHEDULER_H__