• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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/clock.h"
32 #include "update_engine/common/hardware_interface.h"
33 #include "update_engine/common/prefs_interface.h"
34 #include "update_engine/daemon_state_interface.h"
35 #include "update_engine/metrics_reporter_interface.h"
36 #include "update_engine/metrics_utils.h"
37 #include "update_engine/network_selector_interface.h"
38 #include "update_engine/payload_consumer/download_action.h"
39 #include "update_engine/payload_consumer/postinstall_runner_action.h"
40 #include "update_engine/service_delegate_android_interface.h"
41 #include "update_engine/service_observer_interface.h"
42 
43 namespace chromeos_update_engine {
44 
45 class UpdateAttempterAndroid
46     : public ServiceDelegateAndroidInterface,
47       public ActionProcessorDelegate,
48       public DownloadActionDelegate,
49       public PostinstallRunnerAction::DelegateInterface {
50  public:
51   using UpdateStatus = update_engine::UpdateStatus;
52 
53   UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
54                          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;
71   bool ResetStatus(brillo::ErrorPtr* error) override;
72   bool VerifyPayloadApplicable(const std::string& metadata_filename,
73                                brillo::ErrorPtr* error) override;
74 
75   // ActionProcessorDelegate methods:
76   void ProcessingDone(const ActionProcessor* processor,
77                       ErrorCode code) override;
78   void ProcessingStopped(const ActionProcessor* processor) override;
79   void ActionCompleted(ActionProcessor* processor,
80                        AbstractAction* action,
81                        ErrorCode code) override;
82 
83   // DownloadActionDelegate overrides.
84   void BytesReceived(uint64_t bytes_progressed,
85                      uint64_t bytes_received,
86                      uint64_t total) override;
87   bool ShouldCancel(ErrorCode* cancel_reason) override;
88   void DownloadComplete() override;
89 
90   // PostinstallRunnerAction::DelegateInterface
91   void ProgressUpdate(double progress) override;
92 
93  private:
94   friend class UpdateAttempterAndroidTest;
95 
96   // Asynchronously marks the current slot as successful if needed. If already
97   // marked as good, CompleteUpdateBootFlags() is called starting the action
98   // processor.
99   void UpdateBootFlags();
100 
101   // Called when the boot flags have been updated.
102   void CompleteUpdateBootFlags(bool success);
103 
104   // Schedules an event loop callback to start the action processor. This is
105   // scheduled asynchronously to unblock the event loop.
106   void ScheduleProcessingStart();
107 
108   // Notifies an update request completed with the given error |code| to all
109   // observers.
110   void TerminateUpdateAndNotify(ErrorCode error_code);
111 
112   // Sets the status to the given |status| and notifies a status update to
113   // all observers.
114   void SetStatusAndNotify(UpdateStatus status);
115 
116   // Helper method to construct the sequence of actions to be performed for
117   // applying an update from the given |url|.
118   void BuildUpdateActions(const std::string& url);
119 
120   // Writes to the processing completed marker. Does nothing if
121   // |update_completed_marker_| is empty.
122   bool WriteUpdateCompletedMarker();
123 
124   // Returns whether an update was completed in the current boot.
125   bool UpdateCompletedOnThisBoot();
126 
127   // Prefs to use for metrics report
128   // |kPrefsPayloadAttemptNumber|: number of update attempts for the current
129   // payload_id.
130   // |KprefsNumReboots|: number of reboots when applying the current update.
131   // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update.
132   // |kPrefsUpdateTimestampStart|: start timestamp of the current update.
133   // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current
134   // payload_id.
135   // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since
136   // the last successful update.
137 
138   // Metrics report function to call:
139   //   |ReportUpdateAttemptMetrics|
140   //   |ReportSuccessfulUpdateMetrics|
141   // Prefs to update:
142   //   |kPrefsSystemUpdatedMarker|
143   void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
144 
145   // Metrics report function to call:
146   //   |ReportAbnormallyTerminatedUpdateAttemptMetrics|
147   //   |ReportTimeToRebootMetrics|
148   // Prefs to update:
149   //   |kPrefsBootId|, |kPrefsPreviousVersion|
150   void UpdatePrefsAndReportUpdateMetricsOnReboot();
151 
152   // Prefs to update:
153   //   |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|
154   void UpdatePrefsOnUpdateStart(bool is_resume);
155 
156   // Prefs to delete:
157   //   |kPrefsNumReboots|, |kPrefsPayloadAttemptNumber|,
158   //   |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
159   //   |kPrefsCurrentBytesDownloaded|
160   void ClearMetricsPrefs();
161 
162   DaemonStateInterface* daemon_state_;
163 
164   // DaemonStateAndroid pointers.
165   PrefsInterface* prefs_;
166   BootControlInterface* boot_control_;
167   HardwareInterface* hardware_;
168 
169   // Last status notification timestamp used for throttling. Use monotonic
170   // TimeTicks to ensure that notifications are sent even if the system clock is
171   // set back in the middle of an update.
172   base::TimeTicks last_notify_time_;
173 
174   // The list of actions and action processor that runs them asynchronously.
175   // Only used when |ongoing_update_| is true.
176   std::vector<std::shared_ptr<AbstractAction>> actions_;
177   std::unique_ptr<ActionProcessor> processor_;
178 
179   // Pointer to the DownloadAction in the actions_ vector.
180   std::shared_ptr<DownloadAction> download_action_;
181 
182   // Whether there is an ongoing update. This implies that an update was started
183   // but not finished yet. This value will be true even if the update was
184   // suspended.
185   bool ongoing_update_{false};
186 
187   // The InstallPlan used during the ongoing update.
188   InstallPlan install_plan_;
189 
190   // For status:
191   UpdateStatus status_{UpdateStatus::IDLE};
192   double download_progress_{0.0};
193 
194   // The offset in the payload file where the CrAU part starts.
195   int64_t base_offset_{0};
196 
197   // Only direct proxy supported.
198   DirectProxyResolver proxy_resolver_;
199 
200   // Helper class to select the network to use during the update.
201   std::unique_ptr<NetworkSelectorInterface> network_selector_;
202 
203   // Whether we have marked the current slot as good. This step is required
204   // before applying an update to the other slot.
205   bool updated_boot_flags_ = false;
206 
207   std::unique_ptr<ClockInterface> clock_;
208 
209   std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
210 
211   DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
212 };
213 
214 }  // namespace chromeos_update_engine
215 
216 #endif  // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
217