• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetHeader(const std::string & header_name,std::string * header_value)62   bool GetHeader(const std::string& header_name,
63                  std::string* header_value) const override {
64     header_value->clear();
65     return false;
66   }
67 
68   // Suspend the asynchronous file read.
69   void Pause() override;
70 
71   // Resume the suspended file read.
72   void Unpause() override;
73 
GetBytesDownloaded()74   size_t GetBytesDownloaded() override {
75     return static_cast<size_t>(bytes_copied_);
76   }
77 
78   // Ignore all the time limits for files.
set_low_speed_limit(int low_speed_bps,int low_speed_sec)79   void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {}
set_connect_timeout(int connect_timeout_seconds)80   void set_connect_timeout(int connect_timeout_seconds) override {}
set_max_retry_count(int max_retry_count)81   void set_max_retry_count(int max_retry_count) override {}
82 
83  private:
84   // Cleans up the fetcher, resetting its status to a newly constructed one.
85   void CleanUp();
86 
87   // Schedule a new asynchronous read if the stream is not paused and no other
88   // read is in process. This method can be called at any point.
89   void ScheduleRead();
90 
91   // Called from the main loop when a single read from |stream_| succeeds or
92   // fails, calling OnReadDoneCallback() and OnReadErrorCallback() respectively.
93   void OnReadDoneCallback(size_t bytes_read);
94   void OnReadErrorCallback(const brillo::Error* error);
95 
96   // Whether the transfer was started and didn't finish yet.
97   bool transfer_in_progress_{false};
98 
99   // Whether the transfer is paused.
100   bool transfer_paused_{false};
101 
102   // Whether there's an ongoing asynchronous read. When this value is true, the
103   // the |buffer_| is being used by the |stream_|.
104   bool ongoing_read_{false};
105 
106   // Total number of bytes copied.
107   uint64_t bytes_copied_{0};
108 
109   // The offset inside the file where the read should start.
110   uint64_t offset_{0};
111 
112   // The length of the data or -1 if unknown (will read until EOF).
113   int64_t data_length_{-1};
114 
115   brillo::StreamPtr stream_;
116 
117   // The buffer used for reading from the stream.
118   brillo::Blob buffer_;
119 
120   DISALLOW_COPY_AND_ASSIGN(FileFetcher);
121 };
122 
123 }  // namespace chromeos_update_engine
124 
125 #endif  // UPDATE_ENGINE_COMMON_FILE_FETCHER_H_
126