payload_state_interface.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  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. //
  16. #ifndef UPDATE_ENGINE_PAYLOAD_STATE_INTERFACE_H_
  17. #define UPDATE_ENGINE_PAYLOAD_STATE_INTERFACE_H_
  18. #include <string>
  19. #include "update_engine/common/action_processor.h"
  20. #include "update_engine/common/constants.h"
  21. #include "update_engine/omaha_response.h"
  22. namespace chromeos_update_engine {
  23. // Describes the methods that need to be implemented by the PayloadState class.
  24. // This interface has been carved out to support mocking of the PayloadState
  25. // object.
  26. class PayloadStateInterface {
  27. public:
  28. virtual ~PayloadStateInterface() = default;
  29. // Sets the internal payload state based on the given Omaha response. This
  30. // response could be the same or different from the one for which we've stored
  31. // the internal state. If it's different, then this method resets all the
  32. // internal state corresponding to the old response. Since the Omaha response
  33. // has a lot of fields that are not related to payload state, it uses only
  34. // a subset of the fields in the Omaha response to compare equality.
  35. virtual void SetResponse(const OmahaResponse& response) = 0;
  36. // This method should be called whenever we have completed downloading all
  37. // the bytes of a payload and have verified that its size and hash match the
  38. // expected values. We use this notificaiton to increment the payload attempt
  39. // number so that the throttle the next attempt to download the same payload
  40. // (in case there's an error in subsequent steps such as post-install)
  41. // appropriately.
  42. virtual void DownloadComplete() = 0;
  43. // This method should be called whenever we receive new bytes from the
  44. // network for the current payload. We use this notification to reset the
  45. // failure count for a given URL since receipt of some bytes means we are
  46. // able to make forward progress with the current URL.
  47. virtual void DownloadProgress(size_t count) = 0;
  48. // This method should be called every time we resume an update attempt.
  49. virtual void UpdateResumed() = 0;
  50. // This method should be called every time we begin a new update. This method
  51. // should not be called when we resume an update from the previously
  52. // downloaded point. This is used to reset the metrics for each new update.
  53. virtual void UpdateRestarted() = 0;
  54. // This method should be called once after an update attempt succeeds. This
  55. // is when the relevant UMA metrics that are tracked on a per-update-basis
  56. // are uploaded to the UMA server.
  57. virtual void UpdateSucceeded() = 0;
  58. // This method should be called whenever an update attempt fails with the
  59. // given error code. We use this notification to update the payload state
  60. // depending on the type of the error that happened.
  61. virtual void UpdateFailed(ErrorCode error) = 0;
  62. // This method should be called whenever a succeeded update is canceled, and
  63. // thus can only be called after UpdateSucceeded(). This is currently used
  64. // only for manual testing using the update_engine_client.
  65. virtual void ResetUpdateStatus() = 0;
  66. // This method should be called every time we initiate a Rollback.
  67. virtual void Rollback() = 0;
  68. // Sets the expectations to boot into the new version in the next reboot.
  69. // This function is called every time a new update is marked as ready by
  70. // UpdateSuccess(). |target_version_uid| is an unique identifier of the
  71. // applied payload. It can be any string, as long as the same string is used
  72. // for the same payload.
  73. virtual void ExpectRebootInNewVersion(
  74. const std::string& target_version_uid) = 0;
  75. // Sets whether P2P is being used to download the update payload. This
  76. // is used to keep track of download sources being used and should be called
  77. // before the transfer begins.
  78. virtual void SetUsingP2PForDownloading(bool value) = 0;
  79. // Sets whether P2P is being used for sharing the update payloads.
  80. virtual void SetUsingP2PForSharing(bool value) = 0;
  81. // Returns true if we should backoff the current download attempt.
  82. // False otherwise.
  83. virtual bool ShouldBackoffDownload() = 0;
  84. // Returns the currently stored response "signature". The signature is a
  85. // subset of fields that are of interest to the PayloadState behavior.
  86. virtual std::string GetResponseSignature() = 0;
  87. // Returns the payload attempt number.
  88. virtual int GetPayloadAttemptNumber() = 0;
  89. // Returns the payload attempt number of the attempted full payload. Returns
  90. // 0 for delta payloads.
  91. virtual int GetFullPayloadAttemptNumber() = 0;
  92. // Returns the current URL. Returns an empty string if there's no valid URL.
  93. virtual std::string GetCurrentUrl() = 0;
  94. // Returns the current URL's failure count.
  95. virtual uint32_t GetUrlFailureCount() = 0;
  96. // Returns the total number of times a new URL has been switched to
  97. // for the current response.
  98. virtual uint32_t GetUrlSwitchCount() = 0;
  99. // Returns the total number of different responses seen since the
  100. // last successful update.
  101. virtual int GetNumResponsesSeen() = 0;
  102. // Returns the expiry time for the current backoff period.
  103. virtual base::Time GetBackoffExpiryTime() = 0;
  104. // Returns the elapsed time used for this update, including time
  105. // where the device is powered off and sleeping. If the
  106. // update has not completed, returns the time spent so far.
  107. virtual base::TimeDelta GetUpdateDuration() = 0;
  108. // Returns the time used for this update not including time when
  109. // the device is powered off or sleeping. If the update has not
  110. // completed, returns the time spent so far.
  111. virtual base::TimeDelta GetUpdateDurationUptime() = 0;
  112. // Returns the number of bytes that have been downloaded for each source for
  113. // each new update attempt. If we resume an update, we'll continue from the
  114. // previous value, but if we get a new response or if the previous attempt
  115. // failed, we'll reset this to 0 to start afresh.
  116. virtual uint64_t GetCurrentBytesDownloaded(DownloadSource source) = 0;
  117. // Returns the total number of bytes that have been downloaded for each
  118. // source since the the last successful update. This is used to compute the
  119. // overhead we incur.
  120. virtual uint64_t GetTotalBytesDownloaded(DownloadSource source) = 0;
  121. // Returns the reboot count for this update attempt.
  122. virtual uint32_t GetNumReboots() = 0;
  123. // Called at update_engine startup to do various house-keeping.
  124. virtual void UpdateEngineStarted() = 0;
  125. // Returns whether a rollback happened since the last update check with policy
  126. // present.
  127. virtual bool GetRollbackHappened() = 0;
  128. // Sets whether rollback has happened on this device since the last update
  129. // check where policy was available. This info is preserved over powerwash.
  130. // This prevents forced updates happening on a rolled back device before
  131. // device policy is available.
  132. virtual void SetRollbackHappened(bool rollback_happened) = 0;
  133. // Returns the version from before a rollback if our last update was a
  134. // rollback.
  135. virtual std::string GetRollbackVersion() = 0;
  136. // Returns the value of number of attempts we've attempted to
  137. // download the payload via p2p.
  138. virtual int GetP2PNumAttempts() = 0;
  139. // Returns the value of timestamp of the first time we've attempted
  140. // to download the payload via p2p.
  141. virtual base::Time GetP2PFirstAttemptTimestamp() = 0;
  142. // Should be called every time we decide to use p2p for an update
  143. // attempt. This is used to increase the p2p attempt counter and
  144. // set the timestamp for first attempt.
  145. virtual void P2PNewAttempt() = 0;
  146. // Returns |true| if we are allowed to continue using p2p for
  147. // downloading and |false| otherwise. This is done by recording
  148. // and examining how many attempts have been done already as well
  149. // as when the first attempt was.
  150. virtual bool P2PAttemptAllowed() = 0;
  151. // Gets the values previously set with SetUsingP2PForDownloading() and
  152. // SetUsingP2PForSharing().
  153. virtual bool GetUsingP2PForDownloading() const = 0;
  154. virtual bool GetUsingP2PForSharing() const = 0;
  155. // Returns the current (persisted) scattering wallclock-based wait period.
  156. virtual base::TimeDelta GetScatteringWaitPeriod() = 0;
  157. // Sets and persists the scattering wallclock-based wait period.
  158. virtual void SetScatteringWaitPeriod(base::TimeDelta wait_period) = 0;
  159. // Sets/gets the P2P download URL, if one is to be used.
  160. virtual void SetP2PUrl(const std::string& url) = 0;
  161. virtual std::string GetP2PUrl() const = 0;
  162. // Switch to next payload.
  163. virtual bool NextPayload() = 0;
  164. // Sets and persists the staging wallclock-based wait period.
  165. virtual void SetStagingWaitPeriod(base::TimeDelta wait_period) = 0;
  166. };
  167. } // namespace chromeos_update_engine
  168. #endif // UPDATE_ENGINE_PAYLOAD_STATE_INTERFACE_H_