123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- #ifndef UPDATE_ENGINE_PAYLOAD_STATE_H_
- #define UPDATE_ENGINE_PAYLOAD_STATE_H_
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <base/time/time.h>
- #include <gtest/gtest_prod.h> // for FRIEND_TEST
- #include "update_engine/common/prefs_interface.h"
- #include "update_engine/metrics_constants.h"
- #include "update_engine/payload_state_interface.h"
- namespace chromeos_update_engine {
- class SystemState;
- class PayloadState : public PayloadStateInterface {
- public:
- PayloadState();
- ~PayloadState() override {}
-
-
-
-
- bool Initialize(SystemState* system_state);
-
- void SetResponse(const OmahaResponse& response) override;
- void DownloadComplete() override;
- void DownloadProgress(size_t count) override;
- void UpdateResumed() override;
- void UpdateRestarted() override;
- void UpdateSucceeded() override;
- void UpdateFailed(ErrorCode error) override;
- void ResetUpdateStatus() override;
- bool ShouldBackoffDownload() override;
- void Rollback() override;
- void ExpectRebootInNewVersion(const std::string& target_version_uid) override;
- void SetUsingP2PForDownloading(bool value) override;
- void SetUsingP2PForSharing(bool value) override {
- using_p2p_for_sharing_ = value;
- }
- inline std::string GetResponseSignature() override {
- return response_signature_;
- }
- inline int GetFullPayloadAttemptNumber() override {
- return full_payload_attempt_number_;
- }
- inline int GetPayloadAttemptNumber() override {
- return payload_attempt_number_;
- }
- inline std::string GetCurrentUrl() override {
- return (payload_index_ < candidate_urls_.size() &&
- url_index_ < candidate_urls_[payload_index_].size())
- ? candidate_urls_[payload_index_][url_index_]
- : "";
- }
- inline uint32_t GetUrlFailureCount() override { return url_failure_count_; }
- inline uint32_t GetUrlSwitchCount() override { return url_switch_count_; }
- inline int GetNumResponsesSeen() override { return num_responses_seen_; }
- inline base::Time GetBackoffExpiryTime() override {
- return backoff_expiry_time_;
- }
- base::TimeDelta GetUpdateDuration() override;
- base::TimeDelta GetUpdateDurationUptime() override;
- inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override {
- return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
- }
- inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override {
- return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
- }
- inline uint32_t GetNumReboots() override { return num_reboots_; }
- void UpdateEngineStarted() override;
- inline bool GetRollbackHappened() override { return rollback_happened_; }
- void SetRollbackHappened(bool rollback_happened) override;
- inline std::string GetRollbackVersion() override { return rollback_version_; }
- int GetP2PNumAttempts() override;
- base::Time GetP2PFirstAttemptTimestamp() override;
- void P2PNewAttempt() override;
- bool P2PAttemptAllowed() override;
- bool GetUsingP2PForDownloading() const override {
- return using_p2p_for_downloading_;
- }
- bool GetUsingP2PForSharing() const override { return using_p2p_for_sharing_; }
- base::TimeDelta GetScatteringWaitPeriod() override {
- return scattering_wait_period_;
- }
- void SetScatteringWaitPeriod(base::TimeDelta wait_period) override;
- void SetStagingWaitPeriod(base::TimeDelta wait_period) override;
- void SetP2PUrl(const std::string& url) override { p2p_url_ = url; }
- std::string GetP2PUrl() const override { return p2p_url_; }
- bool NextPayload() override;
- private:
- enum class AttemptType {
- kUpdate,
- kRollback,
- };
- friend class PayloadStateTest;
- FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
- FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
- FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
- FRIEND_TEST(PayloadStateTest, RollbackHappened);
- FRIEND_TEST(PayloadStateTest, RollbackVersion);
- FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
-
-
- void AttemptStarted(AttemptType attempt_type);
-
- void IncrementPayloadAttemptNumber();
-
-
- void IncrementFullPayloadAttemptNumber();
-
-
-
-
-
- void IncrementUrlIndex();
-
-
-
- void IncrementFailureCount();
-
-
- void UpdateBackoffExpiryTime();
-
-
-
- void UpdateCurrentDownloadSource();
-
-
- void UpdateBytesDownloaded(size_t count);
-
- PayloadType CalculatePayloadType();
-
- void CollectAndReportAttemptMetrics(ErrorCode code);
-
-
- void PersistAttemptMetrics();
-
- void ClearPersistedAttemptMetrics();
-
-
-
- void ReportAndClearPersistedAttemptMetrics();
-
- void CollectAndReportSuccessfulUpdateMetrics();
-
-
-
-
-
- void ReportFailedBootIfNeeded();
-
-
- void ResetPersistedState();
-
-
- void ResetDownloadSourcesOnNewUpdate();
-
-
-
- std::string CalculateResponseSignature();
-
- void LoadResponseSignature();
-
-
-
- void SetResponseSignature(const std::string& response_signature);
-
- void LoadPayloadAttemptNumber();
-
-
- void LoadFullPayloadAttemptNumber();
-
-
-
- void SetPayloadAttemptNumber(int payload_attempt_number);
-
-
-
- void SetFullPayloadAttemptNumber(int payload_attempt_number);
-
-
-
- void SetPayloadIndex(size_t payload_index);
-
- void LoadUrlIndex();
-
-
-
- void SetUrlIndex(uint32_t url_index);
-
- void LoadUrlFailureCount();
-
-
-
- void SetUrlFailureCount(uint32_t url_failure_count);
-
- void SetUrlSwitchCount(uint32_t url_switch_count);
-
- void LoadUrlSwitchCount();
-
- void LoadBackoffExpiryTime();
-
-
-
- void SetBackoffExpiryTime(const base::Time& new_time);
-
- void LoadUpdateTimestampStart();
-
- void SetUpdateTimestampStart(const base::Time& value);
-
-
-
- void SetUpdateTimestampEnd(const base::Time& value);
-
- void LoadUpdateDurationUptime();
-
-
- void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
- const base::Time& timestamp,
- bool use_logging);
-
-
-
- void SetUpdateDurationUptime(const base::TimeDelta& value);
-
-
-
- void CalculateUpdateDurationUptime();
-
- std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
-
-
-
-
- void LoadCurrentBytesDownloaded(DownloadSource source);
-
-
- void SetCurrentBytesDownloaded(DownloadSource source,
- uint64_t current_bytes_downloaded,
- bool log);
-
-
-
-
- void LoadTotalBytesDownloaded(DownloadSource source);
-
-
- void SetTotalBytesDownloaded(DownloadSource source,
- uint64_t total_bytes_downloaded,
- bool log);
-
-
- void LoadRollbackHappened();
-
- void LoadRollbackVersion();
-
-
- void SetRollbackVersion(const std::string& rollback_version);
-
- void ResetRollbackVersion();
- inline uint32_t GetUrlIndex() {
- return (url_index_ != 0 && payload_index_ < candidate_urls_.size())
- ? std::min(candidate_urls_[payload_index_].size() - 1,
- url_index_)
- : 0;
- }
-
-
- void ComputeCandidateUrls();
-
- void SetNumResponsesSeen(int num_responses_seen);
-
- void LoadNumResponsesSeen();
-
- void LoadNumReboots();
-
-
-
- void SetNumReboots(uint32_t num_reboots);
-
-
- void UpdateNumReboots();
-
-
- void LoadP2PFirstAttemptTimestamp();
-
- void LoadP2PNumAttempts();
-
- void SetP2PNumAttempts(int value);
-
- void SetP2PFirstAttemptTimestamp(const base::Time& time);
-
- void LoadScatteringWaitPeriod();
-
- void LoadStagingWaitPeriod();
-
- int64_t GetPayloadSize();
-
- SystemState* system_state_;
-
-
- PrefsInterface* prefs_;
-
-
-
- PrefsInterface* powerwash_safe_prefs_;
-
- OmahaResponse response_;
-
- bool using_p2p_for_downloading_;
- bool using_p2p_for_sharing_;
-
- std::string p2p_url_;
-
- base::Time p2p_first_attempt_timestamp_;
-
- int p2p_num_attempts_;
-
-
-
-
- std::string response_signature_;
-
-
-
-
-
- int payload_attempt_number_;
-
-
-
-
-
- int full_payload_attempt_number_;
-
- size_t payload_index_ = 0;
-
-
-
-
-
- size_t url_index_;
-
-
-
- int64_t url_failure_count_;
-
- int32_t url_switch_count_;
-
-
-
-
- DownloadSource current_download_source_;
-
-
-
- int num_responses_seen_;
-
-
-
- uint32_t num_reboots_;
-
-
- base::Time backoff_expiry_time_;
-
- base::TimeDelta update_duration_current_;
-
- base::Time update_timestamp_start_;
-
-
- base::Time update_timestamp_end_;
-
- base::TimeDelta update_duration_uptime_;
-
- base::Time update_duration_uptime_timestamp_;
-
-
-
-
-
-
-
- uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
-
-
-
-
-
-
- uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
-
-
-
- static const base::TimeDelta kDurationSlack;
-
-
- std::vector<std::vector<std::string>> candidate_urls_;
-
-
-
- bool rollback_happened_;
-
-
-
-
- std::string rollback_version_;
-
- int64_t attempt_num_bytes_downloaded_;
-
- base::Time attempt_start_time_boot_;
-
- base::Time attempt_start_time_monotonic_;
-
- metrics::ConnectionType attempt_connection_type_;
-
- AttemptType attempt_type_;
-
- base::TimeDelta scattering_wait_period_;
-
- base::TimeDelta staging_wait_period_;
- DISALLOW_COPY_AND_ASSIGN(PayloadState);
- };
- }
- #endif
|