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" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 15 | #include "update_engine/omaha_response_handler_action.h" |
| 16 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 17 | struct UpdateEngineService; |
| 18 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 19 | namespace chromeos_update_engine { |
| 20 | |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame^] | 21 | extern const char* kUpdateCompletedMarker; |
| 22 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 23 | enum UpdateStatus { |
| 24 | UPDATE_STATUS_IDLE = 0, |
| 25 | UPDATE_STATUS_CHECKING_FOR_UPDATE, |
| 26 | UPDATE_STATUS_UPDATE_AVAILABLE, |
| 27 | UPDATE_STATUS_DOWNLOADING, |
| 28 | UPDATE_STATUS_VERIFYING, |
| 29 | UPDATE_STATUS_FINALIZING, |
| 30 | UPDATE_STATUS_UPDATED_NEED_REBOOT |
| 31 | }; |
| 32 | |
| 33 | const char* UpdateStatusToString(UpdateStatus status); |
| 34 | |
| 35 | class UpdateAttempter : public ActionProcessorDelegate, |
| 36 | public DownloadActionDelegate { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 37 | public: |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 38 | UpdateAttempter() : full_update_(false), |
| 39 | dbus_service_(NULL), |
| 40 | status_(UPDATE_STATUS_IDLE), |
| 41 | download_progress_(0.0), |
| 42 | last_checked_time_(0), |
| 43 | new_version_("0.0.0.0"), |
| 44 | new_size_(0) { |
| 45 | last_notify_time_.tv_sec = 0; |
| 46 | last_notify_time_.tv_nsec = 0; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame^] | 47 | if (utils::FileExists(kUpdateCompletedMarker)) |
| 48 | status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 49 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 50 | void Update(bool force_full_update); |
| 51 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 52 | // ActionProcessorDelegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 53 | void ProcessingDone(const ActionProcessor* processor, bool success); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 54 | void ProcessingStopped(const ActionProcessor* processor); |
| 55 | void ActionCompleted(ActionProcessor* processor, |
| 56 | AbstractAction* action, |
| 57 | bool success); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 58 | |
| 59 | // Stop updating. An attempt will be made to record status to the disk |
| 60 | // so that updates can be resumed later. |
| 61 | void Terminate(); |
| 62 | |
| 63 | // Try to resume from a previously Terminate()d update. |
| 64 | void ResumeUpdating(); |
| 65 | |
| 66 | // Returns the current status in the out params. Returns true on success. |
| 67 | bool GetStatus(int64_t* last_checked_time, |
| 68 | double* progress, |
| 69 | std::string* current_operation, |
| 70 | std::string* new_version, |
| 71 | int64_t* new_size); |
| 72 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 73 | void set_dbus_service(struct UpdateEngineService* dbus_service) { |
| 74 | dbus_service_ = dbus_service; |
| 75 | } |
| 76 | |
| 77 | void CheckForUpdate(); |
| 78 | |
| 79 | // DownloadActionDelegate method |
| 80 | void BytesReceived(uint64_t bytes_received, uint64_t total); |
| 81 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 82 | private: |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 83 | // Sets the status to the given status and notifies a status update |
| 84 | // over dbus. |
| 85 | void SetStatusAndNotify(UpdateStatus status); |
| 86 | |
| 87 | struct timespec last_notify_time_; |
| 88 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 89 | bool full_update_; |
| 90 | std::vector<std::tr1::shared_ptr<AbstractAction> > actions_; |
| 91 | ActionProcessor processor_; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 92 | |
| 93 | // If non-null, this UpdateAttempter will send status updates over this |
| 94 | // dbus service. |
| 95 | UpdateEngineService* dbus_service_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 96 | |
| 97 | // pointer to the OmahaResponseHandlerAction in the actions_ vector; |
| 98 | std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 99 | |
| 100 | // For status: |
| 101 | UpdateStatus status_; |
| 102 | double download_progress_; |
| 103 | int64_t last_checked_time_; |
| 104 | std::string new_version_; |
| 105 | int64_t new_size_; |
| 106 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 107 | DISALLOW_COPY_AND_ASSIGN(UpdateAttempter); |
| 108 | }; |
| 109 | |
| 110 | } // namespace chromeos_update_engine |
| 111 | |
| 112 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__ |