blob: 711c8ea0339eaa535d4caae51911ad3854547fde [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
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070018struct UpdateEngineService;
19
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070020namespace chromeos_update_engine {
21
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070022extern const char* kUpdateCompletedMarker;
23
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070024enum UpdateStatus {
25 UPDATE_STATUS_IDLE = 0,
26 UPDATE_STATUS_CHECKING_FOR_UPDATE,
27 UPDATE_STATUS_UPDATE_AVAILABLE,
28 UPDATE_STATUS_DOWNLOADING,
29 UPDATE_STATUS_VERIFYING,
30 UPDATE_STATUS_FINALIZING,
31 UPDATE_STATUS_UPDATED_NEED_REBOOT
32};
33
34const char* UpdateStatusToString(UpdateStatus status);
35
36class UpdateAttempter : public ActionProcessorDelegate,
37 public DownloadActionDelegate {
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070038 public:
Darin Petkova4a8a8c2010-07-15 22:21:12 -070039 UpdateAttempter() : dbus_service_(NULL),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070040 status_(UPDATE_STATUS_IDLE),
41 download_progress_(0.0),
42 last_checked_time_(0),
43 new_version_("0.0.0.0"),
44 new_size_(0) {
45 last_notify_time_.tv_sec = 0;
46 last_notify_time_.tv_nsec = 0;
Andrew de los Reyes6b78e292010-05-10 15:54:39 -070047 if (utils::FileExists(kUpdateCompletedMarker))
48 status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070049 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -070050 void Update();
51
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070052 // ActionProcessorDelegate methods:
Darin Petkovc1a8b422010-07-19 11:34:49 -070053 void ProcessingDone(const ActionProcessor* processor, ActionExitCode code);
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070054 void ProcessingStopped(const ActionProcessor* processor);
55 void ActionCompleted(ActionProcessor* processor,
56 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070057 ActionExitCode code);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070058
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070059 // Stop updating. An attempt will be made to record status to the disk
60 // so that updates can be resumed later.
61 void Terminate();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070062
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070063 // Try to resume from a previously Terminate()d update.
64 void ResumeUpdating();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070065
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070066 // Returns the current status in the out params. Returns true on success.
67 bool GetStatus(int64_t* last_checked_time,
68 double* progress,
69 std::string* current_operation,
70 std::string* new_version,
71 int64_t* new_size);
72
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070073 void set_dbus_service(struct UpdateEngineService* dbus_service) {
74 dbus_service_ = dbus_service;
75 }
76
77 void CheckForUpdate();
78
79 // DownloadActionDelegate method
80 void BytesReceived(uint64_t bytes_received, uint64_t total);
81
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070082 private:
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070083 // Sets the status to the given status and notifies a status update
84 // over dbus.
85 void SetStatusAndNotify(UpdateStatus status);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070086
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070087 struct timespec last_notify_time_;
88
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070089 std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
90 ActionProcessor processor_;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070091
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070092 // If non-null, this UpdateAttempter will send status updates over this
93 // dbus service.
94 UpdateEngineService* dbus_service_;
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -070095
96 // pointer to the OmahaResponseHandlerAction in the actions_ vector;
97 std::tr1::shared_ptr<OmahaResponseHandlerAction> response_handler_action_;
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070098
99 // For status:
100 UpdateStatus status_;
101 double download_progress_;
102 int64_t last_checked_time_;
103 std::string new_version_;
104 int64_t new_size_;
105
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700106 // Device paramaters common to all Omaha requests.
107 OmahaRequestDeviceParams omaha_request_params_;
108
Andrew de los Reyes4e9b9f42010-04-26 15:06:43 -0700109 DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
110};
111
112} // namespace chromeos_update_engine
113
114#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_ATTEMPTER_H__