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