blob: b0c470bc4872d1eb2ad419e9584f5ee573488af3 [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 Petkov9d65b7b2010-07-20 09:13:01 -070018
19#include "metrics/metrics_library.h"
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"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070029
Darin Petkovaf183052010-08-23 12:07:13 -070030using base::TimeDelta;
31using base::TimeTicks;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070032using std::tr1::shared_ptr;
33using std::string;
34using std::vector;
35
36namespace chromeos_update_engine {
37
Darin Petkov1cd885f2010-08-26 09:39:53 -070038namespace {
39
40const int kTimeoutOnce = 7 * 60; // at 7 minutes
41const int kTimeoutPeriodic = 45 * 60; // every 45 minutes
42const int kTimeoutFuzz = 10 * 60; // +/- 5 minutes
43
44gboolean CheckForUpdatePeriodically(void* arg) {
45 UpdateAttempter* update_attempter = reinterpret_cast<UpdateAttempter*>(arg);
46 update_attempter->Update("", "");
47 update_attempter->SchedulePeriodicUpdateCheck(kTimeoutPeriodic);
48 return FALSE; // Don't run again.
49}
50
51} // namespace {}
52
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070053const char* kUpdateCompletedMarker = "/tmp/update_engine_autoupdate_completed";
54
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070055const char* UpdateStatusToString(UpdateStatus status) {
56 switch (status) {
57 case UPDATE_STATUS_IDLE:
58 return "UPDATE_STATUS_IDLE";
59 case UPDATE_STATUS_CHECKING_FOR_UPDATE:
60 return "UPDATE_STATUS_CHECKING_FOR_UPDATE";
61 case UPDATE_STATUS_UPDATE_AVAILABLE:
62 return "UPDATE_STATUS_UPDATE_AVAILABLE";
63 case UPDATE_STATUS_DOWNLOADING:
64 return "UPDATE_STATUS_DOWNLOADING";
65 case UPDATE_STATUS_VERIFYING:
66 return "UPDATE_STATUS_VERIFYING";
67 case UPDATE_STATUS_FINALIZING:
68 return "UPDATE_STATUS_FINALIZING";
69 case UPDATE_STATUS_UPDATED_NEED_REBOOT:
70 return "UPDATE_STATUS_UPDATED_NEED_REBOOT";
Darin Petkov09f96c32010-07-20 09:24:57 -070071 case UPDATE_STATUS_REPORTING_ERROR_EVENT:
72 return "UPDATE_STATUS_REPORTING_ERROR_EVENT";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070073 default:
74 return "unknown status";
75 }
76}
77
Darin Petkov777dbfa2010-07-20 15:03:37 -070078// Turns a generic kActionCodeError to a generic error code specific
79// to |action| (e.g., kActionCodeFilesystemCopierError). If |code| is
80// not kActionCodeError, or the action is not matched, returns |code|
81// unchanged.
82ActionExitCode GetErrorCodeForAction(AbstractAction* action,
83 ActionExitCode code) {
84 if (code != kActionCodeError)
85 return code;
86
87 const string type = action->Type();
88 if (type == OmahaRequestAction::StaticType())
89 return kActionCodeOmahaRequestError;
90 if (type == OmahaResponseHandlerAction::StaticType())
91 return kActionCodeOmahaResponseHandlerError;
92 if (type == FilesystemCopierAction::StaticType())
93 return kActionCodeFilesystemCopierError;
94 if (type == PostinstallRunnerAction::StaticType())
95 return kActionCodePostinstallRunnerError;
96 if (type == SetBootableFlagAction::StaticType())
97 return kActionCodeSetBootableFlagError;
98
99 return code;
100}
101
Darin Petkovc6c135c2010-08-11 13:36:18 -0700102UpdateAttempter::UpdateAttempter(PrefsInterface* prefs,
103 MetricsLibraryInterface* metrics_lib)
104 : dbus_service_(NULL),
105 prefs_(prefs),
106 metrics_lib_(metrics_lib),
107 priority_(utils::kProcessPriorityNormal),
108 manage_priority_source_(NULL),
Darin Petkov9d911fa2010-08-19 09:36:08 -0700109 download_active_(false),
Darin Petkovc6c135c2010-08-11 13:36:18 -0700110 status_(UPDATE_STATUS_IDLE),
111 download_progress_(0.0),
112 last_checked_time_(0),
113 new_version_("0.0.0.0"),
114 new_size_(0) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700115 if (utils::FileExists(kUpdateCompletedMarker))
116 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
117}
118
119UpdateAttempter::~UpdateAttempter() {
120 CleanupPriorityManagement();
121}
122
Darin Petkov5a7f5652010-07-22 21:40:09 -0700123void UpdateAttempter::Update(const std::string& app_version,
124 const std::string& omaha_url) {
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700125 if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
126 LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
127 << "reboot";
128 return;
129 }
130 if (status_ != UPDATE_STATUS_IDLE) {
131 // Update in progress. Do nothing
132 return;
133 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700134 if (!omaha_request_params_.Init(app_version, omaha_url)) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700135 LOG(ERROR) << "Unable to initialize Omaha request device params.";
136 return;
137 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700138 CHECK(!processor_.IsRunning());
139 processor_.set_delegate(this);
140
141 // Actions:
Darin Petkov6a5b3222010-07-13 14:55:28 -0700142 shared_ptr<OmahaRequestAction> update_check_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700143 new OmahaRequestAction(prefs_,
144 omaha_request_params_,
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700145 NULL,
146 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700147 shared_ptr<OmahaResponseHandlerAction> response_handler_action(
148 new OmahaResponseHandlerAction);
149 shared_ptr<FilesystemCopierAction> filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700150 new FilesystemCopierAction(false));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700151 shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700152 new FilesystemCopierAction(true));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700153 shared_ptr<OmahaRequestAction> download_started_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700154 new OmahaRequestAction(prefs_,
155 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700156 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700157 OmahaEvent::kTypeUpdateDownloadStarted),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700158 new LibcurlHttpFetcher));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700159 shared_ptr<DownloadAction> download_action(
160 new DownloadAction(new LibcurlHttpFetcher));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700161 shared_ptr<OmahaRequestAction> download_finished_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700162 new OmahaRequestAction(prefs_,
163 omaha_request_params_,
Darin Petkov8c2980e2010-07-16 15:16:49 -0700164 new OmahaEvent(
Darin Petkove17f86b2010-07-20 09:12:01 -0700165 OmahaEvent::kTypeUpdateDownloadFinished),
Darin Petkov8c2980e2010-07-16 15:16:49 -0700166 new LibcurlHttpFetcher));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700167 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_precommit(
168 new PostinstallRunnerAction(true));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700169 shared_ptr<SetBootableFlagAction> set_bootable_flag_action(
170 new SetBootableFlagAction);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700171 shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
172 new PostinstallRunnerAction(false));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700173 shared_ptr<OmahaRequestAction> update_complete_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700174 new OmahaRequestAction(prefs_,
175 omaha_request_params_,
Darin Petkove17f86b2010-07-20 09:12:01 -0700176 new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700177 new LibcurlHttpFetcher));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700178
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700179 download_action->set_delegate(this);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700180 response_handler_action_ = response_handler_action;
181
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700182 actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
183 actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
184 actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700185 actions_.push_back(shared_ptr<AbstractAction>(
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700186 kernel_filesystem_copier_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700187 actions_.push_back(shared_ptr<AbstractAction>(download_started_action));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700188 actions_.push_back(shared_ptr<AbstractAction>(download_action));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700189 actions_.push_back(shared_ptr<AbstractAction>(download_finished_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700190 actions_.push_back(shared_ptr<AbstractAction>(
191 postinstall_runner_action_precommit));
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700192 actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700193 actions_.push_back(shared_ptr<AbstractAction>(
194 postinstall_runner_action_postcommit));
Darin Petkov8c2980e2010-07-16 15:16:49 -0700195 actions_.push_back(shared_ptr<AbstractAction>(update_complete_action));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700196
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700197 // Enqueue the actions
198 for (vector<shared_ptr<AbstractAction> >::iterator it = actions_.begin();
199 it != actions_.end(); ++it) {
200 processor_.EnqueueAction(it->get());
201 }
202
203 // Bond them together. We have to use the leaf-types when calling
204 // BondActions().
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700205 BondActions(update_check_action.get(),
206 response_handler_action.get());
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700207 BondActions(response_handler_action.get(),
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700208 filesystem_copier_action.get());
209 BondActions(filesystem_copier_action.get(),
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700210 kernel_filesystem_copier_action.get());
211 BondActions(kernel_filesystem_copier_action.get(),
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700212 download_action.get());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700213 BondActions(download_action.get(),
214 postinstall_runner_action_precommit.get());
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700215 BondActions(postinstall_runner_action_precommit.get(),
216 set_bootable_flag_action.get());
217 BondActions(set_bootable_flag_action.get(),
218 postinstall_runner_action_postcommit.get());
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700219
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700220 SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700221 processor_.StartProcessing();
222}
223
Darin Petkov5a7f5652010-07-22 21:40:09 -0700224void UpdateAttempter::CheckForUpdate(const std::string& app_version,
225 const std::string& omaha_url) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700226 if (status_ != UPDATE_STATUS_IDLE) {
227 LOG(INFO) << "Check for update requested, but status is "
228 << UpdateStatusToString(status_) << ", so not checking.";
229 return;
230 }
Darin Petkov5a7f5652010-07-22 21:40:09 -0700231 Update(app_version, omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700232}
233
Darin Petkov296889c2010-07-23 16:20:54 -0700234bool UpdateAttempter::RebootIfNeeded() {
235 if (status_ != UPDATE_STATUS_UPDATED_NEED_REBOOT) {
236 LOG(INFO) << "Reboot requested, but status is "
237 << UpdateStatusToString(status_) << ", so not rebooting.";
238 return false;
239 }
240 TEST_AND_RETURN_FALSE(utils::Reboot());
241 return true;
242}
243
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700244// Delegate methods:
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700245void UpdateAttempter::ProcessingDone(const ActionProcessor* processor,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700246 ActionExitCode code) {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700247 CHECK(response_handler_action_);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700248 LOG(INFO) << "Processing Done.";
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700249 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700250
Darin Petkovc6c135c2010-08-11 13:36:18 -0700251 // Reset process priority back to normal.
252 CleanupPriorityManagement();
253
Darin Petkov09f96c32010-07-20 09:24:57 -0700254 if (status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
255 LOG(INFO) << "Error event sent.";
256 SetStatusAndNotify(UPDATE_STATUS_IDLE);
257 return;
258 }
259
Darin Petkovc1a8b422010-07-19 11:34:49 -0700260 if (code == kActionCodeSuccess) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700261 SetStatusAndNotify(UPDATE_STATUS_UPDATED_NEED_REBOOT);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700262 utils::WriteFile(kUpdateCompletedMarker, "", 0);
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700263
264 // Report the time it took to update the system.
265 int64_t update_time = time(NULL) - last_checked_time_;
266 metrics_lib_->SendToUMA("Installer.UpdateTime",
267 static_cast<int>(update_time), // sample
268 1, // min = 1 second
269 20 * 60, // max = 20 minutes
270 50); // buckets
Darin Petkov09f96c32010-07-20 09:24:57 -0700271 return;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700272 }
Darin Petkov09f96c32010-07-20 09:24:57 -0700273
274 LOG(INFO) << "Update failed.";
275 if (ScheduleErrorEventAction())
276 return;
277 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700278}
279
280void UpdateAttempter::ProcessingStopped(const ActionProcessor* processor) {
Darin Petkovc6c135c2010-08-11 13:36:18 -0700281 // Reset process priority back to normal.
282 CleanupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700283 download_progress_ = 0.0;
284 SetStatusAndNotify(UPDATE_STATUS_IDLE);
Andrew de los Reyes6b78e292010-05-10 15:54:39 -0700285 actions_.clear();
Darin Petkov09f96c32010-07-20 09:24:57 -0700286 error_event_.reset(NULL);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700287}
288
289// Called whenever an action has finished processing, either successfully
290// or otherwise.
291void UpdateAttempter::ActionCompleted(ActionProcessor* processor,
292 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700293 ActionExitCode code) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700294 // Reset download progress regardless of whether or not the download action
295 // succeeded.
296 const string type = action->Type();
297 if (type == DownloadAction::StaticType())
298 download_progress_ = 0.0;
Darin Petkov09f96c32010-07-20 09:24:57 -0700299 if (code != kActionCodeSuccess) {
Darin Petkov777dbfa2010-07-20 15:03:37 -0700300 // On failure, schedule an error event to be sent to Omaha.
301 CreatePendingErrorEvent(action, code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700302 return;
Darin Petkov09f96c32010-07-20 09:24:57 -0700303 }
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700304 // Find out which action completed.
305 if (type == OmahaResponseHandlerAction::StaticType()) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700306 // Note that the status will be updated to DOWNLOADING when some
307 // bytes get actually downloaded from the server and the
308 // BytesReceived callback is invoked. This avoids notifying the
309 // user that a download has started in cases when the server and
310 // the client are unable to initiate the download.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700311 OmahaResponseHandlerAction* omaha_response_handler_action =
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700312 dynamic_cast<OmahaResponseHandlerAction*>(action);
313 CHECK(omaha_response_handler_action);
314 const InstallPlan& plan = omaha_response_handler_action->install_plan();
315 last_checked_time_ = time(NULL);
316 // TODO(adlr): put version in InstallPlan
317 new_version_ = "0.0.0.0";
318 new_size_ = plan.size;
Darin Petkovc6c135c2010-08-11 13:36:18 -0700319 SetupPriorityManagement();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700320 } else if (type == DownloadAction::StaticType()) {
321 SetStatusAndNotify(UPDATE_STATUS_FINALIZING);
322 }
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700323}
324
325// Stop updating. An attempt will be made to record status to the disk
326// so that updates can be resumed later.
327void UpdateAttempter::Terminate() {
328 // TODO(adlr): implement this method.
329 NOTIMPLEMENTED();
330}
331
332// Try to resume from a previously Terminate()d update.
333void UpdateAttempter::ResumeUpdating() {
334 // TODO(adlr): implement this method.
335 NOTIMPLEMENTED();
336}
337
Darin Petkov9d911fa2010-08-19 09:36:08 -0700338void UpdateAttempter::SetDownloadStatus(bool active) {
339 download_active_ = active;
340 LOG(INFO) << "Download status: " << (active ? "active" : "inactive");
341}
342
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700343void UpdateAttempter::BytesReceived(uint64_t bytes_received, uint64_t total) {
Darin Petkov9d911fa2010-08-19 09:36:08 -0700344 if (!download_active_) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700345 LOG(ERROR) << "BytesReceived called while not downloading.";
346 return;
347 }
Darin Petkovaf183052010-08-23 12:07:13 -0700348 double progress = static_cast<double>(bytes_received) /
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700349 static_cast<double>(total);
Darin Petkovaf183052010-08-23 12:07:13 -0700350 // Self throttle based on progress. Also send notifications if
351 // progress is too slow.
352 const double kDeltaPercent = 0.01; // 1%
353 if (status_ != UPDATE_STATUS_DOWNLOADING ||
354 bytes_received == total ||
355 progress - download_progress_ >= kDeltaPercent ||
356 TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
357 download_progress_ = progress;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700358 SetStatusAndNotify(UPDATE_STATUS_DOWNLOADING);
359 }
360}
361
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700362bool UpdateAttempter::GetStatus(int64_t* last_checked_time,
363 double* progress,
364 std::string* current_operation,
365 std::string* new_version,
366 int64_t* new_size) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700367 *last_checked_time = last_checked_time_;
368 *progress = download_progress_;
369 *current_operation = UpdateStatusToString(status_);
370 *new_version = new_version_;
371 *new_size = new_size_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700372 return true;
373}
374
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700375void UpdateAttempter::SetStatusAndNotify(UpdateStatus status) {
376 status_ = status;
377 if (!dbus_service_)
378 return;
Darin Petkovaf183052010-08-23 12:07:13 -0700379 last_notify_time_ = TimeTicks::Now();
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700380 update_engine_service_emit_status_update(
381 dbus_service_,
382 last_checked_time_,
383 download_progress_,
384 UpdateStatusToString(status_),
385 new_version_.c_str(),
386 new_size_);
387}
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700388
Darin Petkov777dbfa2010-07-20 15:03:37 -0700389void UpdateAttempter::CreatePendingErrorEvent(AbstractAction* action,
390 ActionExitCode code) {
Darin Petkov09f96c32010-07-20 09:24:57 -0700391 if (error_event_.get()) {
392 // This shouldn't really happen.
393 LOG(WARNING) << "There's already an existing pending error event.";
394 return;
395 }
Darin Petkov777dbfa2010-07-20 15:03:37 -0700396
397 // For now assume that Omaha response action failure means that
398 // there's no update so don't send an event. Also, double check that
399 // the failure has not occurred while sending an error event -- in
400 // which case don't schedule another. This shouldn't really happen
401 // but just in case...
402 if (action->Type() == OmahaResponseHandlerAction::StaticType() ||
403 status_ == UPDATE_STATUS_REPORTING_ERROR_EVENT) {
404 return;
405 }
406
407 code = GetErrorCodeForAction(action, code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700408 error_event_.reset(new OmahaEvent(OmahaEvent::kTypeUpdateComplete,
409 OmahaEvent::kResultError,
410 code));
411}
412
413bool UpdateAttempter::ScheduleErrorEventAction() {
414 if (error_event_.get() == NULL)
415 return false;
416
417 shared_ptr<OmahaRequestAction> error_event_action(
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700418 new OmahaRequestAction(prefs_,
419 omaha_request_params_,
Darin Petkov09f96c32010-07-20 09:24:57 -0700420 error_event_.release(), // Pass ownership.
421 new LibcurlHttpFetcher));
422 actions_.push_back(shared_ptr<AbstractAction>(error_event_action));
423 processor_.EnqueueAction(error_event_action.get());
424 SetStatusAndNotify(UPDATE_STATUS_REPORTING_ERROR_EVENT);
425 processor_.StartProcessing();
426 return true;
427}
428
Darin Petkov1cd885f2010-08-26 09:39:53 -0700429void UpdateAttempter::InitiatePeriodicUpdateChecks() {
430 if (!utils::IsOfficialBuild()) {
431 LOG(WARNING) << "Non-official build: periodic update checks disabled.";
432 return;
433 }
434 if (utils::IsRemovableDevice(utils::RootDevice(utils::BootDevice()))) {
435 LOG(WARNING) << "Removable device boot: periodic update checks disabled.";
436 return;
437 }
438 // Kick off periodic update checks. The first check is scheduled
439 // |kTimeoutOnce| seconds from now. Subsequent checks are scheduled
440 // at |kTimeoutPeriodic|-second intervals.
441 SchedulePeriodicUpdateCheck(kTimeoutOnce);
442}
443
444void UpdateAttempter::SchedulePeriodicUpdateCheck(int seconds) {
445 seconds = utils::FuzzInt(seconds, kTimeoutFuzz);
446 g_timeout_add_seconds(seconds, CheckForUpdatePeriodically, this);
447 LOG(INFO) << "Next update check in " << seconds << " seconds.";
448}
449
Darin Petkovc6c135c2010-08-11 13:36:18 -0700450void UpdateAttempter::SetPriority(utils::ProcessPriority priority) {
451 if (priority_ == priority) {
452 return;
453 }
454 if (utils::SetProcessPriority(priority)) {
455 priority_ = priority;
456 LOG(INFO) << "Process priority = " << priority_;
457 }
458}
459
460void UpdateAttempter::SetupPriorityManagement() {
461 if (manage_priority_source_) {
462 LOG(ERROR) << "Process priority timeout source hasn't been destroyed.";
463 CleanupPriorityManagement();
464 }
465 const int kPriorityTimeout = 10 * 60; // 10 minutes
466 manage_priority_source_ = g_timeout_source_new_seconds(kPriorityTimeout);
467 g_source_set_callback(manage_priority_source_,
468 StaticManagePriorityCallback,
469 this,
470 NULL);
471 g_source_attach(manage_priority_source_, NULL);
472 SetPriority(utils::kProcessPriorityLow);
473}
474
475void UpdateAttempter::CleanupPriorityManagement() {
476 if (manage_priority_source_) {
477 g_source_destroy(manage_priority_source_);
478 manage_priority_source_ = NULL;
479 }
480 SetPriority(utils::kProcessPriorityNormal);
481}
482
483gboolean UpdateAttempter::StaticManagePriorityCallback(gpointer data) {
484 return reinterpret_cast<UpdateAttempter*>(data)->ManagePriorityCallback();
485}
486
487bool UpdateAttempter::ManagePriorityCallback() {
488 // If the current process priority is below normal, set it to normal
489 // and let GLib invoke this callback again.
490 if (utils::ComparePriorities(priority_, utils::kProcessPriorityNormal) < 0) {
491 SetPriority(utils::kProcessPriorityNormal);
492 return true;
493 }
494 // Set the priority to high and let GLib destroy the timeout source.
495 SetPriority(utils::kProcessPriorityHigh);
496 manage_priority_source_ = NULL;
497 return false;
498}
499
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700500} // namespace chromeos_update_engine