blob: 73b93b8b7829b2dc400fe049bd73235c5c2e0f99 [file] [log] [blame]
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/update_attempter.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -07006
7// From 'man clock_gettime': feature test macro: _POSIX_C_SOURCE >= 199309L
8#ifndef _POSIX_C_SOURCE
9#define _POSIX_C_SOURCE 199309L
10#endif // _POSIX_C_SOURCE
11#include <time.h>
12
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070013#include <tr1/memory>
14#include <string>
15#include <vector>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070016
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070017#include <glib.h>
Darin Petkov1023a602010-08-30 13:47:51 -070018#include <metrics/metrics_library.h>
Darin Petkov9d65b7b2010-07-20 09:13:01 -070019
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070020#include "update_engine/dbus_service.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021#include "update_engine/download_action.h"
22#include "update_engine/filesystem_copier_action.h"
23#include "update_engine/libcurl_http_fetcher.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070024#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070025#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026#include "update_engine/omaha_response_handler_action.h"
27#include "update_engine/postinstall_runner_action.h"
28#include "update_engine/set_bootable_flag_action.h"
Darin Petkov1023a602010-08-30 13:47:51 -070029#include "update_engine/update_check_scheduler.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070030
Darin Petkovaf183052010-08-23 12:07:13 -070031using base::TimeDelta;
32using base::TimeTicks;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070033using std::tr1::shared_ptr;
34using std::string;
35using std::vector;
36
37namespace chromeos_update_engine {
38
Darin Petkovcd1666f2010-09-23 09:53:44 -070039const char* kUpdateCompletedMarker =
40 "/var/run/update_engine_autoupdate_completed";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070041
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070042const char* UpdateStatusToString(UpdateStatus status) {
43 switch (status) {
44 case UPDATE_STATUS_IDLE:
45 return "UPDATE_STATUS_IDLE";
46 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
47 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
48 case UPDATE_STATUS_UPDATE_AVAILABLE:
49 return "UPDATE_STATUS_UPDATE_AVAILABLE";
50 case UPDATE_STATUS_DOWNLOADING:
51 return "UPDATE_STATUS_DOWNLOADING";
52 case UPDATE_STATUS_VERIFYING:
53 return "UPDATE_STATUS_VERIFYING";
54 case UPDATE_STATUS_FINALIZING:
55 return "UPDATE_STATUS_FINALIZING";
56 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
57 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070058 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
59 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070060 default:
61 return "unknown status";
62 }
63}
64
Darin Petkov777dbfa2010-07-20 15:03:37 -070065// Turns a generic kActionCodeError to a generic error code specific
66// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
67// not kActionCodeError, or the action is not matched, returns |code|
68// unchanged.
69ActionExitCode GetErrorCodeForAction(AbstractAction* action,
70 ActionExitCode code) {
71 if (code != kActionCodeError)
72 return code;
73
74 const string type = action->Type();
75 if (type == OmahaRequestAction::StaticType())
76 return kActionCodeOmahaRequestError;
77 if (type == OmahaResponseHandlerAction::StaticType())
78 return kActionCodeOmahaResponseHandlerError;
79 if (type == FilesystemCopierAction::StaticType())
80 return kActionCodeFilesystemCopierError;
81 if (type == PostinstallRunnerAction::StaticType())
82 return kActionCodePostinstallRunnerError;
83 if (type == SetBootableFlagAction::StaticType())
84 return kActionCodeSetBootableFlagError;
85
86 return code;
87}
88
Darin Petkovc6c135c2010-08-11 13:36:18 -070089UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
90 MetricsLibraryInterface* metrics_lib)
Darin Petkovf42cc1c2010-09-01 09:03:02 -070091 : processor_(new ActionProcessor()),
92 dbus_service_(NULL),
Darin Petkovc6c135c2010-08-11 13:36:18 -070093 prefs_(prefs),
94 metrics_lib_(metrics_lib),
Darin Petkov1023a602010-08-30 13:47:51 -070095 update_check_scheduler_(NULL),
96 http_response_code_(0),
Darin Petkovc6c135c2010-08-11 13:36:18 -070097 priority_(utils::kProcessPriorityNormal),
98 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -070099 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700100 status_(UPDATE_STATUS_IDLE),
101 download_progress_(0.0),
102 last_checked_time_(0),
103 new_version_("0.0.0.0"),
104 new_size_(0) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700105 if (utils::FileExists(kUpdateCompletedMarker))
106 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
107}
108
109UpdateAttempter::~UpdateAttempter() {
110 CleanupPriorityManagement();
111}
112
Darin Petkov5a7f5652010-07-22 21:40:09 -0700113void UpdateAttempter::Update(const std::string& app_version,
114 const std::string& omaha_url) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700115 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
116 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
117 << "reboot";
118 return;
119 }
120 if (status_ != UPDATE_STATUS_IDLE) {
121 // Update in progress. Do nothing
122 return;
123 }
Darin Petkov1023a602010-08-30 13:47:51 -0700124 http_response_code_ = 0;
Darin Petkov5a7f5652010-07-22 21:40:09 -0700125 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700126 LOG(ERROR) << "Unable to initialize Omaha request device params.";
127 return;
128 }
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700129 CHECK(!processor_->IsRunning());
130 processor_->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700131
132 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700133 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700134 new OmahaRequestAction(prefs_,
135 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700136 NULL,
137 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700138 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
139 new OmahaResponseHandlerAction);
140 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700141 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700142 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700143 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700144 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700145 new OmahaRequestAction(prefs_,
146 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700147 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700148 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700149 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700150 shared_ptr<DownloadAction> download_action(
151 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700152 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700153 new OmahaRequestAction(prefs_,
154 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700155 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700156 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700157 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700158 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
159 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700160 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
161 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700162 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
163 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700164 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700165 new OmahaRequestAction(prefs_,
166 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700167 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700168 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700169
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700170 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700171 response_handler_action_ = response_handler_action;
172
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700173 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
174 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
175 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700176 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700177 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700178 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700179 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700180 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700181 actions_.push_back(shared_ptr<AbstractAction>(
182 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700183 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700184 actions_.push_back(shared_ptr<AbstractAction>(
185 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700186 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700187
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700188 // Enqueue the actions
189 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
190 it != actions_.end(); ++it) {
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700191 processor_->EnqueueAction(it->get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700192 }
193
194 // Bond them together. We have to use the leaf-types when calling
195 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700196 BondActions(update_check_action.get(),
197 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700198 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700199 filesystem_copier_action.get());
200 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700201 kernel_filesystem_copier_action.get());
202 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700203 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700204 BondActions(download_action.get(),
205 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700206 BondActions(postinstall_runner_action_precommit.get(),
207 set_bootable_flag_action.get());
208 BondActions(set_bootable_flag_action.get(),
209 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700210
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700211 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700212 processor_->StartProcessing();
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700213}
214
Darin Petkov5a7f5652010-07-22 21:40:09 -0700215void UpdateAttempter::CheckForUpdate(const std::string& app_version,
216 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700217 if (status_ != UPDATE_STATUS_IDLE) {
218 LOG(INFO) << "Check for update requested, but status is "
219 << UpdateStatusToString(status_) << ", so not checking.";
220 return;
221 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700222 Update(app_version, omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700223}
224
Darin Petkov296889c2010-07-23 16:20:54 -0700225bool UpdateAttempter::RebootIfNeeded() {
226 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
227 LOG(INFO) << "Reboot requested, but status is "
228 << UpdateStatusToString(status_) << ", so not rebooting.";
229 return false;
230 }
231 TEST_AND_RETURN_FALSE(utils::Reboot());
232 return true;
233}
234
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700235// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700236void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700237 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700238 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700239 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700240 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700241
Darin Petkovc6c135c2010-08-11 13:36:18 -0700242 // Reset process priority back to normal.
243 CleanupPriorityManagement();
244
Darin Petkov09f96c32010-07-20 09:24:57 -0700245 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
246 LOG(INFO) << "Error event sent.";
247 SetStatusAndNotify(UPDATE_STATUS_IDLE);
248 return;
249 }
250
Darin Petkovc1a8b422010-07-19 11:34:49 -0700251 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700252 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700253 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700254
255 // Report the time it took to update the system.
256 int64_t update_time = time(NULL) - last_checked_time_;
257 metrics_lib_->SendToUMA("Installer.UpdateTime",
258 static_cast<int>(update_time), // sample
259 1, // min = 1 second
260 20 * 60, // max = 20 minutes
261 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700262 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700263 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700264
Darin Petkov1023a602010-08-30 13:47:51 -0700265 if (ScheduleErrorEventAction()) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700266 return;
Darin Petkov1023a602010-08-30 13:47:51 -0700267 }
268 LOG(INFO) << "No update.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700269 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700270}
271
272void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700273 // Reset process priority back to normal.
274 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700275 download_progress_ = 0.0;
276 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700277 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700278 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700279}
280
281// Called whenever an action has finished processing, either successfully
282// or otherwise.
283void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
284 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700285 ActionExitCode code) {
Darin Petkov1023a602010-08-30 13:47:51 -0700286 // Reset download progress regardless of whether or not the download
287 // action succeeded. Also, get the response code from HTTP request
288 // actions (update download as well as the initial update check
289 // actions).
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700290 const string type = action->Type();
Darin Petkov1023a602010-08-30 13:47:51 -0700291 if (type == DownloadAction::StaticType()) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700292 download_progress_ = 0.0;
Darin Petkov1023a602010-08-30 13:47:51 -0700293 DownloadAction* download_action = dynamic_cast<DownloadAction*>(action);
294 http_response_code_ = download_action->GetHTTPResponseCode();
295 } else if (type == OmahaRequestAction::StaticType()) {
296 OmahaRequestAction* omaha_request_action =
297 dynamic_cast<OmahaRequestAction*>(action);
298 // If the request is not an event, then it's the update-check.
299 if (!omaha_request_action->IsEvent()) {
300 http_response_code_ = omaha_request_action->GetHTTPResponseCode();
Darin Petkov85ced132010-09-01 10:20:56 -0700301 // Forward the server-dictated poll interval to the update check
302 // scheduler, if any.
303 if (update_check_scheduler_) {
304 update_check_scheduler_->set_poll_interval(
305 omaha_request_action->GetOutputObject().poll_interval);
306 }
Darin Petkov1023a602010-08-30 13:47:51 -0700307 }
308 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700309 if (code != kActionCodeSuccess) {
Darin Petkov777dbfa2010-07-20 15:03:37 -0700310 // On failure, schedule an error event to be sent to Omaha.
311 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700312 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700313 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700314 // Find out which action completed.
315 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700316 // Note that the status will be updated to DOWNLOADING when some
317 // bytes get actually downloaded from the server and the
318 // BytesReceived callback is invoked. This avoids notifying the
319 // user that a download has started in cases when the server and
320 // the client are unable to initiate the download.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700321 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700322 dynamic_cast<OmahaResponseHandlerAction*>(action);
323 CHECK(omaha_response_handler_action);
324 const InstallPlan& plan = omaha_response_handler_action->install_plan();
325 last_checked_time_ = time(NULL);
326 // TODO(adlr): put version in InstallPlan
327 new_version_ = "0.0.0.0";
328 new_size_ = plan.size;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700329 SetupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700330 } else if (type == DownloadAction::StaticType()) {
331 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
332 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700333}
334
335// Stop updating. An attempt will be made to record status to the disk
336// so that updates can be resumed later.
337void UpdateAttempter::Terminate() {
338 // TODO(adlr): implement this method.
339 NOTIMPLEMENTED();
340}
341
342// Try to resume from a previously Terminate()d update.
343void UpdateAttempter::ResumeUpdating() {
344 // TODO(adlr): implement this method.
345 NOTIMPLEMENTED();
346}
347
Darin Petkov9d911fa2010-08-19 09:36:08 -0700348void UpdateAttempter::SetDownloadStatus(bool active) {
349 download_active_ = active;
350 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
351}
352
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700353void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700354 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700355 LOG(ERROR) << "BytesReceived called while not downloading.";
356 return;
357 }
Darin Petkovaf183052010-08-23 12:07:13 -0700358 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700359 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700360 // Self throttle based on progress. Also send notifications if
361 // progress is too slow.
362 const double kDeltaPercent = 0.01; // 1%
363 if (status_ != UPDATE_STATUS_DOWNLOADING ||
364 bytes_received == total ||
365 progress - download_progress_ >= kDeltaPercent ||
366 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
367 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700368 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
369 }
370}
371
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700372bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
373 double* progress,
374 std::string* current_operation,
375 std::string* new_version,
376 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700377 *last_checked_time = last_checked_time_;
378 *progress = download_progress_;
379 *current_operation = UpdateStatusToString(status_);
380 *new_version = new_version_;
381 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700382 return true;
383}
384
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700385void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
386 status_ = status;
Darin Petkov1023a602010-08-30 13:47:51 -0700387 if (update_check_scheduler_) {
388 update_check_scheduler_->SetUpdateStatus(status_);
389 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700390 if (!dbus_service_)
391 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700392 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700393 update_engine_service_emit_status_update(
394 dbus_service_,
395 last_checked_time_,
396 download_progress_,
397 UpdateStatusToString(status_),
398 new_version_.c_str(),
399 new_size_);
400}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700401
Darin Petkov777dbfa2010-07-20 15:03:37 -0700402void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
403 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700404 if (error_event_.get()) {
405 // This shouldn't really happen.
406 LOG(WARNING) << "There's already an existing pending error event.";
407 return;
408 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700409
410 // For now assume that Omaha response action failure means that
411 // there's no update so don't send an event. Also, double check that
412 // the failure has not occurred while sending an error event -- in
413 // which case don't schedule another. This shouldn't really happen
414 // but just in case...
415 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
416 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
417 return;
418 }
419
420 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700421 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
422 OmahaEvent::kResultError,
423 code));
424}
425
426bool UpdateAttempter::ScheduleErrorEventAction() {
427 if (error_event_.get() == NULL)
428 return false;
429
Darin Petkov1023a602010-08-30 13:47:51 -0700430 LOG(INFO) << "Update failed -- reporting the error event.";
Darin Petkov09f96c32010-07-20 09:24:57 -0700431 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700432 new OmahaRequestAction(prefs_,
433 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700434 error_event_.release(), // Pass ownership.
435 new LibcurlHttpFetcher));
436 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700437 processor_->EnqueueAction(error_event_action.get());
Darin Petkov09f96c32010-07-20 09:24:57 -0700438 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700439 processor_->StartProcessing();
Darin Petkov09f96c32010-07-20 09:24:57 -0700440 return true;
441}
442
Darin Petkovc6c135c2010-08-11 13:36:18 -0700443void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
444 if (priority_ == priority) {
445 return;
446 }
447 if (utils::SetProcessPriority(priority)) {
448 priority_ = priority;
449 LOG(INFO) << "Process priority = " << priority_;
450 }
451}
452
453void UpdateAttempter::SetupPriorityManagement() {
454 if (manage_priority_source_) {
455 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
456 CleanupPriorityManagement();
457 }
458 const int kPriorityTimeout = 10 * 60; // 10 minutes
459 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
460 g_source_set_callback(manage_priority_source_,
461 StaticManagePriorityCallback,
462 this,
463 NULL);
464 g_source_attach(manage_priority_source_, NULL);
465 SetPriority(utils::kProcessPriorityLow);
466}
467
468void UpdateAttempter::CleanupPriorityManagement() {
469 if (manage_priority_source_) {
470 g_source_destroy(manage_priority_source_);
471 manage_priority_source_ = NULL;
472 }
473 SetPriority(utils::kProcessPriorityNormal);
474}
475
476gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
477 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
478}
479
480bool UpdateAttempter::ManagePriorityCallback() {
481 // If the current process priority is below normal, set it to normal
482 // and let GLib invoke this callback again.
483 if (utils::ComparePriorities(priority_, utils::kProcessPriorityNormal) < 0) {
484 SetPriority(utils::kProcessPriorityNormal);
485 return true;
486 }
487 // Set the priority to high and let GLib destroy the timeout source.
488 SetPriority(utils::kProcessPriorityHigh);
489 manage_priority_source_ = NULL;
490 return false;
491}
492
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700493} // namespace chromeos_update_engine