blob: 9e91dca5aef2b93f314681589a6e3f4a4456ea94 [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"
31#include "update_engine/common/cpu_limiter.h"
32#include "update_engine/common/hardware_interface.h"
33#include "update_engine/common/prefs_interface.h"
Alex Deymo87792ea2016-07-25 15:40:36 -070034#include "update_engine/network_selector_interface.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080035#include "update_engine/payload_consumer/download_action.h"
Alex Deymo0d298542016-03-30 18:31:49 -070036#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080037#include "update_engine/service_delegate_android_interface.h"
38#include "update_engine/service_observer_interface.h"
39
40namespace chromeos_update_engine {
41
42class DaemonStateAndroid;
43
Alex Deymo0d298542016-03-30 18:31:49 -070044class UpdateAttempterAndroid
45 : public ServiceDelegateAndroidInterface,
46 public ActionProcessorDelegate,
47 public DownloadActionDelegate,
48 public PostinstallRunnerAction::DelegateInterface {
Alex Deymo5e3ea272016-01-28 13:42:23 -080049 public:
50 using UpdateStatus = update_engine::UpdateStatus;
51
52 UpdateAttempterAndroid(DaemonStateAndroid* daemon_state,
53 PrefsInterface* prefs,
54 BootControlInterface* boot_control_,
55 HardwareInterface* hardware_);
56 ~UpdateAttempterAndroid() override;
57
58 // Further initialization to be done post construction.
59 void Init();
60
61 // ServiceDelegateAndroidInterface overrides.
62 bool ApplyPayload(const std::string& payload_url,
63 int64_t payload_offset,
64 int64_t payload_size,
65 const std::vector<std::string>& key_value_pair_headers,
66 brillo::ErrorPtr* error) override;
67 bool SuspendUpdate(brillo::ErrorPtr* error) override;
68 bool ResumeUpdate(brillo::ErrorPtr* error) override;
69 bool CancelUpdate(brillo::ErrorPtr* error) override;
Alex Deymo3b678db2016-02-09 11:50:06 -080070 bool ResetStatus(brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080071
72 // ActionProcessorDelegate methods:
73 void ProcessingDone(const ActionProcessor* processor,
74 ErrorCode code) override;
75 void ProcessingStopped(const ActionProcessor* processor) override;
76 void ActionCompleted(ActionProcessor* processor,
77 AbstractAction* action,
78 ErrorCode code) override;
79
80 // DownloadActionDelegate overrides.
81 void BytesReceived(uint64_t bytes_progressed,
82 uint64_t bytes_received,
83 uint64_t total) override;
84 bool ShouldCancel(ErrorCode* cancel_reason) override;
85 void DownloadComplete() override;
86
Alex Deymo0d298542016-03-30 18:31:49 -070087 // PostinstallRunnerAction::DelegateInterface
88 void ProgressUpdate(double progress) override;
89
Alex Deymo5e3ea272016-01-28 13:42:23 -080090 private:
91 // Asynchronously marks the current slot as successful if needed. If already
92 // marked as good, CompleteUpdateBootFlags() is called starting the action
93 // processor.
94 void UpdateBootFlags();
95
96 // Called when the boot flags have been updated.
97 void CompleteUpdateBootFlags(bool success);
98
99 // Schedules an event loop callback to start the action processor. This is
100 // scheduled asynchronously to unblock the event loop.
101 void ScheduleProcessingStart();
102
103 // Notifies an update request completed with the given error |code| to all
104 // observers.
105 void TerminateUpdateAndNotify(ErrorCode error_code);
106
107 // Sets the status to the given |status| and notifies a status update to
108 // all observers.
109 void SetStatusAndNotify(UpdateStatus status);
110
111 // Helper method to construct the sequence of actions to be performed for
Alex Deymo2c131bb2016-05-26 16:43:13 -0700112 // applying an update from the given |url|.
113 void BuildUpdateActions(const std::string& url);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800114
115 // Sets up the download parameters based on the update requested on the
116 // |install_plan_|.
117 void SetupDownload();
118
119 // Writes to the processing completed marker. Does nothing if
120 // |update_completed_marker_| is empty.
121 bool WriteUpdateCompletedMarker();
122
123 // Returns whether an update was completed in the current boot.
124 bool UpdateCompletedOnThisBoot();
125
126 DaemonStateAndroid* daemon_state_;
127
128 // DaemonStateAndroid pointers.
129 PrefsInterface* prefs_;
130 BootControlInterface* boot_control_;
131 HardwareInterface* hardware_;
132
133 // Last status notification timestamp used for throttling. Use monotonic
134 // TimeTicks to ensure that notifications are sent even if the system clock is
135 // set back in the middle of an update.
136 base::TimeTicks last_notify_time_;
137
138 // The list of actions and action processor that runs them asynchronously.
139 // Only used when |ongoing_update_| is true.
140 std::vector<std::shared_ptr<AbstractAction>> actions_;
141 std::unique_ptr<ActionProcessor> processor_;
142
143 // Pointer to the DownloadAction in the actions_ vector.
144 std::shared_ptr<DownloadAction> download_action_;
145
146 // Whether there is an ongoing update. This implies that an update was started
147 // but not finished yet. This value will be true even if the update was
148 // suspended.
149 bool ongoing_update_{false};
150
151 // The InstallPlan used during the ongoing update.
152 InstallPlan install_plan_;
153
154 // For status:
155 UpdateStatus status_{UpdateStatus::IDLE};
156 double download_progress_{0.0};
157
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800158 // The offset in the payload file where the CrAU part starts.
159 int64_t base_offset_{0};
160
Alex Deymo5e3ea272016-01-28 13:42:23 -0800161 // Only direct proxy supported.
162 DirectProxyResolver proxy_resolver_;
163
164 // CPU limiter during the update.
165 CPULimiter cpu_limiter_;
166
Alex Deymo87792ea2016-07-25 15:40:36 -0700167 // Helper class to select the network to use during the update.
168 std::unique_ptr<NetworkSelectorInterface> network_selector_;
169
Alex Deymo5e3ea272016-01-28 13:42:23 -0800170 // Whether we have marked the current slot as good. This step is required
171 // before applying an update to the other slot.
172 bool updated_boot_flags_ = false;
173
174 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
175};
176
177} // namespace chromeos_update_engine
178
179#endif // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_