blob: 7c324755aa328e3c97f108c637ca086ed65572f8 [file] [log] [blame]
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001// 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 Reyes63b96d72010-05-10 13:08:54 -07006
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 Reyes4e9b9f42010-04-26 15:06:43 -070013#include <string>
Darin Petkov9b230572010-10-08 10:20:09 -070014#include <tr1/memory>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070015#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070016
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070017#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070018#include <metrics/metrics_library.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070019
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070020#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021#include "update_engine/download_action.h"
22#include "update_engine/filesystem_copier_action.h"
23#include "update_engine/libcurl_http_fetcher.h"
Darin Petkov9b230572010-10-08 10:20:09 -070024#include "update_engine/multi_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070025#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070026#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070027#include "update_engine/omaha_response_handler_action.h"
28#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070029#include "update_engine/prefs_interface.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070030#include "update_engine/set_bootable_flag_action.h"
Darin Petkov1023a602010-08-30 13:47:51 -070031#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070032
Darin Petkovaf183052010-08-23 12:07:13 -070033using base::TimeDelta;
34using base::TimeTicks;
Darin Petkov9b230572010-10-08 10:20:09 -070035using std::make_pair;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070036using std::tr1::shared_ptr;
37using std::string;
38using std::vector;
39
40namespace chromeos_update_engine {
41
Darin Petkov36275772010-10-01 11:40:57 -070042const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
43
Darin Petkovcd1666f2010-09-23 09:53:44 -070044const char* kUpdateCompletedMarker =
45 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070046
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070047const char* UpdateStatusToString(UpdateStatus status) {
48 switch (status) {
49 case UPDATE_STATUS_IDLE:
50 return "UPDATE_STATUS_IDLE";
51 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
52 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
53 case UPDATE_STATUS_UPDATE_AVAILABLE:
54 return "UPDATE_STATUS_UPDATE_AVAILABLE";
55 case UPDATE_STATUS_DOWNLOADING:
56 return "UPDATE_STATUS_DOWNLOADING";
57 case UPDATE_STATUS_VERIFYING:
58 return "UPDATE_STATUS_VERIFYING";
59 case UPDATE_STATUS_FINALIZING:
60 return "UPDATE_STATUS_FINALIZING";
61 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
62 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070063 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
64 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070065 default:
66 return "unknown status";
67 }
68}
69
Darin Petkov777dbfa2010-07-20 15:03:37 -070070// Turns a generic kActionCodeError to a generic error code specific
71// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
72// not kActionCodeError, or the action is not matched, returns |code|
73// unchanged.
74ActionExitCode GetErrorCodeForAction(AbstractAction* action,
75 ActionExitCode code) {
76 if (code != kActionCodeError)
77 return code;
78
79 const string type = action->Type();
80 if (type == OmahaRequestAction::StaticType())
81 return kActionCodeOmahaRequestError;
82 if (type == OmahaResponseHandlerAction::StaticType())
83 return kActionCodeOmahaResponseHandlerError;
84 if (type == FilesystemCopierAction::StaticType())
85 return kActionCodeFilesystemCopierError;
86 if (type == PostinstallRunnerAction::StaticType())
87 return kActionCodePostinstallRunnerError;
88 if (type == SetBootableFlagAction::StaticType())
89 return kActionCodeSetBootableFlagError;
90
91 return code;
92}
93
Darin Petkovc6c135c2010-08-11 13:36:18 -070094UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
95 MetricsLibraryInterface* metrics_lib)
Darin Petkovf42cc1c2010-09-01 09:03:02 -070096 : processor_(new ActionProcessor()),
97 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -070098 prefs_(prefs),
99 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -0700100 update_check_scheduler_(NULL),
101 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700102 priority_(utils::kProcessPriorityNormal),
103 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700104 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700105 status_(UPDATE_STATUS_IDLE),
106 download_progress_(0.0),
107 last_checked_time_(0),
108 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700109 new_size_(0),
110 is_full_update_(false) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700111 if (utils::FileExists(kUpdateCompletedMarker))
112 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
113}
114
115UpdateAttempter::~UpdateAttempter() {
116 CleanupPriorityManagement();
117}
118
Darin Petkov5a7f5652010-07-22 21:40:09 -0700119void UpdateAttempter::Update(const std::string& app_version,
120 const std::string& omaha_url) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700121 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
122 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
123 << "reboot";
124 return;
125 }
126 if (status_ != UPDATE_STATUS_IDLE) {
127 // Update in progress. Do nothing
128 return;
129 }
Darin Petkov1023a602010-08-30 13:47:51 -0700130 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700131 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700132 LOG(ERROR) << "Unable to initialize Omaha request device params.";
133 return;
134 }
Darin Petkov36275772010-10-01 11:40:57 -0700135 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700136 CHECK(!processor_->IsRunning());
137 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700138
139 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700140 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700141 new OmahaRequestAction(prefs_,
142 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700143 NULL,
144 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700145 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700146 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700147 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700148 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700149 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700150 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700151 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700152 new OmahaRequestAction(prefs_,
153 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700154 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700155 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700156 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700157 shared_ptr<DownloadAction> download_action(
Darin Petkov9b230572010-10-08 10:20:09 -0700158 new DownloadAction(prefs_, new MultiHttpFetcher<LibcurlHttpFetcher>));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700159 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700160 new OmahaRequestAction(prefs_,
161 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700162 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700163 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700164 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700165 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
166 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700167 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
168 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700169 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
170 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700171 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700172 new OmahaRequestAction(prefs_,
173 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700174 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700175 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700176
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700177 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700178 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700179 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700180
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700181 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
182 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
183 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700184 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700185 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700186 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700187 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700188 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700189 actions_.push_back(shared_ptr<AbstractAction>(
190 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700191 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700192 actions_.push_back(shared_ptr<AbstractAction>(
193 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700194 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700195
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700196 // Enqueue the actions
197 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
198 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700199 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700200 }
201
202 // Bond them together. We have to use the leaf-types when calling
203 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700204 BondActions(update_check_action.get(),
205 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700206 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700207 filesystem_copier_action.get());
208 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700209 kernel_filesystem_copier_action.get());
210 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700211 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700212 BondActions(download_action.get(),
213 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700214 BondActions(postinstall_runner_action_precommit.get(),
215 set_bootable_flag_action.get());
216 BondActions(set_bootable_flag_action.get(),
217 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700218
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700219 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700220 processor_->StartProcessing();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700221}
222
Darin Petkov5a7f5652010-07-22 21:40:09 -0700223void UpdateAttempter::CheckForUpdate(const std::string& app_version,
224 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700225 if (status_ != UPDATE_STATUS_IDLE) {
226 LOG(INFO) << "Check for update requested, but status is "
227 << UpdateStatusToString(status_) << ", so not checking.";
228 return;
229 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700230 Update(app_version, omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700231}
232
Darin Petkov296889c2010-07-23 16:20:54 -0700233bool UpdateAttempter::RebootIfNeeded() {
234 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
235 LOG(INFO) << "Reboot requested, but status is "
236 << UpdateStatusToString(status_) << ", so not rebooting.";
237 return false;
238 }
239 TEST_AND_RETURN_FALSE(utils::Reboot());
240 return true;
241}
242
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700243// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700244void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700245 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700246 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700247 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700248 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700249
Darin Petkovc6c135c2010-08-11 13:36:18 -0700250 // Reset process priority back to normal.
251 CleanupPriorityManagement();
252
Darin Petkov09f96c32010-07-20 09:24:57 -0700253 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
254 LOG(INFO) << "Error event sent.";
255 SetStatusAndNotify(UPDATE_STATUS_IDLE);
256 return;
257 }
258
Darin Petkovc1a8b422010-07-19 11:34:49 -0700259 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700260 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700261 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700262 DeltaPerformer::ResetUpdateProgress(prefs_, false);
263 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700264
265 // Report the time it took to update the system.
266 int64_t update_time = time(NULL) - last_checked_time_;
267 metrics_lib_->SendToUMA("Installer.UpdateTime",
268 static_cast<int>(update_time), // sample
269 1, // min = 1 second
270 20 * 60, // max = 20 minutes
271 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700272 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700273 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700274
Darin Petkov1023a602010-08-30 13:47:51 -0700275 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700276 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700277 }
278 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700279 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700280}
281
282void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700283 // Reset process priority back to normal.
284 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700285 download_progress_ = 0.0;
286 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700287 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700288 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700289}
290
291// Called whenever an action has finished processing, either successfully
292// or otherwise.
293void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
294 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700295 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700296 // Reset download progress regardless of whether or not the download
297 // action succeeded. Also, get the response code from HTTP request
298 // actions (update download as well as the initial update check
299 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700300 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700301 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700302 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700303 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
304 http_response_code_ = download_action->GetHTTPResponseCode();
305 } else if (type == OmahaRequestAction::StaticType()) {
306 OmahaRequestAction* omaha_request_action =
307 dynamic_cast<OmahaRequestAction*>(action);
308 // If the request is not an event, then it's the update-check.
309 if (!omaha_request_action->IsEvent()) {
310 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700311 // Forward the server-dictated poll interval to the update check
312 // scheduler, if any.
313 if (update_check_scheduler_) {
314 update_check_scheduler_->set_poll_interval(
315 omaha_request_action->GetOutputObject().poll_interval);
316 }
Darin Petkov1023a602010-08-30 13:47:51 -0700317 }
318 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700319 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700320 // If this was a delta update attempt and the current state is at or past
321 // the download phase, count the failure in case a switch to full update
322 // becomes necessary. Ignore network transfer timeouts and failures.
323 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
324 !is_full_update_ &&
325 code != kActionCodeDownloadTransferError) {
326 MarkDeltaUpdateFailure();
327 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700328 // On failure, schedule an error event to be sent to Omaha.
329 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700330 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700331 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700332 // Find out which action completed.
333 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700334 // Note that the status will be updated to DOWNLOADING when some bytes get
335 // actually downloaded from the server and the BytesReceived callback is
336 // invoked. This avoids notifying the user that a download has started in
337 // cases when the server and the client are unable to initiate the download.
338 CHECK(action == response_handler_action_.get());
339 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700340 last_checked_time_ = time(NULL);
341 // TODO(adlr): put version in InstallPlan
342 new_version_ = "0.0.0.0";
343 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700344 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700345 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700346 SetupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700347 } else if (type == DownloadAction::StaticType()) {
348 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
349 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700350}
351
352// Stop updating. An attempt will be made to record status to the disk
353// so that updates can be resumed later.
354void UpdateAttempter::Terminate() {
355 // TODO(adlr): implement this method.
356 NOTIMPLEMENTED();
357}
358
359// Try to resume from a previously Terminate()d update.
360void UpdateAttempter::ResumeUpdating() {
361 // TODO(adlr): implement this method.
362 NOTIMPLEMENTED();
363}
364
Darin Petkov9d911fa2010-08-19 09:36:08 -0700365void UpdateAttempter::SetDownloadStatus(bool active) {
366 download_active_ = active;
367 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
368}
369
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700370void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700371 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700372 LOG(ERROR) << "BytesReceived called while not downloading.";
373 return;
374 }
Darin Petkovaf183052010-08-23 12:07:13 -0700375 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700376 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700377 // Self throttle based on progress. Also send notifications if
378 // progress is too slow.
379 const double kDeltaPercent = 0.01; // 1%
380 if (status_ != UPDATE_STATUS_DOWNLOADING ||
381 bytes_received == total ||
382 progress - download_progress_ >= kDeltaPercent ||
383 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
384 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700385 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
386 }
387}
388
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700389bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
390 double* progress,
391 std::string* current_operation,
392 std::string* new_version,
393 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700394 *last_checked_time = last_checked_time_;
395 *progress = download_progress_;
396 *current_operation = UpdateStatusToString(status_);
397 *new_version = new_version_;
398 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700399 return true;
400}
401
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700402void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
403 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700404 if (update_check_scheduler_) {
405 update_check_scheduler_->SetUpdateStatus(status_);
406 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700407 if (!dbus_service_)
408 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700409 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700410 update_engine_service_emit_status_update(
411 dbus_service_,
412 last_checked_time_,
413 download_progress_,
414 UpdateStatusToString(status_),
415 new_version_.c_str(),
416 new_size_);
417}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700418
Darin Petkov777dbfa2010-07-20 15:03:37 -0700419void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
420 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700421 if (error_event_.get()) {
422 // This shouldn't really happen.
423 LOG(WARNING) << "There's already an existing pending error event.";
424 return;
425 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700426
427 // For now assume that Omaha response action failure means that
428 // there's no update so don't send an event. Also, double check that
429 // the failure has not occurred while sending an error event -- in
430 // which case don't schedule another. This shouldn't really happen
431 // but just in case...
432 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
433 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
434 return;
435 }
436
437 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700438 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
439 OmahaEvent::kResultError,
440 code));
441}
442
443bool UpdateAttempter::ScheduleErrorEventAction() {
444 if (error_event_.get() == NULL)
445 return false;
446
Darin Petkov1023a602010-08-30 13:47:51 -0700447 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700448 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700449 new OmahaRequestAction(prefs_,
450 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700451 error_event_.release(), // Pass ownership.
452 new LibcurlHttpFetcher));
453 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700454 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700455 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700456 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700457 return true;
458}
459
Darin Petkovc6c135c2010-08-11 13:36:18 -0700460void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
461 if (priority_ == priority) {
462 return;
463 }
464 if (utils::SetProcessPriority(priority)) {
465 priority_ = priority;
466 LOG(INFO) << "Process priority = " << priority_;
467 }
468}
469
470void UpdateAttempter::SetupPriorityManagement() {
471 if (manage_priority_source_) {
472 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
473 CleanupPriorityManagement();
474 }
475 const int kPriorityTimeout = 10 * 60; // 10 minutes
476 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
477 g_source_set_callback(manage_priority_source_,
478 StaticManagePriorityCallback,
479 this,
480 NULL);
481 g_source_attach(manage_priority_source_, NULL);
482 SetPriority(utils::kProcessPriorityLow);
483}
484
485void UpdateAttempter::CleanupPriorityManagement() {
486 if (manage_priority_source_) {
487 g_source_destroy(manage_priority_source_);
488 manage_priority_source_ = NULL;
489 }
490 SetPriority(utils::kProcessPriorityNormal);
491}
492
493gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
494 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
495}
496
497bool UpdateAttempter::ManagePriorityCallback() {
498 // If the current process priority is below normal, set it to normal
499 // and let GLib invoke this callback again.
500 if (utils::ComparePriorities(priority_, utils::kProcessPriorityNormal) < 0) {
501 SetPriority(utils::kProcessPriorityNormal);
502 return true;
503 }
504 // Set the priority to high and let GLib destroy the timeout source.
505 SetPriority(utils::kProcessPriorityHigh);
506 manage_priority_source_ = NULL;
507 return false;
508}
509
Darin Petkov36275772010-10-01 11:40:57 -0700510void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
511 int64_t delta_failures;
512 if (omaha_request_params_.delta_okay &&
513 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
514 delta_failures >= kMaxDeltaUpdateFailures) {
515 LOG(WARNING) << "Too many delta update failures, forcing full update.";
516 omaha_request_params_.delta_okay = false;
517 }
518}
519
520void UpdateAttempter::MarkDeltaUpdateFailure() {
521 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700522 // Don't try to resume a failed delta update.
523 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700524 int64_t delta_failures;
525 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
526 delta_failures < 0) {
527 delta_failures = 0;
528 }
529 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
530}
531
Darin Petkov9b230572010-10-08 10:20:09 -0700532void UpdateAttempter::SetupDownload() {
533 MultiHttpFetcher<LibcurlHttpFetcher>* fetcher =
534 dynamic_cast<MultiHttpFetcher<LibcurlHttpFetcher>*>(
535 download_action_->http_fetcher());
536 MultiHttpFetcher<LibcurlHttpFetcher>::RangesVect ranges;
537 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700538 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700539 int64_t manifest_metadata_size = 0;
540 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700541 ranges.push_back(make_pair(0, manifest_metadata_size));
542 // If there're remaining unprocessed data blobs, fetch them. Be careful not
543 // to request data beyond the end of the payload to avoid 416 HTTP response
544 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700545 int64_t next_data_offset = 0;
546 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700547 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
548 if (resume_offset < response_handler_action_->install_plan().size) {
549 ranges.push_back(make_pair(resume_offset, -1));
550 }
Darin Petkov9b230572010-10-08 10:20:09 -0700551 } else {
552 ranges.push_back(make_pair(0, -1));
553 }
554 fetcher->set_ranges(ranges);
555}
556
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700557} // namespace chromeos_update_engine