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