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