blob: c301e645dbd8389f7b1ef7d88e8cb80c19cafb8d [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,
50 public PostinstallRunnerAction::DelegateInterface {
Alex Deymo5e3ea272016-01-28 13:42:23 -080051 public:
52 using UpdateStatus = update_engine::UpdateStatus;
53
Alex Deymo03a4de72016-07-20 16:08:23 -070054 UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080055 PrefsInterface* prefs,
56 BootControlInterface* boot_control_,
57 HardwareInterface* hardware_);
58 ~UpdateAttempterAndroid() override;
59
60 // Further initialization to be done post construction.
61 void Init();
62
63 // ServiceDelegateAndroidInterface overrides.
64 bool ApplyPayload(const std::string& payload_url,
65 int64_t payload_offset,
66 int64_t payload_size,
67 const std::vector<std::string>& key_value_pair_headers,
68 brillo::ErrorPtr* error) override;
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090069 bool ApplyPayload(int fd,
70 int64_t payload_offset,
71 int64_t payload_size,
72 const std::vector<std::string>& key_value_pair_headers,
73 brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080074 bool SuspendUpdate(brillo::ErrorPtr* error) override;
75 bool ResumeUpdate(brillo::ErrorPtr* error) override;
76 bool CancelUpdate(brillo::ErrorPtr* error) override;
Alex Deymo3b678db2016-02-09 11:50:06 -080077 bool ResetStatus(brillo::ErrorPtr* error) override;
Sen Jiang28d8ed92018-02-01 13:46:39 -080078 bool VerifyPayloadApplicable(const std::string& metadata_filename,
79 brillo::ErrorPtr* error) override;
Yifan Hong6f7e29f2019-12-13 14:41:06 -080080 uint64_t AllocateSpaceForPayload(
81 const std::string& metadata_filename,
82 const std::vector<std::string>& key_value_pair_headers,
83 brillo::ErrorPtr* error) override;
Yifan Hong2236ea02019-12-13 16:11:22 -080084 int32_t CleanupSuccessfulUpdate(brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080085
86 // ActionProcessorDelegate methods:
87 void ProcessingDone(const ActionProcessor* processor,
88 ErrorCode code) override;
89 void ProcessingStopped(const ActionProcessor* processor) override;
90 void ActionCompleted(ActionProcessor* processor,
91 AbstractAction* action,
92 ErrorCode code) override;
93
94 // DownloadActionDelegate overrides.
95 void BytesReceived(uint64_t bytes_progressed,
96 uint64_t bytes_received,
97 uint64_t total) override;
98 bool ShouldCancel(ErrorCode* cancel_reason) override;
99 void DownloadComplete() override;
100
Alex Deymo0d298542016-03-30 18:31:49 -0700101 // PostinstallRunnerAction::DelegateInterface
102 void ProgressUpdate(double progress) override;
103
Alex Deymo5e3ea272016-01-28 13:42:23 -0800104 private:
Tianjie Xu90aaa102017-10-10 17:39:03 -0700105 friend class UpdateAttempterAndroidTest;
106
Alex Deymo5e3ea272016-01-28 13:42:23 -0800107 // Schedules an event loop callback to start the action processor. This is
108 // scheduled asynchronously to unblock the event loop.
109 void ScheduleProcessingStart();
110
111 // Notifies an update request completed with the given error |code| to all
112 // observers.
113 void TerminateUpdateAndNotify(ErrorCode error_code);
114
115 // Sets the status to the given |status| and notifies a status update to
116 // all observers.
117 void SetStatusAndNotify(UpdateStatus status);
118
119 // Helper method to construct the sequence of actions to be performed for
Amin Hassani667cf7b2018-07-25 14:32:00 -0700120 // applying an update using a given HttpFetcher. The ownership of |fetcher| is
121 // passed to this function.
122 void BuildUpdateActions(HttpFetcher* fetcher);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800123
Alex Deymo5e3ea272016-01-28 13:42:23 -0800124 // Writes to the processing completed marker. Does nothing if
125 // |update_completed_marker_| is empty.
126 bool WriteUpdateCompletedMarker();
127
128 // Returns whether an update was completed in the current boot.
129 bool UpdateCompletedOnThisBoot();
130
Tianjie Xu90aaa102017-10-10 17:39:03 -0700131 // Prefs to use for metrics report
132 // |kPrefsPayloadAttemptNumber|: number of update attempts for the current
133 // payload_id.
134 // |KprefsNumReboots|: number of reboots when applying the current update.
135 // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update.
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700136 // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the
137 // current update.
138 // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of
139 // the current update.
Tianjie Xud4777a12017-10-24 14:54:18 -0700140 // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current
141 // payload_id.
142 // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since
143 // the last successful update.
Tianjie Xu90aaa102017-10-10 17:39:03 -0700144
145 // Metrics report function to call:
146 // |ReportUpdateAttemptMetrics|
147 // |ReportSuccessfulUpdateMetrics|
148 // Prefs to update:
149 // |kPrefsSystemUpdatedMarker|
150 void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
151
152 // Metrics report function to call:
153 // |ReportAbnormallyTerminatedUpdateAttemptMetrics|
154 // |ReportTimeToRebootMetrics|
155 // Prefs to update:
156 // |kPrefsBootId|, |kPrefsPreviousVersion|
157 void UpdatePrefsAndReportUpdateMetricsOnReboot();
158
159 // Prefs to update:
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700160 // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|,
161 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700162 void UpdatePrefsOnUpdateStart(bool is_resume);
163
164 // Prefs to delete:
xunchang9cf52622019-01-25 11:04:58 -0800165 // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded|
Tianjie Xud4777a12017-10-24 14:54:18 -0700166 // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
xunchang9cf52622019-01-25 11:04:58 -0800167 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700168 void ClearMetricsPrefs();
169
Yifan Hongbd47d622019-12-13 14:59:58 -0800170 // Return source and target slots for update.
171 BootControlInterface::Slot GetCurrentSlot() const;
172 BootControlInterface::Slot GetTargetSlot() const;
173
174 // Helper of public VerifyPayloadApplicable. Return the parsed manifest in
175 // |manifest|.
176 static bool VerifyPayloadParseManifest(const std::string& metadata_filename,
177 DeltaArchiveManifest* manifest,
178 brillo::ErrorPtr* error);
179
Alex Deymo03a4de72016-07-20 16:08:23 -0700180 DaemonStateInterface* daemon_state_;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800181
182 // DaemonStateAndroid pointers.
183 PrefsInterface* prefs_;
184 BootControlInterface* boot_control_;
185 HardwareInterface* hardware_;
186
187 // Last status notification timestamp used for throttling. Use monotonic
188 // TimeTicks to ensure that notifications are sent even if the system clock is
189 // set back in the middle of an update.
190 base::TimeTicks last_notify_time_;
191
Amin Hassani04d41622018-12-20 15:35:41 -0800192 // Only direct proxy supported.
193 DirectProxyResolver proxy_resolver_;
194
Amin Hassani667cf7b2018-07-25 14:32:00 -0700195 // The processor for running Actions.
Alex Deymo5e3ea272016-01-28 13:42:23 -0800196 std::unique_ptr<ActionProcessor> processor_;
197
Alex Deymo5e3ea272016-01-28 13:42:23 -0800198 // The InstallPlan used during the ongoing update.
199 InstallPlan install_plan_;
200
201 // For status:
202 UpdateStatus status_{UpdateStatus::IDLE};
203 double download_progress_{0.0};
204
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800205 // The offset in the payload file where the CrAU part starts.
206 int64_t base_offset_{0};
207
Alex Deymo87792ea2016-07-25 15:40:36 -0700208 // Helper class to select the network to use during the update.
209 std::unique_ptr<NetworkSelectorInterface> network_selector_;
210
Tianjie Xu90aaa102017-10-10 17:39:03 -0700211 std::unique_ptr<ClockInterface> clock_;
212
Tianjie Xu1b661142017-09-28 14:03:42 -0700213 std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
214
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900215 ::android::base::unique_fd payload_fd_;
216
Alex Deymo5e3ea272016-01-28 13:42:23 -0800217 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
218};
219
220} // namespace chromeos_update_engine
221
222#endif // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_