Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: multi_range_http_fetcher.cc

Issue 5835004: AU: MultiHttpFetcher cleanup/rewrite (Closed) Base URL: http://git.chromium.org/git/update_engine.git@master
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "update_engine/multi_range_http_fetcher.h"
6
7 namespace chromeos_update_engine {
8
9 // Begins the transfer to the specified URL.
10 // State change: Stopped -> Downloading
11 // (corner case: Stopped -> Stopped for an empty request)
12 void MultiRangeHTTPFetcher::BeginTransfer(const std::string& url) {
13 LOG_IF(ERROR, base_fetcher_active_) << "BeginTransfer but already active.";
petkov 2010/12/16 18:30:57 These conditions would be pretty bad if they actua
adlr 2010/12/17 01:03:51 Done.
14 LOG_IF(ERROR, pending_transfer_ended_) << "BeginTransfer but pending.";
15 LOG_IF(ERROR, terminating_) << "BeginTransfer but terminating.";
16
17 if (ranges_.empty()) {
18 // Note that after the callback returns this object may be destroyed.
19 if (delegate_)
20 delegate_->TransferComplete(this, true);
21 return;
22 }
23 url_ = url;
24 current_index_ = 0;
25 bytes_received_this_fetcher_ = 0;
26 LOG(INFO) << "starting first transfer";
27 base_fetcher_->set_delegate(this);
28 StartTransfer();
29 }
30
31 // Stage change: Downloading -> Pending transfer ended
petkov 2010/12/16 18:30:57 State
adlr 2010/12/17 01:03:51 done. Copy paste FAIL :(
32 void MultiRangeHTTPFetcher::TerminateTransfer() {
33 if (!base_fetcher_active_) {
34 LOG(INFO) << "Called TerminateTransfer but not active.";
35 return;
petkov 2010/12/16 18:30:57 So, if the client invokes TerminateTransfer before
adlr 2010/12/17 01:03:51 I guess it's a bit of a corner case. I don't know
36 }
37 terminating_ = true;
38
39 if (!pending_transfer_ended_) {
40 base_fetcher_->TerminateTransfer();
41 return;
petkov 2010/12/16 18:30:57 no need for this return here.
adlr 2010/12/17 01:03:51 Done.
42 }
43 }
44
45 // Stage change: Stopped or Downloading -> Downloading
46 void MultiRangeHTTPFetcher::StartTransfer() {
47 if (current_index_ >= ranges_.size()) {
48 return;
49 }
50 LOG(INFO) << "Starting a transfer @" << ranges_[current_index_].first << "("
51 << ranges_[current_index_].second << ")";
52 bytes_received_this_fetcher_ = 0;
53 base_fetcher_->SetOffset(ranges_[current_index_].first);
54 if (delegate_)
55 delegate_->SeekToOffset(ranges_[current_index_].first);
56 base_fetcher_active_ = true;
57 base_fetcher_->BeginTransfer(url_);
58 }
59
60 // Stage change: Downloading -> Downloading
61 void MultiRangeHTTPFetcher::ReceivedBytes(HttpFetcher* fetcher,
62 const char* bytes,
63 int length) {
64 TEST_AND_RETURN(current_index_ < ranges_.size());
petkov 2010/12/16 18:30:57 Again, we may as well use CHECKs here -- it might
adlr 2010/12/17 01:03:51 Done.
65 TEST_AND_RETURN(fetcher == base_fetcher_);
66 TEST_AND_RETURN(!pending_transfer_ended_);
67 off_t next_size = length;
68 if (ranges_[current_index_].second >= 0) {
69 next_size = std::min(next_size,
70 ranges_[current_index_].second -
71 bytes_received_this_fetcher_);
72 }
73 LOG_IF(WARNING, next_size <= 0) << "Asked to write length <= 0";
74 if (delegate_) {
75 delegate_->ReceivedBytes(this, bytes, next_size);
76 }
77 bytes_received_this_fetcher_ += length;
78 if (ranges_[current_index_].second >= 0 &&
79 bytes_received_this_fetcher_ >= ranges_[current_index_].second) {
80 // Terminates the current fetcher. Waits for its TransferTerminated
81 // callback before starting the next fetcher so that we don't end up
82 // signalling the delegate that the whole multi-transfer is complete
83 // before all fetchers are really done and cleaned up.
84 pending_transfer_ended_ = true;
85 LOG(INFO) << "terminating transfer";
86 fetcher->TerminateTransfer();
87 }
88 }
89
90 // Stage change: Downloading or Pending transfer ended -> Stopped
91 void MultiRangeHTTPFetcher::TransferEnded(HttpFetcher* fetcher,
92 bool successful) {
93 LOG_IF(ERROR, !base_fetcher_active_) << "Transfer ended unexpectedly.";
94 LOG_IF(ERROR, !pending_transfer_ended_)
petkov 2010/12/16 18:30:57 What happens when the last transfer completes, the
adlr 2010/12/17 01:03:51 Good catch. Yes, this was in the logs. Removed it
95 << "Unexpectedly not waiting for transfer to end.";
96 pending_transfer_ended_ = false;
97 http_response_code_ = fetcher->http_response_code();
98 LOG(INFO) << "TransferEnded w/ code " << http_response_code_;
99 if (terminating_) {
100 LOG(INFO) << "Terminating.";
101 Reset();
102 // Note that after the callback returns this object may be destroyed.
103 if (delegate_)
104 delegate_->TransferTerminated(this);
105 return;
106 }
107
108 // If we didn't get enough bytes, it's failure
109 if (ranges_[current_index_].second >= 0) {
110 if (bytes_received_this_fetcher_ < ranges_[current_index_].second) {
111 // Failure
112 LOG(INFO) << "Didn't get enough bytes. Ending w/ failure.";
113 Reset();
114 // Note that after the callback returns this object may be destroyed.
115 if (delegate_)
116 delegate_->TransferComplete(this, false);
117 return;
118 }
119 // We got enough bytes and there were bytes specified, so this is success.
120 successful = true;
121 }
122
123 // If we have another fetcher, use that.
124 if (current_index_ + 1 < ranges_.size()) {
125 LOG(INFO) << "Starting next transfer.";
126 current_index_++;
127 StartTransfer();
128 return;
129 }
130
131 // LOG(INFO) << "passed in success: " << successful;
petkov 2010/12/16 18:30:57 remove commented code
adlr 2010/12/17 01:03:51 Done.
132 // if (ranges_[current_index_].second >= 0) {
133 // successful = bytes_received_this_fetcher_ >= ranges_[current_index_].seco nd;
134 // }
135 // LOG(INFO) << "computed success: " << successful;
136 LOG(INFO) << "Done w/ all transfers";
137 Reset();
138 // Note that after the callback returns this object may be destroyed.
139 if (delegate_)
140 delegate_->TransferComplete(this, successful);
141 }
142
143 } // namespace chromeos_update_engine
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698