blob: d384a0eb2a126d43cee5f4b425efd41496af0e69 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_PAYLOAD_STATE_INTERFACE_H_
18#define UPDATE_ENGINE_PAYLOAD_STATE_INTERFACE_H_
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080019
20#include <string>
21
Alex Deymo39910dc2015-11-09 17:04:30 -080022#include "update_engine/common/action_processor.h"
23#include "update_engine/common/constants.h"
Jay Srinivasan08262882012-12-28 19:29:43 -080024#include "update_engine/omaha_response.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080025
26namespace chromeos_update_engine {
27
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080028// Describes the methods that need to be implemented by the PayloadState class.
29// This interface has been carved out to support mocking of the PayloadState
30// object.
31class PayloadStateInterface {
32 public:
Alex Deymoe8948702014-11-11 21:44:45 -080033 virtual ~PayloadStateInterface() = default;
34
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080035 // Sets the internal payload state based on the given Omaha response. This
36 // response could be the same or different from the one for which we've stored
37 // the internal state. If it's different, then this method resets all the
38 // internal state corresponding to the old response. Since the Omaha response
39 // has a lot of fields that are not related to payload state, it uses only
40 // a subset of the fields in the Omaha response to compare equality.
41 virtual void SetResponse(const OmahaResponse& response) = 0;
42
43 // This method should be called whenever we have completed downloading all
44 // the bytes of a payload and have verified that its size and hash match the
45 // expected values. We use this notificaiton to increment the payload attempt
46 // number so that the throttle the next attempt to download the same payload
47 // (in case there's an error in subsequent steps such as post-install)
48 // appropriately.
49 virtual void DownloadComplete() = 0;
50
51 // This method should be called whenever we receive new bytes from the
52 // network for the current payload. We use this notification to reset the
53 // failure count for a given URL since receipt of some bytes means we are
54 // able to make forward progress with the current URL.
55 virtual void DownloadProgress(size_t count) = 0;
56
Chris Sosabe45bef2013-04-09 18:25:12 -070057 // This method should be called every time we resume an update attempt.
58 virtual void UpdateResumed() = 0;
59
Jay Srinivasan19409b72013-04-12 19:23:36 -070060 // This method should be called every time we begin a new update. This method
61 // should not be called when we resume an update from the previously
62 // downloaded point. This is used to reset the metrics for each new update.
63 virtual void UpdateRestarted() = 0;
64
65 // This method should be called once after an update attempt succeeds. This
66 // is when the relevant UMA metrics that are tracked on a per-update-basis
67 // are uploaded to the UMA server.
David Zeuthen9a017f22013-04-11 16:10:26 -070068 virtual void UpdateSucceeded() = 0;
69
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080070 // This method should be called whenever an update attempt fails with the
71 // given error code. We use this notification to update the payload state
72 // depending on the type of the error that happened.
David Zeuthena99981f2013-04-29 13:42:47 -070073 virtual void UpdateFailed(ErrorCode error) = 0;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080074
Alex Deymo42432912013-07-12 20:21:15 -070075 // This method should be called whenever a succeeded update is canceled, and
76 // thus can only be called after UpdateSucceeded(). This is currently used
77 // only for manual testing using the update_engine_client.
78 virtual void ResetUpdateStatus() = 0;
79
Chris Sosaaa18e162013-06-20 13:20:30 -070080 // This method should be called every time we initiate a Rollback.
81 virtual void Rollback() = 0;
82
Alex Deymo42432912013-07-12 20:21:15 -070083 // Sets the expectations to boot into the new version in the next reboot.
84 // This function is called every time a new update is marked as ready by
85 // UpdateSuccess(). |target_version_uid| is an unique identifier of the
86 // applied payload. It can be any string, as long as the same string is used
87 // for the same payload.
88 virtual void ExpectRebootInNewVersion(
89 const std::string& target_version_uid) = 0;
90
Gilad Arnold74b5f552014-10-07 08:17:16 -070091 // Sets whether P2P is being used to download the update payload. This
92 // is used to keep track of download sources being used and should be called
David Zeuthenbb8bdc72013-09-03 13:43:48 -070093 // before the transfer begins.
94 virtual void SetUsingP2PForDownloading(bool value) = 0;
95
Gilad Arnold74b5f552014-10-07 08:17:16 -070096 // Sets whether P2P is being used for sharing the update payloads.
97 virtual void SetUsingP2PForSharing(bool value) = 0;
98
Jay Srinivasan08262882012-12-28 19:29:43 -080099 // Returns true if we should backoff the current download attempt.
100 // False otherwise.
101 virtual bool ShouldBackoffDownload() = 0;
102
103 // Returns the currently stored response "signature". The signature is a
104 // subset of fields that are of interest to the PayloadState behavior.
105 virtual std::string GetResponseSignature() = 0;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800106
107 // Returns the payload attempt number.
Alex Deymo820cc702013-06-28 15:43:46 -0700108 virtual int GetPayloadAttemptNumber() = 0;
109
110 // Returns the payload attempt number of the attempted full payload. Returns
111 // 0 for delta payloads.
112 virtual int GetFullPayloadAttemptNumber() = 0;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800113
Jay Srinivasan53173b92013-05-17 17:13:01 -0700114 // Returns the current URL. Returns an empty string if there's no valid URL.
115 virtual std::string GetCurrentUrl() = 0;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800116
117 // Returns the current URL's failure count.
118 virtual uint32_t GetUrlFailureCount() = 0;
Jay Srinivasan08262882012-12-28 19:29:43 -0800119
David Zeuthencc6f9962013-04-18 11:57:24 -0700120 // Returns the total number of times a new URL has been switched to
121 // for the current response.
122 virtual uint32_t GetUrlSwitchCount() = 0;
123
David Zeuthena573d6f2013-06-14 16:13:36 -0700124 // Returns the total number of different responses seen since the
125 // last successful update.
126 virtual int GetNumResponsesSeen() = 0;
127
Jay Srinivasan08262882012-12-28 19:29:43 -0800128 // Returns the expiry time for the current backoff period.
129 virtual base::Time GetBackoffExpiryTime() = 0;
David Zeuthen9a017f22013-04-11 16:10:26 -0700130
131 // Returns the elapsed time used for this update, including time
132 // where the device is powered off and sleeping. If the
133 // update has not completed, returns the time spent so far.
134 virtual base::TimeDelta GetUpdateDuration() = 0;
135
136 // Returns the time used for this update not including time when
137 // the device is powered off or sleeping. If the update has not
138 // completed, returns the time spent so far.
139 virtual base::TimeDelta GetUpdateDurationUptime() = 0;
Jay Srinivasan19409b72013-04-12 19:23:36 -0700140
141 // Returns the number of bytes that have been downloaded for each source for
142 // each new update attempt. If we resume an update, we'll continue from the
143 // previous value, but if we get a new response or if the previous attempt
144 // failed, we'll reset this to 0 to start afresh.
145 virtual uint64_t GetCurrentBytesDownloaded(DownloadSource source) = 0;
146
147 // Returns the total number of bytes that have been downloaded for each
148 // source since the the last successful update. This is used to compute the
149 // overhead we incur.
150 virtual uint64_t GetTotalBytesDownloaded(DownloadSource source) = 0;
Chris Sosabe45bef2013-04-09 18:25:12 -0700151
152 // Returns the reboot count for this update attempt.
153 virtual uint32_t GetNumReboots() = 0;
David Zeuthene4c58bf2013-06-18 17:26:50 -0700154
155 // Called at update_engine startup to do various house-keeping.
156 virtual void UpdateEngineStarted() = 0;
Chris Sosaaa18e162013-06-20 13:20:30 -0700157
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200158 // Returns whether a rollback happened since the last update check with policy
159 // present.
160 virtual bool GetRollbackHappened() = 0;
161
162 // Sets whether rollback has happened on this device since the last update
163 // check where policy was available. This info is preserved over powerwash.
164 // This prevents forced updates happening on a rolled back device before
165 // device policy is available.
166 virtual void SetRollbackHappened(bool rollback_happened) = 0;
167
Chris Sosaaa18e162013-06-20 13:20:30 -0700168 // Returns the version from before a rollback if our last update was a
169 // rollback.
170 virtual std::string GetRollbackVersion() = 0;
David Zeuthendcba8092013-08-06 12:16:35 -0700171
172 // Returns the value of number of attempts we've attempted to
173 // download the payload via p2p.
174 virtual int GetP2PNumAttempts() = 0;
175
176 // Returns the value of timestamp of the first time we've attempted
177 // to download the payload via p2p.
178 virtual base::Time GetP2PFirstAttemptTimestamp() = 0;
179
180 // Should be called every time we decide to use p2p for an update
181 // attempt. This is used to increase the p2p attempt counter and
182 // set the timestamp for first attempt.
183 virtual void P2PNewAttempt() = 0;
184
185 // Returns |true| if we are allowed to continue using p2p for
186 // downloading and |false| otherwise. This is done by recording
187 // and examining how many attempts have been done already as well
188 // as when the first attempt was.
189 virtual bool P2PAttemptAllowed() = 0;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700190
Gilad Arnold74b5f552014-10-07 08:17:16 -0700191 // Gets the values previously set with SetUsingP2PForDownloading() and
192 // SetUsingP2PForSharing().
193 virtual bool GetUsingP2PForDownloading() const = 0;
194 virtual bool GetUsingP2PForSharing() const = 0;
Gilad Arnold519cfc72014-10-02 10:34:54 -0700195
196 // Returns the current (persisted) scattering wallclock-based wait period.
197 virtual base::TimeDelta GetScatteringWaitPeriod() = 0;
198
199 // Sets and persists the scattering wallclock-based wait period.
200 virtual void SetScatteringWaitPeriod(base::TimeDelta wait_period) = 0;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700201
202 // Sets/gets the P2P download URL, if one is to be used.
203 virtual void SetP2PUrl(const std::string& url) = 0;
204 virtual std::string GetP2PUrl() const = 0;
Sen Jiang6c736682017-03-10 15:01:36 -0800205
206 // Switch to next payload.
207 virtual bool NextPayload() = 0;
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700208
209 // Sets and persists the staging wallclock-based wait period.
210 virtual void SetStagingWaitPeriod(base::TimeDelta wait_period) = 0;
David Zeuthendcba8092013-08-06 12:16:35 -0700211};
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800212
213} // namespace chromeos_update_engine
214
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700215#endif // UPDATE_ENGINE_PAYLOAD_STATE_INTERFACE_H_