blob: 28bf90a9a2aedd49d463590d97a67cf9ffe25b3f [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
26#include <base/time/time.h>
27
28#include "update_engine/client_library/include/update_engine/update_status.h"
29#include "update_engine/common/action_processor.h"
30#include "update_engine/common/boot_control_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070031#include "update_engine/common/clock.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080032#include "update_engine/common/hardware_interface.h"
33#include "update_engine/common/prefs_interface.h"
Alex Deymo03a4de72016-07-20 16:08:23 -070034#include "update_engine/daemon_state_interface.h"
Tianjie Xu1b661142017-09-28 14:03:42 -070035#include "update_engine/metrics_reporter_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070036#include "update_engine/metrics_utils.h"
Alex Deymo87792ea2016-07-25 15:40:36 -070037#include "update_engine/network_selector_interface.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080038#include "update_engine/payload_consumer/download_action.h"
Alex Deymo0d298542016-03-30 18:31:49 -070039#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080040#include "update_engine/service_delegate_android_interface.h"
41#include "update_engine/service_observer_interface.h"
42
43namespace chromeos_update_engine {
44
Alex Deymo0d298542016-03-30 18:31:49 -070045class UpdateAttempterAndroid
46 : public ServiceDelegateAndroidInterface,
47 public ActionProcessorDelegate,
48 public DownloadActionDelegate,
49 public PostinstallRunnerAction::DelegateInterface {
Alex Deymo5e3ea272016-01-28 13:42:23 -080050 public:
51 using UpdateStatus = update_engine::UpdateStatus;
52
Alex Deymo03a4de72016-07-20 16:08:23 -070053 UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080054 PrefsInterface* prefs,
55 BootControlInterface* boot_control_,
56 HardwareInterface* hardware_);
57 ~UpdateAttempterAndroid() override;
58
59 // Further initialization to be done post construction.
60 void Init();
61
62 // ServiceDelegateAndroidInterface overrides.
63 bool ApplyPayload(const std::string& payload_url,
64 int64_t payload_offset,
65 int64_t payload_size,
66 const std::vector<std::string>& key_value_pair_headers,
67 brillo::ErrorPtr* error) override;
68 bool SuspendUpdate(brillo::ErrorPtr* error) override;
69 bool ResumeUpdate(brillo::ErrorPtr* error) override;
70 bool CancelUpdate(brillo::ErrorPtr* error) override;
Alex Deymo3b678db2016-02-09 11:50:06 -080071 bool ResetStatus(brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080072
73 // ActionProcessorDelegate methods:
74 void ProcessingDone(const ActionProcessor* processor,
75 ErrorCode code) override;
76 void ProcessingStopped(const ActionProcessor* processor) override;
77 void ActionCompleted(ActionProcessor* processor,
78 AbstractAction* action,
79 ErrorCode code) override;
80
81 // DownloadActionDelegate overrides.
82 void BytesReceived(uint64_t bytes_progressed,
83 uint64_t bytes_received,
84 uint64_t total) override;
85 bool ShouldCancel(ErrorCode* cancel_reason) override;
86 void DownloadComplete() override;
87
Alex Deymo0d298542016-03-30 18:31:49 -070088 // PostinstallRunnerAction::DelegateInterface
89 void ProgressUpdate(double progress) override;
90
Alex Deymo5e3ea272016-01-28 13:42:23 -080091 private:
Tianjie Xu90aaa102017-10-10 17:39:03 -070092 friend class UpdateAttempterAndroidTest;
93
Alex Deymo5e3ea272016-01-28 13:42:23 -080094 // Asynchronously marks the current slot as successful if needed. If already
95 // marked as good, CompleteUpdateBootFlags() is called starting the action
96 // processor.
97 void UpdateBootFlags();
98
99 // Called when the boot flags have been updated.
100 void CompleteUpdateBootFlags(bool success);
101
102 // Schedules an event loop callback to start the action processor. This is
103 // scheduled asynchronously to unblock the event loop.
104 void ScheduleProcessingStart();
105
106 // Notifies an update request completed with the given error |code| to all
107 // observers.
108 void TerminateUpdateAndNotify(ErrorCode error_code);
109
110 // Sets the status to the given |status| and notifies a status update to
111 // all observers.
112 void SetStatusAndNotify(UpdateStatus status);
113
114 // Helper method to construct the sequence of actions to be performed for
Alex Deymo2c131bb2016-05-26 16:43:13 -0700115 // applying an update from the given |url|.
116 void BuildUpdateActions(const std::string& url);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800117
Alex Deymo5e3ea272016-01-28 13:42:23 -0800118 // Writes to the processing completed marker. Does nothing if
119 // |update_completed_marker_| is empty.
120 bool WriteUpdateCompletedMarker();
121
122 // Returns whether an update was completed in the current boot.
123 bool UpdateCompletedOnThisBoot();
124
Tianjie Xu90aaa102017-10-10 17:39:03 -0700125 // Prefs to use for metrics report
126 // |kPrefsPayloadAttemptNumber|: number of update attempts for the current
127 // payload_id.
128 // |KprefsNumReboots|: number of reboots when applying the current update.
129 // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update.
130 // |kPrefsUpdateTimestampStart|: start timestamp of the current update.
Tianjie Xud4777a12017-10-24 14:54:18 -0700131 // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current
132 // payload_id.
133 // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since
134 // the last successful update.
Tianjie Xu90aaa102017-10-10 17:39:03 -0700135
136 // Metrics report function to call:
137 // |ReportUpdateAttemptMetrics|
138 // |ReportSuccessfulUpdateMetrics|
139 // Prefs to update:
140 // |kPrefsSystemUpdatedMarker|
141 void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
142
143 // Metrics report function to call:
144 // |ReportAbnormallyTerminatedUpdateAttemptMetrics|
145 // |ReportTimeToRebootMetrics|
146 // Prefs to update:
147 // |kPrefsBootId|, |kPrefsPreviousVersion|
148 void UpdatePrefsAndReportUpdateMetricsOnReboot();
149
150 // Prefs to update:
151 // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|
152 void UpdatePrefsOnUpdateStart(bool is_resume);
153
154 // Prefs to delete:
155 // |kPrefsNumReboots|, |kPrefsPayloadAttemptNumber|,
Tianjie Xud4777a12017-10-24 14:54:18 -0700156 // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
157 // |kPrefsCurrentBytesDownloaded|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700158 void ClearMetricsPrefs();
159
Alex Deymo03a4de72016-07-20 16:08:23 -0700160 DaemonStateInterface* daemon_state_;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800161
162 // DaemonStateAndroid pointers.
163 PrefsInterface* prefs_;
164 BootControlInterface* boot_control_;
165 HardwareInterface* hardware_;
166
167 // Last status notification timestamp used for throttling. Use monotonic
168 // TimeTicks to ensure that notifications are sent even if the system clock is
169 // set back in the middle of an update.
170 base::TimeTicks last_notify_time_;
171
172 // The list of actions and action processor that runs them asynchronously.
173 // Only used when |ongoing_update_| is true.
174 std::vector<std::shared_ptr<AbstractAction>> actions_;
175 std::unique_ptr<ActionProcessor> processor_;
176
177 // Pointer to the DownloadAction in the actions_ vector.
178 std::shared_ptr<DownloadAction> download_action_;
179
180 // Whether there is an ongoing update. This implies that an update was started
181 // but not finished yet. This value will be true even if the update was
182 // suspended.
183 bool ongoing_update_{false};
184
185 // The InstallPlan used during the ongoing update.
186 InstallPlan install_plan_;
187
188 // For status:
189 UpdateStatus status_{UpdateStatus::IDLE};
190 double download_progress_{0.0};
191
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800192 // The offset in the payload file where the CrAU part starts.
193 int64_t base_offset_{0};
194
Alex Deymo5e3ea272016-01-28 13:42:23 -0800195 // Only direct proxy supported.
196 DirectProxyResolver proxy_resolver_;
197
Alex Deymo87792ea2016-07-25 15:40:36 -0700198 // Helper class to select the network to use during the update.
199 std::unique_ptr<NetworkSelectorInterface> network_selector_;
200
Alex Deymo5e3ea272016-01-28 13:42:23 -0800201 // Whether we have marked the current slot as good. This step is required
202 // before applying an update to the other slot.
203 bool updated_boot_flags_ = false;
204
Tianjie Xu90aaa102017-10-10 17:39:03 -0700205 std::unique_ptr<ClockInterface> clock_;
206
Tianjie Xu1b661142017-09-28 14:03:42 -0700207 std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
208
Alex Deymo5e3ea272016-01-28 13:42:23 -0800209 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
210};
211
212} // namespace chromeos_update_engine
213
214#endif // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_