blob: a9c926187215eeefff18c3c8b3d3774fcf932619 [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 <tr1/memory>
14#include <string>
15#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 Petkov6a5b3222010-07-13 14:55:28 -070024#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070025#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026#include "update_engine/omaha_response_handler_action.h"
27#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070028#include "update_engine/prefs_interface.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070029#include "update_engine/set_bootable_flag_action.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;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070034using std::tr1::shared_ptr;
35using std::string;
36using std::vector;
37
38namespace chromeos_update_engine {
39
Darin Petkov36275772010-10-01 11:40:57 -070040const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
41
Darin Petkovcd1666f2010-09-23 09:53:44 -070042const char* kUpdateCompletedMarker =
43 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070044
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070045const char* UpdateStatusToString(UpdateStatus status) {
46 switch (status) {
47 case UPDATE_STATUS_IDLE:
48 return "UPDATE_STATUS_IDLE";
49 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
50 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
51 case UPDATE_STATUS_UPDATE_AVAILABLE:
52 return "UPDATE_STATUS_UPDATE_AVAILABLE";
53 case UPDATE_STATUS_DOWNLOADING:
54 return "UPDATE_STATUS_DOWNLOADING";
55 case UPDATE_STATUS_VERIFYING:
56 return "UPDATE_STATUS_VERIFYING";
57 case UPDATE_STATUS_FINALIZING:
58 return "UPDATE_STATUS_FINALIZING";
59 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
60 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070061 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
62 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070063 default:
64 return "unknown status";
65 }
66}
67
Darin Petkov777dbfa2010-07-20 15:03:37 -070068// Turns a generic kActionCodeError to a generic error code specific
69// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
70// not kActionCodeError, or the action is not matched, returns |code|
71// unchanged.
72ActionExitCode GetErrorCodeForAction(AbstractAction* action,
73 ActionExitCode code) {
74 if (code != kActionCodeError)
75 return code;
76
77 const string type = action->Type();
78 if (type == OmahaRequestAction::StaticType())
79 return kActionCodeOmahaRequestError;
80 if (type == OmahaResponseHandlerAction::StaticType())
81 return kActionCodeOmahaResponseHandlerError;
82 if (type == FilesystemCopierAction::StaticType())
83 return kActionCodeFilesystemCopierError;
84 if (type == PostinstallRunnerAction::StaticType())
85 return kActionCodePostinstallRunnerError;
86 if (type == SetBootableFlagAction::StaticType())
87 return kActionCodeSetBootableFlagError;
88
89 return code;
90}
91
Darin Petkovc6c135c2010-08-11 13:36:18 -070092UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
93 MetricsLibraryInterface* metrics_lib)
Darin Petkovf42cc1c2010-09-01 09:03:02 -070094 : processor_(new ActionProcessor()),
95 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -070096 prefs_(prefs),
97 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -070098 update_check_scheduler_(NULL),
99 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700100 priority_(utils::kProcessPriorityNormal),
101 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700102 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700103 status_(UPDATE_STATUS_IDLE),
104 download_progress_(0.0),
105 last_checked_time_(0),
106 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700107 new_size_(0),
108 is_full_update_(false) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700109 if (utils::FileExists(kUpdateCompletedMarker))
110 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
111}
112
113UpdateAttempter::~UpdateAttempter() {
114 CleanupPriorityManagement();
115}
116
Darin Petkov5a7f5652010-07-22 21:40:09 -0700117void UpdateAttempter::Update(const std::string& app_version,
118 const std::string& omaha_url) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700119 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
120 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
121 << "reboot";
122 return;
123 }
124 if (status_ != UPDATE_STATUS_IDLE) {
125 // Update in progress. Do nothing
126 return;
127 }
Darin Petkov1023a602010-08-30 13:47:51 -0700128 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700129 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700130 LOG(ERROR) << "Unable to initialize Omaha request device params.";
131 return;
132 }
Darin Petkov36275772010-10-01 11:40:57 -0700133 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700134 CHECK(!processor_->IsRunning());
135 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700136
137 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700138 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700139 new OmahaRequestAction(prefs_,
140 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700141 NULL,
142 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700143 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
144 new OmahaResponseHandlerAction);
145 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700146 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700147 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700148 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700149 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700150 new OmahaRequestAction(prefs_,
151 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700152 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700153 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700154 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700155 shared_ptr<DownloadAction> download_action(
156 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700157 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700158 new OmahaRequestAction(prefs_,
159 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700160 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700161 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700162 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700163 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
164 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700165 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
166 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700167 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
168 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700169 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700170 new OmahaRequestAction(prefs_,
171 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700172 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700173 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700174
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700175 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700176 response_handler_action_ = response_handler_action;
177
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700178 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
179 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
180 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700181 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700182 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700183 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700184 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700185 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700186 actions_.push_back(shared_ptr<AbstractAction>(
187 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700188 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700189 actions_.push_back(shared_ptr<AbstractAction>(
190 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700191 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700192
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700193 // Enqueue the actions
194 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
195 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700196 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700197 }
198
199 // Bond them together. We have to use the leaf-types when calling
200 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700201 BondActions(update_check_action.get(),
202 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700203 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700204 filesystem_copier_action.get());
205 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700206 kernel_filesystem_copier_action.get());
207 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700208 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700209 BondActions(download_action.get(),
210 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700211 BondActions(postinstall_runner_action_precommit.get(),
212 set_bootable_flag_action.get());
213 BondActions(set_bootable_flag_action.get(),
214 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700215
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700216 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700217 processor_->StartProcessing();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700218}
219
Darin Petkov5a7f5652010-07-22 21:40:09 -0700220void UpdateAttempter::CheckForUpdate(const std::string& app_version,
221 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700222 if (status_ != UPDATE_STATUS_IDLE) {
223 LOG(INFO) << "Check for update requested, but status is "
224 << UpdateStatusToString(status_) << ", so not checking.";
225 return;
226 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700227 Update(app_version, omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700228}
229
Darin Petkov296889c2010-07-23 16:20:54 -0700230bool UpdateAttempter::RebootIfNeeded() {
231 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
232 LOG(INFO) << "Reboot requested, but status is "
233 << UpdateStatusToString(status_) << ", so not rebooting.";
234 return false;
235 }
236 TEST_AND_RETURN_FALSE(utils::Reboot());
237 return true;
238}
239
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700240// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700241void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700242 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700243 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700244 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700245 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700246
Darin Petkovc6c135c2010-08-11 13:36:18 -0700247 // Reset process priority back to normal.
248 CleanupPriorityManagement();
249
Darin Petkov09f96c32010-07-20 09:24:57 -0700250 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
251 LOG(INFO) << "Error event sent.";
252 SetStatusAndNotify(UPDATE_STATUS_IDLE);
253 return;
254 }
255
Darin Petkovc1a8b422010-07-19 11:34:49 -0700256 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700257 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700258 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700259 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700260
261 // Report the time it took to update the system.
262 int64_t update_time = time(NULL) - last_checked_time_;
263 metrics_lib_->SendToUMA("Installer.UpdateTime",
264 static_cast<int>(update_time), // sample
265 1, // min = 1 second
266 20 * 60, // max = 20 minutes
267 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700268 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700269 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700270
Darin Petkov1023a602010-08-30 13:47:51 -0700271 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700272 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700273 }
274 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700275 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700276}
277
278void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700279 // Reset process priority back to normal.
280 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700281 download_progress_ = 0.0;
282 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700283 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700284 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700285}
286
287// Called whenever an action has finished processing, either successfully
288// or otherwise.
289void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
290 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700291 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700292 // Reset download progress regardless of whether or not the download
293 // action succeeded. Also, get the response code from HTTP request
294 // actions (update download as well as the initial update check
295 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700296 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700297 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700298 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700299 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
300 http_response_code_ = download_action->GetHTTPResponseCode();
301 } else if (type == OmahaRequestAction::StaticType()) {
302 OmahaRequestAction* omaha_request_action =
303 dynamic_cast<OmahaRequestAction*>(action);
304 // If the request is not an event, then it's the update-check.
305 if (!omaha_request_action->IsEvent()) {
306 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700307 // Forward the server-dictated poll interval to the update check
308 // scheduler, if any.
309 if (update_check_scheduler_) {
310 update_check_scheduler_->set_poll_interval(
311 omaha_request_action->GetOutputObject().poll_interval);
312 }
Darin Petkov1023a602010-08-30 13:47:51 -0700313 }
314 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700315 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700316 // If this was a delta update attempt and the current state is at or past
317 // the download phase, count the failure in case a switch to full update
318 // becomes necessary. Ignore network transfer timeouts and failures.
319 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
320 !is_full_update_ &&
321 code != kActionCodeDownloadTransferError) {
322 MarkDeltaUpdateFailure();
323 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700324 // On failure, schedule an error event to be sent to Omaha.
325 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700326 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700327 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700328 // Find out which action completed.
329 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700330 // Note that the status will be updated to DOWNLOADING when some
331 // bytes get actually downloaded from the server and the
332 // BytesReceived callback is invoked. This avoids notifying the
333 // user that a download has started in cases when the server and
334 // the client are unable to initiate the download.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700335 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700336 dynamic_cast<OmahaResponseHandlerAction*>(action);
337 CHECK(omaha_response_handler_action);
338 const InstallPlan& plan = omaha_response_handler_action->install_plan();
339 last_checked_time_ = time(NULL);
340 // TODO(adlr): put version in InstallPlan
341 new_version_ = "0.0.0.0";
342 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700343 is_full_update_ = plan.is_full_update;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700344 SetupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700345 } else if (type == DownloadAction::StaticType()) {
346 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
347 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700348}
349
350// Stop updating. An attempt will be made to record status to the disk
351// so that updates can be resumed later.
352void UpdateAttempter::Terminate() {
353 // TODO(adlr): implement this method.
354 NOTIMPLEMENTED();
355}
356
357// Try to resume from a previously Terminate()d update.
358void UpdateAttempter::ResumeUpdating() {
359 // TODO(adlr): implement this method.
360 NOTIMPLEMENTED();
361}
362
Darin Petkov9d911fa2010-08-19 09:36:08 -0700363void UpdateAttempter::SetDownloadStatus(bool active) {
364 download_active_ = active;
365 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
366}
367
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700368void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700369 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700370 LOG(ERROR) << "BytesReceived called while not downloading.";
371 return;
372 }
Darin Petkovaf183052010-08-23 12:07:13 -0700373 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700374 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700375 // Self throttle based on progress. Also send notifications if
376 // progress is too slow.
377 const double kDeltaPercent = 0.01; // 1%
378 if (status_ != UPDATE_STATUS_DOWNLOADING ||
379 bytes_received == total ||
380 progress - download_progress_ >= kDeltaPercent ||
381 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
382 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700383 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
384 }
385}
386
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700387bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
388 double* progress,
389 std::string* current_operation,
390 std::string* new_version,
391 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700392 *last_checked_time = last_checked_time_;
393 *progress = download_progress_;
394 *current_operation = UpdateStatusToString(status_);
395 *new_version = new_version_;
396 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700397 return true;
398}
399
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700400void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
401 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700402 if (update_check_scheduler_) {
403 update_check_scheduler_->SetUpdateStatus(status_);
404 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700405 if (!dbus_service_)
406 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700407 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700408 update_engine_service_emit_status_update(
409 dbus_service_,
410 last_checked_time_,
411 download_progress_,
412 UpdateStatusToString(status_),
413 new_version_.c_str(),
414 new_size_);
415}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700416
Darin Petkov777dbfa2010-07-20 15:03:37 -0700417void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
418 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700419 if (error_event_.get()) {
420 // This shouldn't really happen.
421 LOG(WARNING) << "There's already an existing pending error event.";
422 return;
423 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700424
425 // For now assume that Omaha response action failure means that
426 // there's no update so don't send an event. Also, double check that
427 // the failure has not occurred while sending an error event -- in
428 // which case don't schedule another. This shouldn't really happen
429 // but just in case...
430 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
431 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
432 return;
433 }
434
435 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700436 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
437 OmahaEvent::kResultError,
438 code));
439}
440
441bool UpdateAttempter::ScheduleErrorEventAction() {
442 if (error_event_.get() == NULL)
443 return false;
444
Darin Petkov1023a602010-08-30 13:47:51 -0700445 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700446 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700447 new OmahaRequestAction(prefs_,
448 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700449 error_event_.release(), // Pass ownership.
450 new LibcurlHttpFetcher));
451 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700452 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700453 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700454 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700455 return true;
456}
457
Darin Petkovc6c135c2010-08-11 13:36:18 -0700458void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
459 if (priority_ == priority) {
460 return;
461 }
462 if (utils::SetProcessPriority(priority)) {
463 priority_ = priority;
464 LOG(INFO) << "Process priority = " << priority_;
465 }
466}
467
468void UpdateAttempter::SetupPriorityManagement() {
469 if (manage_priority_source_) {
470 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
471 CleanupPriorityManagement();
472 }
473 const int kPriorityTimeout = 10 * 60; // 10 minutes
474 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
475 g_source_set_callback(manage_priority_source_,
476 StaticManagePriorityCallback,
477 this,
478 NULL);
479 g_source_attach(manage_priority_source_, NULL);
480 SetPriority(utils::kProcessPriorityLow);
481}
482
483void UpdateAttempter::CleanupPriorityManagement() {
484 if (manage_priority_source_) {
485 g_source_destroy(manage_priority_source_);
486 manage_priority_source_ = NULL;
487 }
488 SetPriority(utils::kProcessPriorityNormal);
489}
490
491gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
492 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
493}
494
495bool UpdateAttempter::ManagePriorityCallback() {
496 // If the current process priority is below normal, set it to normal
497 // and let GLib invoke this callback again.
498 if (utils::ComparePriorities(priority_, utils::kProcessPriorityNormal) < 0) {
499 SetPriority(utils::kProcessPriorityNormal);
500 return true;
501 }
502 // Set the priority to high and let GLib destroy the timeout source.
503 SetPriority(utils::kProcessPriorityHigh);
504 manage_priority_source_ = NULL;
505 return false;
506}
507
Darin Petkov36275772010-10-01 11:40:57 -0700508void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
509 int64_t delta_failures;
510 if (omaha_request_params_.delta_okay &&
511 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
512 delta_failures >= kMaxDeltaUpdateFailures) {
513 LOG(WARNING) << "Too many delta update failures, forcing full update.";
514 omaha_request_params_.delta_okay = false;
515 }
516}
517
518void UpdateAttempter::MarkDeltaUpdateFailure() {
519 CHECK(!is_full_update_);
520 int64_t delta_failures;
521 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
522 delta_failures < 0) {
523 delta_failures = 0;
524 }
525 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
526}
527
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700528} // namespace chromeos_update_engine