blob: 6fd4c1626e24869b9cd19e06da63f22423292267 [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>
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -07009#include <tr1/memory>
10#include <string>
11#include <vector>
12#include <glib.h>
13#include "update_engine/action_processor.h"
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070014#include "update_engine/download_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070015#include "update_engine/omaha_request_params.h"
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070016#include "update_engine/omaha_response_handler_action.h"
17
Darin Petkov9d65b7b2010-07-20 09:13:01 -070018class MetricsLibraryInterface;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070019struct UpdateEngineService;
20
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070021namespace chromeos_update_engine {
22
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070023extern const char* kUpdateCompletedMarker;
24
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070025enum UpdateStatus {
26 UPDATE_STATUS_IDLE = 0,
27 UPDATE_STATUS_CHECKING_FOR_UPDATE,
28 UPDATE_STATUS_UPDATE_AVAILABLE,
29 UPDATE_STATUS_DOWNLOADING,
30 UPDATE_STATUS_VERIFYING,
31 UPDATE_STATUS_FINALIZING,
Darin Petkov09f96c32010-07-20 09:24:57 -070032 UPDATE_STATUS_UPDATED_NEED_REBOOT,
33 UPDATE_STATUS_REPORTING_ERROR_EVENT,
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070034};
35
36const char* UpdateStatusToString(UpdateStatus status);
37
38class UpdateAttempter : public ActionProcessorDelegate,
39 public DownloadActionDelegate {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070040 public:
Darin Petkov1cbd78f2010-07-29 12:38:34 -070041 UpdateAttempter(PrefsInterface* prefs, MetricsLibraryInterface* metrics_lib)
Darin Petkov9d65b7b2010-07-20 09:13:01 -070042 : dbus_service_(NULL),
Darin Petkov1cbd78f2010-07-29 12:38:34 -070043 prefs_(prefs),
Darin Petkov9d65b7b2010-07-20 09:13:01 -070044 metrics_lib_(metrics_lib),
45 status_(UPDATE_STATUS_IDLE),
46 download_progress_(0.0),
47 last_checked_time_(0),
48 new_version_("0.0.0.0"),
49 new_size_(0) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070050 last_notify_time_.tv_sec = 0;
51 last_notify_time_.tv_nsec = 0;
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070052 if (utils::FileExists(kUpdateCompletedMarker))
53 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070054 }
Darin Petkov5a7f5652010-07-22 21:40:09 -070055 // Checks for update and, if a newer version is available, attempts
56 // to update the system. Non-empty |in_app_version| or
57 // |in_update_url| prevents automatic detection of the parameter.
58 void Update(const std::string& app_version, const std::string& omaha_url);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070059
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070060 // ActionProcessorDelegate methods:
Darin Petkovc1a8b422010-07-19 11:34:49 -070061 void ProcessingDone(const ActionProcessor* processor, ActionExitCode code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070062 void ProcessingStopped(const ActionProcessor* processor);
63 void ActionCompleted(ActionProcessor* processor,
64 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070065 ActionExitCode code);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070066
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070067 // Stop updating. An attempt will be made to record status to the disk
68 // so that updates can be resumed later.
69 void Terminate();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070070
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070071 // Try to resume from a previously Terminate()d update.
72 void ResumeUpdating();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070073
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070074 // Returns the current status in the out params. Returns true on success.
75 bool GetStatus(int64_t* last_checked_time,
76 double* progress,
77 std::string* current_operation,
78 std::string* new_version,
79 int64_t* new_size);
80
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070081 void set_dbus_service(struct UpdateEngineService* dbus_service) {
82 dbus_service_ = dbus_service;
83 }
84
Darin Petkov5a7f5652010-07-22 21:40:09 -070085 // This is the D-Bus service entry point for going through an
86 // update. If the current status is idle invokes Update.
87 void CheckForUpdate(const std::string& app_version,
88 const std::string& omaha_url);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070089
Darin Petkov296889c2010-07-23 16:20:54 -070090 // Initiates a reboot if the current state is
91 // UPDATED_NEED_REBOOT. Returns true on sucess, false otherwise.
92 bool RebootIfNeeded();
93
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070094 // DownloadActionDelegate method
95 void BytesReceived(uint64_t bytes_received, uint64_t total);
96
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070097 private:
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070098 // Sets the status to the given status and notifies a status update
99 // over dbus.
100 void SetStatusAndNotify(UpdateStatus status);
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700101
Darin Petkov09f96c32010-07-20 09:24:57 -0700102 // Creates an error event object in |error_event_| to be included in
103 // an OmahaRequestAction once the current action processor is done.
Darin Petkov777dbfa2010-07-20 15:03:37 -0700104 void CreatePendingErrorEvent(AbstractAction* action, ActionExitCode code);
Darin Petkov09f96c32010-07-20 09:24:57 -0700105
106 // If there's a pending error event allocated in |error_event_|,
107 // schedules an OmahaRequestAction with that event in the current
108 // processor, clears the pending event, updates the status and
109 // returns true. Returns false otherwise.
110 bool ScheduleErrorEventAction();
111
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700112 struct timespec last_notify_time_;
113
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700114 std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
115 ActionProcessor processor_;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700116
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700117 // If non-null, this UpdateAttempter will send status updates over this
118 // dbus service.
119 UpdateEngineService* dbus_service_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700120
121 // pointer to the OmahaResponseHandlerAction in the actions_ vector;
122 std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700123
Darin Petkov1cbd78f2010-07-29 12:38:34 -0700124 // Pointer to the preferences store interface.
125 PrefsInterface* prefs_;
126
Darin Petkov9d65b7b2010-07-20 09:13:01 -0700127 // Pointer to the UMA metrics collection library.
128 MetricsLibraryInterface* metrics_lib_;
129
Darin Petkov09f96c32010-07-20 09:24:57 -0700130 // Pending error event, if any.
131 scoped_ptr<OmahaEvent> error_event_;
132
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700133 // For status:
134 UpdateStatus status_;
135 double download_progress_;
136 int64_t last_checked_time_;
137 std::string new_version_;
138 int64_t new_size_;
139
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700140 // Device paramaters common to all Omaha requests.
141 OmahaRequestDeviceParams omaha_request_params_;
142
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700143 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
144};
145
146} // namespace chromeos_update_engine
147
148#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__