blob: 1c9ad40577791b52cbf3f9a7050936ba8f893e42 [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 Petkovc6c135c2010-08-11 13:36:18 -070027namespace utils {
28enum ProcessPriority;
29};
30
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070031extern const char* kUpdateCompletedMarker;
32
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070033enum UpdateStatus {
34 UPDATE_STATUS_IDLE = 0,
35 UPDATE_STATUS_CHECKING_FOR_UPDATE,
36 UPDATE_STATUS_UPDATE_AVAILABLE,
37 UPDATE_STATUS_DOWNLOADING,
38 UPDATE_STATUS_VERIFYING,
39 UPDATE_STATUS_FINALIZING,
Darin Petkov09f96c32010-07-20 09:24:57 -070040 UPDATE_STATUS_UPDATED_NEED_REBOOT,
41 UPDATE_STATUS_REPORTING_ERROR_EVENT,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070042};
43
44const char* UpdateStatusToString(UpdateStatus status);
45
46class UpdateAttempter : public ActionProcessorDelegate,
47 public DownloadActionDelegate {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070048 public:
Darin Petkovc6c135c2010-08-11 13:36:18 -070049 UpdateAttempter(PrefsInterface* prefs, MetricsLibraryInterface* metrics_lib);
50 ~UpdateAttempter();
51
Darin Petkov5a7f5652010-07-22 21:40:09 -070052 // Checks for update and, if a newer version is available, attempts
53 // to update the system. Non-empty |in_app_version| or
54 // |in_update_url| prevents automatic detection of the parameter.
55 void Update(const std::string& app_version, 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
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070078 void set_dbus_service(struct UpdateEngineService* dbus_service) {
79 dbus_service_ = dbus_service;
80 }
81
Darin Petkov5a7f5652010-07-22 21:40:09 -070082 // This is the D-Bus service entry point for going through an
83 // update. If the current status is idle invokes Update.
84 void CheckForUpdate(const std::string& app_version,
85 const std::string& omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070086
Darin Petkov296889c2010-07-23 16:20:54 -070087 // Initiates a reboot if the current state is
88 // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise.
89 bool RebootIfNeeded();
90
Darin Petkov1cd885f2010-08-26 09:39:53 -070091 // Kicks off the periodic update checks, if necessary.
92 void InitiatePeriodicUpdateChecks();
93
94 // Schedules the next periodic update check |seconds| from now. Note
95 // that the actual timeout will be fuzzed.
96 void SchedulePeriodicUpdateCheck(int seconds);
97
Darin Petkov9d911fa2010-08-19 09:36:08 -070098 // DownloadActionDelegate methods
99 void SetDownloadStatus(bool active);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700100 void BytesReceived(uint64_t bytes_received, uint64_t total);
101
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700102 private:
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700103 // Sets the status to the given status and notifies a status update
104 // over dbus.
105 void SetStatusAndNotify(UpdateStatus status);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700106
Darin Petkov09f96c32010-07-20 09:24:57 -0700107 // Creates an error event object in |error_event_| to be included in
108 // an OmahaRequestAction once the current action processor is done.
Darin Petkov777dbfa2010-07-20 15:03:37 -0700109 void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700110
111 // If there's a pending error event allocated in |error_event_|,
112 // schedules an OmahaRequestAction with that event in the current
113 // processor, clears the pending event, updates the status and
114 // returns true. Returns false otherwise.
115 bool ScheduleErrorEventAction();
116
Darin Petkovc6c135c2010-08-11 13:36:18 -0700117 // Sets the process priority to |priority| and updates |priority_|
118 // if the new |priority| is different than the current |priority_|,
119 // otherwise simply returns.
120 void SetPriority(utils::ProcessPriority priority);
121
122 // Set the process priority to low and sets up timeout events to
123 // increase the priority gradually to high.
124 void SetupPriorityManagement();
125
126 // Resets the process priority to normal and destroys any scheduled
127 // timeout sources.
128 void CleanupPriorityManagement();
129
130 // The process priority timeout source callback increases the
131 // current priority by one step (low goes to normal, normal goes to
132 // high). Returns true if the callback must be invoked again after a
133 // timeout, or false if GLib can destroy this timeout source.
134 static gboolean StaticManagePriorityCallback(gpointer data);
135 bool ManagePriorityCallback();
136
Darin Petkovaf183052010-08-23 12:07:13 -0700137 // Last status notification timestamp used for throttling. Use
138 // monotonic TimeTicks to ensure that notifications are sent even if
139 // the system clock is set back in the middle of an update.
140 base::TimeTicks last_notify_time_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700141
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700142 std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
143 ActionProcessor processor_;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700144
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700145 // If non-null, this UpdateAttempter will send status updates over this
146 // dbus service.
147 UpdateEngineService* dbus_service_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700148
149 // pointer to the OmahaResponseHandlerAction in the actions_ vector;
150 std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700151
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700152 // Pointer to the preferences store interface.
153 PrefsInterface* prefs_;
154
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700155 // Pointer to the UMA metrics collection library.
156 MetricsLibraryInterface* metrics_lib_;
157
Darin Petkov09f96c32010-07-20 09:24:57 -0700158 // Pending error event, if any.
159 scoped_ptr<OmahaEvent> error_event_;
160
Darin Petkovc6c135c2010-08-11 13:36:18 -0700161 // Current process priority.
162 utils::ProcessPriority priority_;
163
164 // The process priority management timeout source.
165 GSource* manage_priority_source_;
166
Darin Petkov9d911fa2010-08-19 09:36:08 -0700167 // Set to true if an update download is active (and BytesReceived
168 // will be called), set to false otherwise.
169 bool download_active_;
170
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700171 // For status:
172 UpdateStatus status_;
173 double download_progress_;
174 int64_t last_checked_time_;
175 std::string new_version_;
176 int64_t new_size_;
177
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700178 // Device paramaters common to all Omaha requests.
179 OmahaRequestDeviceParams omaha_request_params_;
180
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700181 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
182};
183
184} // namespace chromeos_update_engine
185
186#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__