| Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 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 |  | 
|  | 17 | #ifndef UPDATE_ENGINE_COMMON_FILE_FETCHER_H_ | 
|  | 18 | #define UPDATE_ENGINE_COMMON_FILE_FETCHER_H_ | 
|  | 19 |  | 
|  | 20 | #include <memory> | 
|  | 21 | #include <string> | 
|  | 22 | #include <utility> | 
|  | 23 |  | 
|  | 24 | #include <base/logging.h> | 
|  | 25 | #include <base/macros.h> | 
| Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 26 | #include <brillo/streams/stream.h> | 
|  | 27 |  | 
|  | 28 | #include "update_engine/common/http_fetcher.h" | 
|  | 29 |  | 
|  | 30 | // This is a concrete implementation of HttpFetcher that reads files | 
|  | 31 | // asynchronously. | 
|  | 32 |  | 
|  | 33 | namespace chromeos_update_engine { | 
|  | 34 |  | 
|  | 35 | class FileFetcher : public HttpFetcher { | 
|  | 36 | public: | 
|  | 37 | // Returns whether the passed url is supported. | 
|  | 38 | static bool SupportedUrl(const std::string& url); | 
|  | 39 |  | 
| Kelvin Zhang | c7a1d1f | 2022-07-29 13:36:29 -0700 | [diff] [blame] | 40 | FileFetcher() : HttpFetcher() {} | 
| Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | // Cleans up all internal state. Does not notify delegate. | 
|  | 43 | ~FileFetcher() override; | 
|  | 44 |  | 
|  | 45 | // HttpFetcher overrides. | 
|  | 46 | void SetOffset(off_t offset) override { offset_ = offset; } | 
|  | 47 | void SetLength(size_t length) override { data_length_ = length; } | 
|  | 48 | void UnsetLength() override { SetLength(0); } | 
|  | 49 |  | 
|  | 50 | // Begins the transfer if it hasn't already begun. | 
|  | 51 | void BeginTransfer(const std::string& url) override; | 
|  | 52 |  | 
|  | 53 | // If the transfer is in progress, aborts the transfer early. The transfer | 
|  | 54 | // cannot be resumed. | 
|  | 55 | void TerminateTransfer() override; | 
|  | 56 |  | 
|  | 57 | // Ignore all extra headers for files. | 
|  | 58 | void SetHeader(const std::string& header_name, | 
| Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 59 | const std::string& header_value) override {} | 
| Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 60 |  | 
| Jae Hoon Kim | 0ae8fe1 | 2019-06-26 14:32:50 -0700 | [diff] [blame] | 61 | bool GetHeader(const std::string& header_name, | 
|  | 62 | std::string* header_value) const override { | 
|  | 63 | header_value->clear(); | 
|  | 64 | return false; | 
|  | 65 | } | 
|  | 66 |  | 
| Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 67 | // Suspend the asynchronous file read. | 
|  | 68 | void Pause() override; | 
|  | 69 |  | 
|  | 70 | // Resume the suspended file read. | 
|  | 71 | void Unpause() override; | 
|  | 72 |  | 
|  | 73 | size_t GetBytesDownloaded() override { | 
|  | 74 | return static_cast<size_t>(bytes_copied_); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | // Ignore all the time limits for files. | 
|  | 78 | void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {} | 
|  | 79 | void set_connect_timeout(int connect_timeout_seconds) override {} | 
|  | 80 | void set_max_retry_count(int max_retry_count) override {} | 
|  | 81 |  | 
|  | 82 | private: | 
|  | 83 | // Cleans up the fetcher, resetting its status to a newly constructed one. | 
|  | 84 | void CleanUp(); | 
|  | 85 |  | 
|  | 86 | // Schedule a new asynchronous read if the stream is not paused and no other | 
|  | 87 | // read is in process. This method can be called at any point. | 
|  | 88 | void ScheduleRead(); | 
|  | 89 |  | 
|  | 90 | // Called from the main loop when a single read from |stream_| succeeds or | 
|  | 91 | // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively. | 
|  | 92 | void OnReadDoneCallback(size_t bytes_read); | 
|  | 93 | void OnReadErrorCallback(const brillo::Error* error); | 
|  | 94 |  | 
|  | 95 | // Whether the transfer was started and didn't finish yet. | 
|  | 96 | bool transfer_in_progress_{false}; | 
|  | 97 |  | 
|  | 98 | // Whether the transfer is paused. | 
|  | 99 | bool transfer_paused_{false}; | 
|  | 100 |  | 
|  | 101 | // Whether there's an ongoing asynchronous read. When this value is true, the | 
|  | 102 | // the |buffer_| is being used by the |stream_|. | 
|  | 103 | bool ongoing_read_{false}; | 
|  | 104 |  | 
|  | 105 | // Total number of bytes copied. | 
|  | 106 | uint64_t bytes_copied_{0}; | 
|  | 107 |  | 
|  | 108 | // The offset inside the file where the read should start. | 
|  | 109 | uint64_t offset_{0}; | 
|  | 110 |  | 
|  | 111 | // The length of the data or -1 if unknown (will read until EOF). | 
|  | 112 | int64_t data_length_{-1}; | 
|  | 113 |  | 
|  | 114 | brillo::StreamPtr stream_; | 
|  | 115 |  | 
|  | 116 | // The buffer used for reading from the stream. | 
|  | 117 | brillo::Blob buffer_; | 
|  | 118 |  | 
|  | 119 | DISALLOW_COPY_AND_ASSIGN(FileFetcher); | 
|  | 120 | }; | 
|  | 121 |  | 
|  | 122 | }  // namespace chromeos_update_engine | 
|  | 123 |  | 
|  | 124 | #endif  // UPDATE_ENGINE_COMMON_FILE_FETCHER_H_ |