blob: e49a5511dda029f49c64b549643dcc5bf242f69e [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;
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 Reyes000d8952011-03-02 15:21:14 -0800128 chrome_proxy_resolver_.Init();
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700129 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
130 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
131 << "reboot";
132 return;
133 }
134 if (status_ != UPDATE_STATUS_IDLE) {
135 // Update in progress. Do nothing
136 return;
137 }
Darin Petkov1023a602010-08-30 13:47:51 -0700138 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700139 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700140 LOG(ERROR) << "Unable to initialize Omaha request device params.";
141 return;
142 }
Darin Petkov3aefa862010-12-07 14:45:00 -0800143
Andrew de los Reyes45168102010-11-22 11:13:50 -0800144 obeying_proxies_ = true;
145 if (obey_proxies || proxy_manual_checks_ == 0) {
146 LOG(INFO) << "forced to obey proxies";
147 // If forced to obey proxies, every 20th request will not use proxies
148 proxy_manual_checks_++;
149 LOG(INFO) << "proxy manual checks: " << proxy_manual_checks_;
150 if (proxy_manual_checks_ >= kMaxConsecutiveObeyProxyRequests) {
151 proxy_manual_checks_ = 0;
152 obeying_proxies_ = false;
153 }
154 } else if (base::RandInt(0, 4) == 0) {
155 obeying_proxies_ = false;
156 }
157 LOG_IF(INFO, !obeying_proxies_) << "To help ensure updates work, this update "
158 "check we are ignoring the proxy settings and using "
159 "direct connections.";
160
Darin Petkov36275772010-10-01 11:40:57 -0700161 DisableDeltaUpdateIfNeeded();
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700162 CHECK(!processor_->IsRunning());
163 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700164
165 // Actions:
Darin Petkova0929552010-11-29 14:19:06 -0800166 LibcurlHttpFetcher* update_check_fetcher =
167 new LibcurlHttpFetcher(GetProxyResolver());
Andrew de los Reyes5d0783d2010-11-29 18:14:16 -0800168 // Try harder to connect to the network. See comment in
169 // libcurl_http_fetcher.cc.
170 update_check_fetcher->set_no_network_max_retries(3);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700171 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700172 new OmahaRequestAction(prefs_,
173 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700174 NULL,
Darin Petkova0929552010-11-29 14:19:06 -0800175 update_check_fetcher)); // passes ownership
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700176 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
Darin Petkov73058b42010-10-06 16:32:19 -0700177 new OmahaResponseHandlerAction(prefs_));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700178 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800179 new FilesystemCopierAction(false, false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700180 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Darin Petkov3aefa862010-12-07 14:45:00 -0800181 new FilesystemCopierAction(true, false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700182 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700183 new OmahaRequestAction(prefs_,
184 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700185 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700186 OmahaEvent::kTypeUpdateDownloadStarted),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800187 new LibcurlHttpFetcher(GetProxyResolver())));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700188 shared_ptr<DownloadAction> download_action(
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800189 new DownloadAction(prefs_, new MultiRangeHTTPFetcher(
190 new LibcurlHttpFetcher(GetProxyResolver()))));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700191 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700192 new OmahaRequestAction(prefs_,
193 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700194 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700195 OmahaEvent::kTypeUpdateDownloadFinished),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800196 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov3aefa862010-12-07 14:45:00 -0800197 shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
198 new FilesystemCopierAction(false, true));
199 shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
200 new FilesystemCopierAction(true, true));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800201 shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
202 new PostinstallRunnerAction);
Darin Petkov8c2980e2010-07-16 15:16:49 -0700203 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700204 new OmahaRequestAction(prefs_,
205 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700206 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Andrew de los Reyes45168102010-11-22 11:13:50 -0800207 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700208
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700209 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700210 response_handler_action_ = response_handler_action;
Darin Petkov9b230572010-10-08 10:20:09 -0700211 download_action_ = download_action;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700212
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700213 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
214 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
215 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700216 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700217 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700218 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700219 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700220 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Darin Petkov3aefa862010-12-07 14:45:00 -0800221 actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
222 actions_.push_back(shared_ptr<AbstractAction>(
223 kernel_filesystem_verifier_action));
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800224 actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700225 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700226
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700227 // Enqueue the actions
228 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
229 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700230 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700231 }
232
233 // Bond them together. We have to use the leaf-types when calling
234 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700235 BondActions(update_check_action.get(),
236 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700237 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700238 filesystem_copier_action.get());
239 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700240 kernel_filesystem_copier_action.get());
241 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700242 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700243 BondActions(download_action.get(),
Darin Petkov3aefa862010-12-07 14:45:00 -0800244 filesystem_verifier_action.get());
245 BondActions(filesystem_verifier_action.get(),
246 kernel_filesystem_verifier_action.get());
247 BondActions(kernel_filesystem_verifier_action.get(),
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800248 postinstall_runner_action.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700249
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700250 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700251 processor_->StartProcessing();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700252}
253
Darin Petkov5a7f5652010-07-22 21:40:09 -0700254void UpdateAttempter::CheckForUpdate(const std::string& app_version,
255 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700256 if (status_ != UPDATE_STATUS_IDLE) {
257 LOG(INFO) << "Check for update requested, but status is "
258 << UpdateStatusToString(status_) << ", so not checking.";
259 return;
260 }
Andrew de los Reyes45168102010-11-22 11:13:50 -0800261 Update(app_version, omaha_url, true);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700262}
263
Darin Petkov296889c2010-07-23 16:20:54 -0700264bool UpdateAttempter::RebootIfNeeded() {
265 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
266 LOG(INFO) << "Reboot requested, but status is "
267 << UpdateStatusToString(status_) << ", so not rebooting.";
268 return false;
269 }
270 TEST_AND_RETURN_FALSE(utils::Reboot());
271 return true;
272}
273
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700274// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700275void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700276 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700277 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700278 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700279 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700280
Darin Petkovc6c135c2010-08-11 13:36:18 -0700281 // Reset process priority back to normal.
282 CleanupPriorityManagement();
283
Darin Petkov09f96c32010-07-20 09:24:57 -0700284 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
285 LOG(INFO) << "Error event sent.";
286 SetStatusAndNotify(UPDATE_STATUS_IDLE);
287 return;
288 }
289
Darin Petkovc1a8b422010-07-19 11:34:49 -0700290 if (code == kActionCodeSuccess) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700291 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov36275772010-10-01 11:40:57 -0700292 prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
Darin Petkov95508da2011-01-05 12:42:29 -0800293 prefs_->SetString(kPrefsPreviousVersion, omaha_request_params_.app_version);
Darin Petkov9b230572010-10-08 10:20:09 -0700294 DeltaPerformer::ResetUpdateProgress(prefs_, false);
295 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700296
297 // Report the time it took to update the system.
298 int64_t update_time = time(NULL) - last_checked_time_;
299 metrics_lib_->SendToUMA("Installer.UpdateTime",
300 static_cast<int>(update_time), // sample
301 1, // min = 1 second
302 20 * 60, // max = 20 minutes
303 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700304 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700305 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700306
Darin Petkov1023a602010-08-30 13:47:51 -0700307 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700308 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700309 }
310 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700311 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700312}
313
314void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700315 // Reset process priority back to normal.
316 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700317 download_progress_ = 0.0;
318 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700319 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700320 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700321}
322
323// Called whenever an action has finished processing, either successfully
324// or otherwise.
325void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
326 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700327 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700328 // Reset download progress regardless of whether or not the download
329 // action succeeded. Also, get the response code from HTTP request
330 // actions (update download as well as the initial update check
331 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700332 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700333 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700334 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700335 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
336 http_response_code_ = download_action->GetHTTPResponseCode();
337 } else if (type == OmahaRequestAction::StaticType()) {
338 OmahaRequestAction* omaha_request_action =
339 dynamic_cast<OmahaRequestAction*>(action);
340 // If the request is not an event, then it's the update-check.
341 if (!omaha_request_action->IsEvent()) {
342 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700343 // Forward the server-dictated poll interval to the update check
344 // scheduler, if any.
345 if (update_check_scheduler_) {
346 update_check_scheduler_->set_poll_interval(
347 omaha_request_action->GetOutputObject().poll_interval);
348 }
Darin Petkov1023a602010-08-30 13:47:51 -0700349 }
350 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700351 if (code != kActionCodeSuccess) {
Darin Petkov36275772010-10-01 11:40:57 -0700352 // If this was a delta update attempt and the current state is at or past
353 // the download phase, count the failure in case a switch to full update
354 // becomes necessary. Ignore network transfer timeouts and failures.
355 if (status_ >= UPDATE_STATUS_DOWNLOADING &&
356 !is_full_update_ &&
357 code != kActionCodeDownloadTransferError) {
358 MarkDeltaUpdateFailure();
359 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700360 // On failure, schedule an error event to be sent to Omaha.
361 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700362 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700363 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700364 // Find out which action completed.
365 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9b230572010-10-08 10:20:09 -0700366 // Note that the status will be updated to DOWNLOADING when some bytes get
367 // actually downloaded from the server and the BytesReceived callback is
368 // invoked. This avoids notifying the user that a download has started in
369 // cases when the server and the client are unable to initiate the download.
370 CHECK(action == response_handler_action_.get());
371 const InstallPlan& plan = response_handler_action_->install_plan();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700372 last_checked_time_ = time(NULL);
373 // TODO(adlr): put version in InstallPlan
374 new_version_ = "0.0.0.0";
375 new_size_ = plan.size;
Darin Petkov36275772010-10-01 11:40:57 -0700376 is_full_update_ = plan.is_full_update;
Darin Petkov9b230572010-10-08 10:20:09 -0700377 SetupDownload();
Darin Petkovc6c135c2010-08-11 13:36:18 -0700378 SetupPriorityManagement();
Darin Petkovb00bccc2010-10-26 14:13:08 -0700379 SetStatusAndNotify(UPDATE_STATUS_UPDATE_AVAILABLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700380 } else if (type == DownloadAction::StaticType()) {
381 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
382 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700383}
384
385// Stop updating. An attempt will be made to record status to the disk
386// so that updates can be resumed later.
387void UpdateAttempter::Terminate() {
388 // TODO(adlr): implement this method.
389 NOTIMPLEMENTED();
390}
391
392// Try to resume from a previously Terminate()d update.
393void UpdateAttempter::ResumeUpdating() {
394 // TODO(adlr): implement this method.
395 NOTIMPLEMENTED();
396}
397
Darin Petkov9d911fa2010-08-19 09:36:08 -0700398void UpdateAttempter::SetDownloadStatus(bool active) {
399 download_active_ = active;
400 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
401}
402
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700403void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700404 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700405 LOG(ERROR) << "BytesReceived called while not downloading.";
406 return;
407 }
Darin Petkovaf183052010-08-23 12:07:13 -0700408 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700409 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700410 // Self throttle based on progress. Also send notifications if
411 // progress is too slow.
412 const double kDeltaPercent = 0.01; // 1%
413 if (status_ != UPDATE_STATUS_DOWNLOADING ||
414 bytes_received == total ||
415 progress - download_progress_ >= kDeltaPercent ||
416 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
417 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700418 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
419 }
420}
421
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700422bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
423 double* progress,
424 std::string* current_operation,
425 std::string* new_version,
426 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700427 *last_checked_time = last_checked_time_;
428 *progress = download_progress_;
429 *current_operation = UpdateStatusToString(status_);
430 *new_version = new_version_;
431 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700432 return true;
433}
434
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700435void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
436 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700437 if (update_check_scheduler_) {
438 update_check_scheduler_->SetUpdateStatus(status_);
439 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700440 if (!dbus_service_)
441 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700442 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700443 update_engine_service_emit_status_update(
444 dbus_service_,
445 last_checked_time_,
446 download_progress_,
447 UpdateStatusToString(status_),
448 new_version_.c_str(),
449 new_size_);
450}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700451
Darin Petkov777dbfa2010-07-20 15:03:37 -0700452void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
453 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700454 if (error_event_.get()) {
455 // This shouldn't really happen.
456 LOG(WARNING) << "There's already an existing pending error event.";
457 return;
458 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700459
Darin Petkovabc7bc02011-02-23 14:39:43 -0800460 // For now assume that a generic Omaha response action failure means that
461 // there's no update so don't send an event. Also, double check that the
462 // failure has not occurred while sending an error event -- in which case
463 // don't schedule another. This shouldn't really happen but just in case...
464 if ((action->Type() == OmahaResponseHandlerAction::StaticType() &&
465 code == kActionCodeError) ||
Darin Petkov777dbfa2010-07-20 15:03:37 -0700466 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
467 return;
468 }
469
470 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700471 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
472 OmahaEvent::kResultError,
473 code));
474}
475
476bool UpdateAttempter::ScheduleErrorEventAction() {
477 if (error_event_.get() == NULL)
478 return false;
479
Darin Petkov1023a602010-08-30 13:47:51 -0700480 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700481 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700482 new OmahaRequestAction(prefs_,
483 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700484 error_event_.release(), // Pass ownership.
Andrew de los Reyes45168102010-11-22 11:13:50 -0800485 new LibcurlHttpFetcher(GetProxyResolver())));
Darin Petkov09f96c32010-07-20 09:24:57 -0700486 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700487 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700488 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700489 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700490 return true;
491}
492
Darin Petkovc6c135c2010-08-11 13:36:18 -0700493void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
494 if (priority_ == priority) {
495 return;
496 }
497 if (utils::SetProcessPriority(priority)) {
498 priority_ = priority;
499 LOG(INFO) << "Process priority = " << priority_;
500 }
501}
502
503void UpdateAttempter::SetupPriorityManagement() {
504 if (manage_priority_source_) {
505 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
506 CleanupPriorityManagement();
507 }
Darin Petkovf622ef72010-10-26 13:49:24 -0700508 const int kPriorityTimeout = 2 * 60 * 60; // 2 hours
Darin Petkovc6c135c2010-08-11 13:36:18 -0700509 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
510 g_source_set_callback(manage_priority_source_,
511 StaticManagePriorityCallback,
512 this,
513 NULL);
514 g_source_attach(manage_priority_source_, NULL);
515 SetPriority(utils::kProcessPriorityLow);
516}
517
518void UpdateAttempter::CleanupPriorityManagement() {
519 if (manage_priority_source_) {
520 g_source_destroy(manage_priority_source_);
521 manage_priority_source_ = NULL;
522 }
523 SetPriority(utils::kProcessPriorityNormal);
524}
525
526gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
527 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
528}
529
530bool UpdateAttempter::ManagePriorityCallback() {
Darin Petkovf622ef72010-10-26 13:49:24 -0700531 SetPriority(utils::kProcessPriorityNormal);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700532 manage_priority_source_ = NULL;
Darin Petkovf622ef72010-10-26 13:49:24 -0700533 return false; // Destroy the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700534}
535
Darin Petkov36275772010-10-01 11:40:57 -0700536void UpdateAttempter::DisableDeltaUpdateIfNeeded() {
537 int64_t delta_failures;
538 if (omaha_request_params_.delta_okay &&
539 prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) &&
540 delta_failures >= kMaxDeltaUpdateFailures) {
541 LOG(WARNING) << "Too many delta update failures, forcing full update.";
542 omaha_request_params_.delta_okay = false;
543 }
544}
545
546void UpdateAttempter::MarkDeltaUpdateFailure() {
547 CHECK(!is_full_update_);
Darin Petkov2dd01092010-10-08 15:43:05 -0700548 // Don't try to resume a failed delta update.
549 DeltaPerformer::ResetUpdateProgress(prefs_, false);
Darin Petkov36275772010-10-01 11:40:57 -0700550 int64_t delta_failures;
551 if (!prefs_->GetInt64(kPrefsDeltaUpdateFailures, &delta_failures) ||
552 delta_failures < 0) {
553 delta_failures = 0;
554 }
555 prefs_->SetInt64(kPrefsDeltaUpdateFailures, ++delta_failures);
556}
557
Darin Petkov9b230572010-10-08 10:20:09 -0700558void UpdateAttempter::SetupDownload() {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800559 MultiRangeHTTPFetcher* fetcher =
560 dynamic_cast<MultiRangeHTTPFetcher*>(download_action_->http_fetcher());
561 fetcher->ClearRanges();
Darin Petkov9b230572010-10-08 10:20:09 -0700562 if (response_handler_action_->install_plan().is_resume) {
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700563 // Resuming an update so fetch the update manifest metadata first.
Darin Petkov9b230572010-10-08 10:20:09 -0700564 int64_t manifest_metadata_size = 0;
565 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800566 fetcher->AddRange(0, manifest_metadata_size);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700567 // If there're remaining unprocessed data blobs, fetch them. Be careful not
568 // to request data beyond the end of the payload to avoid 416 HTTP response
569 // error codes.
Darin Petkov9b230572010-10-08 10:20:09 -0700570 int64_t next_data_offset = 0;
571 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700572 uint64_t resume_offset = manifest_metadata_size + next_data_offset;
573 if (resume_offset < response_handler_action_->install_plan().size) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800574 fetcher->AddRange(resume_offset, -1);
Darin Petkovb21ce5d2010-10-21 16:03:05 -0700575 }
Darin Petkov9b230572010-10-08 10:20:09 -0700576 } else {
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800577 fetcher->AddRange(0, -1);
Darin Petkov9b230572010-10-08 10:20:09 -0700578 }
Darin Petkov9b230572010-10-08 10:20:09 -0700579}
580
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700581} // namespace chromeos_update_engine