blob: a2dc41a183eee25f0d34c93b546d08e607ed51ae [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
Alex Deymo759c2752014-03-17 21:09:36 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_SCHEDULER_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_SCHEDULER_H_
Darin Petkov1023a602010-08-30 13:47:51 -07007
8#include <base/basictypes.h>
9#include <glib.h>
10#include <gtest/gtest_prod.h> // for FRIEND_TEST
11
Jay Srinivasan43488792012-06-19 00:25:31 -070012#include "update_engine/system_state.h"
Alex Deymo7984bf02014-04-02 20:41:57 -070013#include "update_engine/update_attempter.h"
Darin Petkov1023a602010-08-30 13:47:51 -070014
15namespace chromeos_update_engine {
16
17// UpdateCheckScheduler manages the periodic background update checks. This is
18// the basic update check cycle:
19//
20// Run
21// |
22// v
23// /->ScheduleCheck
24// | |
25// | v
26// | StaticCheck (invoked through a GLib timeout source)
27// | |
28// | v
29// | UpdateAttempter::Update
30// | |
31// | v
32// | SetUpdateStatus (invoked by UpdateAttempter on state transitions)
33// | |
34// | v
35// | ScheduleNextCheck (invoked when UpdateAttempter becomes idle)
36// \---/
37class UpdateCheckScheduler {
38 public:
Gilad Arnold1ebd8132012-03-05 10:19:29 -080039 static const int kTimeoutInitialInterval;
40 static const int kTimeoutPeriodicInterval;
41 static const int kTimeoutQuickInterval;
Darin Petkov1023a602010-08-30 13:47:51 -070042 static const int kTimeoutRegularFuzz;
Gilad Arnold1ebd8132012-03-05 10:19:29 -080043 static const int kTimeoutMaxBackoffInterval;
Darin Petkov1023a602010-08-30 13:47:51 -070044
Gilad Arnold4d740eb2012-05-15 08:48:13 -070045 UpdateCheckScheduler(UpdateAttempter* update_attempter,
Jay Srinivasan08fce042012-06-07 16:31:01 -070046 SystemState* system_state);
Darin Petkov1023a602010-08-30 13:47:51 -070047 virtual ~UpdateCheckScheduler();
48
49 // Initiates the periodic update checks, if necessary.
50 void Run();
51
Gilad Arnold1ebd8132012-03-05 10:19:29 -080052 // Sets the new update status. This is invoked by UpdateAttempter. |notice|
53 // is used for passing supplemental information about recent update events,
54 // which may influence scheduling decisions.
55 void SetUpdateStatus(UpdateStatus status, UpdateNotice notice);
Darin Petkov1023a602010-08-30 13:47:51 -070056
Darin Petkov85ced132010-09-01 10:20:56 -070057 void set_poll_interval(int interval) { poll_interval_ = interval; }
Darin Petkov1b003102010-11-30 10:18:36 -080058 int poll_interval() const { return poll_interval_; }
Darin Petkov85ced132010-09-01 10:20:56 -070059
Darin Petkov1023a602010-08-30 13:47:51 -070060 private:
61 friend class UpdateCheckSchedulerTest;
62 FRIEND_TEST(UpdateCheckSchedulerTest, CanScheduleTest);
63 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzBackoffTest);
Darin Petkov85ced132010-09-01 10:20:56 -070064 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPollTest);
65 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPriorityTest);
Darin Petkov1023a602010-08-30 13:47:51 -070066 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzTest);
67 FRIEND_TEST(UpdateCheckSchedulerTest, GTimeoutAddSecondsTest);
Darin Petkov1023a602010-08-30 13:47:51 -070068 FRIEND_TEST(UpdateCheckSchedulerTest, RunBootDeviceRemovableTest);
69 FRIEND_TEST(UpdateCheckSchedulerTest, RunNonOfficialBuildTest);
70 FRIEND_TEST(UpdateCheckSchedulerTest, RunTest);
71 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckDisabledTest);
72 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckEnabledTest);
73 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckNegativeIntervalTest);
74 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleNextCheckDisabledTest);
75 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleNextCheckEnabledTest);
76 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusIdleDisabledTest);
77 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusIdleEnabledTest);
78 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusNonIdleTest);
Darin Petkov2a0e6332010-09-24 14:43:41 -070079 FRIEND_TEST(UpdateCheckSchedulerTest, StaticCheckOOBECompleteTest);
80 FRIEND_TEST(UpdateCheckSchedulerTest, StaticCheckOOBENotCompleteTest);
Thieu Le116fda32011-04-19 11:01:54 -070081 FRIEND_TEST(UpdateAttempterTest, PingOmahaTest);
Darin Petkov1023a602010-08-30 13:47:51 -070082
83 // Wraps GLib's g_timeout_add_seconds so that it can be mocked in tests.
84 virtual guint GTimeoutAddSeconds(guint interval, GSourceFunc function);
85
Darin Petkov1023a602010-08-30 13:47:51 -070086 // Returns true if an update check can be scheduled. An update check should
87 // not be scheduled if periodic update checks are disabled or if one is
88 // already scheduled.
89 bool CanSchedule() { return enabled_ && !scheduled_; }
90
91 // Schedules the next periodic update check |interval| seconds from now
92 // randomized by +/- |fuzz|/2.
93 void ScheduleCheck(int interval, int fuzz);
94
95 // GLib timeout source callback. Initiates an update check through the update
96 // attempter.
97 static gboolean StaticCheck(void* scheduler);
98
Gilad Arnold1ebd8132012-03-05 10:19:29 -080099 // Schedules the next update check by setting up a timeout source;
100 // |is_force_quick| will enforce a quick subsequent update check interval.
101 void ScheduleNextCheck(bool is_force_quick);
Darin Petkov1023a602010-08-30 13:47:51 -0700102
103 // Computes the timeout interval along with its random fuzz range for the next
104 // update check by taking into account the last timeout interval as well as
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800105 // the last update status. A nonzero |forced_interval|, however, will override
106 // all other considerations.
107 void ComputeNextIntervalAndFuzz(const int forced_interval,
108 int* next_interval, int* next_fuzz);
Darin Petkov1023a602010-08-30 13:47:51 -0700109
110 // The UpdateAttempter to use for update checks.
111 UpdateAttempter* update_attempter_;
112
113 // True if automatic update checks should be scheduled, false otherwise.
114 bool enabled_;
115
116 // True if there's an update check scheduled already, false otherwise.
117 bool scheduled_;
118
119 // The timeout interval (before fuzzing) for the last update check.
120 int last_interval_;
121
Darin Petkov85ced132010-09-01 10:20:56 -0700122 // Server dictated poll interval in seconds, if positive.
123 int poll_interval_;
124
Gilad Arnold4d740eb2012-05-15 08:48:13 -0700125 // A flag indicating whether a test update cycle was already attempted.
126 bool is_test_update_attempted_;
127
Jay Srinivasan08fce042012-06-07 16:31:01 -0700128 // The external state of the system outside the update_engine process.
129 SystemState* system_state_;
130
Darin Petkov1023a602010-08-30 13:47:51 -0700131 DISALLOW_COPY_AND_ASSIGN(UpdateCheckScheduler);
132};
133
134} // namespace chromeos_update_engine
135
Alex Deymo759c2752014-03-17 21:09:36 -0700136#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_SCHEDULER_H_