blob: f56c0d1275ffd3dca0aeb1e720b5ac59e342a3e4 [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"
Andrew de los Reyes819fef22010-12-17 11:33:58 -080025#include "update_engine/multi_range_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;
Andrew de los Reyes21816e12011-04-07 14:18:56 -070035using google::protobuf::NewPermanentCallback;
Darin Petkov9b230572010-10-08 10:20:09 -070036using std::make_pair;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070037using std::tr1::shared_ptr;
38using std::string;
39using std::vector;
40
41namespace chromeos_update_engine {
42
Darin Petkov36275772010-10-01 11:40:57 -070043const int UpdateAttempter::kMaxDeltaUpdateFailures = 3;
44
Darin Petkovcd1666f2010-09-23 09:53:44 -070045const char* kUpdateCompletedMarker =
46 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070047
Andrew de los Reyes45168102010-11-22 11:13:50 -080048namespace {
49const int kMaxConsecutiveObeyProxyRequests = 20;
50} // namespace {}
51
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070052const char* UpdateStatusToString(UpdateStatus status) {
53 switch (status) {
54 case UPDATE_STATUS_IDLE:
55 return "UPDATE_STATUS_IDLE";
56 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
57 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
58 case UPDATE_STATUS_UPDATE_AVAILABLE:
59 return "UPDATE_STATUS_UPDATE_AVAILABLE";
60 case UPDATE_STATUS_DOWNLOADING:
61 return "UPDATE_STATUS_DOWNLOADING";
62 case UPDATE_STATUS_VERIFYING:
63 return "UPDATE_STATUS_VERIFYING";
64 case UPDATE_STATUS_FINALIZING:
65 return "UPDATE_STATUS_FINALIZING";
66 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
67 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070068 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
69 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070070 default:
71 return "unknown status";
72 }
73}
74
Darin Petkov777dbfa2010-07-20 15:03:37 -070075// Turns a generic kActionCodeError to a generic error code specific
76// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
77// not kActionCodeError, or the action is not matched, returns |code|
78// unchanged.
79ActionExitCode GetErrorCodeForAction(AbstractAction* action,
80 ActionExitCode code) {
81 if (code != kActionCodeError)
82 return code;
83
84 const string type = action->Type();
85 if (type == OmahaRequestAction::StaticType())
86 return kActionCodeOmahaRequestError;
87 if (type == OmahaResponseHandlerAction::StaticType())
88 return kActionCodeOmahaResponseHandlerError;
89 if (type == FilesystemCopierAction::StaticType())
90 return kActionCodeFilesystemCopierError;
91 if (type == PostinstallRunnerAction::StaticType())
92 return kActionCodePostinstallRunnerError;
Darin Petkov777dbfa2010-07-20 15:03:37 -070093
94 return code;
95}
96
Darin Petkovc6c135c2010-08-11 13:36:18 -070097UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
Andrew de los Reyes45168102010-11-22 11:13:50 -080098 MetricsLibraryInterface* metrics_lib,
99 DbusGlibInterface* dbus_iface)
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700100 : processor_(new ActionProcessor()),
101 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700102 prefs_(prefs),
103 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -0700104 update_check_scheduler_(NULL),
105 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700106 priority_(utils::kProcessPriorityNormal),
107 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700108 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700109 status_(UPDATE_STATUS_IDLE),
110 download_progress_(0.0),
111 last_checked_time_(0),
112 new_version_("0.0.0.0"),
Darin Petkov36275772010-10-01 11:40:57 -0700113 new_size_(0),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800114 is_full_update_(false),
115 proxy_manual_checks_(0),
116 obeying_proxies_(true),
117 chrome_proxy_resolver_(dbus_iface) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700118 if (utils::FileExists(kUpdateCompletedMarker))
119 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
120}
121
122UpdateAttempter::~UpdateAttempter() {
123 CleanupPriorityManagement();
124}
125
Darin Petkov5a7f5652010-07-22 21:40:09 -0700126void UpdateAttempter::Update(const std::string& app_version,
Andrew de los Reyes45168102010-11-22 11:13:50 -0800127 const std::string& omaha_url,
128 bool obey_proxies) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800129 chrome_proxy_resolver_.Init();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700130 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
131 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
132 << "reboot";
133 return;
134 }
135 if (status_ != UPDATE_STATUS_IDLE) {
136 // Update in progress. Do nothing
137 return;
138 }
Darin Petkov1023a602010-08-30 13:47:51 -0700139 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700140 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700141 LOG(ERROR) << "Unable to initialize Omaha request device params.";
142 return;
143 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800144
Andrew de los Reyes45168102010-11-22 11:13:50 -0800145 obeying_proxies_ = true;
146 if (obey_proxies || proxy_manual_checks_ == 0) {
147 LOG(INFO) << "forced to obey proxies";
148 // If forced to obey proxies, every 20th request will not use proxies
149 proxy_manual_checks_++;
150 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
151 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
152 proxy_manual_checks_ = 0;
153 obeying_proxies_ = false;
154 }
155 } else if (base::RandInt(0, 4) == 0) {
156 obeying_proxies_ = false;
157 }
158 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
159 "check we are ignoring the proxy settings and using "
160 "direct connections.";
161
Darin Petkov36275772010-10-01 11:40:57 -0700162 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700163 CHECK(!processor_->IsRunning());
164 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700165
166 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800167 LibcurlHttpFetcher* update_check_fetcher =
168 new LibcurlHttpFetcher(GetProxyResolver());
Andrew de los Reyes5d0783d2010-11-29 18:14:16 -0800169 // Try harder to connect to the network. See comment in
170 // libcurl_http_fetcher.cc.
171 update_check_fetcher->set_no_network_max_retries(3);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700172 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700173 new OmahaRequestAction(prefs_,
174 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700175 NULL,
Darin Petkova0929552010-11-29 14:19:06 -0800176 update_check_fetcher)); // passes ownership
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700177 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700178 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700179 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800180 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700181 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800182 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700183 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700184 new OmahaRequestAction(prefs_,
185 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700186 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700187 OmahaEvent::kTypeUpdateDownloadStarted),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800188 new LibcurlHttpFetcher(GetProxyResolver())));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700189 shared_ptr<DownloadAction> download_action(
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800190 new DownloadAction(prefs_, new MultiRangeHTTPFetcher(
191 new LibcurlHttpFetcher(GetProxyResolver()))));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700192 // This action is always initially in place to warn OS vendor of a
193 // signature failure. If it's not needed, it will be told to skip.
194 shared_ptr<OmahaRequestAction> download_signature_warning(
195 new OmahaRequestAction(
196 prefs_,
197 omaha_request_params_,
198 new OmahaEvent(
199 OmahaEvent::kTypeUpdateDownloadFinished,
200 OmahaEvent::kResultError,
201 kActionCodeDownloadPayloadPubKeyVerificationError),
202 new LibcurlHttpFetcher(GetProxyResolver())));
203 download_action->set_skip_reporting_signature_fail(
204 NewPermanentCallback(download_signature_warning.get(),
205 &OmahaRequestAction::set_should_skip,
206 true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700207 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700208 new OmahaRequestAction(prefs_,
209 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700210 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700211 OmahaEvent::kTypeUpdateDownloadFinished),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800212 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov3aefa862010-12-07 14:45:00 -0800213 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
214 new FilesystemCopierAction(false, true));
215 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
216 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800217 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
218 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700219 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700220 new OmahaRequestAction(prefs_,
221 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700222 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800223 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700224
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700225 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700226 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700227 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700228
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700229 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
230 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
231 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700232 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700233 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700234 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700235 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Andrew de los Reyes21816e12011-04-07 14:18:56 -0700236 actions_.push_back(shared_ptr<AbstractAction>(download_signature_warning));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700237 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800238 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
239 actions_.push_back(shared_ptr<AbstractAction>(
240 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800241 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700242 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700243
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700244 // Enqueue the actions
245 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
246 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700247 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700248 }
249
250 // Bond them together. We have to use the leaf-types when calling
251 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700252 BondActions(update_check_action.get(),
253 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700254 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700255 filesystem_copier_action.get());
256 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700257 kernel_filesystem_copier_action.get());
258 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700259 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700260 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800261 filesystem_verifier_action.get());
262 BondActions(filesystem_verifier_action.get(),
263 kernel_filesystem_verifier_action.get());
264 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800265 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700266
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700267 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkove6ef2f82011-03-07 17:31:11 -0800268
269 // Start the processing asynchronously to unblock the event loop.
270 g_idle_add(&StaticStartProcessing, this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700271}
272
Darin Petkov5a7f5652010-07-22 21:40:09 -0700273void UpdateAttempter::CheckForUpdate(const std::string& app_version,
274 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700275 if (status_ != UPDATE_STATUS_IDLE) {
276 LOG(INFO) << "Check for update requested, but status is "
277 << UpdateStatusToString(status_) << ", so not checking.";
278 return;
279 }
Andrew de los Reyes45168102010-11-22 11:13:50 -0800280 Update(app_version, omaha_url, true);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700281}
282
Darin Petkov296889c2010-07-23 16:20:54 -0700283bool UpdateAttempter::RebootIfNeeded() {
284 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
285 LOG(INFO) << "Reboot requested, but status is "
286 << UpdateStatusToString(status_) << ", so not rebooting.";
287 return false;
288 }
289 TEST_AND_RETURN_FALSE(utils::Reboot());
290 return true;
291}
292
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700293// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700294void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700295 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700296 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700297 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700298 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700299
Darin Petkovc6c135c2010-08-11 13:36:18 -0700300 // Reset process priority back to normal.
301 CleanupPriorityManagement();
302
Darin Petkov09f96c32010-07-20 09:24:57 -0700303 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
304 LOG(INFO) << "Error event sent.";
305 SetStatusAndNotify(UPDATE_STATUS_IDLE);
306 return;
307 }
308
Darin Petkovc1a8b422010-07-19 11:34:49 -0700309 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700310 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700311 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov95508da2011-01-05 12:42:29 -0800312 prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version);
Darin Petkov9b230572010-10-08 10:20:09 -0700313 DeltaPerformer::ResetUpdateProgress(prefs_, false);
314 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700315
316 // Report the time it took to update the system.
317 int64_t update_time = time(NULL) - last_checked_time_;
318 metrics_lib_->SendToUMA("Installer.UpdateTime",
319 static_cast<int>(update_time), // sample
320 1, // min = 1 second
321 20 * 60, // max = 20 minutes
322 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700323 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700324 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700325
Darin Petkov1023a602010-08-30 13:47:51 -0700326 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700327 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700328 }
329 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700330 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700331}
332
333void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700334 // Reset process priority back to normal.
335 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700336 download_progress_ = 0.0;
337 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700338 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700339 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700340}
341
342// Called whenever an action has finished processing, either successfully
343// or otherwise.
344void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
345 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700346 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700347 // Reset download progress regardless of whether or not the download
348 // action succeeded. Also, get the response code from HTTP request
349 // actions (update download as well as the initial update check
350 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700351 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700352 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700353 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700354 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
355 http_response_code_ = download_action->GetHTTPResponseCode();
356 } else if (type == OmahaRequestAction::StaticType()) {
357 OmahaRequestAction* omaha_request_action =
358 dynamic_cast<OmahaRequestAction*>(action);
359 // If the request is not an event, then it's the update-check.
360 if (!omaha_request_action->IsEvent()) {
361 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700362 // Forward the server-dictated poll interval to the update check
363 // scheduler, if any.
364 if (update_check_scheduler_) {
365 update_check_scheduler_->set_poll_interval(
366 omaha_request_action->GetOutputObject().poll_interval);
367 }
Darin Petkov1023a602010-08-30 13:47:51 -0700368 }
369 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700370 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700371 // If this was a delta update attempt and the current state is at or past
372 // the download phase, count the failure in case a switch to full update
373 // becomes necessary. Ignore network transfer timeouts and failures.
374 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
375 !is_full_update_ &&
376 code != kActionCodeDownloadTransferError) {
377 MarkDeltaUpdateFailure();
378 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700379 // On failure, schedule an error event to be sent to Omaha.
380 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700381 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700382 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700383 // Find out which action completed.
384 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700385 // Note that the status will be updated to DOWNLOADING when some bytes get
386 // actually downloaded from the server and the BytesReceived callback is
387 // invoked. This avoids notifying the user that a download has started in
388 // cases when the server and the client are unable to initiate the download.
389 CHECK(action == response_handler_action_.get());
390 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700391 last_checked_time_ = time(NULL);
392 // TODO(adlr): put version in InstallPlan
393 new_version_ = "0.0.0.0";
394 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700395 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700396 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700397 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700398 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700399 } else if (type == DownloadAction::StaticType()) {
400 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
401 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700402}
403
404// Stop updating. An attempt will be made to record status to the disk
405// so that updates can be resumed later.
406void UpdateAttempter::Terminate() {
407 // TODO(adlr): implement this method.
408 NOTIMPLEMENTED();
409}
410
411// Try to resume from a previously Terminate()d update.
412void UpdateAttempter::ResumeUpdating() {
413 // TODO(adlr): implement this method.
414 NOTIMPLEMENTED();
415}
416
Darin Petkov9d911fa2010-08-19 09:36:08 -0700417void UpdateAttempter::SetDownloadStatus(bool active) {
418 download_active_ = active;
419 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
420}
421
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700422void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700423 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700424 LOG(ERROR) << "BytesReceived called while not downloading.";
425 return;
426 }
Darin Petkovaf183052010-08-23 12:07:13 -0700427 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700428 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700429 // Self throttle based on progress. Also send notifications if
430 // progress is too slow.
431 const double kDeltaPercent = 0.01; // 1%
432 if (status_ != UPDATE_STATUS_DOWNLOADING ||
433 bytes_received == total ||
434 progress - download_progress_ >= kDeltaPercent ||
435 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
436 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700437 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
438 }
439}
440
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700441bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
442 double* progress,
443 std::string* current_operation,
444 std::string* new_version,
445 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700446 *last_checked_time = last_checked_time_;
447 *progress = download_progress_;
448 *current_operation = UpdateStatusToString(status_);
449 *new_version = new_version_;
450 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700451 return true;
452}
453
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700454void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
455 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700456 if (update_check_scheduler_) {
457 update_check_scheduler_->SetUpdateStatus(status_);
458 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700459 if (!dbus_service_)
460 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700461 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700462 update_engine_service_emit_status_update(
463 dbus_service_,
464 last_checked_time_,
465 download_progress_,
466 UpdateStatusToString(status_),
467 new_version_.c_str(),
468 new_size_);
469}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700470
Darin Petkov777dbfa2010-07-20 15:03:37 -0700471void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
472 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700473 if (error_event_.get()) {
474 // This shouldn't really happen.
475 LOG(WARNING) << "There's already an existing pending error event.";
476 return;
477 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700478
Darin Petkovabc7bc02011-02-23 14:39:43 -0800479 // For now assume that a generic Omaha response action failure means that
480 // there's no update so don't send an event. Also, double check that the
481 // failure has not occurred while sending an error event -- in which case
482 // don't schedule another. This shouldn't really happen but just in case...
483 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
484 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700485 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
486 return;
487 }
488
489 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700490 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
491 OmahaEvent::kResultError,
492 code));
493}
494
495bool UpdateAttempter::ScheduleErrorEventAction() {
496 if (error_event_.get() == NULL)
497 return false;
498
Darin Petkov1023a602010-08-30 13:47:51 -0700499 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700500 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700501 new OmahaRequestAction(prefs_,
502 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700503 error_event_.release(), // Pass ownership.
Andrew de los Reyes45168102010-11-22 11:13:50 -0800504 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov09f96c32010-07-20 09:24:57 -0700505 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700506 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700507 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700508 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700509 return true;
510}
511
Darin Petkovc6c135c2010-08-11 13:36:18 -0700512void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
513 if (priority_ == priority) {
514 return;
515 }
516 if (utils::SetProcessPriority(priority)) {
517 priority_ = priority;
518 LOG(INFO) << "Process priority = " << priority_;
519 }
520}
521
522void UpdateAttempter::SetupPriorityManagement() {
523 if (manage_priority_source_) {
524 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
525 CleanupPriorityManagement();
526 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700527 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700528 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
529 g_source_set_callback(manage_priority_source_,
530 StaticManagePriorityCallback,
531 this,
532 NULL);
533 g_source_attach(manage_priority_source_, NULL);
534 SetPriority(utils::kProcessPriorityLow);
535}
536
537void UpdateAttempter::CleanupPriorityManagement() {
538 if (manage_priority_source_) {
539 g_source_destroy(manage_priority_source_);
540 manage_priority_source_ = NULL;
541 }
542 SetPriority(utils::kProcessPriorityNormal);
543}
544
545gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
546 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
547}
548
Darin Petkove6ef2f82011-03-07 17:31:11 -0800549gboolean UpdateAttempter::StaticStartProcessing(gpointer data) {
550 reinterpret_cast<UpdateAttempter*>(data)->processor_->StartProcessing();
551 return FALSE; // Don't call this callback again.
552}
553
Darin Petkovc6c135c2010-08-11 13:36:18 -0700554bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700555 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700556 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700557 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700558}
559
Darin Petkov36275772010-10-01 11:40:57 -0700560void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
561 int64_t delta_failures;
562 if (omaha_request_params_.delta_okay &&
563 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
564 delta_failures >= kMaxDeltaUpdateFailures) {
565 LOG(WARNING) << "Too many delta update failures, forcing full update.";
566 omaha_request_params_.delta_okay = false;
567 }
568}
569
570void UpdateAttempter::MarkDeltaUpdateFailure() {
571 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700572 // Don't try to resume a failed delta update.
573 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700574 int64_t delta_failures;
575 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
576 delta_failures < 0) {
577 delta_failures = 0;
578 }
579 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
580}
581
Darin Petkov9b230572010-10-08 10:20:09 -0700582void UpdateAttempter::SetupDownload() {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800583 MultiRangeHTTPFetcher* fetcher =
584 dynamic_cast<MultiRangeHTTPFetcher*>(download_action_->http_fetcher());
585 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -0700586 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700587 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700588 int64_t manifest_metadata_size = 0;
589 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800590 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700591 // If there're remaining unprocessed data blobs, fetch them. Be careful not
592 // to request data beyond the end of the payload to avoid 416 HTTP response
593 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700594 int64_t next_data_offset = 0;
595 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700596 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
597 if (resume_offset < response_handler_action_->install_plan().size) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800598 fetcher->AddRange(resume_offset, -1);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700599 }
Darin Petkov9b230572010-10-08 10:20:09 -0700600 } else {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800601 fetcher->AddRange(0, -1);
Darin Petkov9b230572010-10-08 10:20:09 -0700602 }
Darin Petkov9b230572010-10-08 10:20:09 -0700603}
604
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700605} // namespace chromeos_update_engine