update_attempter_android.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifndef UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
  17. #define UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
  18. #include <stdint.h>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include <base/time/time.h>
  23. #include "update_engine/client_library/include/update_engine/update_status.h"
  24. #include "update_engine/common/action_processor.h"
  25. #include "update_engine/common/boot_control_interface.h"
  26. #include "update_engine/common/clock.h"
  27. #include "update_engine/common/hardware_interface.h"
  28. #include "update_engine/common/prefs_interface.h"
  29. #include "update_engine/daemon_state_interface.h"
  30. #include "update_engine/metrics_reporter_interface.h"
  31. #include "update_engine/metrics_utils.h"
  32. #include "update_engine/network_selector_interface.h"
  33. #include "update_engine/payload_consumer/download_action.h"
  34. #include "update_engine/payload_consumer/postinstall_runner_action.h"
  35. #include "update_engine/service_delegate_android_interface.h"
  36. #include "update_engine/service_observer_interface.h"
  37. namespace chromeos_update_engine {
  38. class UpdateAttempterAndroid
  39. : public ServiceDelegateAndroidInterface,
  40. public ActionProcessorDelegate,
  41. public DownloadActionDelegate,
  42. public PostinstallRunnerAction::DelegateInterface {
  43. public:
  44. using UpdateStatus = update_engine::UpdateStatus;
  45. UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
  46. PrefsInterface* prefs,
  47. BootControlInterface* boot_control_,
  48. HardwareInterface* hardware_);
  49. ~UpdateAttempterAndroid() override;
  50. // Further initialization to be done post construction.
  51. void Init();
  52. // ServiceDelegateAndroidInterface overrides.
  53. bool ApplyPayload(const std::string& payload_url,
  54. int64_t payload_offset,
  55. int64_t payload_size,
  56. const std::vector<std::string>& key_value_pair_headers,
  57. brillo::ErrorPtr* error) override;
  58. bool SuspendUpdate(brillo::ErrorPtr* error) override;
  59. bool ResumeUpdate(brillo::ErrorPtr* error) override;
  60. bool CancelUpdate(brillo::ErrorPtr* error) override;
  61. bool ResetStatus(brillo::ErrorPtr* error) override;
  62. bool VerifyPayloadApplicable(const std::string& metadata_filename,
  63. brillo::ErrorPtr* error) override;
  64. // ActionProcessorDelegate methods:
  65. void ProcessingDone(const ActionProcessor* processor,
  66. ErrorCode code) override;
  67. void ProcessingStopped(const ActionProcessor* processor) override;
  68. void ActionCompleted(ActionProcessor* processor,
  69. AbstractAction* action,
  70. ErrorCode code) override;
  71. // DownloadActionDelegate overrides.
  72. void BytesReceived(uint64_t bytes_progressed,
  73. uint64_t bytes_received,
  74. uint64_t total) override;
  75. bool ShouldCancel(ErrorCode* cancel_reason) override;
  76. void DownloadComplete() override;
  77. // PostinstallRunnerAction::DelegateInterface
  78. void ProgressUpdate(double progress) override;
  79. private:
  80. friend class UpdateAttempterAndroidTest;
  81. // Schedules an event loop callback to start the action processor. This is
  82. // scheduled asynchronously to unblock the event loop.
  83. void ScheduleProcessingStart();
  84. // Notifies an update request completed with the given error |code| to all
  85. // observers.
  86. void TerminateUpdateAndNotify(ErrorCode error_code);
  87. // Sets the status to the given |status| and notifies a status update to
  88. // all observers.
  89. void SetStatusAndNotify(UpdateStatus status);
  90. // Helper method to construct the sequence of actions to be performed for
  91. // applying an update using a given HttpFetcher. The ownership of |fetcher| is
  92. // passed to this function.
  93. void BuildUpdateActions(HttpFetcher* fetcher);
  94. // Writes to the processing completed marker. Does nothing if
  95. // |update_completed_marker_| is empty.
  96. bool WriteUpdateCompletedMarker();
  97. // Returns whether an update was completed in the current boot.
  98. bool UpdateCompletedOnThisBoot();
  99. // Prefs to use for metrics report
  100. // |kPrefsPayloadAttemptNumber|: number of update attempts for the current
  101. // payload_id.
  102. // |KprefsNumReboots|: number of reboots when applying the current update.
  103. // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update.
  104. // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the
  105. // current update.
  106. // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of
  107. // the current update.
  108. // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current
  109. // payload_id.
  110. // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since
  111. // the last successful update.
  112. // Metrics report function to call:
  113. // |ReportUpdateAttemptMetrics|
  114. // |ReportSuccessfulUpdateMetrics|
  115. // Prefs to update:
  116. // |kPrefsSystemUpdatedMarker|
  117. void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
  118. // Metrics report function to call:
  119. // |ReportAbnormallyTerminatedUpdateAttemptMetrics|
  120. // |ReportTimeToRebootMetrics|
  121. // Prefs to update:
  122. // |kPrefsBootId|, |kPrefsPreviousVersion|
  123. void UpdatePrefsAndReportUpdateMetricsOnReboot();
  124. // Prefs to update:
  125. // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|,
  126. // |kPrefsUpdateBootTimestampStart|
  127. void UpdatePrefsOnUpdateStart(bool is_resume);
  128. // Prefs to delete:
  129. // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded|
  130. // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
  131. // |kPrefsUpdateBootTimestampStart|
  132. void ClearMetricsPrefs();
  133. DaemonStateInterface* daemon_state_;
  134. // DaemonStateAndroid pointers.
  135. PrefsInterface* prefs_;
  136. BootControlInterface* boot_control_;
  137. HardwareInterface* hardware_;
  138. // Last status notification timestamp used for throttling. Use monotonic
  139. // TimeTicks to ensure that notifications are sent even if the system clock is
  140. // set back in the middle of an update.
  141. base::TimeTicks last_notify_time_;
  142. // Only direct proxy supported.
  143. DirectProxyResolver proxy_resolver_;
  144. // The processor for running Actions.
  145. std::unique_ptr<ActionProcessor> processor_;
  146. // The InstallPlan used during the ongoing update.
  147. InstallPlan install_plan_;
  148. // For status:
  149. UpdateStatus status_{UpdateStatus::IDLE};
  150. double download_progress_{0.0};
  151. // The offset in the payload file where the CrAU part starts.
  152. int64_t base_offset_{0};
  153. // Helper class to select the network to use during the update.
  154. std::unique_ptr<NetworkSelectorInterface> network_selector_;
  155. std::unique_ptr<ClockInterface> clock_;
  156. std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
  157. DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
  158. };
  159. } // namespace chromeos_update_engine
  160. #endif // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_