[go: nahoru, domu]

blob: a05c21897542f12f7677a12e1cebedbec6914eab [file] [log] [blame]
tbreisacher@chromium.org9987dd72012-01-25 23:40:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
rvargas@google.com8bf26f49a2009-06-12 17:35:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/http/partial_data.h"
6
jhawkins@chromium.org2a65aceb82011-12-19 20:59:277#include "base/bind.h"
8#include "base/bind_helpers.h"
evan@chromium.org34b2b002009-11-20 06:53:289#include "base/format_macros.h"
rvargas@google.com8bf26f49a2009-06-12 17:35:5010#include "base/logging.h"
avi@chromium.org125ef482013-06-11 18:32:4711#include "base/strings/string_number_conversions.h"
12#include "base/strings/string_util.h"
13#include "base/strings/stringprintf.h"
rvargas@google.com8bf26f49a2009-06-12 17:35:5014#include "net/base/net_errors.h"
15#include "net/disk_cache/disk_cache.h"
rvargas@google.com95792eb12009-06-22 21:30:4016#include "net/http/http_response_headers.h"
rvargas@google.com8bf26f49a2009-06-12 17:35:5017#include "net/http/http_util.h"
rvargas@google.com95792eb12009-06-22 21:30:4018
willchan@chromium.org8c76ae22010-04-20 22:15:4319namespace net {
20
rvargas@google.com95792eb12009-06-22 21:30:4021namespace {
22
23// The headers that we have to process.
24const char kLengthHeader[] = "Content-Length";
25const char kRangeHeader[] = "Content-Range";
rvargas@google.come5dad132009-08-18 00:53:4126const int kDataStream = 1;
rvargas@google.com95792eb12009-06-22 21:30:4027
willchan@chromium.org8c76ae22010-04-20 22:15:4328} // namespace
rvargas@google.com8bf26f49a2009-06-12 17:35:5029
rvargas@google.com034740a2010-06-11 17:16:4830// A core object that can be detached from the Partialdata object at destruction
31// so that asynchronous operations cleanup can be performed.
32class PartialData::Core {
33 public:
34 // Build a new core object. Lifetime management is automatic.
35 static Core* CreateCore(PartialData* owner) {
36 return new Core(owner);
37 }
38
39 // Wrapper for Entry::GetAvailableRange. If this method returns ERR_IO_PENDING
40 // PartialData::GetAvailableRangeCompleted() will be invoked on the owner
41 // object when finished (unless Cancel() is called first).
42 int GetAvailableRange(disk_cache::Entry* entry, int64 offset, int len,
43 int64* start);
44
45 // Cancels a pending operation. It is a mistake to call this method if there
46 // is no operation in progress; in fact, there will be no object to do so.
47 void Cancel();
48
49 private:
50 explicit Core(PartialData* owner);
51 ~Core();
52
53 // Pending io completion routine.
54 void OnIOComplete(int result);
55
56 PartialData* owner_;
57 int64 start_;
jhawkins@chromium.org2a65aceb82011-12-19 20:59:2758
rvargas@google.com034740a2010-06-11 17:16:4859 DISALLOW_COPY_AND_ASSIGN(Core);
60};
61
62PartialData::Core::Core(PartialData* owner)
tbreisacher@chromium.org9987dd72012-01-25 23:40:0463 : owner_(owner), start_(0) {
rvargas@google.com034740a2010-06-11 17:16:4864 DCHECK(!owner_->core_);
65 owner_->core_ = this;
66}
67
68PartialData::Core::~Core() {
69 if (owner_)
70 owner_->core_ = NULL;
71}
72
73void PartialData::Core::Cancel() {
74 DCHECK(owner_);
75 owner_ = NULL;
76}
77
78int PartialData::Core::GetAvailableRange(disk_cache::Entry* entry, int64 offset,
79 int len, int64* start) {
jhawkins@chromium.org2a65aceb82011-12-19 20:59:2780 int rv = entry->GetAvailableRange(
81 offset, len, &start_, base::Bind(&PartialData::Core::OnIOComplete,
82 base::Unretained(this)));
rvargas@google.com034740a2010-06-11 17:16:4883 if (rv != net::ERR_IO_PENDING) {
84 // The callback will not be invoked. Lets cleanup.
85 *start = start_;
86 delete this;
87 }
88 return rv;
89}
90
91void PartialData::Core::OnIOComplete(int result) {
92 if (owner_)
93 owner_->GetAvailableRangeCompleted(result, start_);
94 delete this;
95}
96
97// -----------------------------------------------------------------------------
98
erg@google.comb104b502010-10-18 20:21:3199PartialData::PartialData()
100 : range_present_(false),
101 final_range_(false),
102 sparse_entry_(true),
103 truncated_(false),
rvargas@google.com634739b2011-03-02 18:08:25104 initial_validation_(false),
jhawkins@chromium.org49639fa2011-12-20 23:22:41105 core_(NULL) {
erg@google.comb104b502010-10-18 20:21:31106}
107
rvargas@google.com034740a2010-06-11 17:16:48108PartialData::~PartialData() {
109 if (core_)
110 core_->Cancel();
111}
112
willchan@chromium.org8c76ae22010-04-20 22:15:43113bool PartialData::Init(const HttpRequestHeaders& headers) {
114 std::string range_header;
115 if (!headers.GetHeader(HttpRequestHeaders::kRange, &range_header))
116 return false;
117
rvargas@google.com8bf26f49a2009-06-12 17:35:50118 std::vector<HttpByteRange> ranges;
willchan@chromium.org8c76ae22010-04-20 22:15:43119 if (!HttpUtil::ParseRangeHeader(range_header, &ranges) || ranges.size() != 1)
rvargas@google.com8bf26f49a2009-06-12 17:35:50120 return false;
121
122 // We can handle this range request.
123 byte_range_ = ranges[0];
124 if (!byte_range_.IsValid())
125 return false;
126
rvargas@google.com95792eb12009-06-22 21:30:40127 resource_size_ = 0;
rvargas@google.com8bf26f49a2009-06-12 17:35:50128 current_range_start_ = byte_range_.first_byte_position();
rvargas@google.coma5c9d982010-10-12 20:48:02129
130 DVLOG(1) << "Range start: " << current_range_start_ << " end: " <<
131 byte_range_.last_byte_position();
rvargas@google.com8bf26f49a2009-06-12 17:35:50132 return true;
133}
134
willchan@chromium.org8c76ae22010-04-20 22:15:43135void PartialData::SetHeaders(const HttpRequestHeaders& headers) {
136 DCHECK(extra_headers_.IsEmpty());
137 extra_headers_.CopyFrom(headers);
rvargas@google.come75e8af2009-11-03 00:04:20138}
139
willchan@chromium.org8c76ae22010-04-20 22:15:43140void PartialData::RestoreHeaders(HttpRequestHeaders* headers) const {
rvargas@google.com67fe45c2009-06-24 17:44:57141 DCHECK(current_range_start_ >= 0 || byte_range_.IsSuffixByteRange());
142 int64 end = byte_range_.IsSuffixByteRange() ?
143 byte_range_.suffix_length() : byte_range_.last_byte_position();
rvargas@google.com8bf26f49a2009-06-12 17:35:50144
willchan@chromium.org8c76ae22010-04-20 22:15:43145 headers->CopyFrom(extra_headers_);
tommycli@chromium.orgb7572ea2013-11-26 20:16:38146 if (truncated_ || !byte_range_.IsValid())
147 return;
148
149 if (current_range_start_ < 0) {
150 headers->SetHeader(HttpRequestHeaders::kRange,
151 HttpByteRange::Suffix(end).GetHeaderValue());
152 } else {
153 headers->SetHeader(HttpRequestHeaders::kRange,
154 HttpByteRange::Bounded(
155 current_range_start_, end).GetHeaderValue());
156 }
rvargas@google.com8bf26f49a2009-06-12 17:35:50157}
158
rvargas@google.com034740a2010-06-11 17:16:48159int PartialData::ShouldValidateCache(disk_cache::Entry* entry,
jhawkins@chromium.org49639fa2011-12-20 23:22:41160 const CompletionCallback& callback) {
rvargas@google.com034740a2010-06-11 17:16:48161 DCHECK_GE(current_range_start_, 0);
rvargas@google.com8bf26f49a2009-06-12 17:35:50162
163 // Scan the disk cache for the first cached portion within this range.
rvargas@google.com034740a2010-06-11 17:16:48164 int len = GetNextRangeLen();
rvargas@google.com8bf26f49a2009-06-12 17:35:50165 if (!len)
166 return 0;
rvargas@google.com8bf26f49a2009-06-12 17:35:50167
rvargas@google.coma5c9d982010-10-12 20:48:02168 DVLOG(3) << "ShouldValidateCache len: " << len;
169
rvargas@google.come5dad132009-08-18 00:53:41170 if (sparse_entry_) {
jhawkins@chromium.org49639fa2011-12-20 23:22:41171 DCHECK(callback_.is_null());
rvargas@google.com034740a2010-06-11 17:16:48172 Core* core = Core::CreateCore(this);
173 cached_min_len_ = core->GetAvailableRange(entry, current_range_start_, len,
174 &cached_start_);
175
176 if (cached_min_len_ == ERR_IO_PENDING) {
177 callback_ = callback;
178 return ERR_IO_PENDING;
179 }
rvargas@google.com634739b2011-03-02 18:08:25180 } else if (!truncated_) {
rvargas@google.coma5c9d982010-10-12 20:48:02181 if (byte_range_.HasFirstBytePosition() &&
182 byte_range_.first_byte_position() >= resource_size_) {
183 // The caller should take care of this condition because we should have
184 // failed IsRequestedRangeOK(), but it's better to be consistent here.
185 len = 0;
186 }
rvargas@google.come5dad132009-08-18 00:53:41187 cached_min_len_ = len;
188 cached_start_ = current_range_start_;
189 }
190
rvargas@google.com034740a2010-06-11 17:16:48191 if (cached_min_len_ < 0)
rvargas@google.com8bf26f49a2009-06-12 17:35:50192 return cached_min_len_;
rvargas@google.com034740a2010-06-11 17:16:48193
194 // Return a positive number to indicate success (versus error or finished).
195 return 1;
196}
197
198void PartialData::PrepareCacheValidation(disk_cache::Entry* entry,
199 HttpRequestHeaders* headers) {
200 DCHECK_GE(current_range_start_, 0);
201 DCHECK_GE(cached_min_len_, 0);
202
203 int len = GetNextRangeLen();
204 DCHECK_NE(0, len);
205 range_present_ = false;
rvargas@google.com8bf26f49a2009-06-12 17:35:50206
willchan@chromium.org8c76ae22010-04-20 22:15:43207 headers->CopyFrom(extra_headers_);
rvargas@google.com8bf26f49a2009-06-12 17:35:50208
209 if (!cached_min_len_) {
210 // We don't have anything else stored.
211 final_range_ = true;
rvargas@google.com28accfe2009-09-04 23:36:33212 cached_start_ =
213 byte_range_.HasLastBytePosition() ? current_range_start_ + len : 0;
rvargas@google.com8bf26f49a2009-06-12 17:35:50214 }
215
216 if (current_range_start_ == cached_start_) {
217 // The data lives in the cache.
218 range_present_ = true;
219 if (len == cached_min_len_)
220 final_range_ = true;
tommycli@chromium.orgb7572ea2013-11-26 20:16:38221 headers->SetHeader(
222 HttpRequestHeaders::kRange,
223 net::HttpByteRange::Bounded(
224 current_range_start_,
225 cached_start_ + cached_min_len_ - 1).GetHeaderValue());
rvargas@google.com8bf26f49a2009-06-12 17:35:50226 } else {
227 // This range is not in the cache.
tommycli@chromium.orgb7572ea2013-11-26 20:16:38228 headers->SetHeader(
229 HttpRequestHeaders::kRange,
230 net::HttpByteRange::Bounded(
231 current_range_start_, cached_start_ - 1).GetHeaderValue());
rvargas@google.com8bf26f49a2009-06-12 17:35:50232 }
rvargas@google.com8bf26f49a2009-06-12 17:35:50233}
234
235bool PartialData::IsCurrentRangeCached() const {
236 return range_present_;
237}
238
239bool PartialData::IsLastRange() const {
240 return final_range_;
241}
242
rvargas@google.com44f873a62009-08-12 00:14:48243bool PartialData::UpdateFromStoredHeaders(const HttpResponseHeaders* headers,
rvargas@google.com28accfe2009-09-04 23:36:33244 disk_cache::Entry* entry,
245 bool truncated) {
rvargas@google.com67fe45c2009-06-24 17:44:57246 resource_size_ = 0;
rvargas@google.com28accfe2009-09-04 23:36:33247 if (truncated) {
248 DCHECK_EQ(headers->response_code(), 200);
rvargas@google.com28accfe2009-09-04 23:36:33249 // We don't have the real length and the user may be trying to create a
250 // sparse entry so let's not write to this entry.
251 if (byte_range_.IsValid())
rvargas@google.com9f03cb7a2012-07-30 23:15:20252 return false;
253
254 if (!headers->HasStrongValidators())
rvargas@google.com28accfe2009-09-04 23:36:33255 return false;
256
rvargas@google.comdbd39fb2010-01-08 01:13:36257 // Now we avoid resume if there is no content length, but that was not
258 // always the case so double check here.
259 int64 total_length = headers->GetContentLength();
rvargas@google.combd069d72011-05-19 01:11:11260 if (total_length <= 0)
rvargas@google.comdbd39fb2010-01-08 01:13:36261 return false;
262
hclam@chromium.orgecd8becb2009-10-02 17:57:45263 truncated_ = true;
rvargas@google.com634739b2011-03-02 18:08:25264 initial_validation_ = true;
hclam@chromium.orgecd8becb2009-10-02 17:57:45265 sparse_entry_ = false;
rvargas@google.com634739b2011-03-02 18:08:25266 int current_len = entry->GetDataSize(kDataStream);
267 byte_range_.set_first_byte_position(current_len);
rvargas@google.comdbd39fb2010-01-08 01:13:36268 resource_size_ = total_length;
rvargas@google.com634739b2011-03-02 18:08:25269 current_range_start_ = current_len;
270 cached_min_len_ = current_len;
271 cached_start_ = current_len + 1;
rvargas@google.com28accfe2009-09-04 23:36:33272 return true;
273 }
274
rvargas@google.com9f03cb7a2012-07-30 23:15:20275 if (headers->response_code() != 206) {
rvargas@google.come5dad132009-08-18 00:53:41276 DCHECK(byte_range_.IsValid());
277 sparse_entry_ = false;
278 resource_size_ = entry->GetDataSize(kDataStream);
rvargas@google.coma5c9d982010-10-12 20:48:02279 DVLOG(2) << "UpdateFromStoredHeaders size: " << resource_size_;
rvargas@google.come5dad132009-08-18 00:53:41280 return true;
281 }
282
rvargas@google.com8a301142011-04-13 18:33:40283 if (!headers->HasStrongValidators())
284 return false;
285
rvargas@google.comdbd39fb2010-01-08 01:13:36286 int64 length_value = headers->GetContentLength();
287 if (length_value <= 0)
rvargas@google.com67fe45c2009-06-24 17:44:57288 return false; // We must have stored the resource length.
289
rvargas@google.comdbd39fb2010-01-08 01:13:36290 resource_size_ = length_value;
rvargas@google.com67fe45c2009-06-24 17:44:57291
rvargas@google.come5dad132009-08-18 00:53:41292 // Make sure that this is really a sparse entry.
rvargas@google.com034740a2010-06-11 17:16:48293 return entry->CouldBeSparse();
rvargas@google.come5dad132009-08-18 00:53:41294}
295
rvargas@google.com634739b2011-03-02 18:08:25296void PartialData::SetRangeToStartDownload() {
297 DCHECK(truncated_);
298 DCHECK(!sparse_entry_);
299 current_range_start_ = 0;
300 cached_start_ = 0;
301 initial_validation_ = false;
302}
303
rvargas@google.come5dad132009-08-18 00:53:41304bool PartialData::IsRequestedRangeOK() {
rvargas@google.com44f873a62009-08-12 00:14:48305 if (byte_range_.IsValid()) {
306 if (!byte_range_.ComputeBounds(resource_size_))
307 return false;
rvargas@google.com634739b2011-03-02 18:08:25308 if (truncated_)
309 return true;
rvargas@google.com67fe45c2009-06-24 17:44:57310
rvargas@google.com44f873a62009-08-12 00:14:48311 if (current_range_start_ < 0)
312 current_range_start_ = byte_range_.first_byte_position();
313 } else {
314 // This is not a range request but we have partial data stored.
315 current_range_start_ = 0;
316 byte_range_.set_last_byte_position(resource_size_ - 1);
317 }
318
rvargas@google.come5dad132009-08-18 00:53:41319 bool rv = current_range_start_ >= 0;
320 if (!rv)
321 current_range_start_ = 0;
rvargas@google.com67fe45c2009-06-24 17:44:57322
rvargas@google.come5dad132009-08-18 00:53:41323 return rv;
rvargas@google.com95792eb12009-06-22 21:30:40324}
325
326bool PartialData::ResponseHeadersOK(const HttpResponseHeaders* headers) {
rvargas@google.come5dad132009-08-18 00:53:41327 if (headers->response_code() == 304) {
hclam@chromium.orgd9adff2c2009-09-05 01:15:45328 if (!byte_range_.IsValid() || truncated_)
rvargas@google.com28accfe2009-09-04 23:36:33329 return true;
330
rvargas@google.come5dad132009-08-18 00:53:41331 // We must have a complete range here.
332 return byte_range_.HasFirstBytePosition() &&
cbentzel@chromium.org2227c692010-05-04 15:36:11333 byte_range_.HasLastBytePosition();
rvargas@google.come5dad132009-08-18 00:53:41334 }
335
rvargas@google.com95792eb12009-06-22 21:30:40336 int64 start, end, total_length;
337 if (!headers->GetContentRange(&start, &end, &total_length))
338 return false;
339 if (total_length <= 0)
340 return false;
341
rvargas@chromium.org9f10ec32013-11-01 00:51:53342 DCHECK_EQ(headers->response_code(), 206);
343
344 // A server should return a valid content length with a 206 (per the standard)
345 // but relax the requirement because some servers don't do that.
rvargas@google.com7eab0d2262009-10-14 22:05:54346 int64 content_length = headers->GetContentLength();
rvargas@chromium.org9f10ec32013-11-01 00:51:53347 if (content_length > 0 && content_length != end - start + 1)
rvargas@google.com7eab0d2262009-10-14 22:05:54348 return false;
349
rvargas@google.com95792eb12009-06-22 21:30:40350 if (!resource_size_) {
351 // First response. Update our values with the ones provided by the server.
352 resource_size_ = total_length;
rvargas@google.com67fe45c2009-06-24 17:44:57353 if (!byte_range_.HasFirstBytePosition()) {
rvargas@google.com95792eb12009-06-22 21:30:40354 byte_range_.set_first_byte_position(start);
rvargas@google.com67fe45c2009-06-24 17:44:57355 current_range_start_ = start;
356 }
rvargas@google.com95792eb12009-06-22 21:30:40357 if (!byte_range_.HasLastBytePosition())
358 byte_range_.set_last_byte_position(end);
359 } else if (resource_size_ != total_length) {
360 return false;
361 }
362
rvargas@google.comdbd39fb2010-01-08 01:13:36363 if (truncated_) {
364 if (!byte_range_.HasLastBytePosition())
365 byte_range_.set_last_byte_position(end);
366 }
367
rvargas@google.com95792eb12009-06-22 21:30:40368 if (start != current_range_start_)
369 return false;
370
rvargas@google.com44f873a62009-08-12 00:14:48371 if (byte_range_.IsValid() && end > byte_range_.last_byte_position())
rvargas@google.com95792eb12009-06-22 21:30:40372 return false;
373
374 return true;
375}
376
377// We are making multiple requests to complete the range requested by the user.
378// Just assume that everything is fine and say that we are returning what was
379// requested.
rvargas@google.coma5c9d982010-10-12 20:48:02380void PartialData::FixResponseHeaders(HttpResponseHeaders* headers,
381 bool success) {
rvargas@google.com28accfe2009-09-04 23:36:33382 if (truncated_)
383 return;
384
kinuko@chromium.org3d47dac9622014-03-10 07:28:58385 if (byte_range_.IsValid() && success) {
386 headers->UpdateWithNewRange(byte_range_, resource_size_, !sparse_entry_);
387 return;
388 }
389
rvargas@google.com95792eb12009-06-22 21:30:40390 headers->RemoveHeader(kLengthHeader);
391 headers->RemoveHeader(kRangeHeader);
392
rvargas@google.com44f873a62009-08-12 00:14:48393 if (byte_range_.IsValid()) {
kinuko@chromium.org3d47dac9622014-03-10 07:28:58394 headers->ReplaceStatusLine("HTTP/1.1 416 Requested Range Not Satisfiable");
395 headers->AddHeader(base::StringPrintf("%s: bytes 0-0/%" PRId64,
396 kRangeHeader, resource_size_));
397 headers->AddHeader(base::StringPrintf("%s: 0", kLengthHeader));
rvargas@google.com44f873a62009-08-12 00:14:48398 } else {
399 // TODO(rvargas): Is it safe to change the protocol version?
400 headers->ReplaceStatusLine("HTTP/1.1 200 OK");
401 DCHECK_NE(resource_size_, 0);
kinuko@chromium.org3d47dac9622014-03-10 07:28:58402 headers->AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader,
403 resource_size_));
rvargas@google.com44f873a62009-08-12 00:14:48404 }
rvargas@google.com95792eb12009-06-22 21:30:40405}
406
407void PartialData::FixContentLength(HttpResponseHeaders* headers) {
408 headers->RemoveHeader(kLengthHeader);
tfarina@chromium.orgd8eb84242010-09-25 02:25:06409 headers->AddHeader(base::StringPrintf("%s: %" PRId64, kLengthHeader,
410 resource_size_));
rvargas@google.com95792eb12009-06-22 21:30:40411}
412
jhawkins@chromium.org2a65aceb82011-12-19 20:59:27413int PartialData::CacheRead(
414 disk_cache::Entry* entry, IOBuffer* data, int data_len,
415 const net::CompletionCallback& callback) {
rvargas@google.com8bf26f49a2009-06-12 17:35:50416 int read_len = std::min(data_len, cached_min_len_);
hclam@chromium.org8f28d632009-10-01 22:09:21417 if (!read_len)
418 return 0;
419
rvargas@google.come5dad132009-08-18 00:53:41420 int rv = 0;
421 if (sparse_entry_) {
422 rv = entry->ReadSparseData(current_range_start_, data, read_len,
423 callback);
424 } else {
425 if (current_range_start_ > kint32max)
426 return ERR_INVALID_ARGUMENT;
427
428 rv = entry->ReadData(kDataStream, static_cast<int>(current_range_start_),
429 data, read_len, callback);
430 }
rvargas@google.com8bf26f49a2009-06-12 17:35:50431 return rv;
432}
433
jhawkins@chromium.org2a65aceb82011-12-19 20:59:27434int PartialData::CacheWrite(
435 disk_cache::Entry* entry, IOBuffer* data, int data_len,
436 const net::CompletionCallback& callback) {
rvargas@google.coma5c9d982010-10-12 20:48:02437 DVLOG(3) << "To write: " << data_len;
rvargas@google.come5dad132009-08-18 00:53:41438 if (sparse_entry_) {
jhawkins@chromium.org2a65aceb82011-12-19 20:59:27439 return entry->WriteSparseData(
440 current_range_start_, data, data_len, callback);
rvargas@google.come5dad132009-08-18 00:53:41441 } else {
442 if (current_range_start_ > kint32max)
443 return ERR_INVALID_ARGUMENT;
444
445 return entry->WriteData(kDataStream, static_cast<int>(current_range_start_),
rvargas@google.com28accfe2009-09-04 23:36:33446 data, data_len, callback, true);
rvargas@google.come5dad132009-08-18 00:53:41447 }
rvargas@google.com8bf26f49a2009-06-12 17:35:50448}
449
450void PartialData::OnCacheReadCompleted(int result) {
rvargas@google.coma5c9d982010-10-12 20:48:02451 DVLOG(3) << "Read: " << result;
rvargas@google.com8bf26f49a2009-06-12 17:35:50452 if (result > 0) {
453 current_range_start_ += result;
454 cached_min_len_ -= result;
rvargas@google.com034740a2010-06-11 17:16:48455 DCHECK_GE(cached_min_len_, 0);
rvargas@google.com8bf26f49a2009-06-12 17:35:50456 }
457}
458
459void PartialData::OnNetworkReadCompleted(int result) {
460 if (result > 0)
461 current_range_start_ += result;
rvargas@google.com8bf26f49a2009-06-12 17:35:50462}
rvargas@google.com8bf26f49a2009-06-12 17:35:50463
rvargas@google.com034740a2010-06-11 17:16:48464int PartialData::GetNextRangeLen() {
465 int64 range_len =
466 byte_range_.HasLastBytePosition() ?
467 byte_range_.last_byte_position() - current_range_start_ + 1 :
468 kint32max;
469 if (range_len > kint32max)
470 range_len = kint32max;
471 return static_cast<int32>(range_len);
472}
473
474void PartialData::GetAvailableRangeCompleted(int result, int64 start) {
jhawkins@chromium.org49639fa2011-12-20 23:22:41475 DCHECK(!callback_.is_null());
rvargas@google.com034740a2010-06-11 17:16:48476 DCHECK_NE(ERR_IO_PENDING, result);
477
478 cached_start_ = start;
479 cached_min_len_ = result;
480 if (result >= 0)
481 result = 1; // Return success, go ahead and validate the entry.
482
jhawkins@chromium.org49639fa2011-12-20 23:22:41483 CompletionCallback cb = callback_;
484 callback_.Reset();
485 cb.Run(result);
rvargas@google.com034740a2010-06-11 17:16:48486}
487
rvargas@google.com8bf26f49a2009-06-12 17:35:50488} // namespace net