OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_RANGE_HTTP_FETCHER_H__ | |
6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_RANGE_HTTP_FETCHER_H__ | |
7 | |
8 #include <deque> | |
9 #include <utility> | |
10 #include <vector> | |
11 | |
12 #include <base/scoped_ptr.h> | |
13 | |
14 #include "update_engine/http_fetcher.h" | |
15 | |
16 // This class is a simple wrapper around an HttpFetcher. The client | |
petkov
2010/12/17 13:37:25
maybe we should document here that the same base h
adlr
2010/12/17 19:34:15
Done.
| |
17 // specifies a vector of byte ranges. MultiRangeHTTPFetcher will fetch bytes | |
18 // from those offsets. Pass -1 as a length to specify unlimited length. | |
19 // It really only would make sense for the last range specified to have | |
20 // unlimited length, tho it is legal for other entries to have unlimited | |
21 // length. | |
22 | |
23 // There are three states a MultiRangeHTTPFetcher object will be in: | |
24 // - Stopped (start state) | |
25 // - Downloading | |
26 // - Pending transfer ended | |
27 // Various functions below that might change state indicate possible | |
28 // state changes. | |
29 | |
30 namespace chromeos_update_engine { | |
31 | |
32 class MultiRangeHTTPFetcher : public HttpFetcher, public HttpFetcherDelegate { | |
33 public: | |
34 // Takes ownership of the passed in fetcher. | |
35 explicit MultiRangeHTTPFetcher(HttpFetcher* base_fetcher) | |
36 : HttpFetcher(base_fetcher->proxy_resolver()), | |
37 base_fetcher_(base_fetcher), | |
38 base_fetcher_active_(false), | |
39 pending_transfer_ended_(false), | |
40 terminating_(false), | |
41 current_index_(0), | |
42 bytes_received_this_range_(0) {} | |
43 ~MultiRangeHTTPFetcher() {} | |
44 | |
45 void ClearRanges() { ranges_.clear(); } | |
46 | |
47 void AddRange(off_t offset, off_t size) { | |
48 ranges_.push_back(std::make_pair(offset, size)); | |
49 } | |
50 | |
51 virtual void SetOffset(off_t offset) {} // for now, doesn't support this | |
52 | |
53 // Begins the transfer to the specified URL. | |
54 // State change: Stopped -> Downloading | |
55 // (corner case: Stopped -> Stopped for an empty request) | |
56 virtual void BeginTransfer(const std::string& url); | |
57 | |
58 // State change: Downloading -> Pending transfer ended | |
59 virtual void TerminateTransfer(); | |
60 | |
61 virtual void Pause() { base_fetcher_->Pause(); } | |
62 | |
63 virtual void Unpause() { base_fetcher_->Unpause(); } | |
64 | |
65 // These functions are overloaded in LibcurlHttp fetcher for testing purposes. | |
66 virtual void set_idle_seconds(int seconds) { | |
67 base_fetcher_->set_idle_seconds(seconds); | |
68 } | |
69 virtual void set_retry_seconds(int seconds) { | |
70 base_fetcher_->set_retry_seconds(seconds); | |
71 } | |
72 virtual void SetConnectionAsExpensive(bool is_expensive) { | |
73 base_fetcher_->SetConnectionAsExpensive(is_expensive); | |
74 } | |
75 virtual void SetBuildType(bool is_official) { | |
76 base_fetcher_->SetBuildType(is_official); | |
77 } | |
78 virtual void SetProxies(const std::deque<std::string>& proxies) { | |
79 base_fetcher_->SetProxies(proxies); | |
80 } | |
81 | |
82 private: | |
83 // State change: Stopped or Downloading -> Downloading | |
84 void StartTransfer(); | |
85 | |
86 // State change: Downloading -> Downloading | |
87 virtual void ReceivedBytes(HttpFetcher* fetcher, | |
88 const char* bytes, | |
89 int length); | |
90 | |
91 // State change: Pending transfer ended -> Stopped | |
92 void TransferEnded(HttpFetcher* fetcher, bool successful); | |
93 // These two call TransferEnded(): | |
94 virtual void TransferComplete(HttpFetcher* fetcher, bool successful); | |
95 virtual void TransferTerminated(HttpFetcher* fetcher); | |
96 | |
97 void Reset(); | |
98 | |
99 scoped_ptr<HttpFetcher> base_fetcher_; | |
100 | |
101 // If true, do not send any more data or TransferComplete to the delegate. | |
102 bool base_fetcher_active_; | |
103 | |
104 // If true, the next fetcher needs to be started when TransferTerminated is | |
105 // received from the current fetcher. | |
106 bool pending_transfer_ended_; | |
107 | |
108 // True if we are waiting for base fetcher to terminate b/c we are | |
109 // ourselves terminating. | |
110 bool terminating_; | |
111 | |
112 // pair<offset, length>: | |
113 typedef std::vector<std::pair<off_t, off_t> > RangesVect; | |
petkov
2010/12/17 13:37:25
per style, the typedef should be right after priva
adlr
2010/12/17 19:34:15
Done.
| |
114 | |
115 RangesVect ranges_; | |
116 | |
117 RangesVect::size_type current_index_; // index into ranges_ | |
118 off_t bytes_received_this_range_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(MultiRangeHTTPFetcher); | |
121 }; | |
122 | |
123 } // namespace chromeos_update_engine | |
124 | |
125 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_RANGE_HTTP_FETCHER_H__ | |
OLD | NEW |