Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -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_ATTEMPTER_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__ |
| 7 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 8 | #include <time.h> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 9 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 10 | #include <tr1/memory> |
| 11 | #include <string> |
| 12 | #include <vector> |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 13 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 14 | #include <base/time.h> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 15 | #include <glib.h> |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 17 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 18 | #include "update_engine/action_processor.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 19 | #include "update_engine/download_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 20 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 21 | #include "update_engine/omaha_response_handler_action.h" |
| 22 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 23 | class MetricsLibraryInterface; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 24 | struct UpdateEngineService; |
| 25 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 26 | namespace chromeos_update_engine { |
| 27 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 28 | class UpdateCheckScheduler; |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 29 | |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 30 | extern const char* kUpdateCompletedMarker; |
| 31 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 32 | enum UpdateStatus { |
| 33 | UPDATE_STATUS_IDLE = 0, |
| 34 | UPDATE_STATUS_CHECKING_FOR_UPDATE, |
| 35 | UPDATE_STATUS_UPDATE_AVAILABLE, |
| 36 | UPDATE_STATUS_DOWNLOADING, |
| 37 | UPDATE_STATUS_VERIFYING, |
| 38 | UPDATE_STATUS_FINALIZING, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 39 | UPDATE_STATUS_UPDATED_NEED_REBOOT, |
| 40 | UPDATE_STATUS_REPORTING_ERROR_EVENT, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | const char* UpdateStatusToString(UpdateStatus status); |
| 44 | |
| 45 | class UpdateAttempter : public ActionProcessorDelegate, |
| 46 | public DownloadActionDelegate { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 47 | public: |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 48 | static const int kMaxDeltaUpdateFailures; |
| 49 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 50 | UpdateAttempter(PrefsInterface* prefs, MetricsLibraryInterface* metrics_lib); |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 51 | virtual ~UpdateAttempter(); |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 52 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 53 | // Checks for update and, if a newer version is available, attempts |
| 54 | // to update the system. Non-empty |in_app_version| or |
| 55 | // |in_update_url| prevents automatic detection of the parameter. |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 56 | virtual void Update(const std::string& app_version, |
| 57 | const std::string& omaha_url); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 58 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 59 | // ActionProcessorDelegate methods: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 60 | void ProcessingDone(const ActionProcessor* processor, ActionExitCode code); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 61 | void ProcessingStopped(const ActionProcessor* processor); |
| 62 | void ActionCompleted(ActionProcessor* processor, |
| 63 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 64 | ActionExitCode code); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 65 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 66 | // Stop updating. An attempt will be made to record status to the disk |
| 67 | // so that updates can be resumed later. |
| 68 | void Terminate(); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 69 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 70 | // Try to resume from a previously Terminate()d update. |
| 71 | void ResumeUpdating(); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 72 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 73 | // Returns the current status in the out params. Returns true on success. |
| 74 | bool GetStatus(int64_t* last_checked_time, |
| 75 | double* progress, |
| 76 | std::string* current_operation, |
| 77 | std::string* new_version, |
| 78 | int64_t* new_size); |
| 79 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 80 | UpdateStatus status() const { return status_; } |
| 81 | |
| 82 | int http_response_code() const { return http_response_code_; } |
| 83 | void set_http_response_code(int code) { http_response_code_ = code; } |
| 84 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 85 | void set_dbus_service(struct UpdateEngineService* dbus_service) { |
| 86 | dbus_service_ = dbus_service; |
| 87 | } |
| 88 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 89 | UpdateCheckScheduler* update_check_scheduler() const { |
| 90 | return update_check_scheduler_; |
| 91 | } |
| 92 | void set_update_check_scheduler(UpdateCheckScheduler* scheduler) { |
| 93 | update_check_scheduler_ = scheduler; |
| 94 | } |
| 95 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 96 | // This is the D-Bus service entry point for going through an |
| 97 | // update. If the current status is idle invokes Update. |
| 98 | void CheckForUpdate(const std::string& app_version, |
| 99 | const std::string& omaha_url); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 100 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 101 | // Initiates a reboot if the current state is |
| 102 | // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise. |
| 103 | bool RebootIfNeeded(); |
| 104 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 105 | // DownloadActionDelegate methods |
| 106 | void SetDownloadStatus(bool active); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 107 | void BytesReceived(uint64_t bytes_received, uint64_t total); |
| 108 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 109 | private: |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 110 | friend class UpdateAttempterTest; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 111 | FRIEND_TEST(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest); |
| 112 | FRIEND_TEST(UpdateAttempterTest, MarkDeltaUpdateFailureTest); |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 113 | FRIEND_TEST(UpdateAttempterTest, UpdateTest); |
| 114 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 115 | // Sets the status to the given status and notifies a status update |
| 116 | // over dbus. |
| 117 | void SetStatusAndNotify(UpdateStatus status); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 118 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 119 | // Sets up the download parameters after receiving the update check response. |
| 120 | void SetupDownload(); |
| 121 | |
| 122 | // Creates an error event object in |error_event_| to be included in an |
| 123 | // OmahaRequestAction once the current action processor is done. |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 124 | void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 125 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 126 | // If there's a pending error event allocated in |error_event_|, schedules an |
| 127 | // OmahaRequestAction with that event in the current processor, clears the |
| 128 | // pending event, updates the status and returns true. Returns false |
| 129 | // otherwise. |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 130 | bool ScheduleErrorEventAction(); |
| 131 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 132 | // Sets the process priority to |priority| and updates |priority_| if the new |
| 133 | // |priority| is different than the current |priority_|, otherwise simply |
| 134 | // returns. |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 135 | void SetPriority(utils::ProcessPriority priority); |
| 136 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 137 | // Set the process priority to low and sets up timeout events to increase the |
| 138 | // priority gradually to high. |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 139 | void SetupPriorityManagement(); |
| 140 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 141 | // Resets the process priority to normal and destroys any scheduled timeout |
| 142 | // sources. |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 143 | void CleanupPriorityManagement(); |
| 144 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 145 | // The process priority timeout source callback increases the current priority |
| 146 | // by one step (low goes to normal, normal goes to high). Returns true if the |
| 147 | // callback must be invoked again after a timeout, or false if GLib can |
| 148 | // destroy this timeout source. |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 149 | static gboolean StaticManagePriorityCallback(gpointer data); |
| 150 | bool ManagePriorityCallback(); |
| 151 | |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 152 | // Checks if a full update is needed and forces it by updating the Omaha |
| 153 | // request params. |
| 154 | void DisableDeltaUpdateIfNeeded(); |
| 155 | |
| 156 | // If this was a delta update attempt that failed, count it so that a full |
| 157 | // update can be tried when needed. |
| 158 | void MarkDeltaUpdateFailure(); |
| 159 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 160 | // Last status notification timestamp used for throttling. Use monotonic |
| 161 | // TimeTicks to ensure that notifications are sent even if the system clock is |
| 162 | // set back in the middle of an update. |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 163 | base::TimeTicks last_notify_time_; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 164 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 165 | std::vector<std::tr1::shared_ptr<AbstractAction> > actions_; |
Darin Petkov | f42cc1c | 2010-09-01 09:03:02 -0700 | [diff] [blame] | 166 | scoped_ptr<ActionProcessor> processor_; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 167 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 168 | // If non-null, this UpdateAttempter will send status updates over this |
| 169 | // dbus service. |
| 170 | UpdateEngineService* dbus_service_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 171 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 172 | // Pointer to the OmahaResponseHandlerAction in the actions_ vector. |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 173 | std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 174 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame^] | 175 | // Pointer to the DownloadAction in the actions_ vector. |
| 176 | std::tr1::shared_ptr<DownloadAction> download_action_; |
| 177 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 178 | // Pointer to the preferences store interface. |
| 179 | PrefsInterface* prefs_; |
| 180 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 181 | // Pointer to the UMA metrics collection library. |
| 182 | MetricsLibraryInterface* metrics_lib_; |
| 183 | |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 184 | // The current UpdateCheckScheduler to notify of state transitions. |
| 185 | UpdateCheckScheduler* update_check_scheduler_; |
| 186 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 187 | // Pending error event, if any. |
| 188 | scoped_ptr<OmahaEvent> error_event_; |
| 189 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 190 | // HTTP server response code from the last HTTP request action. |
Darin Petkov | 1023a60 | 2010-08-30 13:47:51 -0700 | [diff] [blame] | 191 | int http_response_code_; |
| 192 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 193 | // Current process priority. |
| 194 | utils::ProcessPriority priority_; |
| 195 | |
| 196 | // The process priority management timeout source. |
| 197 | GSource* manage_priority_source_; |
| 198 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 199 | // Set to true if an update download is active (and BytesReceived |
| 200 | // will be called), set to false otherwise. |
| 201 | bool download_active_; |
| 202 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 203 | // For status: |
| 204 | UpdateStatus status_; |
| 205 | double download_progress_; |
| 206 | int64_t last_checked_time_; |
| 207 | std::string new_version_; |
| 208 | int64_t new_size_; |
Darin Petkov | 3627577 | 2010-10-01 11:40:57 -0700 | [diff] [blame] | 209 | bool is_full_update_; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 210 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 211 | // Device paramaters common to all Omaha requests. |
| 212 | OmahaRequestDeviceParams omaha_request_params_; |
| 213 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 214 | DISALLOW_COPY_AND_ASSIGN(UpdateAttempter); |
| 215 | }; |
| 216 | |
| 217 | } // namespace chromeos_update_engine |
| 218 | |
| 219 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__ |