blob: 19933219282184f31f15ee6710b7bfa0d8a9d20a [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
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070016#include "update_engine/action_processor.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070017#include "update_engine/download_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070018#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070019#include "update_engine/omaha_response_handler_action.h"
20
Darin Petkov9d65b7b2010-07-20 09:13:01 -070021class MetricsLibraryInterface;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070022struct UpdateEngineService;
23
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070024namespace chromeos_update_engine {
25
Darin Petkovc6c135c2010-08-11 13:36:18 -070026namespace utils {
27enum ProcessPriority;
28};
29
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);
49 ~UpdateAttempter();
50
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.
54 void Update(const std::string& app_version, 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
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070077 void set_dbus_service(struct UpdateEngineService* dbus_service) {
78 dbus_service_ = dbus_service;
79 }
80
Darin Petkov5a7f5652010-07-22 21:40:09 -070081 // This is the D-Bus service entry point for going through an
82 // update. If the current status is idle invokes Update.
83 void CheckForUpdate(const std::string& app_version,
84 const std::string& omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070085
Darin Petkov296889c2010-07-23 16:20:54 -070086 // Initiates a reboot if the current state is
87 // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise.
88 bool RebootIfNeeded();
89
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070090 // DownloadActionDelegate method
91 void BytesReceived(uint64_t bytes_received, uint64_t total);
92
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070093 private:
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070094 // Sets the status to the given status and notifies a status update
95 // over dbus.
96 void SetStatusAndNotify(UpdateStatus status);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070097
Darin Petkov09f96c32010-07-20 09:24:57 -070098 // Creates an error event object in |error_event_| to be included in
99 // an OmahaRequestAction once the current action processor is done.
Darin Petkov777dbfa2010-07-20 15:03:37 -0700100 void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700101
102 // If there's a pending error event allocated in |error_event_|,
103 // schedules an OmahaRequestAction with that event in the current
104 // processor, clears the pending event, updates the status and
105 // returns true. Returns false otherwise.
106 bool ScheduleErrorEventAction();
107
Darin Petkovc6c135c2010-08-11 13:36:18 -0700108 // Sets the process priority to |priority| and updates |priority_|
109 // if the new |priority| is different than the current |priority_|,
110 // otherwise simply returns.
111 void SetPriority(utils::ProcessPriority priority);
112
113 // Set the process priority to low and sets up timeout events to
114 // increase the priority gradually to high.
115 void SetupPriorityManagement();
116
117 // Resets the process priority to normal and destroys any scheduled
118 // timeout sources.
119 void CleanupPriorityManagement();
120
121 // The process priority timeout source callback increases the
122 // current priority by one step (low goes to normal, normal goes to
123 // high). Returns true if the callback must be invoked again after a
124 // timeout, or false if GLib can destroy this timeout source.
125 static gboolean StaticManagePriorityCallback(gpointer data);
126 bool ManagePriorityCallback();
127
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700128 struct timespec last_notify_time_;
129
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700130 std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
131 ActionProcessor processor_;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700132
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700133 // If non-null, this UpdateAttempter will send status updates over this
134 // dbus service.
135 UpdateEngineService* dbus_service_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700136
137 // pointer to the OmahaResponseHandlerAction in the actions_ vector;
138 std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700139
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700140 // Pointer to the preferences store interface.
141 PrefsInterface* prefs_;
142
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700143 // Pointer to the UMA metrics collection library.
144 MetricsLibraryInterface* metrics_lib_;
145
Darin Petkov09f96c32010-07-20 09:24:57 -0700146 // Pending error event, if any.
147 scoped_ptr<OmahaEvent> error_event_;
148
Darin Petkovc6c135c2010-08-11 13:36:18 -0700149 // Current process priority.
150 utils::ProcessPriority priority_;
151
152 // The process priority management timeout source.
153 GSource* manage_priority_source_;
154
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700155 // For status:
156 UpdateStatus status_;
157 double download_progress_;
158 int64_t last_checked_time_;
159 std::string new_version_;
160 int64_t new_size_;
161
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700162 // Device paramaters common to all Omaha requests.
163 OmahaRequestDeviceParams omaha_request_params_;
164
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700165 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
166};
167
168} // namespace chromeos_update_engine
169
170#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__