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 | |
| 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 | |
| 14 | namespace 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 | // \---/ |
| 36 | class 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 Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 52 | void set_poll_interval(int interval) { poll_interval_ = interval; } |
| 53 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 54 | private: |
| 55 | friend class UpdateCheckSchedulerTest; |
| 56 | FRIEND_TEST(UpdateCheckSchedulerTest, CanScheduleTest); |
| 57 | FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzBackoffTest); |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 58 | FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPollTest); |
| 59 | FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzPriorityTest); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 60 | FRIEND_TEST(UpdateCheckSchedulerTest, ComputeNextIntervalAndFuzzTest); |
| 61 | FRIEND_TEST(UpdateCheckSchedulerTest, GTimeoutAddSecondsTest); |
| 62 | FRIEND_TEST(UpdateCheckSchedulerTest, IsBootDeviceRemovableTest); |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame^] | 63 | FRIEND_TEST(UpdateCheckSchedulerTest, IsOOBECompleteTest); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 64 | FRIEND_TEST(UpdateCheckSchedulerTest, IsOfficialBuildTest); |
| 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); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 78 | |
| 79 | // Wraps GLib's g_timeout_add_seconds so that it can be mocked in tests. |
| 80 | virtual guint GTimeoutAddSeconds(guint interval, GSourceFunc function); |
| 81 | |
| 82 | // Wrappers for utils functions so that they can be mocked in tests. |
| 83 | virtual bool IsBootDeviceRemovable(); |
Darin Petkov | 2a0e633 | 2010-09-24 14:43:41 -0700 | [diff] [blame^] | 84 | virtual bool IsOOBEComplete(); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 85 | virtual bool IsOfficialBuild(); |
| 86 | |
| 87 | // Returns true if an update check can be scheduled. An update check should |
| 88 | // not be scheduled if periodic update checks are disabled or if one is |
| 89 | // already scheduled. |
| 90 | bool CanSchedule() { return enabled_ && !scheduled_; } |
| 91 | |
| 92 | // Schedules the next periodic update check |interval| seconds from now |
| 93 | // randomized by +/- |fuzz|/2. |
| 94 | void ScheduleCheck(int interval, int fuzz); |
| 95 | |
| 96 | // GLib timeout source callback. Initiates an update check through the update |
| 97 | // attempter. |
| 98 | static gboolean StaticCheck(void* scheduler); |
| 99 | |
| 100 | // Schedules the next update check by setting up a timeout source. |
| 101 | void ScheduleNextCheck(); |
| 102 | |
| 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 |
| 105 | // the last update status. |
| 106 | void ComputeNextIntervalAndFuzz(int* next_interval, int* next_fuzz); |
| 107 | |
| 108 | // The UpdateAttempter to use for update checks. |
| 109 | UpdateAttempter* update_attempter_; |
| 110 | |
| 111 | // True if automatic update checks should be scheduled, false otherwise. |
| 112 | bool enabled_; |
| 113 | |
| 114 | // True if there's an update check scheduled already, false otherwise. |
| 115 | bool scheduled_; |
| 116 | |
| 117 | // The timeout interval (before fuzzing) for the last update check. |
| 118 | int last_interval_; |
| 119 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 120 | // Server dictated poll interval in seconds, if positive. |
| 121 | int poll_interval_; |
| 122 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 123 | DISALLOW_COPY_AND_ASSIGN(UpdateCheckScheduler); |
| 124 | }; |
| 125 | |
| 126 | } // namespace chromeos_update_engine |
| 127 | |
| 128 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_CHECK_SCHEDULER_H__ |