blob: 4e7d7bf586b64be10db6aa22237b66b75e8ab315 [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 Reyes45168102010-11-22 11:13:50 -080017#include <base/rand_util.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070018#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070019#include <metrics/metrics_library.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070020
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070021#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070022#include "update_engine/download_action.h"
23#include "update_engine/filesystem_copier_action.h"
24#include "update_engine/libcurl_http_fetcher.h"
Darin Petkov9b230572010-10-08 10:20:09 -070025#include "update_engine/multi_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070026#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070027#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070028#include "update_engine/omaha_response_handler_action.h"
29#include "update_engine/postinstall_runner_action.h"
Darin Petkov36275772010-10-01 11:40:57 -070030#include "update_engine/prefs_interface.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 Reyes45168102010-11-22 11:13:50 -080047namespace {
48const int kMaxConsecutiveObeyProxyRequests = 20;
49} // namespace {}
50
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070051const char* UpdateStatusToString(UpdateStatus status) {
52 switch (status) {
53 case UPDATE_STATUS_IDLE:
54 return "UPDATE_STATUS_IDLE";
55 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
56 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
57 case UPDATE_STATUS_UPDATE_AVAILABLE:
58 return "UPDATE_STATUS_UPDATE_AVAILABLE";
59 case UPDATE_STATUS_DOWNLOADING:
60 return "UPDATE_STATUS_DOWNLOADING";
61 case UPDATE_STATUS_VERIFYING:
62 return "UPDATE_STATUS_VERIFYING";
63 case UPDATE_STATUS_FINALIZING:
64 return "UPDATE_STATUS_FINALIZING";
65 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
66 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070067 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
68 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070069 default:
70 return "unknown status";
71 }
72}
73
Darin Petkov777dbfa2010-07-20 15:03:37 -070074// Turns a generic kActionCodeError to a generic error code specific
75// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
76// not kActionCodeError, or the action is not matched, returns |code|
77// unchanged.
78ActionExitCode GetErrorCodeForAction(AbstractAction* action,
79 ActionExitCode code) {
80 if (code != kActionCodeError)
81 return code;
82
83 const string type = action->Type();
84 if (type == OmahaRequestAction::StaticType())
85 return kActionCodeOmahaRequestError;
86 if (type == OmahaResponseHandlerAction::StaticType())
87 return kActionCodeOmahaResponseHandlerError;
88 if (type == FilesystemCopierAction::StaticType())
89 return kActionCodeFilesystemCopierError;
90 if (type == PostinstallRunnerAction::StaticType())
91 return kActionCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -070092
93 return code;
94}
95
Darin Petkovc6c135c2010-08-11 13:36:18 -070096UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
Andrew de los Reyes45168102010-11-22 11:13:50 -080097 MetricsLibraryInterface* metrics_lib,
98 DbusGlibInterface* dbus_iface)
Darin Petkovf42cc1c2010-09-01 09:03:02 -070099 : processor_(new ActionProcessor()),
100 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700101 prefs_(prefs),
102 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -0700103 update_check_scheduler_(NULL),
104 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700105 priority_(utils::kProcessPriorityNormal),
106 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700107 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700108 status_(UPDATE_STATUS_IDLE),
109 download_progress_(0.0),
110 last_checked_time_(0),
111 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700112 new_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800113 is_full_update_(false),
114 proxy_manual_checks_(0),
115 obeying_proxies_(true),
116 chrome_proxy_resolver_(dbus_iface) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700117 if (utils::FileExists(kUpdateCompletedMarker))
118 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
119}
120
121UpdateAttempter::~UpdateAttempter() {
122 CleanupPriorityManagement();
123}
124
Darin Petkov5a7f5652010-07-22 21:40:09 -0700125void UpdateAttempter::Update(const std::string& app_version,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800126 const std::string& omaha_url,
127 bool obey_proxies) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700128 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
129 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
130 << "reboot";
131 return;
132 }
133 if (status_ != UPDATE_STATUS_IDLE) {
134 // Update in progress. Do nothing
135 return;
136 }
Darin Petkov1023a602010-08-30 13:47:51 -0700137 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700138 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700139 LOG(ERROR) << "Unable to initialize Omaha request device params.";
140 return;
141 }
Andrew de los Reyes45168102010-11-22 11:13:50 -0800142
143 obeying_proxies_ = true;
144 if (obey_proxies || proxy_manual_checks_ == 0) {
145 LOG(INFO) << "forced to obey proxies";
146 // If forced to obey proxies, every 20th request will not use proxies
147 proxy_manual_checks_++;
148 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
149 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
150 proxy_manual_checks_ = 0;
151 obeying_proxies_ = false;
152 }
153 } else if (base::RandInt(0, 4) == 0) {
154 obeying_proxies_ = false;
155 }
156 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
157 "check we are ignoring the proxy settings and using "
158 "direct connections.";
159
Darin Petkov36275772010-10-01 11:40:57 -0700160 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700161 CHECK(!processor_->IsRunning());
162 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700163
164 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700165 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700166 new OmahaRequestAction(prefs_,
167 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700168 NULL,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800169 new LibcurlHttpFetcher(GetProxyResolver())));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700170 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700171 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700172 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700173 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700174 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700175 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700176 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700177 new OmahaRequestAction(prefs_,
178 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700179 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700180 OmahaEvent::kTypeUpdateDownloadStarted),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800181 new LibcurlHttpFetcher(GetProxyResolver())));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700182 shared_ptr<DownloadAction> download_action(
Andrew de los Reyes45168102010-11-22 11:13:50 -0800183 new DownloadAction(prefs_, new MultiHttpFetcher<LibcurlHttpFetcher>(
184 GetProxyResolver())));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700185 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700186 new OmahaRequestAction(prefs_,
187 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700188 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700189 OmahaEvent::kTypeUpdateDownloadFinished),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800190 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800191 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
192 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700193 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700194 new OmahaRequestAction(prefs_,
195 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700196 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800197 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700198
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700199 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700200 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700201 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700202
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700203 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
204 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
205 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700206 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700207 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700208 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700209 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700210 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800211 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700212 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700213
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700214 // Enqueue the actions
215 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
216 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700217 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700218 }
219
220 // Bond them together. We have to use the leaf-types when calling
221 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700222 BondActions(update_check_action.get(),
223 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700224 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700225 filesystem_copier_action.get());
226 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700227 kernel_filesystem_copier_action.get());
228 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700229 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700230 BondActions(download_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800231 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700232
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700233 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700234 processor_->StartProcessing();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700235}
236
Darin Petkov5a7f5652010-07-22 21:40:09 -0700237void UpdateAttempter::CheckForUpdate(const std::string& app_version,
238 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700239 if (status_ != UPDATE_STATUS_IDLE) {
240 LOG(INFO) << "Check for update requested, but status is "
241 << UpdateStatusToString(status_) << ", so not checking.";
242 return;
243 }
Andrew de los Reyes45168102010-11-22 11:13:50 -0800244 Update(app_version, omaha_url, true);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700245}
246
Darin Petkov296889c2010-07-23 16:20:54 -0700247bool UpdateAttempter::RebootIfNeeded() {
248 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
249 LOG(INFO) << "Reboot requested, but status is "
250 << UpdateStatusToString(status_) << ", so not rebooting.";
251 return false;
252 }
253 TEST_AND_RETURN_FALSE(utils::Reboot());
254 return true;
255}
256
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700257// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700258void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700259 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700260 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700261 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700262 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700263
Darin Petkovc6c135c2010-08-11 13:36:18 -0700264 // Reset process priority back to normal.
265 CleanupPriorityManagement();
266
Darin Petkov09f96c32010-07-20 09:24:57 -0700267 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
268 LOG(INFO) << "Error event sent.";
269 SetStatusAndNotify(UPDATE_STATUS_IDLE);
270 return;
271 }
272
Darin Petkovc1a8b422010-07-19 11:34:49 -0700273 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700274 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700275 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov9b230572010-10-08 10:20:09 -0700276 DeltaPerformer::ResetUpdateProgress(prefs_, false);
277 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700278
279 // Report the time it took to update the system.
280 int64_t update_time = time(NULL) - last_checked_time_;
281 metrics_lib_->SendToUMA("Installer.UpdateTime",
282 static_cast<int>(update_time), // sample
283 1, // min = 1 second
284 20 * 60, // max = 20 minutes
285 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700286 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700287 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700288
Darin Petkov1023a602010-08-30 13:47:51 -0700289 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700290 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700291 }
292 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700293 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700294}
295
296void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700297 // Reset process priority back to normal.
298 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700299 download_progress_ = 0.0;
300 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700301 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700302 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700303}
304
305// Called whenever an action has finished processing, either successfully
306// or otherwise.
307void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
308 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700309 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700310 // Reset download progress regardless of whether or not the download
311 // action succeeded. Also, get the response code from HTTP request
312 // actions (update download as well as the initial update check
313 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700314 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700315 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700316 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700317 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
318 http_response_code_ = download_action->GetHTTPResponseCode();
319 } else if (type == OmahaRequestAction::StaticType()) {
320 OmahaRequestAction* omaha_request_action =
321 dynamic_cast<OmahaRequestAction*>(action);
322 // If the request is not an event, then it's the update-check.
323 if (!omaha_request_action->IsEvent()) {
324 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700325 // Forward the server-dictated poll interval to the update check
326 // scheduler, if any.
327 if (update_check_scheduler_) {
328 update_check_scheduler_->set_poll_interval(
329 omaha_request_action->GetOutputObject().poll_interval);
330 }
Darin Petkov1023a602010-08-30 13:47:51 -0700331 }
332 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700333 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700334 // If this was a delta update attempt and the current state is at or past
335 // the download phase, count the failure in case a switch to full update
336 // becomes necessary. Ignore network transfer timeouts and failures.
337 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
338 !is_full_update_ &&
339 code != kActionCodeDownloadTransferError) {
340 MarkDeltaUpdateFailure();
341 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700342 // On failure, schedule an error event to be sent to Omaha.
343 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700344 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700345 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700346 // Find out which action completed.
347 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700348 // Note that the status will be updated to DOWNLOADING when some bytes get
349 // actually downloaded from the server and the BytesReceived callback is
350 // invoked. This avoids notifying the user that a download has started in
351 // cases when the server and the client are unable to initiate the download.
352 CHECK(action == response_handler_action_.get());
353 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700354 last_checked_time_ = time(NULL);
355 // TODO(adlr): put version in InstallPlan
356 new_version_ = "0.0.0.0";
357 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700358 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700359 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700360 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700361 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700362 } else if (type == DownloadAction::StaticType()) {
363 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
364 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700365}
366
367// Stop updating. An attempt will be made to record status to the disk
368// so that updates can be resumed later.
369void UpdateAttempter::Terminate() {
370 // TODO(adlr): implement this method.
371 NOTIMPLEMENTED();
372}
373
374// Try to resume from a previously Terminate()d update.
375void UpdateAttempter::ResumeUpdating() {
376 // TODO(adlr): implement this method.
377 NOTIMPLEMENTED();
378}
379
Darin Petkov9d911fa2010-08-19 09:36:08 -0700380void UpdateAttempter::SetDownloadStatus(bool active) {
381 download_active_ = active;
382 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
383}
384
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700385void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700386 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700387 LOG(ERROR) << "BytesReceived called while not downloading.";
388 return;
389 }
Darin Petkovaf183052010-08-23 12:07:13 -0700390 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700391 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700392 // Self throttle based on progress. Also send notifications if
393 // progress is too slow.
394 const double kDeltaPercent = 0.01; // 1%
395 if (status_ != UPDATE_STATUS_DOWNLOADING ||
396 bytes_received == total ||
397 progress - download_progress_ >= kDeltaPercent ||
398 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
399 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700400 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
401 }
402}
403
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700404bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
405 double* progress,
406 std::string* current_operation,
407 std::string* new_version,
408 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700409 *last_checked_time = last_checked_time_;
410 *progress = download_progress_;
411 *current_operation = UpdateStatusToString(status_);
412 *new_version = new_version_;
413 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700414 return true;
415}
416
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700417void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
418 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700419 if (update_check_scheduler_) {
420 update_check_scheduler_->SetUpdateStatus(status_);
421 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700422 if (!dbus_service_)
423 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700424 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700425 update_engine_service_emit_status_update(
426 dbus_service_,
427 last_checked_time_,
428 download_progress_,
429 UpdateStatusToString(status_),
430 new_version_.c_str(),
431 new_size_);
432}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700433
Darin Petkov777dbfa2010-07-20 15:03:37 -0700434void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
435 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700436 if (error_event_.get()) {
437 // This shouldn't really happen.
438 LOG(WARNING) << "There's already an existing pending error event.";
439 return;
440 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700441
442 // For now assume that Omaha response action failure means that
443 // there's no update so don't send an event. Also, double check that
444 // the failure has not occurred while sending an error event -- in
445 // which case don't schedule another. This shouldn't really happen
446 // but just in case...
447 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
448 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
449 return;
450 }
451
452 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700453 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
454 OmahaEvent::kResultError,
455 code));
456}
457
458bool UpdateAttempter::ScheduleErrorEventAction() {
459 if (error_event_.get() == NULL)
460 return false;
461
Darin Petkov1023a602010-08-30 13:47:51 -0700462 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700463 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700464 new OmahaRequestAction(prefs_,
465 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700466 error_event_.release(), // Pass ownership.
Andrew de los Reyes45168102010-11-22 11:13:50 -0800467 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov09f96c32010-07-20 09:24:57 -0700468 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700469 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700470 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700471 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700472 return true;
473}
474
Darin Petkovc6c135c2010-08-11 13:36:18 -0700475void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
476 if (priority_ == priority) {
477 return;
478 }
479 if (utils::SetProcessPriority(priority)) {
480 priority_ = priority;
481 LOG(INFO) << "Process priority = " << priority_;
482 }
483}
484
485void UpdateAttempter::SetupPriorityManagement() {
486 if (manage_priority_source_) {
487 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
488 CleanupPriorityManagement();
489 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700490 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700491 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
492 g_source_set_callback(manage_priority_source_,
493 StaticManagePriorityCallback,
494 this,
495 NULL);
496 g_source_attach(manage_priority_source_, NULL);
497 SetPriority(utils::kProcessPriorityLow);
498}
499
500void UpdateAttempter::CleanupPriorityManagement() {
501 if (manage_priority_source_) {
502 g_source_destroy(manage_priority_source_);
503 manage_priority_source_ = NULL;
504 }
505 SetPriority(utils::kProcessPriorityNormal);
506}
507
508gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
509 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
510}
511
512bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700513 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700514 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700515 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700516}
517
Darin Petkov36275772010-10-01 11:40:57 -0700518void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
519 int64_t delta_failures;
520 if (omaha_request_params_.delta_okay &&
521 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
522 delta_failures >= kMaxDeltaUpdateFailures) {
523 LOG(WARNING) << "Too many delta update failures, forcing full update.";
524 omaha_request_params_.delta_okay = false;
525 }
526}
527
528void UpdateAttempter::MarkDeltaUpdateFailure() {
529 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700530 // Don't try to resume a failed delta update.
531 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700532 int64_t delta_failures;
533 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
534 delta_failures < 0) {
535 delta_failures = 0;
536 }
537 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
538}
539
Darin Petkov9b230572010-10-08 10:20:09 -0700540void UpdateAttempter::SetupDownload() {
541 MultiHttpFetcher<LibcurlHttpFetcher>* fetcher =
542 dynamic_cast<MultiHttpFetcher<LibcurlHttpFetcher>*>(
543 download_action_->http_fetcher());
544 MultiHttpFetcher<LibcurlHttpFetcher>::RangesVect ranges;
545 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700546 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700547 int64_t manifest_metadata_size = 0;
548 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700549 ranges.push_back(make_pair(0, manifest_metadata_size));
550 // If there're remaining unprocessed data blobs, fetch them. Be careful not
551 // to request data beyond the end of the payload to avoid 416 HTTP response
552 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700553 int64_t next_data_offset = 0;
554 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700555 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
556 if (resume_offset < response_handler_action_->install_plan().size) {
557 ranges.push_back(make_pair(resume_offset, -1));
558 }
Darin Petkov9b230572010-10-08 10:20:09 -0700559 } else {
560 ranges.push_back(make_pair(0, -1));
561 }
562 fetcher->set_ranges(ranges);
563}
564
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700565} // namespace chromeos_update_engine