OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ |
6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ |
7 | 7 |
| 8 #include <deque> |
8 #include <tr1/memory> | 9 #include <tr1/memory> |
9 #include <utility> | 10 #include <utility> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "update_engine/http_fetcher.h" | 13 #include "update_engine/http_fetcher.h" |
13 #include "update_engine/utils.h" | 14 #include "update_engine/utils.h" |
14 | 15 |
15 // This class is a simple wrapper around an HttpFetcher. The client | 16 // This class is a simple wrapper around an HttpFetcher. The client |
16 // specifies a vector of byte ranges. MultiHttpFetcher will fetch bytes | 17 // specifies a vector of byte ranges. MultiHttpFetcher will fetch bytes |
17 // from those offsets. Pass -1 as a length to specify unlimited length. | 18 // from those offsets. Pass -1 as a length to specify unlimited length. |
18 // It really only would make sense for the last range specified to have | 19 // It really only would make sense for the last range specified to have |
19 // unlimited length. | 20 // unlimited length. |
20 | 21 |
21 namespace chromeos_update_engine { | 22 namespace chromeos_update_engine { |
22 | 23 |
23 template<typename BaseHttpFetcher> | 24 template<typename BaseHttpFetcher> |
24 class MultiHttpFetcher : public HttpFetcher, public HttpFetcherDelegate { | 25 class MultiHttpFetcher : public HttpFetcher, public HttpFetcherDelegate { |
25 public: | 26 public: |
26 typedef std::vector<std::pair<off_t, off_t> > RangesVect; | 27 typedef std::vector<std::pair<off_t, off_t> > RangesVect; |
| 28 typedef std::vector<std::tr1::shared_ptr<BaseHttpFetcher> > FetchersVect; |
27 | 29 |
28 MultiHttpFetcher() | 30 MultiHttpFetcher(ProxyResolver* proxy_resolver) |
29 : sent_transfer_complete_(false), | 31 : HttpFetcher(proxy_resolver), |
| 32 sent_transfer_complete_(false), |
30 pending_next_fetcher_(false), | 33 pending_next_fetcher_(false), |
31 current_index_(0), | 34 current_index_(0), |
32 bytes_received_this_fetcher_(0) {} | 35 bytes_received_this_fetcher_(0) {} |
33 ~MultiHttpFetcher() {} | 36 ~MultiHttpFetcher() {} |
34 | 37 |
35 void set_ranges(const RangesVect& ranges) { | 38 void set_ranges(const RangesVect& ranges) { |
36 ranges_ = ranges; | 39 ranges_ = ranges; |
37 fetchers_.resize(ranges_.size()); // Allocate the fetchers | 40 fetchers_.resize(ranges_.size()); // Allocate the fetchers |
38 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> | 41 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> |
39 >::iterator it = fetchers_.begin(), e = fetchers_.end(); | 42 >::iterator it = fetchers_.begin(), e = fetchers_.end(); |
40 it != e; ++it) { | 43 it != e; ++it) { |
41 (*it) = std::tr1::shared_ptr<BaseHttpFetcher>(new BaseHttpFetcher); | 44 (*it) = std::tr1::shared_ptr<BaseHttpFetcher>( |
| 45 new BaseHttpFetcher(proxy_resolver_)); |
42 (*it)->set_delegate(this); | 46 (*it)->set_delegate(this); |
43 } | 47 } |
| 48 LOG(INFO) << "done w/ list"; |
44 } | 49 } |
45 | 50 |
46 void SetOffset(off_t offset) {} // for now, doesn't support this | 51 void SetOffset(off_t offset) {} // for now, doesn't support this |
47 | 52 |
48 // Begins the transfer to the specified URL. | 53 // Begins the transfer to the specified URL. |
49 void BeginTransfer(const std::string& url) { | 54 void BeginTransfer(const std::string& url) { |
50 url_ = url; | 55 url_ = url; |
51 if (ranges_.empty()) { | 56 if (ranges_.empty()) { |
52 if (delegate_) | 57 if (delegate_) |
53 delegate_->TransferComplete(this, true); | 58 delegate_->TransferComplete(this, true); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 (*it)->SetConnectionAsExpensive(is_expensive); | 132 (*it)->SetConnectionAsExpensive(is_expensive); |
128 } | 133 } |
129 } | 134 } |
130 void SetBuildType(bool is_official) { | 135 void SetBuildType(bool is_official) { |
131 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> >::iterator | 136 for (typename std::vector<std::tr1::shared_ptr<BaseHttpFetcher> >::iterator |
132 it = fetchers_.begin(), | 137 it = fetchers_.begin(), |
133 e = fetchers_.end(); it != e; ++it) { | 138 e = fetchers_.end(); it != e; ++it) { |
134 (*it)->SetBuildType(is_official); | 139 (*it)->SetBuildType(is_official); |
135 } | 140 } |
136 } | 141 } |
| 142 |
| 143 virtual void SetProxies(const std::deque<std::string>& proxies) { |
| 144 for (typename FetchersVect::iterator it = fetchers_.begin(), |
| 145 e = fetchers_.end(); it != e; ++it) { |
| 146 (*it)->SetProxies(proxies); |
| 147 } |
| 148 } |
137 | 149 |
138 private: | 150 private: |
139 void SendTransferComplete(HttpFetcher* fetcher, bool successful) { | 151 void SendTransferComplete(HttpFetcher* fetcher, bool successful) { |
140 if (sent_transfer_complete_) | 152 if (sent_transfer_complete_) |
141 return; | 153 return; |
142 LOG(INFO) << "Sending transfer complete"; | 154 LOG(INFO) << "Sending transfer complete"; |
143 sent_transfer_complete_ = true; | 155 sent_transfer_complete_ = true; |
144 http_response_code_ = fetcher->http_response_code(); | 156 http_response_code_ = fetcher->http_response_code(); |
145 if (delegate_) | 157 if (delegate_) |
146 delegate_->TransferComplete(this, successful); | 158 delegate_->TransferComplete(this, successful); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 } | 225 } |
214 | 226 |
215 // If true, do not send any more data or TransferComplete to the delegate. | 227 // If true, do not send any more data or TransferComplete to the delegate. |
216 bool sent_transfer_complete_; | 228 bool sent_transfer_complete_; |
217 | 229 |
218 // If true, the next fetcher needs to be started when TransferTerminated is | 230 // If true, the next fetcher needs to be started when TransferTerminated is |
219 // received from the current fetcher. | 231 // received from the current fetcher. |
220 bool pending_next_fetcher_; | 232 bool pending_next_fetcher_; |
221 | 233 |
222 RangesVect ranges_; | 234 RangesVect ranges_; |
223 std::vector<std::tr1::shared_ptr<BaseHttpFetcher> > fetchers_; | 235 FetchersVect fetchers_; |
224 | 236 |
225 RangesVect::size_type current_index_; // index into ranges_, fetchers_ | 237 RangesVect::size_type current_index_; // index into ranges_, fetchers_ |
226 off_t bytes_received_this_fetcher_; | 238 off_t bytes_received_this_fetcher_; |
227 | 239 |
228 DISALLOW_COPY_AND_ASSIGN(MultiHttpFetcher); | 240 DISALLOW_COPY_AND_ASSIGN(MultiHttpFetcher); |
229 }; | 241 }; |
230 | 242 |
231 } // namespace chromeos_update_engine | 243 } // namespace chromeos_update_engine |
232 | 244 |
233 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ | 245 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MULTI_HTTP_FETCHER_H__ |
OLD | NEW |