blob: 3a4ff49960b1b2bbc98a007b2b50d7e2fc4caa3a [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#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__
7
Andrew de los Reyes63b96d72010-05-10 13:08:54 -07008#include <time.h>
Darin Petkovc6c135c2010-08-11 13:36:18 -07009
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070010#include <tr1/memory>
11#include <string>
12#include <vector>
Darin Petkovc6c135c2010-08-11 13:36:18 -070013
Darin Petkov85ced132010-09-01 10:20:56 -070014#include <base/time.h>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070015#include <glib.h>
Darin Petkovf42cc1c2010-09-01 09:03:02 -070016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovc6c135c2010-08-11 13:36:18 -070017
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070018#include "update_engine/action_processor.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070019#include "update_engine/download_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070020#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021#include "update_engine/omaha_response_handler_action.h"
22
Darin Petkov9d65b7b2010-07-20 09:13:01 -070023class MetricsLibraryInterface;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070024struct UpdateEngineService;
25
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070026namespace chromeos_update_engine {
27
Darin Petkov1023a602010-08-30 13:47:51 -070028class UpdateCheckScheduler;
Darin Petkovc6c135c2010-08-11 13:36:18 -070029
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070030extern const char* kUpdateCompletedMarker;
31
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070032enum UpdateStatus {
33 UPDATE_STATUS_IDLE = 0,
34 UPDATE_STATUS_CHECKING_FOR_UPDATE,
35 UPDATE_STATUS_UPDATE_AVAILABLE,
36 UPDATE_STATUS_DOWNLOADING,
37 UPDATE_STATUS_VERIFYING,
38 UPDATE_STATUS_FINALIZING,
Darin Petkov09f96c32010-07-20 09:24:57 -070039 UPDATE_STATUS_UPDATED_NEED_REBOOT,
40 UPDATE_STATUS_REPORTING_ERROR_EVENT,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070041};
42
43const char* UpdateStatusToString(UpdateStatus status);
44
45class UpdateAttempter : public ActionProcessorDelegate,
46 public DownloadActionDelegate {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070047 public:
Darin Petkov36275772010-10-01 11:40:57 -070048 static const int kMaxDeltaUpdateFailures;
49
Darin Petkovc6c135c2010-08-11 13:36:18 -070050 UpdateAttempter(PrefsInterface* prefs, MetricsLibraryInterface* metrics_lib);
Darin Petkov1023a602010-08-30 13:47:51 -070051 virtual ~UpdateAttempter();
Darin Petkovc6c135c2010-08-11 13:36:18 -070052
Darin Petkov5a7f5652010-07-22 21:40:09 -070053 // Checks for update and, if a newer version is available, attempts
54 // to update the system. Non-empty |in_app_version| or
55 // |in_update_url| prevents automatic detection of the parameter.
Darin Petkov1023a602010-08-30 13:47:51 -070056 virtual void Update(const std::string& app_version,
57 const std::string& omaha_url);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070058
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070059 // ActionProcessorDelegate methods:
Darin Petkovc1a8b422010-07-19 11:34:49 -070060 void ProcessingDone(const ActionProcessor* processor, ActionExitCode code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070061 void ProcessingStopped(const ActionProcessor* processor);
62 void ActionCompleted(ActionProcessor* processor,
63 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070064 ActionExitCode code);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070065
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070066 // Stop updating. An attempt will be made to record status to the disk
67 // so that updates can be resumed later.
68 void Terminate();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070069
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070070 // Try to resume from a previously Terminate()d update.
71 void ResumeUpdating();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070072
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070073 // Returns the current status in the out params. Returns true on success.
74 bool GetStatus(int64_t* last_checked_time,
75 double* progress,
76 std::string* current_operation,
77 std::string* new_version,
78 int64_t* new_size);
79
Darin Petkov1023a602010-08-30 13:47:51 -070080 UpdateStatus status() const { return status_; }
81
82 int http_response_code() const { return http_response_code_; }
83 void set_http_response_code(int code) { http_response_code_ = code; }
84
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070085 void set_dbus_service(struct UpdateEngineService* dbus_service) {
86 dbus_service_ = dbus_service;
87 }
88
Darin Petkov1023a602010-08-30 13:47:51 -070089 UpdateCheckScheduler* update_check_scheduler() const {
90 return update_check_scheduler_;
91 }
92 void set_update_check_scheduler(UpdateCheckScheduler* scheduler) {
93 update_check_scheduler_ = scheduler;
94 }
95
Darin Petkov5a7f5652010-07-22 21:40:09 -070096 // This is the D-Bus service entry point for going through an
97 // update. If the current status is idle invokes Update.
98 void CheckForUpdate(const std::string& app_version,
99 const std::string& omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700100
Darin Petkov296889c2010-07-23 16:20:54 -0700101 // Initiates a reboot if the current state is
102 // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise.
103 bool RebootIfNeeded();
104
Darin Petkov9d911fa2010-08-19 09:36:08 -0700105 // DownloadActionDelegate methods
106 void SetDownloadStatus(bool active);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700107 void BytesReceived(uint64_t bytes_received, uint64_t total);
108
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700109 private:
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700110 friend class UpdateAttempterTest;
Darin Petkov36275772010-10-01 11:40:57 -0700111 FRIEND_TEST(UpdateAttempterTest, DisableDeltaUpdateIfNeededTest);
112 FRIEND_TEST(UpdateAttempterTest, MarkDeltaUpdateFailureTest);
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700113 FRIEND_TEST(UpdateAttempterTest, UpdateTest);
114
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700115 // Sets the status to the given status and notifies a status update
116 // over dbus.
117 void SetStatusAndNotify(UpdateStatus status);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700118
Darin Petkov9b230572010-10-08 10:20:09 -0700119 // Sets up the download parameters after receiving the update check response.
120 void SetupDownload();
121
122 // Creates an error event object in |error_event_| to be included in an
123 // OmahaRequestAction once the current action processor is done.
Darin Petkov777dbfa2010-07-20 15:03:37 -0700124 void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700125
Darin Petkov9b230572010-10-08 10:20:09 -0700126 // If there's a pending error event allocated in |error_event_|, schedules an
127 // OmahaRequestAction with that event in the current processor, clears the
128 // pending event, updates the status and returns true. Returns false
129 // otherwise.
Darin Petkov09f96c32010-07-20 09:24:57 -0700130 bool ScheduleErrorEventAction();
131
Darin Petkov9b230572010-10-08 10:20:09 -0700132 // Sets the process priority to |priority| and updates |priority_| if the new
133 // |priority| is different than the current |priority_|, otherwise simply
134 // returns.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700135 void SetPriority(utils::ProcessPriority priority);
136
Darin Petkovf622ef72010-10-26 13:49:24 -0700137 // Sets the process priority to low and sets up timeout events to increase it.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700138 void SetupPriorityManagement();
139
Darin Petkov9b230572010-10-08 10:20:09 -0700140 // Resets the process priority to normal and destroys any scheduled timeout
141 // sources.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700142 void CleanupPriorityManagement();
143
Darin Petkovf622ef72010-10-26 13:49:24 -0700144 // The process priority timeout source callback sets the current priority to
145 // normal. Returns false so that GLib destroys the timeout source.
Darin Petkovc6c135c2010-08-11 13:36:18 -0700146 static gboolean StaticManagePriorityCallback(gpointer data);
147 bool ManagePriorityCallback();
148
Darin Petkov36275772010-10-01 11:40:57 -0700149 // Checks if a full update is needed and forces it by updating the Omaha
150 // request params.
151 void DisableDeltaUpdateIfNeeded();
152
153 // If this was a delta update attempt that failed, count it so that a full
154 // update can be tried when needed.
155 void MarkDeltaUpdateFailure();
156
Darin Petkov9b230572010-10-08 10:20:09 -0700157 // Last status notification timestamp used for throttling. Use monotonic
158 // TimeTicks to ensure that notifications are sent even if the system clock is
159 // set back in the middle of an update.
Darin Petkovaf183052010-08-23 12:07:13 -0700160 base::TimeTicks last_notify_time_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700161
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700162 std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700163 scoped_ptr<ActionProcessor> processor_;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700164
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700165 // If non-null, this UpdateAttempter will send status updates over this
166 // dbus service.
167 UpdateEngineService* dbus_service_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700168
Darin Petkov9b230572010-10-08 10:20:09 -0700169 // Pointer to the OmahaResponseHandlerAction in the actions_ vector.
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700170 std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700171
Darin Petkov9b230572010-10-08 10:20:09 -0700172 // Pointer to the DownloadAction in the actions_ vector.
173 std::tr1::shared_ptr<DownloadAction> download_action_;
174
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700175 // Pointer to the preferences store interface.
176 PrefsInterface* prefs_;
177
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700178 // Pointer to the UMA metrics collection library.
179 MetricsLibraryInterface* metrics_lib_;
180
Darin Petkov1023a602010-08-30 13:47:51 -0700181 // The current UpdateCheckScheduler to notify of state transitions.
182 UpdateCheckScheduler* update_check_scheduler_;
183
Darin Petkov09f96c32010-07-20 09:24:57 -0700184 // Pending error event, if any.
185 scoped_ptr<OmahaEvent> error_event_;
186
Darin Petkov85ced132010-09-01 10:20:56 -0700187 // HTTP server response code from the last HTTP request action.
Darin Petkov1023a602010-08-30 13:47:51 -0700188 int http_response_code_;
189
Darin Petkovc6c135c2010-08-11 13:36:18 -0700190 // Current process priority.
191 utils::ProcessPriority priority_;
192
193 // The process priority management timeout source.
194 GSource* manage_priority_source_;
195
Darin Petkov9d911fa2010-08-19 09:36:08 -0700196 // Set to true if an update download is active (and BytesReceived
197 // will be called), set to false otherwise.
198 bool download_active_;
199
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700200 // For status:
201 UpdateStatus status_;
202 double download_progress_;
203 int64_t last_checked_time_;
204 std::string new_version_;
205 int64_t new_size_;
Darin Petkov36275772010-10-01 11:40:57 -0700206 bool is_full_update_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700207
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700208 // Device paramaters common to all Omaha requests.
209 OmahaRequestDeviceParams omaha_request_params_;
210
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700211 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
212};
213
214} // namespace chromeos_update_engine
215
216#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__