1 // 2 // Copyright (C) 2011 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_COMMON_DOWNLOAD_ACTION_H_ 18 #define UPDATE_ENGINE_COMMON_DOWNLOAD_ACTION_H_ 19 20 #include <fcntl.h> 21 #include <sys/stat.h> 22 #include <sys/types.h> 23 24 #include <memory> 25 #include <string> 26 #include <utility> 27 28 #include "update_engine/common/boot_control_interface.h" 29 #include "update_engine/common/http_fetcher.h" 30 #include "update_engine/common/multi_range_http_fetcher.h" 31 #include "update_engine/payload_consumer/delta_performer.h" 32 #include "update_engine/payload_consumer/install_plan.h" 33 34 // The Download Action downloads a specified url to disk. The url should point 35 // to an update in a delta payload format. The payload will be piped into a 36 // DeltaPerformer that will apply the delta to the disk. 37 38 namespace chromeos_update_engine { 39 40 class DownloadActionDelegate { 41 public: 42 virtual ~DownloadActionDelegate() = default; 43 44 // Called periodically after bytes are received. This method will be invoked 45 // only if the DownloadAction is running. |bytes_progressed| is the number of 46 // bytes downloaded since the last call of this method, |bytes_received| 47 // the number of bytes downloaded thus far and |total| is the number of bytes 48 // expected. 49 virtual void BytesReceived(uint64_t bytes_progressed, 50 uint64_t bytes_received, 51 uint64_t total) = 0; 52 53 // Returns whether the download should be canceled, in which case the 54 // |cancel_reason| error should be set to the reason why the download was 55 // canceled. 56 virtual bool ShouldCancel(ErrorCode* cancel_reason) = 0; 57 58 // Called once the complete payload has been downloaded. Note that any errors 59 // while applying or downloading the partial payload will result in this 60 // method not being called. 61 virtual void DownloadComplete() = 0; 62 }; 63 64 class PrefsInterface; 65 66 class DownloadAction : public InstallPlanAction, public HttpFetcherDelegate { 67 public: 68 // Debugging/logging StaticType()69 static std::string StaticType() { return "DownloadAction"; } 70 71 // Takes ownership of the passed in HttpFetcher. Useful for testing. 72 // A good calling pattern is: 73 // DownloadAction(prefs, boot_contol, hardware, 74 // new WhateverHttpFetcher, false); 75 DownloadAction( 76 PrefsInterface* prefs, 77 BootControlInterface* boot_control, 78 HardwareInterface* hardware, 79 HttpFetcher* http_fetcher, 80 bool interactive, 81 std::string update_certs_path = constants::kUpdateCertificatesPath); 82 ~DownloadAction() override; 83 84 // InstallPlanAction overrides. 85 void PerformAction() override; 86 void SuspendAction() override; 87 void ResumeAction() override; 88 void TerminateProcessing() override; Type()89 std::string Type() const override { return StaticType(); } 90 91 // Testing SetTestFileWriter(std::unique_ptr<DeltaPerformer> writer)92 void SetTestFileWriter(std::unique_ptr<DeltaPerformer> writer) { 93 delta_performer_ = std::move(writer); 94 } 95 GetHTTPResponseCode()96 int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); } 97 98 // HttpFetcherDelegate methods (see http_fetcher.h) 99 bool ReceivedBytes(HttpFetcher* fetcher, 100 const void* bytes, 101 size_t length) override; 102 void SeekToOffset(off_t offset) override; 103 void TransferComplete(HttpFetcher* fetcher, bool successful) override; 104 void TransferTerminated(HttpFetcher* fetcher) override; 105 delegate()106 DownloadActionDelegate* delegate() const { return delegate_; } set_delegate(DownloadActionDelegate * delegate)107 void set_delegate(DownloadActionDelegate* delegate) { delegate_ = delegate; } 108 set_base_offset(int64_t base_offset)109 void set_base_offset(int64_t base_offset) { base_offset_ = base_offset; } 110 http_fetcher()111 HttpFetcher* http_fetcher() { return http_fetcher_.get(); } 112 113 private: 114 // Attempt to load cached manifest data from prefs 115 // return true on success, false otherwise. 116 bool LoadCachedManifest(int64_t manifest_size); 117 118 // Start downloading the current payload using delta_performer. 119 void StartDownloading(); 120 121 // Pointer to the current payload in install_plan_.payloads. 122 InstallPlan::Payload* payload_{nullptr}; 123 124 // Required pointers. 125 PrefsInterface* prefs_; 126 BootControlInterface* boot_control_; 127 HardwareInterface* hardware_; 128 129 // Pointer to the MultiRangeHttpFetcher that does the http work. 130 std::unique_ptr<MultiRangeHttpFetcher> http_fetcher_; 131 132 // If |true|, the update is user initiated (vs. periodic update checks). Hence 133 // the |delta_performer_| can decide not to use O_DSYNC flag for faster 134 // update. 135 bool interactive_; 136 137 std::unique_ptr<DeltaPerformer> delta_performer_; 138 139 // Used by TransferTerminated to figure if this action terminated itself or 140 // was terminated by the action processor. 141 ErrorCode code_; 142 143 // For reporting status to outsiders 144 DownloadActionDelegate* delegate_; 145 uint64_t bytes_received_{0}; // per file/range 146 uint64_t bytes_received_previous_payloads_{0}; 147 uint64_t bytes_total_{0}; 148 bool download_active_{false}; 149 150 // Loaded from prefs before downloading any payload. 151 size_t resume_payload_index_{0}; 152 153 // Offset of the payload in the download URL, used by UpdateAttempterAndroid. 154 int64_t base_offset_{0}; 155 156 // The path to the zip file with X509 certificates. 157 const std::string update_certificates_path_; 158 159 DISALLOW_COPY_AND_ASSIGN(DownloadAction); 160 }; 161 162 // We want to be sure that we're compiled with large file support on linux, 163 // just in case we find ourselves downloading large images. 164 static_assert(8 == sizeof(off_t), "off_t not 64 bit"); 165 166 } // namespace chromeos_update_engine 167 168 #endif // UPDATE_ENGINE_COMMON_DOWNLOAD_ACTION_H_ 169