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 | |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 30 | using base::TimeDelta; |
| 31 | using base::TimeTicks; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 32 | using std::tr1::shared_ptr; |
| 33 | using std::string; |
| 34 | using std::vector; |
| 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 38 | const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed"; |
| 39 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 40 | const char* UpdateStatusToString(UpdateStatus status) { |
| 41 | switch (status) { |
| 42 | case UPDATE_STATUS_IDLE: |
| 43 | return "UPDATE_STATUS_IDLE"; |
| 44 | case UPDATE_STATUS_CHECKING_FOR_UPDATE: |
| 45 | return "UPDATE_STATUS_CHECKING_FOR_UPDATE"; |
| 46 | case UPDATE_STATUS_UPDATE_AVAILABLE: |
| 47 | return "UPDATE_STATUS_UPDATE_AVAILABLE"; |
| 48 | case UPDATE_STATUS_DOWNLOADING: |
| 49 | return "UPDATE_STATUS_DOWNLOADING"; |
| 50 | case UPDATE_STATUS_VERIFYING: |
| 51 | return "UPDATE_STATUS_VERIFYING"; |
| 52 | case UPDATE_STATUS_FINALIZING: |
| 53 | return "UPDATE_STATUS_FINALIZING"; |
| 54 | case UPDATE_STATUS_UPDATED_NEED_REBOOT: |
| 55 | return "UPDATE_STATUS_UPDATED_NEED_REBOOT"; |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 56 | case UPDATE_STATUS_REPORTING_ERROR_EVENT: |
| 57 | return "UPDATE_STATUS_REPORTING_ERROR_EVENT"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 58 | default: |
| 59 | return "unknown status"; |
| 60 | } |
| 61 | } |
| 62 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 63 | // Turns a generic kActionCodeError to a generic error code specific |
| 64 | // to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is |
| 65 | // not kActionCodeError, or the action is not matched, returns |code| |
| 66 | // unchanged. |
| 67 | ActionExitCode GetErrorCodeForAction(AbstractAction* action, |
| 68 | ActionExitCode code) { |
| 69 | if (code != kActionCodeError) |
| 70 | return code; |
| 71 | |
| 72 | const string type = action->Type(); |
| 73 | if (type == OmahaRequestAction::StaticType()) |
| 74 | return kActionCodeOmahaRequestError; |
| 75 | if (type == OmahaResponseHandlerAction::StaticType()) |
| 76 | return kActionCodeOmahaResponseHandlerError; |
| 77 | if (type == FilesystemCopierAction::StaticType()) |
| 78 | return kActionCodeFilesystemCopierError; |
| 79 | if (type == PostinstallRunnerAction::StaticType()) |
| 80 | return kActionCodePostinstallRunnerError; |
| 81 | if (type == SetBootableFlagAction::StaticType()) |
| 82 | return kActionCodeSetBootableFlagError; |
| 83 | |
| 84 | return code; |
| 85 | } |
| 86 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 87 | UpdateAttempter::UpdateAttempter(PrefsInterface* prefs, |
| 88 | MetricsLibraryInterface* metrics_lib) |
| 89 | : dbus_service_(NULL), |
| 90 | prefs_(prefs), |
| 91 | metrics_lib_(metrics_lib), |
| 92 | priority_(utils::kProcessPriorityNormal), |
| 93 | manage_priority_source_(NULL), |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 94 | download_active_(false), |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 95 | status_(UPDATE_STATUS_IDLE), |
| 96 | download_progress_(0.0), |
| 97 | last_checked_time_(0), |
| 98 | new_version_("0.0.0.0"), |
| 99 | new_size_(0) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 100 | if (utils::FileExists(kUpdateCompletedMarker)) |
| 101 | status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT; |
| 102 | } |
| 103 | |
| 104 | UpdateAttempter::~UpdateAttempter() { |
| 105 | CleanupPriorityManagement(); |
| 106 | } |
| 107 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 108 | void UpdateAttempter::Update(const std::string& app_version, |
| 109 | const std::string& omaha_url) { |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 110 | if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 111 | LOG(INFO) << "Not updating b/c we already updated and we're waiting for " |
| 112 | << "reboot"; |
| 113 | return; |
| 114 | } |
| 115 | if (status_ != UPDATE_STATUS_IDLE) { |
| 116 | // Update in progress. Do nothing |
| 117 | return; |
| 118 | } |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 119 | if (!omaha_request_params_.Init(app_version, omaha_url)) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 120 | LOG(ERROR) << "Unable to initialize Omaha request device params."; |
| 121 | return; |
| 122 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 123 | CHECK(!processor_.IsRunning()); |
| 124 | processor_.set_delegate(this); |
| 125 | |
| 126 | // Actions: |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 127 | shared_ptr<OmahaRequestAction> update_check_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 128 | new OmahaRequestAction(prefs_, |
| 129 | omaha_request_params_, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 130 | NULL, |
| 131 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 132 | shared_ptr<OmahaResponseHandlerAction> response_handler_action( |
| 133 | new OmahaResponseHandlerAction); |
| 134 | shared_ptr<FilesystemCopierAction> filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 135 | new FilesystemCopierAction(false)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 136 | shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action( |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 137 | new FilesystemCopierAction(true)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 138 | shared_ptr<OmahaRequestAction> download_started_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 139 | new OmahaRequestAction(prefs_, |
| 140 | omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 141 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 142 | OmahaEvent::kTypeUpdateDownloadStarted), |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 143 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 144 | shared_ptr<DownloadAction> download_action( |
| 145 | new DownloadAction(new LibcurlHttpFetcher)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 146 | shared_ptr<OmahaRequestAction> download_finished_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 147 | new OmahaRequestAction(prefs_, |
| 148 | omaha_request_params_, |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 149 | new OmahaEvent( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 150 | OmahaEvent::kTypeUpdateDownloadFinished), |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 151 | new LibcurlHttpFetcher)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 152 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit( |
| 153 | new PostinstallRunnerAction(true)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 154 | shared_ptr<SetBootableFlagAction> set_bootable_flag_action( |
| 155 | new SetBootableFlagAction); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 156 | shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit( |
| 157 | new PostinstallRunnerAction(false)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 158 | shared_ptr<OmahaRequestAction> update_complete_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 159 | new OmahaRequestAction(prefs_, |
| 160 | omaha_request_params_, |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 161 | new OmahaEvent(OmahaEvent::kTypeUpdateComplete), |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 162 | new LibcurlHttpFetcher)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 163 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 164 | download_action->set_delegate(this); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 165 | response_handler_action_ = response_handler_action; |
| 166 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 167 | actions_.push_back(shared_ptr<AbstractAction>(update_check_action)); |
| 168 | actions_.push_back(shared_ptr<AbstractAction>(response_handler_action)); |
| 169 | actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action)); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 170 | actions_.push_back(shared_ptr<AbstractAction>( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 171 | kernel_filesystem_copier_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 172 | actions_.push_back(shared_ptr<AbstractAction>(download_started_action)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 173 | actions_.push_back(shared_ptr<AbstractAction>(download_action)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 174 | actions_.push_back(shared_ptr<AbstractAction>(download_finished_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 175 | actions_.push_back(shared_ptr<AbstractAction>( |
| 176 | postinstall_runner_action_precommit)); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 177 | actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action)); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 178 | actions_.push_back(shared_ptr<AbstractAction>( |
| 179 | postinstall_runner_action_postcommit)); |
Darin Petkov | 8c2980e | 2010-07-16 15:16:49 -0700 | [diff] [blame] | 180 | actions_.push_back(shared_ptr<AbstractAction>(update_complete_action)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 181 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 182 | // Enqueue the actions |
| 183 | for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin(); |
| 184 | it != actions_.end(); ++it) { |
| 185 | processor_.EnqueueAction(it->get()); |
| 186 | } |
| 187 | |
| 188 | // Bond them together. We have to use the leaf-types when calling |
| 189 | // BondActions(). |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 190 | BondActions(update_check_action.get(), |
| 191 | response_handler_action.get()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 192 | BondActions(response_handler_action.get(), |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 193 | filesystem_copier_action.get()); |
| 194 | BondActions(filesystem_copier_action.get(), |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 195 | kernel_filesystem_copier_action.get()); |
| 196 | BondActions(kernel_filesystem_copier_action.get(), |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 197 | download_action.get()); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 198 | BondActions(download_action.get(), |
| 199 | postinstall_runner_action_precommit.get()); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 200 | BondActions(postinstall_runner_action_precommit.get(), |
| 201 | set_bootable_flag_action.get()); |
| 202 | BondActions(set_bootable_flag_action.get(), |
| 203 | postinstall_runner_action_postcommit.get()); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 204 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 205 | SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE); |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 206 | processor_.StartProcessing(); |
| 207 | } |
| 208 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 209 | void UpdateAttempter::CheckForUpdate(const std::string& app_version, |
| 210 | const std::string& omaha_url) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 211 | if (status_ != UPDATE_STATUS_IDLE) { |
| 212 | LOG(INFO) << "Check for update requested, but status is " |
| 213 | << UpdateStatusToString(status_) << ", so not checking."; |
| 214 | return; |
| 215 | } |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 216 | Update(app_version, omaha_url); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 219 | bool UpdateAttempter::RebootIfNeeded() { |
| 220 | if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 221 | LOG(INFO) << "Reboot requested, but status is " |
| 222 | << UpdateStatusToString(status_) << ", so not rebooting."; |
| 223 | return false; |
| 224 | } |
| 225 | TEST_AND_RETURN_FALSE(utils::Reboot()); |
| 226 | return true; |
| 227 | } |
| 228 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 229 | // Delegate methods: |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 230 | void UpdateAttempter::ProcessingDone(const ActionProcessor* processor, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 231 | ActionExitCode code) { |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 232 | CHECK(response_handler_action_); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 233 | LOG(INFO) << "Processing Done."; |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 234 | actions_.clear(); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 235 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 236 | // Reset process priority back to normal. |
| 237 | CleanupPriorityManagement(); |
| 238 | |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 239 | if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 240 | LOG(INFO) << "Error event sent."; |
| 241 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
| 242 | return; |
| 243 | } |
| 244 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 245 | if (code == kActionCodeSuccess) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 246 | SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT); |
Andrew de los Reyes | 6b78e29 | 2010-05-10 15:54:39 -0700 | [diff] [blame] | 247 | utils::WriteFile(kUpdateCompletedMarker, "", 0); |
Darin Petkov | 9d65b7b | 2010-07-20 09:13:01 -0700 | [diff] [blame] | 248 | |
| 249 | // Report the time it took to update the system. |
| 250 | int64_t update_time = time(NULL) - last_checked_time_; |
| 251 | metrics_lib_->SendToUMA("Installer.UpdateTime", |
| 252 | static_cast<int>(update_time), // sample |
| 253 | 1, // min = 1 second |
| 254 | 20 * 60, // max = 20 minutes |
| 255 | 50); // buckets |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 256 | return; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 257 | } |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 258 | |
| 259 | LOG(INFO) << "Update failed."; |
| 260 | if (ScheduleErrorEventAction()) |
| 261 | return; |
| 262 | SetStatusAndNotify(UPDATE_STATUS_IDLE); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) { |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 266 | // Reset process priority back to normal. |
| 267 | CleanupPriorityManagement(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 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()) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 291 | // Note that the status will be updated to DOWNLOADING when some |
| 292 | // bytes get actually downloaded from the server and the |
| 293 | // BytesReceived callback is invoked. This avoids notifying the |
| 294 | // user that a download has started in cases when the server and |
| 295 | // the client are unable to initiate the download. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 296 | OmahaResponseHandlerAction* omaha_response_handler_action = |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 297 | dynamic_cast<OmahaResponseHandlerAction*>(action); |
| 298 | CHECK(omaha_response_handler_action); |
| 299 | const InstallPlan& plan = omaha_response_handler_action->install_plan(); |
| 300 | last_checked_time_ = time(NULL); |
| 301 | // TODO(adlr): put version in InstallPlan |
| 302 | new_version_ = "0.0.0.0"; |
| 303 | new_size_ = plan.size; |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 304 | SetupPriorityManagement(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 305 | } else if (type == DownloadAction::StaticType()) { |
| 306 | SetStatusAndNotify(UPDATE_STATUS_FINALIZING); |
| 307 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Stop updating. An attempt will be made to record status to the disk |
| 311 | // so that updates can be resumed later. |
| 312 | void UpdateAttempter::Terminate() { |
| 313 | // TODO(adlr): implement this method. |
| 314 | NOTIMPLEMENTED(); |
| 315 | } |
| 316 | |
| 317 | // Try to resume from a previously Terminate()d update. |
| 318 | void UpdateAttempter::ResumeUpdating() { |
| 319 | // TODO(adlr): implement this method. |
| 320 | NOTIMPLEMENTED(); |
| 321 | } |
| 322 | |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 323 | void UpdateAttempter::SetDownloadStatus(bool active) { |
| 324 | download_active_ = active; |
| 325 | LOG(INFO) << "Download status: " << (active ? "active" : "inactive"); |
| 326 | } |
| 327 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 328 | void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) { |
Darin Petkov | 9d911fa | 2010-08-19 09:36:08 -0700 | [diff] [blame] | 329 | if (!download_active_) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 330 | LOG(ERROR) << "BytesReceived called while not downloading."; |
| 331 | return; |
| 332 | } |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 333 | double progress = static_cast<double>(bytes_received) / |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 334 | static_cast<double>(total); |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 335 | // Self throttle based on progress. Also send notifications if |
| 336 | // progress is too slow. |
| 337 | const double kDeltaPercent = 0.01; // 1% |
| 338 | if (status_ != UPDATE_STATUS_DOWNLOADING || |
| 339 | bytes_received == total || |
| 340 | progress - download_progress_ >= kDeltaPercent || |
| 341 | TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) { |
| 342 | download_progress_ = progress; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 343 | SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING); |
| 344 | } |
| 345 | } |
| 346 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 347 | bool UpdateAttempter::GetStatus(int64_t* last_checked_time, |
| 348 | double* progress, |
| 349 | std::string* current_operation, |
| 350 | std::string* new_version, |
| 351 | int64_t* new_size) { |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 352 | *last_checked_time = last_checked_time_; |
| 353 | *progress = download_progress_; |
| 354 | *current_operation = UpdateStatusToString(status_); |
| 355 | *new_version = new_version_; |
| 356 | *new_size = new_size_; |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 357 | return true; |
| 358 | } |
| 359 | |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 360 | void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) { |
| 361 | status_ = status; |
| 362 | if (!dbus_service_) |
| 363 | return; |
Darin Petkov | af18305 | 2010-08-23 12:07:13 -0700 | [diff] [blame] | 364 | last_notify_time_ = TimeTicks::Now(); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 365 | update_engine_service_emit_status_update( |
| 366 | dbus_service_, |
| 367 | last_checked_time_, |
| 368 | download_progress_, |
| 369 | UpdateStatusToString(status_), |
| 370 | new_version_.c_str(), |
| 371 | new_size_); |
| 372 | } |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 373 | |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 374 | void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action, |
| 375 | ActionExitCode code) { |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 376 | if (error_event_.get()) { |
| 377 | // This shouldn't really happen. |
| 378 | LOG(WARNING) << "There's already an existing pending error event."; |
| 379 | return; |
| 380 | } |
Darin Petkov | 777dbfa | 2010-07-20 15:03:37 -0700 | [diff] [blame] | 381 | |
| 382 | // For now assume that Omaha response action failure means that |
| 383 | // there's no update so don't send an event. Also, double check that |
| 384 | // the failure has not occurred while sending an error event -- in |
| 385 | // which case don't schedule another. This shouldn't really happen |
| 386 | // but just in case... |
| 387 | if (action->Type() == OmahaResponseHandlerAction::StaticType() || |
| 388 | status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) { |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | code = GetErrorCodeForAction(action, code); |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 393 | error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete, |
| 394 | OmahaEvent::kResultError, |
| 395 | code)); |
| 396 | } |
| 397 | |
| 398 | bool UpdateAttempter::ScheduleErrorEventAction() { |
| 399 | if (error_event_.get() == NULL) |
| 400 | return false; |
| 401 | |
| 402 | shared_ptr<OmahaRequestAction> error_event_action( |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 403 | new OmahaRequestAction(prefs_, |
| 404 | omaha_request_params_, |
Darin Petkov | 09f96c3 | 2010-07-20 09:24:57 -0700 | [diff] [blame] | 405 | error_event_.release(), // Pass ownership. |
| 406 | new LibcurlHttpFetcher)); |
| 407 | actions_.push_back(shared_ptr<AbstractAction>(error_event_action)); |
| 408 | processor_.EnqueueAction(error_event_action.get()); |
| 409 | SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT); |
| 410 | processor_.StartProcessing(); |
| 411 | return true; |
| 412 | } |
| 413 | |
Darin Petkov | c6c135c | 2010-08-11 13:36:18 -0700 | [diff] [blame] | 414 | void UpdateAttempter::SetPriority(utils::ProcessPriority priority) { |
| 415 | if (priority_ == priority) { |
| 416 | return; |
| 417 | } |
| 418 | if (utils::SetProcessPriority(priority)) { |
| 419 | priority_ = priority; |
| 420 | LOG(INFO) << "Process priority = " << priority_; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void UpdateAttempter::SetupPriorityManagement() { |
| 425 | if (manage_priority_source_) { |
| 426 | LOG(ERROR) << "Process priority timeout source hasn't been destroyed."; |
| 427 | CleanupPriorityManagement(); |
| 428 | } |
| 429 | const int kPriorityTimeout = 10 * 60; // 10 minutes |
| 430 | manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout); |
| 431 | g_source_set_callback(manage_priority_source_, |
| 432 | StaticManagePriorityCallback, |
| 433 | this, |
| 434 | NULL); |
| 435 | g_source_attach(manage_priority_source_, NULL); |
| 436 | SetPriority(utils::kProcessPriorityLow); |
| 437 | } |
| 438 | |
| 439 | void UpdateAttempter::CleanupPriorityManagement() { |
| 440 | if (manage_priority_source_) { |
| 441 | g_source_destroy(manage_priority_source_); |
| 442 | manage_priority_source_ = NULL; |
| 443 | } |
| 444 | SetPriority(utils::kProcessPriorityNormal); |
| 445 | } |
| 446 | |
| 447 | gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) { |
| 448 | return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback(); |
| 449 | } |
| 450 | |
| 451 | bool UpdateAttempter::ManagePriorityCallback() { |
| 452 | // If the current process priority is below normal, set it to normal |
| 453 | // and let GLib invoke this callback again. |
| 454 | if (utils::ComparePriorities(priority_, utils::kProcessPriorityNormal) < 0) { |
| 455 | SetPriority(utils::kProcessPriorityNormal); |
| 456 | return true; |
| 457 | } |
| 458 | // Set the priority to high and let GLib destroy the timeout source. |
| 459 | SetPriority(utils::kProcessPriorityHigh); |
| 460 | manage_priority_source_ = NULL; |
| 461 | return false; |
| 462 | } |
| 463 | |
Andrew de los Reyes | 4e9b9f4 | 2010-04-26 15:06:43 -0700 | [diff] [blame] | 464 | } // namespace chromeos_update_engine |