blob: a108001d7fb7892d9e9197234c349dd01ea7bd1b [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));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700162 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
163 new PostinstallRunnerAction(true));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700164 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
165 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700166 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700167 new OmahaRequestAction(prefs_,
168 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700169 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700170 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700171
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700172 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700173 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700174 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700175
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700176 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
177 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
178 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700179 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700180 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700181 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700182 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700183 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700184 actions_.push_back(shared_ptr<AbstractAction>(
185 postinstall_runner_action_precommit));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700186 actions_.push_back(shared_ptr<AbstractAction>(
187 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700188 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700189
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700190 // Enqueue the actions
191 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
192 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700193 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700194 }
195
196 // Bond them together. We have to use the leaf-types when calling
197 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700198 BondActions(update_check_action.get(),
199 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700200 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700201 filesystem_copier_action.get());
202 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700203 kernel_filesystem_copier_action.get());
204 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700205 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700206 BondActions(download_action.get(),
207 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700208 BondActions(postinstall_runner_action_precommit.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700209 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700210
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700211 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700212 processor_->StartProcessing();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700213}
214
Darin Petkov5a7f5652010-07-22 21:40:09 -0700215void UpdateAttempter::CheckForUpdate(const std::string& app_version,
216 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700217 if (status_ != UPDATE_STATUS_IDLE) {
218 LOG(INFO) << "Check for update requested, but status is "
219 << UpdateStatusToString(status_) << ", so not checking.";
220 return;
221 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700222 Update(app_version, omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700223}
224
Darin Petkov296889c2010-07-23 16:20:54 -0700225bool UpdateAttempter::RebootIfNeeded() {
226 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
227 LOG(INFO) << "Reboot requested, but status is "
228 << UpdateStatusToString(status_) << ", so not rebooting.";
229 return false;
230 }
231 TEST_AND_RETURN_FALSE(utils::Reboot());
232 return true;
233}
234
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700235// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700236void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700237 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700238 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700239 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700240 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700241
Darin Petkovc6c135c2010-08-11 13:36:18 -0700242 // Reset process priority back to normal.
243 CleanupPriorityManagement();
244
Darin Petkov09f96c32010-07-20 09:24:57 -0700245 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
246 LOG(INFO) << "Error event sent.";
247 SetStatusAndNotify(UPDATE_STATUS_IDLE);
248 return;
249 }
250
Darin Petkovc1a8b422010-07-19 11:34:49 -0700251 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700252 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700253 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700254 DeltaPerformer::ResetUpdateProgress(prefs_, false);
255 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700256
257 // Report the time it took to update the system.
258 int64_t update_time = time(NULL) - last_checked_time_;
259 metrics_lib_->SendToUMA("Installer.UpdateTime",
260 static_cast<int>(update_time), // sample
261 1, // min = 1 second
262 20 * 60, // max = 20 minutes
263 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700264 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700265 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700266
Darin Petkov1023a602010-08-30 13:47:51 -0700267 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700268 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700269 }
270 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700271 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700272}
273
274void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700275 // Reset process priority back to normal.
276 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700277 download_progress_ = 0.0;
278 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700279 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700280 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700281}
282
283// Called whenever an action has finished processing, either successfully
284// or otherwise.
285void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
286 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700287 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700288 // Reset download progress regardless of whether or not the download
289 // action succeeded. Also, get the response code from HTTP request
290 // actions (update download as well as the initial update check
291 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700292 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700293 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700294 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700295 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
296 http_response_code_ = download_action->GetHTTPResponseCode();
297 } else if (type == OmahaRequestAction::StaticType()) {
298 OmahaRequestAction* omaha_request_action =
299 dynamic_cast<OmahaRequestAction*>(action);
300 // If the request is not an event, then it's the update-check.
301 if (!omaha_request_action->IsEvent()) {
302 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700303 // Forward the server-dictated poll interval to the update check
304 // scheduler, if any.
305 if (update_check_scheduler_) {
306 update_check_scheduler_->set_poll_interval(
307 omaha_request_action->GetOutputObject().poll_interval);
308 }
Darin Petkov1023a602010-08-30 13:47:51 -0700309 }
310 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700311 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700312 // If this was a delta update attempt and the current state is at or past
313 // the download phase, count the failure in case a switch to full update
314 // becomes necessary. Ignore network transfer timeouts and failures.
315 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
316 !is_full_update_ &&
317 code != kActionCodeDownloadTransferError) {
318 MarkDeltaUpdateFailure();
319 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700320 // On failure, schedule an error event to be sent to Omaha.
321 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700322 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700323 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700324 // Find out which action completed.
325 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700326 // Note that the status will be updated to DOWNLOADING when some bytes get
327 // actually downloaded from the server and the BytesReceived callback is
328 // invoked. This avoids notifying the user that a download has started in
329 // cases when the server and the client are unable to initiate the download.
330 CHECK(action == response_handler_action_.get());
331 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700332 last_checked_time_ = time(NULL);
333 // TODO(adlr): put version in InstallPlan
334 new_version_ = "0.0.0.0";
335 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700336 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700337 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700338 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700339 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700340 } else if (type == DownloadAction::StaticType()) {
341 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
342 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700343}
344
345// Stop updating. An attempt will be made to record status to the disk
346// so that updates can be resumed later.
347void UpdateAttempter::Terminate() {
348 // TODO(adlr): implement this method.
349 NOTIMPLEMENTED();
350}
351
352// Try to resume from a previously Terminate()d update.
353void UpdateAttempter::ResumeUpdating() {
354 // TODO(adlr): implement this method.
355 NOTIMPLEMENTED();
356}
357
Darin Petkov9d911fa2010-08-19 09:36:08 -0700358void UpdateAttempter::SetDownloadStatus(bool active) {
359 download_active_ = active;
360 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
361}
362
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700363void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700364 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700365 LOG(ERROR) << "BytesReceived called while not downloading.";
366 return;
367 }
Darin Petkovaf183052010-08-23 12:07:13 -0700368 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700369 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700370 // Self throttle based on progress. Also send notifications if
371 // progress is too slow.
372 const double kDeltaPercent = 0.01; // 1%
373 if (status_ != UPDATE_STATUS_DOWNLOADING ||
374 bytes_received == total ||
375 progress - download_progress_ >= kDeltaPercent ||
376 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
377 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700378 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
379 }
380}
381
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700382bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
383 double* progress,
384 std::string* current_operation,
385 std::string* new_version,
386 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700387 *last_checked_time = last_checked_time_;
388 *progress = download_progress_;
389 *current_operation = UpdateStatusToString(status_);
390 *new_version = new_version_;
391 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700392 return true;
393}
394
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700395void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
396 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700397 if (update_check_scheduler_) {
398 update_check_scheduler_->SetUpdateStatus(status_);
399 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700400 if (!dbus_service_)
401 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700402 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700403 update_engine_service_emit_status_update(
404 dbus_service_,
405 last_checked_time_,
406 download_progress_,
407 UpdateStatusToString(status_),
408 new_version_.c_str(),
409 new_size_);
410}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700411
Darin Petkov777dbfa2010-07-20 15:03:37 -0700412void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
413 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700414 if (error_event_.get()) {
415 // This shouldn't really happen.
416 LOG(WARNING) << "There's already an existing pending error event.";
417 return;
418 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700419
420 // For now assume that Omaha response action failure means that
421 // there's no update so don't send an event. Also, double check that
422 // the failure has not occurred while sending an error event -- in
423 // which case don't schedule another. This shouldn't really happen
424 // but just in case...
425 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
426 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
427 return;
428 }
429
430 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700431 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
432 OmahaEvent::kResultError,
433 code));
434}
435
436bool UpdateAttempter::ScheduleErrorEventAction() {
437 if (error_event_.get() == NULL)
438 return false;
439
Darin Petkov1023a602010-08-30 13:47:51 -0700440 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700441 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700442 new OmahaRequestAction(prefs_,
443 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700444 error_event_.release(), // Pass ownership.
445 new LibcurlHttpFetcher));
446 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700447 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700448 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700449 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700450 return true;
451}
452
Darin Petkovc6c135c2010-08-11 13:36:18 -0700453void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
454 if (priority_ == priority) {
455 return;
456 }
457 if (utils::SetProcessPriority(priority)) {
458 priority_ = priority;
459 LOG(INFO) << "Process priority = " << priority_;
460 }
461}
462
463void UpdateAttempter::SetupPriorityManagement() {
464 if (manage_priority_source_) {
465 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
466 CleanupPriorityManagement();
467 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700468 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700469 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
470 g_source_set_callback(manage_priority_source_,
471 StaticManagePriorityCallback,
472 this,
473 NULL);
474 g_source_attach(manage_priority_source_, NULL);
475 SetPriority(utils::kProcessPriorityLow);
476}
477
478void UpdateAttempter::CleanupPriorityManagement() {
479 if (manage_priority_source_) {
480 g_source_destroy(manage_priority_source_);
481 manage_priority_source_ = NULL;
482 }
483 SetPriority(utils::kProcessPriorityNormal);
484}
485
486gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
487 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
488}
489
490bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700491 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700492 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700493 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700494}
495
Darin Petkov36275772010-10-01 11:40:57 -0700496void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
497 int64_t delta_failures;
498 if (omaha_request_params_.delta_okay &&
499 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
500 delta_failures >= kMaxDeltaUpdateFailures) {
501 LOG(WARNING) << "Too many delta update failures, forcing full update.";
502 omaha_request_params_.delta_okay = false;
503 }
504}
505
506void UpdateAttempter::MarkDeltaUpdateFailure() {
507 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700508 // Don't try to resume a failed delta update.
509 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700510 int64_t delta_failures;
511 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
512 delta_failures < 0) {
513 delta_failures = 0;
514 }
515 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
516}
517
Darin Petkov9b230572010-10-08 10:20:09 -0700518void UpdateAttempter::SetupDownload() {
519 MultiHttpFetcher<LibcurlHttpFetcher>* fetcher =
520 dynamic_cast<MultiHttpFetcher<LibcurlHttpFetcher>*>(
521 download_action_->http_fetcher());
522 MultiHttpFetcher<LibcurlHttpFetcher>::RangesVect ranges;
523 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700524 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700525 int64_t manifest_metadata_size = 0;
526 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700527 ranges.push_back(make_pair(0, manifest_metadata_size));
528 // If there're remaining unprocessed data blobs, fetch them. Be careful not
529 // to request data beyond the end of the payload to avoid 416 HTTP response
530 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700531 int64_t next_data_offset = 0;
532 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700533 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
534 if (resume_offset < response_handler_action_->install_plan().size) {
535 ranges.push_back(make_pair(resume_offset, -1));
536 }
Darin Petkov9b230572010-10-08 10:20:09 -0700537 } else {
538 ranges.push_back(make_pair(0, -1));
539 }
540 fetcher->set_ranges(ranges);
541}
542
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700543} // namespace chromeos_update_engine