Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 1 | // 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 Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_SCHEDULER_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_SCHEDULER_H_ |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 7 | |
| 8 | #include <base/basictypes.h> |
| 9 | #include <glib.h> |
| 10 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 11 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 12 | #include "update_engine/system_state.h" |
Alex Deymo | 7984bf0 | 2014-04-02 20:41:57 -0700 | [diff] [blame] | 13 | #include "update_engine/update_attempter.h" |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 14 | |
| 15 | namespace 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 | // \---/ |
| 37 | class UpdateCheckScheduler { |
| 38 | public: |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 39 | static const int kTimeoutInitialInterval; |
| 40 | static const int kTimeoutPeriodicInterval; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 41 | static const int kTimeoutRegularFuzz; |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 42 | static const int kTimeoutMaxBackoffInterval; |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 43 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 44 | UpdateCheckScheduler(UpdateAttempter* update_attempter, |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 45 | SystemState* system_state); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 46 | virtual ~UpdateCheckScheduler(); |
| 47 | |
| 48 | // Initiates the periodic update checks, if necessary. |
| 49 | void Run(); |
| 50 | |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 51 | // Sets the new update status. This is invoked by UpdateAttempter. |
| 52 | void SetUpdateStatus(UpdateStatus status); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 53 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 54 | void set_poll_interval(int interval) { poll_interval_ = interval; } |
Darin Petkov | 1b00310 | 2010-11-30 10:18:36 -0800 | [diff] [blame] | 55 | int poll_interval() const { return poll_interval_; } |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 56 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 57 | private: |
| 58 | friend class UpdateCheckSchedulerTest; |
| 59 | FRIEND_TEST(UpdateCheckSchedulerTest, CanScheduleTest); |
| 60 | FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzBackoffTest); |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 61 | FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPollTest); |
| 62 | FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPriorityTest); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 63 | FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzTest); |
| 64 | FRIEND_TEST(UpdateCheckSchedulerTest, GTimeoutAddSecondsTest); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 65 | FRIEND_TEST(UpdateCheckSchedulerTest, RunBootDeviceRemovableTest); |
| 66 | FRIEND_TEST(UpdateCheckSchedulerTest, RunNonOfficialBuildTest); |
| 67 | FRIEND_TEST(UpdateCheckSchedulerTest, RunTest); |
| 68 | FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckDisabledTest); |
| 69 | FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckEnabledTest); |
| 70 | FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleCheckNegativeIntervalTest); |
| 71 | FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleNextCheckDisabledTest); |
| 72 | FRIEND_TEST(UpdateCheckSchedulerTest, ScheduleNextCheckEnabledTest); |
| 73 | FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusIdleDisabledTest); |
| 74 | FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusIdleEnabledTest); |
| 75 | FRIEND_TEST(UpdateCheckSchedulerTest, SetUpdateStatusNonIdleTest); |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame] | 76 | FRIEND_TEST(UpdateCheckSchedulerTest, StaticCheckOOBECompleteTest); |
| 77 | FRIEND_TEST(UpdateCheckSchedulerTest, StaticCheckOOBENotCompleteTest); |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 78 | FRIEND_TEST(UpdateAttempterTest, PingOmahaTest); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 79 | |
| 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 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 83 | // Returns true if an update check can be scheduled. An update check should |
| 84 | // not be scheduled if periodic update checks are disabled or if one is |
| 85 | // already scheduled. |
| 86 | bool CanSchedule() { return enabled_ && !scheduled_; } |
| 87 | |
| 88 | // Schedules the next periodic update check |interval| seconds from now |
| 89 | // randomized by +/- |fuzz|/2. |
| 90 | void ScheduleCheck(int interval, int fuzz); |
| 91 | |
| 92 | // GLib timeout source callback. Initiates an update check through the update |
| 93 | // attempter. |
| 94 | static gboolean StaticCheck(void* scheduler); |
| 95 | |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 96 | // Schedules the next update check by setting up a timeout source; |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 97 | void ScheduleNextCheck(); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 98 | |
| 99 | // Computes the timeout interval along with its random fuzz range for the next |
| 100 | // update check by taking into account the last timeout interval as well as |
Nam T. Nguyen | 7d623eb | 2014-05-13 16:06:28 -0700 | [diff] [blame] | 101 | // the last update status. |
| 102 | void ComputeNextIntervalAndFuzz(int* next_interval, int* next_fuzz); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 103 | |
| 104 | // The UpdateAttempter to use for update checks. |
| 105 | UpdateAttempter* update_attempter_; |
| 106 | |
| 107 | // True if automatic update checks should be scheduled, false otherwise. |
| 108 | bool enabled_; |
| 109 | |
| 110 | // True if there's an update check scheduled already, false otherwise. |
| 111 | bool scheduled_; |
| 112 | |
| 113 | // The timeout interval (before fuzzing) for the last update check. |
| 114 | int last_interval_; |
| 115 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 116 | // Server dictated poll interval in seconds, if positive. |
| 117 | int poll_interval_; |
| 118 | |
Jay Srinivasan | 08fce04 | 2012-06-07 16:31:01 -0700 | [diff] [blame] | 119 | // The external state of the system outside the update_engine process. |
| 120 | SystemState* system_state_; |
| 121 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(UpdateCheckScheduler); |
| 123 | }; |
| 124 | |
| 125 | } // namespace chromeos_update_engine |
| 126 | |
Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame] | 127 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_CHECK_SCHEDULER_H_ |