blob: a2e3169b90fb4de9c9ec15b43fef2c1c91e81c10 [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;
66
67 // ActionProcessorDelegate methods:
68 void ProcessingDone(const ActionProcessor* processor,
69 ErrorCode code) override;
70 void ProcessingStopped(const ActionProcessor* processor) override;
71 void ActionCompleted(ActionProcessor* processor,
72 AbstractAction* action,
73 ErrorCode code) override;
74
75 // DownloadActionDelegate overrides.
76 void BytesReceived(uint64_t bytes_progressed,
77 uint64_t bytes_received,
78 uint64_t total) override;
79 bool ShouldCancel(ErrorCode* cancel_reason) override;
80 void DownloadComplete() override;
81
82 private:
83 // Asynchronously marks the current slot as successful if needed. If already
84 // marked as good, CompleteUpdateBootFlags() is called starting the action
85 // processor.
86 void UpdateBootFlags();
87
88 // Called when the boot flags have been updated.
89 void CompleteUpdateBootFlags(bool success);
90
91 // Schedules an event loop callback to start the action processor. This is
92 // scheduled asynchronously to unblock the event loop.
93 void ScheduleProcessingStart();
94
95 // Notifies an update request completed with the given error |code| to all
96 // observers.
97 void TerminateUpdateAndNotify(ErrorCode error_code);
98
99 // Sets the status to the given |status| and notifies a status update to
100 // all observers.
101 void SetStatusAndNotify(UpdateStatus status);
102
103 // Helper method to construct the sequence of actions to be performed for
104 // applying an update.
105 void BuildUpdateActions();
106
107 // Sets up the download parameters based on the update requested on the
108 // |install_plan_|.
109 void SetupDownload();
110
111 // Writes to the processing completed marker. Does nothing if
112 // |update_completed_marker_| is empty.
113 bool WriteUpdateCompletedMarker();
114
115 // Returns whether an update was completed in the current boot.
116 bool UpdateCompletedOnThisBoot();
117
118 DaemonStateAndroid* daemon_state_;
119
120 // DaemonStateAndroid pointers.
121 PrefsInterface* prefs_;
122 BootControlInterface* boot_control_;
123 HardwareInterface* hardware_;
124
125 // Last status notification timestamp used for throttling. Use monotonic
126 // TimeTicks to ensure that notifications are sent even if the system clock is
127 // set back in the middle of an update.
128 base::TimeTicks last_notify_time_;
129
130 // The list of actions and action processor that runs them asynchronously.
131 // Only used when |ongoing_update_| is true.
132 std::vector<std::shared_ptr<AbstractAction>> actions_;
133 std::unique_ptr<ActionProcessor> processor_;
134
135 // Pointer to the DownloadAction in the actions_ vector.
136 std::shared_ptr<DownloadAction> download_action_;
137
138 // Whether there is an ongoing update. This implies that an update was started
139 // but not finished yet. This value will be true even if the update was
140 // suspended.
141 bool ongoing_update_{false};
142
143 // The InstallPlan used during the ongoing update.
144 InstallPlan install_plan_;
145
146 // For status:
147 UpdateStatus status_{UpdateStatus::IDLE};
148 double download_progress_{0.0};
149
150 // Only direct proxy supported.
151 DirectProxyResolver proxy_resolver_;
152
153 // CPU limiter during the update.
154 CPULimiter cpu_limiter_;
155
156 // Whether we have marked the current slot as good. This step is required
157 // before applying an update to the other slot.
158 bool updated_boot_flags_ = false;
159
160 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
161};
162
163} // namespace chromeos_update_engine
164
165#endif // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_