xref: /aosp_15_r20/system/update_engine/common/file_fetcher.h (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2016 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #ifndef UPDATE_ENGINE_COMMON_FILE_FETCHER_H_
18*5a923131SAndroid Build Coastguard Worker #define UPDATE_ENGINE_COMMON_FILE_FETCHER_H_
19*5a923131SAndroid Build Coastguard Worker 
20*5a923131SAndroid Build Coastguard Worker #include <memory>
21*5a923131SAndroid Build Coastguard Worker #include <string>
22*5a923131SAndroid Build Coastguard Worker #include <utility>
23*5a923131SAndroid Build Coastguard Worker 
24*5a923131SAndroid Build Coastguard Worker #include <base/logging.h>
25*5a923131SAndroid Build Coastguard Worker #include <android-base/macros.h>
26*5a923131SAndroid Build Coastguard Worker #include <brillo/streams/stream.h>
27*5a923131SAndroid Build Coastguard Worker 
28*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/http_fetcher.h"
29*5a923131SAndroid Build Coastguard Worker 
30*5a923131SAndroid Build Coastguard Worker // This is a concrete implementation of HttpFetcher that reads files
31*5a923131SAndroid Build Coastguard Worker // asynchronously.
32*5a923131SAndroid Build Coastguard Worker 
33*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
34*5a923131SAndroid Build Coastguard Worker 
35*5a923131SAndroid Build Coastguard Worker class FileFetcher : public HttpFetcher {
36*5a923131SAndroid Build Coastguard Worker  public:
37*5a923131SAndroid Build Coastguard Worker   // Returns whether the passed url is supported.
38*5a923131SAndroid Build Coastguard Worker   static bool SupportedUrl(const std::string& url);
39*5a923131SAndroid Build Coastguard Worker 
FileFetcher()40*5a923131SAndroid Build Coastguard Worker   FileFetcher() : HttpFetcher() {}
41*5a923131SAndroid Build Coastguard Worker 
42*5a923131SAndroid Build Coastguard Worker   // Cleans up all internal state. Does not notify delegate.
43*5a923131SAndroid Build Coastguard Worker   ~FileFetcher() override;
44*5a923131SAndroid Build Coastguard Worker 
45*5a923131SAndroid Build Coastguard Worker   // HttpFetcher overrides.
SetOffset(off_t offset)46*5a923131SAndroid Build Coastguard Worker   void SetOffset(off_t offset) override { offset_ = offset; }
SetLength(size_t length)47*5a923131SAndroid Build Coastguard Worker   void SetLength(size_t length) override { data_length_ = length; }
UnsetLength()48*5a923131SAndroid Build Coastguard Worker   void UnsetLength() override { SetLength(0); }
49*5a923131SAndroid Build Coastguard Worker 
50*5a923131SAndroid Build Coastguard Worker   // Begins the transfer if it hasn't already begun.
51*5a923131SAndroid Build Coastguard Worker   void BeginTransfer(const std::string& url) override;
52*5a923131SAndroid Build Coastguard Worker 
53*5a923131SAndroid Build Coastguard Worker   // If the transfer is in progress, aborts the transfer early. The transfer
54*5a923131SAndroid Build Coastguard Worker   // cannot be resumed.
55*5a923131SAndroid Build Coastguard Worker   void TerminateTransfer() override;
56*5a923131SAndroid Build Coastguard Worker 
57*5a923131SAndroid Build Coastguard Worker   // Ignore all extra headers for files.
SetHeader(const std::string & header_name,const std::string & header_value)58*5a923131SAndroid Build Coastguard Worker   void SetHeader(const std::string& header_name,
59*5a923131SAndroid Build Coastguard Worker                  const std::string& header_value) override {}
60*5a923131SAndroid Build Coastguard Worker 
GetHeader(const std::string & header_name,std::string * header_value)61*5a923131SAndroid Build Coastguard Worker   bool GetHeader(const std::string& header_name,
62*5a923131SAndroid Build Coastguard Worker                  std::string* header_value) const override {
63*5a923131SAndroid Build Coastguard Worker     header_value->clear();
64*5a923131SAndroid Build Coastguard Worker     return false;
65*5a923131SAndroid Build Coastguard Worker   }
66*5a923131SAndroid Build Coastguard Worker 
67*5a923131SAndroid Build Coastguard Worker   // Suspend the asynchronous file read.
68*5a923131SAndroid Build Coastguard Worker   void Pause() override;
69*5a923131SAndroid Build Coastguard Worker 
70*5a923131SAndroid Build Coastguard Worker   // Resume the suspended file read.
71*5a923131SAndroid Build Coastguard Worker   void Unpause() override;
72*5a923131SAndroid Build Coastguard Worker 
GetBytesDownloaded()73*5a923131SAndroid Build Coastguard Worker   size_t GetBytesDownloaded() override {
74*5a923131SAndroid Build Coastguard Worker     return static_cast<size_t>(bytes_copied_);
75*5a923131SAndroid Build Coastguard Worker   }
76*5a923131SAndroid Build Coastguard Worker 
77*5a923131SAndroid Build Coastguard Worker   // Ignore all the time limits for files.
set_low_speed_limit(int low_speed_bps,int low_speed_sec)78*5a923131SAndroid Build Coastguard Worker   void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {}
set_connect_timeout(int connect_timeout_seconds)79*5a923131SAndroid Build Coastguard Worker   void set_connect_timeout(int connect_timeout_seconds) override {}
set_max_retry_count(int max_retry_count)80*5a923131SAndroid Build Coastguard Worker   void set_max_retry_count(int max_retry_count) override {}
81*5a923131SAndroid Build Coastguard Worker 
82*5a923131SAndroid Build Coastguard Worker  private:
83*5a923131SAndroid Build Coastguard Worker   // Cleans up the fetcher, resetting its status to a newly constructed one.
84*5a923131SAndroid Build Coastguard Worker   void CleanUp();
85*5a923131SAndroid Build Coastguard Worker 
86*5a923131SAndroid Build Coastguard Worker   // Schedule a new asynchronous read if the stream is not paused and no other
87*5a923131SAndroid Build Coastguard Worker   // read is in process. This method can be called at any point.
88*5a923131SAndroid Build Coastguard Worker   void ScheduleRead();
89*5a923131SAndroid Build Coastguard Worker 
90*5a923131SAndroid Build Coastguard Worker   // Called from the main loop when a single read from |stream_| succeeds or
91*5a923131SAndroid Build Coastguard Worker   // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
92*5a923131SAndroid Build Coastguard Worker   void OnReadDoneCallback(size_t bytes_read);
93*5a923131SAndroid Build Coastguard Worker   void OnReadErrorCallback(const brillo::Error* error);
94*5a923131SAndroid Build Coastguard Worker 
95*5a923131SAndroid Build Coastguard Worker   // Whether the transfer was started and didn't finish yet.
96*5a923131SAndroid Build Coastguard Worker   bool transfer_in_progress_{false};
97*5a923131SAndroid Build Coastguard Worker 
98*5a923131SAndroid Build Coastguard Worker   // Whether the transfer is paused.
99*5a923131SAndroid Build Coastguard Worker   bool transfer_paused_{false};
100*5a923131SAndroid Build Coastguard Worker 
101*5a923131SAndroid Build Coastguard Worker   // Whether there's an ongoing asynchronous read. When this value is true, the
102*5a923131SAndroid Build Coastguard Worker   // the |buffer_| is being used by the |stream_|.
103*5a923131SAndroid Build Coastguard Worker   bool ongoing_read_{false};
104*5a923131SAndroid Build Coastguard Worker 
105*5a923131SAndroid Build Coastguard Worker   // Total number of bytes copied.
106*5a923131SAndroid Build Coastguard Worker   uint64_t bytes_copied_{0};
107*5a923131SAndroid Build Coastguard Worker 
108*5a923131SAndroid Build Coastguard Worker   // The offset inside the file where the read should start.
109*5a923131SAndroid Build Coastguard Worker   uint64_t offset_{0};
110*5a923131SAndroid Build Coastguard Worker 
111*5a923131SAndroid Build Coastguard Worker   // The length of the data or -1 if unknown (will read until EOF).
112*5a923131SAndroid Build Coastguard Worker   int64_t data_length_{-1};
113*5a923131SAndroid Build Coastguard Worker 
114*5a923131SAndroid Build Coastguard Worker   brillo::StreamPtr stream_;
115*5a923131SAndroid Build Coastguard Worker 
116*5a923131SAndroid Build Coastguard Worker   // The buffer used for reading from the stream.
117*5a923131SAndroid Build Coastguard Worker   brillo::Blob buffer_;
118*5a923131SAndroid Build Coastguard Worker 
119*5a923131SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(FileFetcher);
120*5a923131SAndroid Build Coastguard Worker };
121*5a923131SAndroid Build Coastguard Worker 
122*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
123*5a923131SAndroid Build Coastguard Worker 
124*5a923131SAndroid Build Coastguard Worker #endif  // UPDATE_ENGINE_COMMON_FILE_FETCHER_H_
125