blob: 5df6af930a4f3fe4edba2ace4e0ba2f1c2c177b4 [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 Petkovc6c135c2010-08-11 13:36:18 -070048 UpdateAttempter(PrefsInterface* prefs, MetricsLibraryInterface* metrics_lib);
Darin Petkov1023a602010-08-30 13:47:51 -070049 virtual ~UpdateAttempter();
Darin Petkovc6c135c2010-08-11 13:36:18 -070050
Darin Petkov5a7f5652010-07-22 21:40:09 -070051 // Checks for update and, if a newer version is available, attempts
52 // to update the system. Non-empty |in_app_version| or
53 // |in_update_url| prevents automatic detection of the parameter.
Darin Petkov1023a602010-08-30 13:47:51 -070054 virtual void Update(const std::string& app_version,
55 const std::string& omaha_url);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070056
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070057 // ActionProcessorDelegate methods:
Darin Petkovc1a8b422010-07-19 11:34:49 -070058 void ProcessingDone(const ActionProcessor* processor, ActionExitCode code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070059 void ProcessingStopped(const ActionProcessor* processor);
60 void ActionCompleted(ActionProcessor* processor,
61 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070062 ActionExitCode code);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070063
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070064 // Stop updating. An attempt will be made to record status to the disk
65 // so that updates can be resumed later.
66 void Terminate();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070067
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070068 // Try to resume from a previously Terminate()d update.
69 void ResumeUpdating();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070070
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070071 // Returns the current status in the out params. Returns true on success.
72 bool GetStatus(int64_t* last_checked_time,
73 double* progress,
74 std::string* current_operation,
75 std::string* new_version,
76 int64_t* new_size);
77
Darin Petkov1023a602010-08-30 13:47:51 -070078 UpdateStatus status() const { return status_; }
79
80 int http_response_code() const { return http_response_code_; }
81 void set_http_response_code(int code) { http_response_code_ = code; }
82
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070083 void set_dbus_service(struct UpdateEngineService* dbus_service) {
84 dbus_service_ = dbus_service;
85 }
86
Darin Petkov1023a602010-08-30 13:47:51 -070087 UpdateCheckScheduler* update_check_scheduler() const {
88 return update_check_scheduler_;
89 }
90 void set_update_check_scheduler(UpdateCheckScheduler* scheduler) {
91 update_check_scheduler_ = scheduler;
92 }
93
Darin Petkov5a7f5652010-07-22 21:40:09 -070094 // This is the D-Bus service entry point for going through an
95 // update. If the current status is idle invokes Update.
96 void CheckForUpdate(const std::string& app_version,
97 const std::string& omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070098
Darin Petkov296889c2010-07-23 16:20:54 -070099 // Initiates a reboot if the current state is
100 // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise.
101 bool RebootIfNeeded();
102
Darin Petkov9d911fa2010-08-19 09:36:08 -0700103 // DownloadActionDelegate methods
104 void SetDownloadStatus(bool active);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700105 void BytesReceived(uint64_t bytes_received, uint64_t total);
106
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700107 private:
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700108 friend class UpdateAttempterTest;
109 FRIEND_TEST(UpdateAttempterTest, UpdateTest);
110
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700111 // Sets the status to the given status and notifies a status update
112 // over dbus.
113 void SetStatusAndNotify(UpdateStatus status);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700114
Darin Petkov09f96c32010-07-20 09:24:57 -0700115 // Creates an error event object in |error_event_| to be included in
116 // an OmahaRequestAction once the current action processor is done.
Darin Petkov777dbfa2010-07-20 15:03:37 -0700117 void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700118
119 // If there's a pending error event allocated in |error_event_|,
120 // schedules an OmahaRequestAction with that event in the current
121 // processor, clears the pending event, updates the status and
122 // returns true. Returns false otherwise.
123 bool ScheduleErrorEventAction();
124
Darin Petkovc6c135c2010-08-11 13:36:18 -0700125 // Sets the process priority to |priority| and updates |priority_|
126 // if the new |priority| is different than the current |priority_|,
127 // otherwise simply returns.
128 void SetPriority(utils::ProcessPriority priority);
129
130 // Set the process priority to low and sets up timeout events to
131 // increase the priority gradually to high.
132 void SetupPriorityManagement();
133
134 // Resets the process priority to normal and destroys any scheduled
135 // timeout sources.
136 void CleanupPriorityManagement();
137
138 // The process priority timeout source callback increases the
139 // current priority by one step (low goes to normal, normal goes to
140 // high). Returns true if the callback must be invoked again after a
141 // timeout, or false if GLib can destroy this timeout source.
142 static gboolean StaticManagePriorityCallback(gpointer data);
143 bool ManagePriorityCallback();
144
Darin Petkovaf183052010-08-23 12:07:13 -0700145 // Last status notification timestamp used for throttling. Use
146 // monotonic TimeTicks to ensure that notifications are sent even if
147 // the system clock is set back in the middle of an update.
148 base::TimeTicks last_notify_time_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700149
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700150 std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
Darin Petkovf42cc1c2010-09-01 09:03:02 -0700151 scoped_ptr<ActionProcessor> processor_;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700152
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700153 // If non-null, this UpdateAttempter will send status updates over this
154 // dbus service.
155 UpdateEngineService* dbus_service_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700156
157 // pointer to the OmahaResponseHandlerAction in the actions_ vector;
158 std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700159
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700160 // Pointer to the preferences store interface.
161 PrefsInterface* prefs_;
162
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700163 // Pointer to the UMA metrics collection library.
164 MetricsLibraryInterface* metrics_lib_;
165
Darin Petkov1023a602010-08-30 13:47:51 -0700166 // The current UpdateCheckScheduler to notify of state transitions.
167 UpdateCheckScheduler* update_check_scheduler_;
168
Darin Petkov09f96c32010-07-20 09:24:57 -0700169 // Pending error event, if any.
170 scoped_ptr<OmahaEvent> error_event_;
171
Darin Petkov85ced132010-09-01 10:20:56 -0700172 // HTTP server response code from the last HTTP request action.
Darin Petkov1023a602010-08-30 13:47:51 -0700173 int http_response_code_;
174
Darin Petkovc6c135c2010-08-11 13:36:18 -0700175 // Current process priority.
176 utils::ProcessPriority priority_;
177
178 // The process priority management timeout source.
179 GSource* manage_priority_source_;
180
Darin Petkov9d911fa2010-08-19 09:36:08 -0700181 // Set to true if an update download is active (and BytesReceived
182 // will be called), set to false otherwise.
183 bool download_active_;
184
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700185 // For status:
186 UpdateStatus status_;
187 double download_progress_;
188 int64_t last_checked_time_;
189 std::string new_version_;
190 int64_t new_size_;
191
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700192 // Device paramaters common to all Omaha requests.
193 OmahaRequestDeviceParams omaha_request_params_;
194
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700195 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
196};
197
198} // namespace chromeos_update_engine
199
200#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__