blob: 181129149463363e29f9533d2b041ae86311c284 [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:
38 static const int kTimeoutOnce;
39 static const int kTimeoutPeriodic;
40 static const int kTimeoutRegularFuzz;
41 static const int kTimeoutMaxBackoff;
42
43 UpdateCheckScheduler(UpdateAttempter* update_attempter);
44 virtual ~UpdateCheckScheduler();
45
46 // Initiates the periodic update checks, if necessary.
47 void Run();
48
49 // Sets the new update status. This is invoked by UpdateAttempter.
50 void SetUpdateStatus(UpdateStatus status);
51
Darin Petkov85ced132010-09-01 10:20:56 -070052 void set_poll_interval(int interval) { poll_interval_ = interval; }
Darin Petkov1b003102010-11-30 10:18:36 -080053 int poll_interval() const { return poll_interval_; }
Darin Petkov85ced132010-09-01 10:20:56 -070054
Darin Petkov1023a602010-08-30 13:47:51 -070055 private:
56 friend class UpdateCheckSchedulerTest;
57 FRIEND_TEST(UpdateCheckSchedulerTest, CanScheduleTest);
58 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzBackoffTest);
Darin Petkov85ced132010-09-01 10:20:56 -070059 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPollTest);
60 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPriorityTest);
Darin Petkov1023a602010-08-30 13:47:51 -070061 FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzTest);
62 FRIEND_TEST(UpdateCheckSchedulerTest, GTimeoutAddSecondsTest);
63 FRIEND_TEST(UpdateCheckSchedulerTest, IsBootDeviceRemovableTest);
Darin Petkov2a0e6332010-09-24 14:43:41 -070064 FRIEND_TEST(UpdateCheckSchedulerTest, IsOOBECompleteTest);
Darin Petkov1023a602010-08-30 13:47:51 -070065 FRIEND_TEST(UpdateCheckSchedulerTest, IsOfficialBuildTest);
66 FRIEND_TEST(UpdateCheckSchedulerTest, RunBootDeviceRemovableTest);
67 FRIEND_TEST(UpdateCheckSchedulerTest, RunNonOfficialBuildTest);
68 FRIEND_TEST(UpdateCheckSchedulerTest, RunTest);
69 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckDisabledTest);
70 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckEnabledTest);
71 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckNegativeIntervalTest);
72 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleNextCheckDisabledTest);
73 FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleNextCheckEnabledTest);
74 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusIdleDisabledTest);
75 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusIdleEnabledTest);
76 FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusNonIdleTest);
Darin Petkov2a0e6332010-09-24 14:43:41 -070077 FRIEND_TEST(UpdateCheckSchedulerTest, StaticCheckOOBECompleteTest);
78 FRIEND_TEST(UpdateCheckSchedulerTest, StaticCheckOOBENotCompleteTest);
Darin Petkov1023a602010-08-30 13:47:51 -070079
80 // Wraps GLib's g_timeout_add_seconds so that it can be mocked in tests.
81 virtual guint GTimeoutAddSeconds(guint interval, GSourceFunc function);
82
83 // Wrappers for utils functions so that they can be mocked in tests.
84 virtual bool IsBootDeviceRemovable();
Darin Petkov2a0e6332010-09-24 14:43:41 -070085 virtual bool IsOOBEComplete();
Darin Petkov1023a602010-08-30 13:47:51 -070086 virtual bool IsOfficialBuild();
87
88 // Returns true if an update check can be scheduled. An update check should
89 // not be scheduled if periodic update checks are disabled or if one is
90 // already scheduled.
91 bool CanSchedule() { return enabled_ && !scheduled_; }
92
93 // Schedules the next periodic update check |interval| seconds from now
94 // randomized by +/- |fuzz|/2.
95 void ScheduleCheck(int interval, int fuzz);
96
97 // GLib timeout source callback. Initiates an update check through the update
98 // attempter.
99 static gboolean StaticCheck(void* scheduler);
100
101 // Schedules the next update check by setting up a timeout source.
102 void ScheduleNextCheck();
103
104 // Computes the timeout interval along with its random fuzz range for the next
105 // update check by taking into account the last timeout interval as well as
106 // the last update status.
107 void ComputeNextIntervalAndFuzz(int* next_interval, int* next_fuzz);
108
109 // The UpdateAttempter to use for update checks.
110 UpdateAttempter* update_attempter_;
111
112 // True if automatic update checks should be scheduled, false otherwise.
113 bool enabled_;
114
115 // True if there's an update check scheduled already, false otherwise.
116 bool scheduled_;
117
118 // The timeout interval (before fuzzing) for the last update check.
119 int last_interval_;
120
Darin Petkov85ced132010-09-01 10:20:56 -0700121 // Server dictated poll interval in seconds, if positive.
122 int poll_interval_;
123
Darin Petkov1023a602010-08-30 13:47:51 -0700124 DISALLOW_COPY_AND_ASSIGN(UpdateCheckScheduler);
125};
126
127} // namespace chromeos_update_engine
128
129#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_CHECK_SCHEDULER_H__