blob: fd9bc2a9b14a59dc25bc2abcf00c87f02c363424 [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
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070014#include <glib.h>
Darin Petkovc6c135c2010-08-11 13:36:18 -070015
Darin Petkovaf183052010-08-23 12:07:13 -070016#include "base/time.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070017#include "update_engine/action_processor.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070018#include "update_engine/download_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070019#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070020#include "update_engine/omaha_response_handler_action.h"
21
Darin Petkov9d65b7b2010-07-20 09:13:01 -070022class MetricsLibraryInterface;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070023struct UpdateEngineService;
24
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070025namespace chromeos_update_engine {
26
Darin Petkov1023a602010-08-30 13:47:51 -070027class UpdateCheckScheduler;
Darin Petkovc6c135c2010-08-11 13:36:18 -070028
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070029extern const char* kUpdateCompletedMarker;
30
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070031enum UpdateStatus {
32 UPDATE_STATUS_IDLE = 0,
33 UPDATE_STATUS_CHECKING_FOR_UPDATE,
34 UPDATE_STATUS_UPDATE_AVAILABLE,
35 UPDATE_STATUS_DOWNLOADING,
36 UPDATE_STATUS_VERIFYING,
37 UPDATE_STATUS_FINALIZING,
Darin Petkov09f96c32010-07-20 09:24:57 -070038 UPDATE_STATUS_UPDATED_NEED_REBOOT,
39 UPDATE_STATUS_REPORTING_ERROR_EVENT,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040};
41
42const char* UpdateStatusToString(UpdateStatus status);
43
44class UpdateAttempter : public ActionProcessorDelegate,
45 public DownloadActionDelegate {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070046 public:
Darin Petkovc6c135c2010-08-11 13:36:18 -070047 UpdateAttempter(PrefsInterface* prefs, MetricsLibraryInterface* metrics_lib);
Darin Petkov1023a602010-08-30 13:47:51 -070048 virtual ~UpdateAttempter();
Darin Petkovc6c135c2010-08-11 13:36:18 -070049
Darin Petkov5a7f5652010-07-22 21:40:09 -070050 // Checks for update and, if a newer version is available, attempts
51 // to update the system. Non-empty |in_app_version| or
52 // |in_update_url| prevents automatic detection of the parameter.
Darin Petkov1023a602010-08-30 13:47:51 -070053 virtual void Update(const std::string& app_version,
54 const std::string& omaha_url);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070056 // ActionProcessorDelegate methods:
Darin Petkovc1a8b422010-07-19 11:34:49 -070057 void ProcessingDone(const ActionProcessor* processor, ActionExitCode code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070058 void ProcessingStopped(const ActionProcessor* processor);
59 void ActionCompleted(ActionProcessor* processor,
60 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070061 ActionExitCode code);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070062
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070063 // Stop updating. An attempt will be made to record status to the disk
64 // so that updates can be resumed later.
65 void Terminate();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070066
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070067 // Try to resume from a previously Terminate()d update.
68 void ResumeUpdating();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070069
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070070 // Returns the current status in the out params. Returns true on success.
71 bool GetStatus(int64_t* last_checked_time,
72 double* progress,
73 std::string* current_operation,
74 std::string* new_version,
75 int64_t* new_size);
76
Darin Petkov1023a602010-08-30 13:47:51 -070077 UpdateStatus status() const { return status_; }
78
79 int http_response_code() const { return http_response_code_; }
80 void set_http_response_code(int code) { http_response_code_ = code; }
81
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070082 void set_dbus_service(struct UpdateEngineService* dbus_service) {
83 dbus_service_ = dbus_service;
84 }
85
Darin Petkov1023a602010-08-30 13:47:51 -070086 UpdateCheckScheduler* update_check_scheduler() const {
87 return update_check_scheduler_;
88 }
89 void set_update_check_scheduler(UpdateCheckScheduler* scheduler) {
90 update_check_scheduler_ = scheduler;
91 }
92
Darin Petkov5a7f5652010-07-22 21:40:09 -070093 // This is the D-Bus service entry point for going through an
94 // update. If the current status is idle invokes Update.
95 void CheckForUpdate(const std::string& app_version,
96 const std::string& omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070097
Darin Petkov296889c2010-07-23 16:20:54 -070098 // Initiates a reboot if the current state is
99 // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise.
100 bool RebootIfNeeded();
101
Darin Petkov9d911fa2010-08-19 09:36:08 -0700102 // DownloadActionDelegate methods
103 void SetDownloadStatus(bool active);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700104 void BytesReceived(uint64_t bytes_received, uint64_t total);
105
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700106 private:
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700107 // Sets the status to the given status and notifies a status update
108 // over dbus.
109 void SetStatusAndNotify(UpdateStatus status);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700110
Darin Petkov09f96c32010-07-20 09:24:57 -0700111 // Creates an error event object in |error_event_| to be included in
112 // an OmahaRequestAction once the current action processor is done.
Darin Petkov777dbfa2010-07-20 15:03:37 -0700113 void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700114
115 // If there's a pending error event allocated in |error_event_|,
116 // schedules an OmahaRequestAction with that event in the current
117 // processor, clears the pending event, updates the status and
118 // returns true. Returns false otherwise.
119 bool ScheduleErrorEventAction();
120
Darin Petkovc6c135c2010-08-11 13:36:18 -0700121 // Sets the process priority to |priority| and updates |priority_|
122 // if the new |priority| is different than the current |priority_|,
123 // otherwise simply returns.
124 void SetPriority(utils::ProcessPriority priority);
125
126 // Set the process priority to low and sets up timeout events to
127 // increase the priority gradually to high.
128 void SetupPriorityManagement();
129
130 // Resets the process priority to normal and destroys any scheduled
131 // timeout sources.
132 void CleanupPriorityManagement();
133
134 // The process priority timeout source callback increases the
135 // current priority by one step (low goes to normal, normal goes to
136 // high). Returns true if the callback must be invoked again after a
137 // timeout, or false if GLib can destroy this timeout source.
138 static gboolean StaticManagePriorityCallback(gpointer data);
139 bool ManagePriorityCallback();
140
Darin Petkovaf183052010-08-23 12:07:13 -0700141 // Last status notification timestamp used for throttling. Use
142 // monotonic TimeTicks to ensure that notifications are sent even if
143 // the system clock is set back in the middle of an update.
144 base::TimeTicks last_notify_time_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700145
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700146 std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
147 ActionProcessor processor_;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700148
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700149 // If non-null, this UpdateAttempter will send status updates over this
150 // dbus service.
151 UpdateEngineService* dbus_service_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700152
153 // pointer to the OmahaResponseHandlerAction in the actions_ vector;
154 std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700155
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700156 // Pointer to the preferences store interface.
157 PrefsInterface* prefs_;
158
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700159 // Pointer to the UMA metrics collection library.
160 MetricsLibraryInterface* metrics_lib_;
161
Darin Petkov1023a602010-08-30 13:47:51 -0700162 // The current UpdateCheckScheduler to notify of state transitions.
163 UpdateCheckScheduler* update_check_scheduler_;
164
Darin Petkov09f96c32010-07-20 09:24:57 -0700165 // Pending error event, if any.
166 scoped_ptr<OmahaEvent> error_event_;
167
Darin Petkov1023a602010-08-30 13:47:51 -0700168 int http_response_code_;
169
Darin Petkovc6c135c2010-08-11 13:36:18 -0700170 // Current process priority.
171 utils::ProcessPriority priority_;
172
173 // The process priority management timeout source.
174 GSource* manage_priority_source_;
175
Darin Petkov9d911fa2010-08-19 09:36:08 -0700176 // Set to true if an update download is active (and BytesReceived
177 // will be called), set to false otherwise.
178 bool download_active_;
179
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700180 // For status:
181 UpdateStatus status_;
182 double download_progress_;
183 int64_t last_checked_time_;
184 std::string new_version_;
185 int64_t new_size_;
186
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700187 // Device paramaters common to all Omaha requests.
188 OmahaRequestDeviceParams omaha_request_params_;
189
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700190 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
191};
192
193} // namespace chromeos_update_engine
194
195#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__