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