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 | #include "update_engine/update_attempter.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 6 | |
| 7 | // From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L |
| 8 | #ifndef _POSIX_C_SOURCE |
| 9 | #define _POSIX_C_SOURCE 199309L |
| 10 | #endif // _POSIX_C_SOURCE |
| 11 | #include <time.h> |
| 12 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 13 | #include <tr1/memory> |
| 14 | #include <string> |
| 15 | #include <vector> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 16 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 17 | #include <glib.h> |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 18 | |
| 19 | #include "metrics/metrics_library.h" |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 20 | #include "update_engine/dbus_service.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 21 | #include "update_engine/download_action.h" |
| 22 | #include "update_engine/filesystem_copier_action.h" |
| 23 | #include "update_engine/libcurl_http_fetcher.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 24 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 25 | #include "update_engine/omaha_request_params.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 26 | #include "update_engine/omaha_response_handler_action.h" |
| 27 | #include "update_engine/postinstall_runner_action.h" |
| 28 | #include "update_engine/set_bootable_flag_action.h" |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 29 | |
| 30 | using std::tr1::shared_ptr; |
| 31 | using std::string; |
| 32 | using std::vector; |
| 33 | |
| 34 | namespace chromeos_update_engine { |
| 35 | |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 36 | const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed"; |
| 37 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 38 | namespace { |
| 39 | // Returns true on success. |
| 40 | bool GetCPUClockTime(struct timespec* out) { |
| 41 | return clock_gettime(CLOCK_REALTIME, out) == 0; |
| 42 | } |
| 43 | // Returns stop - start. |
| 44 | struct timespec CPUClockTimeElapsed(const struct timespec& start, |
| 45 | const struct timespec& stop) { |
| 46 | CHECK(start.tv_sec >= 0); |
| 47 | CHECK(stop.tv_sec >= 0); |
| 48 | CHECK(start.tv_nsec >= 0); |
| 49 | CHECK(stop.tv_nsec >= 0); |
| 50 | |
| 51 | const int64_t kOneBillion = 1000000000L; |
| 52 | const int64_t start64 = start.tv_sec * kOneBillion + start.tv_nsec; |
| 53 | const int64_t stop64 = stop.tv_sec * kOneBillion + stop.tv_nsec; |
| 54 | |
| 55 | const int64_t result64 = stop64 - start64; |
| 56 | |
| 57 | struct timespec ret; |
| 58 | ret.tv_sec = result64 / kOneBillion; |
| 59 | ret.tv_nsec = result64 % kOneBillion; |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | bool CPUClockTimeGreaterThanHalfSecond(const struct timespec& spec) { |
| 64 | if (spec.tv_sec >= 1) |
| 65 | return true; |
| 66 | return (spec.tv_nsec > 500000000); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | const char* UpdateStatusToString(UpdateStatus status) { |
| 71 | switch (status) { |
| 72 | case UPDATE_STATUS_IDLE: |
| 73 | return "UPDATE_STATUS_IDLE"; |
| 74 | case UPDATE_STATUS_CHECKING_FOR_UPDATE: |
| 75 | return "UPDATE_STATUS_CHECKING_FOR_UPDATE"; |
| 76 | case UPDATE_STATUS_UPDATE_AVAILABLE: |
| 77 | return "UPDATE_STATUS_UPDATE_AVAILABLE"; |
| 78 | case UPDATE_STATUS_DOWNLOADING: |
| 79 | return "UPDATE_STATUS_DOWNLOADING"; |
| 80 | case UPDATE_STATUS_VERIFYING: |
| 81 | return "UPDATE_STATUS_VERIFYING"; |
| 82 | case UPDATE_STATUS_FINALIZING: |
| 83 | return "UPDATE_STATUS_FINALIZING"; |
| 84 | case UPDATE_STATUS_UPDATED_NEED_REBOOT: |
| 85 | return "UPDATE_STATUS_UPDATED_NEED_REBOOT"; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 86 | case UPDATE_STATUS_REPORTING_ERROR_EVENT: |
| 87 | return "UPDATE_STATUS_REPORTING_ERROR_EVENT"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 88 | default: |
| 89 | return "unknown status"; |
| 90 | } |
| 91 | } |
| 92 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 93 | // Turns a generic kActionCodeError to a generic error code specific |
| 94 | // to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is |
| 95 | // not kActionCodeError, or the action is not matched, returns |code| |
| 96 | // unchanged. |
| 97 | ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 98 | ActionExitCode code) { |
| 99 | if (code != kActionCodeError) |
| 100 | return code; |
| 101 | |
| 102 | const string type = action->Type(); |
| 103 | if (type == OmahaRequestAction::StaticType()) |
| 104 | return kActionCodeOmahaRequestError; |
| 105 | if (type == OmahaResponseHandlerAction::StaticType()) |
| 106 | return kActionCodeOmahaResponseHandlerError; |
| 107 | if (type == FilesystemCopierAction::StaticType()) |
| 108 | return kActionCodeFilesystemCopierError; |
| 109 | if (type == PostinstallRunnerAction::StaticType()) |
| 110 | return kActionCodePostinstallRunnerError; |
| 111 | if (type == SetBootableFlagAction::StaticType()) |
| 112 | return kActionCodeSetBootableFlagError; |
| 113 | |
| 114 | return code; |
| 115 | } |
| 116 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 117 | void UpdateAttempter::Update(const std::string& app_version, |
| 118 | const std::string& omaha_url) { |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 119 | if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 120 | LOG(INFO) << "Not updating b/c we already updated and we're waiting for " |
| 121 | << "reboot"; |
| 122 | return; |
| 123 | } |
| 124 | if (status_ != UPDATE_STATUS_IDLE) { |
| 125 | // Update in progress. Do nothing |
| 126 | return; |
| 127 | } |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 128 | if (!omaha_request_params_.Init(app_version, omaha_url)) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 129 | LOG(ERROR) << "Unable to initialize Omaha request device params."; |
| 130 | return; |
| 131 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 132 | CHECK(!processor_.IsRunning()); |
| 133 | processor_.set_delegate(this); |
| 134 | |
| 135 | // Actions: |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 136 | shared_ptr<OmahaRequestAction> update_check_action( |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 137 | new OmahaRequestAction(omaha_request_params_, |
| 138 | NULL, |
| 139 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 140 | shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
| 141 | new OmahaResponseHandlerAction); |
| 142 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 143 | new FilesystemCopierAction(false)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 144 | shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 145 | new FilesystemCopierAction(true)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 146 | shared_ptr<OmahaRequestAction> download_started_action( |
| 147 | new OmahaRequestAction(omaha_request_params_, |
| 148 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 149 | OmahaEvent::kTypeUpdateDownloadStarted), |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 150 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 151 | shared_ptr<DownloadAction> download_action( |
| 152 | new DownloadAction(new LibcurlHttpFetcher)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 153 | shared_ptr<OmahaRequestAction> download_finished_action( |
| 154 | new OmahaRequestAction(omaha_request_params_, |
| 155 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 156 | OmahaEvent::kTypeUpdateDownloadFinished), |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 157 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 158 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit( |
| 159 | new PostinstallRunnerAction(true)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 160 | shared_ptr<SetBootableFlagAction> set_bootable_flag_action( |
| 161 | new SetBootableFlagAction); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 162 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit( |
| 163 | new PostinstallRunnerAction(false)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 164 | shared_ptr<OmahaRequestAction> update_complete_action( |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 165 | new OmahaRequestAction(omaha_request_params_, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 166 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 167 | new LibcurlHttpFetcher)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 168 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 169 | download_action->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 170 | response_handler_action_ = response_handler_action; |
| 171 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 172 | actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
| 173 | actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
| 174 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 175 | actions_.push_back(shared_ptr<AbstractAction>( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 176 | kernel_filesystem_copier_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 177 | actions_.push_back(shared_ptr<AbstractAction>(download_started_action)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 178 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 179 | actions_.push_back(shared_ptr<AbstractAction>(download_finished_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 180 | actions_.push_back(shared_ptr<AbstractAction>( |
| 181 | postinstall_runner_action_precommit)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 182 | actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 183 | actions_.push_back(shared_ptr<AbstractAction>( |
| 184 | postinstall_runner_action_postcommit)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 185 | actions_.push_back(shared_ptr<AbstractAction>(update_complete_action)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 186 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 187 | // Enqueue the actions |
| 188 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 189 | it != actions_.end(); ++it) { |
| 190 | processor_.EnqueueAction(it->get()); |
| 191 | } |
| 192 | |
| 193 | // Bond them together. We have to use the leaf-types when calling |
| 194 | // BondActions(). |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 195 | BondActions(update_check_action.get(), |
| 196 | response_handler_action.get()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 197 | BondActions(response_handler_action.get(), |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 198 | filesystem_copier_action.get()); |
| 199 | BondActions(filesystem_copier_action.get(), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 200 | kernel_filesystem_copier_action.get()); |
| 201 | BondActions(kernel_filesystem_copier_action.get(), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 202 | download_action.get()); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 203 | BondActions(download_action.get(), |
| 204 | postinstall_runner_action_precommit.get()); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 205 | BondActions(postinstall_runner_action_precommit.get(), |
| 206 | set_bootable_flag_action.get()); |
| 207 | BondActions(set_bootable_flag_action.get(), |
| 208 | postinstall_runner_action_postcommit.get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 209 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 210 | SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 211 | processor_.StartProcessing(); |
| 212 | } |
| 213 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 214 | void UpdateAttempter::CheckForUpdate(const std::string& app_version, |
| 215 | const std::string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 216 | if (status_ != UPDATE_STATUS_IDLE) { |
| 217 | LOG(INFO) << "Check for update requested, but status is " |
| 218 | << UpdateStatusToString(status_) << ", so not checking."; |
| 219 | return; |
| 220 | } |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 221 | Update(app_version, omaha_url); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame^] | 224 | bool UpdateAttempter::RebootIfNeeded() { |
| 225 | if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 226 | LOG(INFO) << "Reboot requested, but status is " |
| 227 | << UpdateStatusToString(status_) << ", so not rebooting."; |
| 228 | return false; |
| 229 | } |
| 230 | TEST_AND_RETURN_FALSE(utils::Reboot()); |
| 231 | return true; |
| 232 | } |
| 233 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 234 | // Delegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 235 | void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 236 | ActionExitCode code) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 237 | CHECK(response_handler_action_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 238 | LOG(INFO) << "Processing Done."; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 239 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 240 | |
| 241 | if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 242 | LOG(INFO) << "Error event sent."; |
| 243 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
| 244 | return; |
| 245 | } |
| 246 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 247 | if (code == kActionCodeSuccess) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 248 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 249 | utils::WriteFile(kUpdateCompletedMarker, "", 0); |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 250 | |
| 251 | // Report the time it took to update the system. |
| 252 | int64_t update_time = time(NULL) - last_checked_time_; |
| 253 | metrics_lib_->SendToUMA("Installer.UpdateTime", |
| 254 | static_cast<int>(update_time), // sample |
| 255 | 1, // min = 1 second |
| 256 | 20 * 60, // max = 20 minutes |
| 257 | 50); // buckets |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 258 | return; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 259 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 260 | |
| 261 | LOG(INFO) << "Update failed."; |
| 262 | if (ScheduleErrorEventAction()) |
| 263 | return; |
| 264 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { |
| 268 | download_progress_ = 0.0; |
| 269 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 270 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 271 | error_event_.reset(NULL); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Called whenever an action has finished processing, either successfully |
| 275 | // or otherwise. |
| 276 | void UpdateAttempter::ActionCompleted(ActionProcessor* processor, |
| 277 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 278 | ActionExitCode code) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 279 | // Reset download progress regardless of whether or not the download action |
| 280 | // succeeded. |
| 281 | const string type = action->Type(); |
| 282 | if (type == DownloadAction::StaticType()) |
| 283 | download_progress_ = 0.0; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 284 | if (code != kActionCodeSuccess) { |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 285 | // On failure, schedule an error event to be sent to Omaha. |
| 286 | CreatePendingErrorEvent(action, code); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 287 | return; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 288 | } |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 289 | // Find out which action completed. |
| 290 | if (type == OmahaResponseHandlerAction::StaticType()) { |
| 291 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 292 | OmahaResponseHandlerAction* omaha_response_handler_action = |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 293 | dynamic_cast<OmahaResponseHandlerAction*>(action); |
| 294 | CHECK(omaha_response_handler_action); |
| 295 | const InstallPlan& plan = omaha_response_handler_action->install_plan(); |
| 296 | last_checked_time_ = time(NULL); |
| 297 | // TODO(adlr): put version in InstallPlan |
| 298 | new_version_ = "0.0.0.0"; |
| 299 | new_size_ = plan.size; |
| 300 | } else if (type == DownloadAction::StaticType()) { |
| 301 | SetStatusAndNotify(UPDATE_STATUS_FINALIZING); |
| 302 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Stop updating. An attempt will be made to record status to the disk |
| 306 | // so that updates can be resumed later. |
| 307 | void UpdateAttempter::Terminate() { |
| 308 | // TODO(adlr): implement this method. |
| 309 | NOTIMPLEMENTED(); |
| 310 | } |
| 311 | |
| 312 | // Try to resume from a previously Terminate()d update. |
| 313 | void UpdateAttempter::ResumeUpdating() { |
| 314 | // TODO(adlr): implement this method. |
| 315 | NOTIMPLEMENTED(); |
| 316 | } |
| 317 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 318 | void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { |
| 319 | if (status_ != UPDATE_STATUS_DOWNLOADING) { |
| 320 | LOG(ERROR) << "BytesReceived called while not downloading."; |
| 321 | return; |
| 322 | } |
| 323 | download_progress_ = static_cast<double>(bytes_received) / |
| 324 | static_cast<double>(total); |
| 325 | // We self throttle here |
| 326 | timespec now; |
| 327 | now.tv_sec = 0; |
| 328 | now.tv_nsec = 0; |
| 329 | if (GetCPUClockTime(&now) && |
| 330 | CPUClockTimeGreaterThanHalfSecond( |
| 331 | CPUClockTimeElapsed(last_notify_time_, now))) { |
| 332 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
| 333 | } |
| 334 | } |
| 335 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 336 | bool UpdateAttempter::GetStatus(int64_t* last_checked_time, |
| 337 | double* progress, |
| 338 | std::string* current_operation, |
| 339 | std::string* new_version, |
| 340 | int64_t* new_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 341 | *last_checked_time = last_checked_time_; |
| 342 | *progress = download_progress_; |
| 343 | *current_operation = UpdateStatusToString(status_); |
| 344 | *new_version = new_version_; |
| 345 | *new_size = new_size_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 349 | void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) { |
| 350 | status_ = status; |
| 351 | if (!dbus_service_) |
| 352 | return; |
| 353 | GetCPUClockTime(&last_notify_time_); |
| 354 | update_engine_service_emit_status_update( |
| 355 | dbus_service_, |
| 356 | last_checked_time_, |
| 357 | download_progress_, |
| 358 | UpdateStatusToString(status_), |
| 359 | new_version_.c_str(), |
| 360 | new_size_); |
| 361 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 362 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 363 | void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action, |
| 364 | ActionExitCode code) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 365 | if (error_event_.get()) { |
| 366 | // This shouldn't really happen. |
| 367 | LOG(WARNING) << "There's already an existing pending error event."; |
| 368 | return; |
| 369 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 370 | |
| 371 | // For now assume that Omaha response action failure means that |
| 372 | // there's no update so don't send an event. Also, double check that |
| 373 | // the failure has not occurred while sending an error event -- in |
| 374 | // which case don't schedule another. This shouldn't really happen |
| 375 | // but just in case... |
| 376 | if (action->Type() == OmahaResponseHandlerAction::StaticType() || |
| 377 | status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | code = GetErrorCodeForAction(action, code); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 382 | error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 383 | OmahaEvent::kResultError, |
| 384 | code)); |
| 385 | } |
| 386 | |
| 387 | bool UpdateAttempter::ScheduleErrorEventAction() { |
| 388 | if (error_event_.get() == NULL) |
| 389 | return false; |
| 390 | |
| 391 | shared_ptr<OmahaRequestAction> error_event_action( |
| 392 | new OmahaRequestAction(omaha_request_params_, |
| 393 | error_event_.release(), // Pass ownership. |
| 394 | new LibcurlHttpFetcher)); |
| 395 | actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); |
| 396 | processor_.EnqueueAction(error_event_action.get()); |
| 397 | SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT); |
| 398 | processor_.StartProcessing(); |
| 399 | return true; |
| 400 | } |
| 401 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 402 | } // namespace chromeos_update_engine |