blob: ef6a949ccf3c418d67cfeaf4d0d52f4759967c83 [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"
Darin Petkov1023a602010-08-30 13:47:51 -070030#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070031
Darin Petkovaf183052010-08-23 12:07:13 -070032using base::TimeDelta;
33using base::TimeTicks;
Darin Petkov9b230572010-10-08 10:20:09 -070034using std::make_pair;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070035using std::tr1::shared_ptr;
36using std::string;
37using std::vector;
38
39namespace chromeos_update_engine {
40
Darin Petkov36275772010-10-01 11:40:57 -070041const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
42
Darin Petkovcd1666f2010-09-23 09:53:44 -070043const char* kUpdateCompletedMarker =
44 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070045
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070046const char* UpdateStatusToString(UpdateStatus status) {
47 switch (status) {
48 case UPDATE_STATUS_IDLE:
49 return "UPDATE_STATUS_IDLE";
50 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
51 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
52 case UPDATE_STATUS_UPDATE_AVAILABLE:
53 return "UPDATE_STATUS_UPDATE_AVAILABLE";
54 case UPDATE_STATUS_DOWNLOADING:
55 return "UPDATE_STATUS_DOWNLOADING";
56 case UPDATE_STATUS_VERIFYING:
57 return "UPDATE_STATUS_VERIFYING";
58 case UPDATE_STATUS_FINALIZING:
59 return "UPDATE_STATUS_FINALIZING";
60 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
61 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070062 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
63 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070064 default:
65 return "unknown status";
66 }
67}
68
Darin Petkov777dbfa2010-07-20 15:03:37 -070069// Turns a generic kActionCodeError to a generic error code specific
70// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
71// not kActionCodeError, or the action is not matched, returns |code|
72// unchanged.
73ActionExitCode GetErrorCodeForAction(AbstractAction* action,
74 ActionExitCode code) {
75 if (code != kActionCodeError)
76 return code;
77
78 const string type = action->Type();
79 if (type == OmahaRequestAction::StaticType())
80 return kActionCodeOmahaRequestError;
81 if (type == OmahaResponseHandlerAction::StaticType())
82 return kActionCodeOmahaResponseHandlerError;
83 if (type == FilesystemCopierAction::StaticType())
84 return kActionCodeFilesystemCopierError;
85 if (type == PostinstallRunnerAction::StaticType())
86 return kActionCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -070087
88 return code;
89}
90
Darin Petkovc6c135c2010-08-11 13:36:18 -070091UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
92 MetricsLibraryInterface* metrics_lib)
Darin Petkovf42cc1c2010-09-01 09:03:02 -070093 : processor_(new ActionProcessor()),
94 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -070095 prefs_(prefs),
96 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -070097 update_check_scheduler_(NULL),
98 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -070099 priority_(utils::kProcessPriorityNormal),
100 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700101 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700102 status_(UPDATE_STATUS_IDLE),
103 download_progress_(0.0),
104 last_checked_time_(0),
105 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700106 new_size_(0),
107 is_full_update_(false) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700108 if (utils::FileExists(kUpdateCompletedMarker))
109 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
110}
111
112UpdateAttempter::~UpdateAttempter() {
113 CleanupPriorityManagement();
114}
115
Darin Petkov5a7f5652010-07-22 21:40:09 -0700116void UpdateAttempter::Update(const std::string& app_version,
117 const std::string& omaha_url) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700118 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
119 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
120 << "reboot";
121 return;
122 }
123 if (status_ != UPDATE_STATUS_IDLE) {
124 // Update in progress. Do nothing
125 return;
126 }
Darin Petkov1023a602010-08-30 13:47:51 -0700127 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700128 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700129 LOG(ERROR) << "Unable to initialize Omaha request device params.";
130 return;
131 }
Darin Petkov36275772010-10-01 11:40:57 -0700132 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700133 CHECK(!processor_->IsRunning());
134 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700135
136 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700137 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700138 new OmahaRequestAction(prefs_,
139 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700140 NULL,
141 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700142 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700143 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700144 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700145 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700146 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700147 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700148 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700149 new OmahaRequestAction(prefs_,
150 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700151 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700152 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700153 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700154 shared_ptr<DownloadAction> download_action(
Darin Petkov9b230572010-10-08 10:20:09 -0700155 new DownloadAction(prefs_, new MultiHttpFetcher<LibcurlHttpFetcher>));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700156 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700157 new OmahaRequestAction(prefs_,
158 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700159 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700160 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700161 new LibcurlHttpFetcher));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800162 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
163 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700164 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700165 new OmahaRequestAction(prefs_,
166 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700167 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700168 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700169
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700170 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700171 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700172 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700173
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700174 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
175 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
176 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700177 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700178 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700179 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700180 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700181 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800182 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700183 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700184
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700185 // Enqueue the actions
186 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
187 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700188 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700189 }
190
191 // Bond them together. We have to use the leaf-types when calling
192 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700193 BondActions(update_check_action.get(),
194 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700195 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700196 filesystem_copier_action.get());
197 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700198 kernel_filesystem_copier_action.get());
199 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700200 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700201 BondActions(download_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800202 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700203
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700204 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700205 processor_->StartProcessing();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700206}
207
Darin Petkov5a7f5652010-07-22 21:40:09 -0700208void UpdateAttempter::CheckForUpdate(const std::string& app_version,
209 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700210 if (status_ != UPDATE_STATUS_IDLE) {
211 LOG(INFO) << "Check for update requested, but status is "
212 << UpdateStatusToString(status_) << ", so not checking.";
213 return;
214 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700215 Update(app_version, omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700216}
217
Darin Petkov296889c2010-07-23 16:20:54 -0700218bool UpdateAttempter::RebootIfNeeded() {
219 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
220 LOG(INFO) << "Reboot requested, but status is "
221 << UpdateStatusToString(status_) << ", so not rebooting.";
222 return false;
223 }
224 TEST_AND_RETURN_FALSE(utils::Reboot());
225 return true;
226}
227
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700228// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700229void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700230 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700231 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700232 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700233 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700234
Darin Petkovc6c135c2010-08-11 13:36:18 -0700235 // Reset process priority back to normal.
236 CleanupPriorityManagement();
237
Darin Petkov09f96c32010-07-20 09:24:57 -0700238 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
239 LOG(INFO) << "Error event sent.";
240 SetStatusAndNotify(UPDATE_STATUS_IDLE);
241 return;
242 }
243
Darin Petkovc1a8b422010-07-19 11:34:49 -0700244 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700245 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700246 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700247 DeltaPerformer::ResetUpdateProgress(prefs_, false);
248 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700249
250 // Report the time it took to update the system.
251 int64_t update_time = time(NULL) - last_checked_time_;
252 metrics_lib_->SendToUMA("Installer.UpdateTime",
253 static_cast<int>(update_time), // sample
254 1, // min = 1 second
255 20 * 60, // max = 20 minutes
256 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700257 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700258 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700259
Darin Petkov1023a602010-08-30 13:47:51 -0700260 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700261 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700262 }
263 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700264 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700265}
266
267void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700268 // Reset process priority back to normal.
269 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700270 download_progress_ = 0.0;
271 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700272 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700273 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700274}
275
276// Called whenever an action has finished processing, either successfully
277// or otherwise.
278void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
279 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700280 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700281 // Reset download progress regardless of whether or not the download
282 // action succeeded. Also, get the response code from HTTP request
283 // actions (update download as well as the initial update check
284 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700285 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700286 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700287 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700288 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
289 http_response_code_ = download_action->GetHTTPResponseCode();
290 } else if (type == OmahaRequestAction::StaticType()) {
291 OmahaRequestAction* omaha_request_action =
292 dynamic_cast<OmahaRequestAction*>(action);
293 // If the request is not an event, then it's the update-check.
294 if (!omaha_request_action->IsEvent()) {
295 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700296 // Forward the server-dictated poll interval to the update check
297 // scheduler, if any.
298 if (update_check_scheduler_) {
299 update_check_scheduler_->set_poll_interval(
300 omaha_request_action->GetOutputObject().poll_interval);
301 }
Darin Petkov1023a602010-08-30 13:47:51 -0700302 }
303 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700304 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700305 // If this was a delta update attempt and the current state is at or past
306 // the download phase, count the failure in case a switch to full update
307 // becomes necessary. Ignore network transfer timeouts and failures.
308 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
309 !is_full_update_ &&
310 code != kActionCodeDownloadTransferError) {
311 MarkDeltaUpdateFailure();
312 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700313 // On failure, schedule an error event to be sent to Omaha.
314 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700315 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700316 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700317 // Find out which action completed.
318 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700319 // Note that the status will be updated to DOWNLOADING when some bytes get
320 // actually downloaded from the server and the BytesReceived callback is
321 // invoked. This avoids notifying the user that a download has started in
322 // cases when the server and the client are unable to initiate the download.
323 CHECK(action == response_handler_action_.get());
324 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700325 last_checked_time_ = time(NULL);
326 // TODO(adlr): put version in InstallPlan
327 new_version_ = "0.0.0.0";
328 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700329 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700330 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700331 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700332 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700333 } else if (type == DownloadAction::StaticType()) {
334 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
335 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700336}
337
338// Stop updating. An attempt will be made to record status to the disk
339// so that updates can be resumed later.
340void UpdateAttempter::Terminate() {
341 // TODO(adlr): implement this method.
342 NOTIMPLEMENTED();
343}
344
345// Try to resume from a previously Terminate()d update.
346void UpdateAttempter::ResumeUpdating() {
347 // TODO(adlr): implement this method.
348 NOTIMPLEMENTED();
349}
350
Darin Petkov9d911fa2010-08-19 09:36:08 -0700351void UpdateAttempter::SetDownloadStatus(bool active) {
352 download_active_ = active;
353 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
354}
355
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700356void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700357 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700358 LOG(ERROR) << "BytesReceived called while not downloading.";
359 return;
360 }
Darin Petkovaf183052010-08-23 12:07:13 -0700361 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700362 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700363 // Self throttle based on progress. Also send notifications if
364 // progress is too slow.
365 const double kDeltaPercent = 0.01; // 1%
366 if (status_ != UPDATE_STATUS_DOWNLOADING ||
367 bytes_received == total ||
368 progress - download_progress_ >= kDeltaPercent ||
369 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
370 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700371 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
372 }
373}
374
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700375bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
376 double* progress,
377 std::string* current_operation,
378 std::string* new_version,
379 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700380 *last_checked_time = last_checked_time_;
381 *progress = download_progress_;
382 *current_operation = UpdateStatusToString(status_);
383 *new_version = new_version_;
384 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700385 return true;
386}
387
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700388void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
389 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700390 if (update_check_scheduler_) {
391 update_check_scheduler_->SetUpdateStatus(status_);
392 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700393 if (!dbus_service_)
394 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700395 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700396 update_engine_service_emit_status_update(
397 dbus_service_,
398 last_checked_time_,
399 download_progress_,
400 UpdateStatusToString(status_),
401 new_version_.c_str(),
402 new_size_);
403}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700404
Darin Petkov777dbfa2010-07-20 15:03:37 -0700405void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
406 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700407 if (error_event_.get()) {
408 // This shouldn't really happen.
409 LOG(WARNING) << "There's already an existing pending error event.";
410 return;
411 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700412
413 // For now assume that Omaha response action failure means that
414 // there's no update so don't send an event. Also, double check that
415 // the failure has not occurred while sending an error event -- in
416 // which case don't schedule another. This shouldn't really happen
417 // but just in case...
418 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
419 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
420 return;
421 }
422
423 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700424 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
425 OmahaEvent::kResultError,
426 code));
427}
428
429bool UpdateAttempter::ScheduleErrorEventAction() {
430 if (error_event_.get() == NULL)
431 return false;
432
Darin Petkov1023a602010-08-30 13:47:51 -0700433 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700434 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700435 new OmahaRequestAction(prefs_,
436 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700437 error_event_.release(), // Pass ownership.
438 new LibcurlHttpFetcher));
439 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700440 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700441 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700442 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700443 return true;
444}
445
Darin Petkovc6c135c2010-08-11 13:36:18 -0700446void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
447 if (priority_ == priority) {
448 return;
449 }
450 if (utils::SetProcessPriority(priority)) {
451 priority_ = priority;
452 LOG(INFO) << "Process priority = " << priority_;
453 }
454}
455
456void UpdateAttempter::SetupPriorityManagement() {
457 if (manage_priority_source_) {
458 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
459 CleanupPriorityManagement();
460 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700461 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700462 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
463 g_source_set_callback(manage_priority_source_,
464 StaticManagePriorityCallback,
465 this,
466 NULL);
467 g_source_attach(manage_priority_source_, NULL);
468 SetPriority(utils::kProcessPriorityLow);
469}
470
471void UpdateAttempter::CleanupPriorityManagement() {
472 if (manage_priority_source_) {
473 g_source_destroy(manage_priority_source_);
474 manage_priority_source_ = NULL;
475 }
476 SetPriority(utils::kProcessPriorityNormal);
477}
478
479gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
480 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
481}
482
483bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700484 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700485 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700486 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700487}
488
Darin Petkov36275772010-10-01 11:40:57 -0700489void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
490 int64_t delta_failures;
491 if (omaha_request_params_.delta_okay &&
492 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
493 delta_failures >= kMaxDeltaUpdateFailures) {
494 LOG(WARNING) << "Too many delta update failures, forcing full update.";
495 omaha_request_params_.delta_okay = false;
496 }
497}
498
499void UpdateAttempter::MarkDeltaUpdateFailure() {
500 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700501 // Don't try to resume a failed delta update.
502 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700503 int64_t delta_failures;
504 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
505 delta_failures < 0) {
506 delta_failures = 0;
507 }
508 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
509}
510
Darin Petkov9b230572010-10-08 10:20:09 -0700511void UpdateAttempter::SetupDownload() {
512 MultiHttpFetcher<LibcurlHttpFetcher>* fetcher =
513 dynamic_cast<MultiHttpFetcher<LibcurlHttpFetcher>*>(
514 download_action_->http_fetcher());
515 MultiHttpFetcher<LibcurlHttpFetcher>::RangesVect ranges;
516 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700517 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700518 int64_t manifest_metadata_size = 0;
519 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700520 ranges.push_back(make_pair(0, manifest_metadata_size));
521 // If there're remaining unprocessed data blobs, fetch them. Be careful not
522 // to request data beyond the end of the payload to avoid 416 HTTP response
523 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700524 int64_t next_data_offset = 0;
525 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700526 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
527 if (resume_offset < response_handler_action_->install_plan().size) {
528 ranges.push_back(make_pair(resume_offset, -1));
529 }
Darin Petkov9b230572010-10-08 10:20:09 -0700530 } else {
531 ranges.push_back(make_pair(0, -1));
532 }
533 fetcher->set_ranges(ranges);
534}
535
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700536} // namespace chromeos_update_engine