blob: 106e1171e35f49de3f28475488f04836e43b89f4 [file] [log] [blame]
Alex Deymo5e3ea272016-01-28 13:42:23 -08001//
2// Copyright (C) 2016 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
18#define UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
19
20#include <stdint.h>
21
22#include <memory>
23#include <string>
24#include <vector>
25
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090026#include <android-base/unique_fd.h>
Alex Deymo5e3ea272016-01-28 13:42:23 -080027#include <base/time/time.h>
28
29#include "update_engine/client_library/include/update_engine/update_status.h"
30#include "update_engine/common/action_processor.h"
31#include "update_engine/common/boot_control_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070032#include "update_engine/common/clock.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080033#include "update_engine/common/hardware_interface.h"
34#include "update_engine/common/prefs_interface.h"
Alex Deymo03a4de72016-07-20 16:08:23 -070035#include "update_engine/daemon_state_interface.h"
Tianjie Xu1b661142017-09-28 14:03:42 -070036#include "update_engine/metrics_reporter_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070037#include "update_engine/metrics_utils.h"
Alex Deymo87792ea2016-07-25 15:40:36 -070038#include "update_engine/network_selector_interface.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080039#include "update_engine/payload_consumer/download_action.h"
Alex Deymo0d298542016-03-30 18:31:49 -070040#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080041#include "update_engine/service_delegate_android_interface.h"
42#include "update_engine/service_observer_interface.h"
43
44namespace chromeos_update_engine {
45
Alex Deymo0d298542016-03-30 18:31:49 -070046class UpdateAttempterAndroid
47 : public ServiceDelegateAndroidInterface,
48 public ActionProcessorDelegate,
49 public DownloadActionDelegate,
Yifan Hong90965502020-02-19 15:22:47 -080050 public PostinstallRunnerAction::DelegateInterface,
51 public CleanupPreviousUpdateActionDelegateInterface {
Alex Deymo5e3ea272016-01-28 13:42:23 -080052 public:
53 using UpdateStatus = update_engine::UpdateStatus;
54
Alex Deymo03a4de72016-07-20 16:08:23 -070055 UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080056 PrefsInterface* prefs,
57 BootControlInterface* boot_control_,
58 HardwareInterface* hardware_);
59 ~UpdateAttempterAndroid() override;
60
61 // Further initialization to be done post construction.
62 void Init();
63
64 // ServiceDelegateAndroidInterface overrides.
65 bool ApplyPayload(const std::string& payload_url,
66 int64_t payload_offset,
67 int64_t payload_size,
68 const std::vector<std::string>& key_value_pair_headers,
69 brillo::ErrorPtr* error) override;
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090070 bool ApplyPayload(int fd,
71 int64_t payload_offset,
72 int64_t payload_size,
73 const std::vector<std::string>& key_value_pair_headers,
74 brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080075 bool SuspendUpdate(brillo::ErrorPtr* error) override;
76 bool ResumeUpdate(brillo::ErrorPtr* error) override;
77 bool CancelUpdate(brillo::ErrorPtr* error) override;
Alex Deymo3b678db2016-02-09 11:50:06 -080078 bool ResetStatus(brillo::ErrorPtr* error) override;
Sen Jiang28d8ed92018-02-01 13:46:39 -080079 bool VerifyPayloadApplicable(const std::string& metadata_filename,
80 brillo::ErrorPtr* error) override;
Yifan Hong6f7e29f2019-12-13 14:41:06 -080081 uint64_t AllocateSpaceForPayload(
82 const std::string& metadata_filename,
83 const std::vector<std::string>& key_value_pair_headers,
84 brillo::ErrorPtr* error) override;
Yifan Hong2236ea02019-12-13 16:11:22 -080085 int32_t CleanupSuccessfulUpdate(brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080086
87 // ActionProcessorDelegate methods:
88 void ProcessingDone(const ActionProcessor* processor,
89 ErrorCode code) override;
90 void ProcessingStopped(const ActionProcessor* processor) override;
91 void ActionCompleted(ActionProcessor* processor,
92 AbstractAction* action,
93 ErrorCode code) override;
94
95 // DownloadActionDelegate overrides.
96 void BytesReceived(uint64_t bytes_progressed,
97 uint64_t bytes_received,
98 uint64_t total) override;
99 bool ShouldCancel(ErrorCode* cancel_reason) override;
100 void DownloadComplete() override;
101
Alex Deymo0d298542016-03-30 18:31:49 -0700102 // PostinstallRunnerAction::DelegateInterface
103 void ProgressUpdate(double progress) override;
104
Yifan Hong90965502020-02-19 15:22:47 -0800105 // CleanupPreviousUpdateActionDelegateInterface
106 void OnCleanupProgressUpdate(double progress) override;
107
Alex Deymo5e3ea272016-01-28 13:42:23 -0800108 private:
Tianjie Xu90aaa102017-10-10 17:39:03 -0700109 friend class UpdateAttempterAndroidTest;
110
Alex Deymo5e3ea272016-01-28 13:42:23 -0800111 // Schedules an event loop callback to start the action processor. This is
112 // scheduled asynchronously to unblock the event loop.
113 void ScheduleProcessingStart();
114
115 // Notifies an update request completed with the given error |code| to all
116 // observers.
117 void TerminateUpdateAndNotify(ErrorCode error_code);
118
119 // Sets the status to the given |status| and notifies a status update to
120 // all observers.
121 void SetStatusAndNotify(UpdateStatus status);
122
123 // Helper method to construct the sequence of actions to be performed for
Amin Hassani667cf7b2018-07-25 14:32:00 -0700124 // applying an update using a given HttpFetcher. The ownership of |fetcher| is
125 // passed to this function.
126 void BuildUpdateActions(HttpFetcher* fetcher);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800127
Alex Deymo5e3ea272016-01-28 13:42:23 -0800128 // Writes to the processing completed marker. Does nothing if
129 // |update_completed_marker_| is empty.
130 bool WriteUpdateCompletedMarker();
131
132 // Returns whether an update was completed in the current boot.
133 bool UpdateCompletedOnThisBoot();
134
Tianjie Xu90aaa102017-10-10 17:39:03 -0700135 // Prefs to use for metrics report
136 // |kPrefsPayloadAttemptNumber|: number of update attempts for the current
137 // payload_id.
138 // |KprefsNumReboots|: number of reboots when applying the current update.
139 // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update.
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700140 // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the
141 // current update.
142 // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of
143 // the current update.
Tianjie Xud4777a12017-10-24 14:54:18 -0700144 // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current
145 // payload_id.
146 // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since
147 // the last successful update.
Tianjie Xu90aaa102017-10-10 17:39:03 -0700148
149 // Metrics report function to call:
150 // |ReportUpdateAttemptMetrics|
151 // |ReportSuccessfulUpdateMetrics|
152 // Prefs to update:
153 // |kPrefsSystemUpdatedMarker|
154 void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
155
156 // Metrics report function to call:
157 // |ReportAbnormallyTerminatedUpdateAttemptMetrics|
158 // |ReportTimeToRebootMetrics|
159 // Prefs to update:
160 // |kPrefsBootId|, |kPrefsPreviousVersion|
161 void UpdatePrefsAndReportUpdateMetricsOnReboot();
162
163 // Prefs to update:
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700164 // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|,
165 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700166 void UpdatePrefsOnUpdateStart(bool is_resume);
167
168 // Prefs to delete:
xunchang9cf52622019-01-25 11:04:58 -0800169 // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded|
Tianjie Xud4777a12017-10-24 14:54:18 -0700170 // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
xunchang9cf52622019-01-25 11:04:58 -0800171 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700172 void ClearMetricsPrefs();
173
Yifan Hongbd47d622019-12-13 14:59:58 -0800174 // Return source and target slots for update.
175 BootControlInterface::Slot GetCurrentSlot() const;
176 BootControlInterface::Slot GetTargetSlot() const;
177
178 // Helper of public VerifyPayloadApplicable. Return the parsed manifest in
179 // |manifest|.
180 static bool VerifyPayloadParseManifest(const std::string& metadata_filename,
181 DeltaArchiveManifest* manifest,
182 brillo::ErrorPtr* error);
183
Yifan Hong90965502020-02-19 15:22:47 -0800184 // Enqueue and run a CleanupPreviousUpdateAction.
185 void ScheduleCleanupPreviousUpdate();
186
Alex Deymo03a4de72016-07-20 16:08:23 -0700187 DaemonStateInterface* daemon_state_;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800188
189 // DaemonStateAndroid pointers.
190 PrefsInterface* prefs_;
191 BootControlInterface* boot_control_;
192 HardwareInterface* hardware_;
193
194 // Last status notification timestamp used for throttling. Use monotonic
195 // TimeTicks to ensure that notifications are sent even if the system clock is
196 // set back in the middle of an update.
197 base::TimeTicks last_notify_time_;
198
Amin Hassani04d41622018-12-20 15:35:41 -0800199 // Only direct proxy supported.
200 DirectProxyResolver proxy_resolver_;
201
Amin Hassani667cf7b2018-07-25 14:32:00 -0700202 // The processor for running Actions.
Alex Deymo5e3ea272016-01-28 13:42:23 -0800203 std::unique_ptr<ActionProcessor> processor_;
204
Alex Deymo5e3ea272016-01-28 13:42:23 -0800205 // The InstallPlan used during the ongoing update.
206 InstallPlan install_plan_;
207
208 // For status:
209 UpdateStatus status_{UpdateStatus::IDLE};
210 double download_progress_{0.0};
211
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800212 // The offset in the payload file where the CrAU part starts.
213 int64_t base_offset_{0};
214
Alex Deymo87792ea2016-07-25 15:40:36 -0700215 // Helper class to select the network to use during the update.
216 std::unique_ptr<NetworkSelectorInterface> network_selector_;
217
Tianjie Xu90aaa102017-10-10 17:39:03 -0700218 std::unique_ptr<ClockInterface> clock_;
219
Tianjie Xu1b661142017-09-28 14:03:42 -0700220 std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
221
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900222 ::android::base::unique_fd payload_fd_;
223
Alex Deymo5e3ea272016-01-28 13:42:23 -0800224 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
225};
226
227} // namespace chromeos_update_engine
228
229#endif // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_