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> |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 9 | #include <tr1/memory> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | #include <glib.h> |
| 13 | #include "update_engine/action_processor.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 14 | #include "update_engine/download_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 15 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 16 | #include "update_engine/omaha_response_handler_action.h" |
| 17 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 18 | class MetricsLibraryInterface; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 19 | struct UpdateEngineService; |
| 20 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 21 | namespace chromeos_update_engine { |
| 22 | |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 23 | extern const char* kUpdateCompletedMarker; |
| 24 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 25 | enum UpdateStatus { |
| 26 | UPDATE_STATUS_IDLE = 0, |
| 27 | UPDATE_STATUS_CHECKING_FOR_UPDATE, |
| 28 | UPDATE_STATUS_UPDATE_AVAILABLE, |
| 29 | UPDATE_STATUS_DOWNLOADING, |
| 30 | UPDATE_STATUS_VERIFYING, |
| 31 | UPDATE_STATUS_FINALIZING, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 32 | UPDATE_STATUS_UPDATED_NEED_REBOOT, |
| 33 | UPDATE_STATUS_REPORTING_ERROR_EVENT, |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | const char* UpdateStatusToString(UpdateStatus status); |
| 37 | |
| 38 | class UpdateAttempter : public ActionProcessorDelegate, |
| 39 | public DownloadActionDelegate { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 40 | public: |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 41 | UpdateAttempter(MetricsLibraryInterface* metrics_lib) |
| 42 | : dbus_service_(NULL), |
| 43 | metrics_lib_(metrics_lib), |
| 44 | status_(UPDATE_STATUS_IDLE), |
| 45 | download_progress_(0.0), |
| 46 | last_checked_time_(0), |
| 47 | new_version_("0.0.0.0"), |
| 48 | new_size_(0) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 49 | last_notify_time_.tv_sec = 0; |
| 50 | last_notify_time_.tv_nsec = 0; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 51 | if (utils::FileExists(kUpdateCompletedMarker)) |
| 52 | status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 53 | } |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 54 | // Checks for update and, if a newer version is available, attempts |
| 55 | // to update the system. Non-empty |in_app_version| or |
| 56 | // |in_update_url| prevents automatic detection of the parameter. |
| 57 | void Update(const std::string& app_version, 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 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 80 | void set_dbus_service(struct UpdateEngineService* dbus_service) { |
| 81 | dbus_service_ = dbus_service; |
| 82 | } |
| 83 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 84 | // This is the D-Bus service entry point for going through an |
| 85 | // update. If the current status is idle invokes Update. |
| 86 | void CheckForUpdate(const std::string& app_version, |
| 87 | const std::string& omaha_url); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 88 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 89 | // Initiates a reboot if the current state is |
| 90 | // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise. |
| 91 | bool RebootIfNeeded(); |
| 92 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 93 | // DownloadActionDelegate method |
| 94 | void BytesReceived(uint64_t bytes_received, uint64_t total); |
| 95 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 96 | private: |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 97 | // Sets the status to the given status and notifies a status update |
| 98 | // over dbus. |
| 99 | void SetStatusAndNotify(UpdateStatus status); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 100 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 101 | // Creates an error event object in |error_event_| to be included in |
| 102 | // an OmahaRequestAction once the current action processor is done. |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 103 | void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 104 | |
| 105 | // If there's a pending error event allocated in |error_event_|, |
| 106 | // schedules an OmahaRequestAction with that event in the current |
| 107 | // processor, clears the pending event, updates the status and |
| 108 | // returns true. Returns false otherwise. |
| 109 | bool ScheduleErrorEventAction(); |
| 110 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 111 | struct timespec last_notify_time_; |
| 112 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 113 | std::vector<std::tr1::shared_ptr<AbstractAction> > actions_; |
| 114 | ActionProcessor processor_; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 115 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 116 | // If non-null, this UpdateAttempter will send status updates over this |
| 117 | // dbus service. |
| 118 | UpdateEngineService* dbus_service_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 119 | |
| 120 | // pointer to the OmahaResponseHandlerAction in the actions_ vector; |
| 121 | std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 122 | |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 123 | // Pointer to the UMA metrics collection library. |
| 124 | MetricsLibraryInterface* metrics_lib_; |
| 125 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 126 | // Pending error event, if any. |
| 127 | scoped_ptr<OmahaEvent> error_event_; |
| 128 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 129 | // For status: |
| 130 | UpdateStatus status_; |
| 131 | double download_progress_; |
| 132 | int64_t last_checked_time_; |
| 133 | std::string new_version_; |
| 134 | int64_t new_size_; |
| 135 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 136 | // Device paramaters common to all Omaha requests. |
| 137 | OmahaRequestDeviceParams omaha_request_params_; |
| 138 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 139 | DISALLOW_COPY_AND_ASSIGN(UpdateAttempter); |
| 140 | }; |
| 141 | |
| 142 | } // namespace chromeos_update_engine |
| 143 | |
| 144 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__ |