Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium 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 #include "net/url_request/url_request_simple_job.h" | 5 #include "net/url_request/url_request_simple_job.h" |
| 6 | 6 |
| 7 #include <vector> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 10 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/http/http_request_headers.h" | |
| 15 #include "net/http/http_util.h" | |
| 12 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
| 13 | 17 |
| 14 namespace net { | 18 namespace net { |
| 15 | 19 |
| 16 URLRequestSimpleJob::URLRequestSimpleJob( | 20 URLRequestSimpleJob::URLRequestSimpleJob( |
| 17 URLRequest* request, NetworkDelegate* network_delegate) | 21 URLRequest* request, NetworkDelegate* network_delegate) |
| 18 : URLRequestJob(request, network_delegate), | 22 : URLRangeRequestJob(request, network_delegate), |
| 19 data_offset_(0), | 23 data_offset_(0), |
| 20 weak_factory_(this) {} | 24 weak_factory_(this) {} |
| 21 | 25 |
| 22 void URLRequestSimpleJob::Start() { | 26 void URLRequestSimpleJob::Start() { |
| 23 // Start reading asynchronously so that all error reporting and data | 27 // Start reading asynchronously so that all error reporting and data |
| 24 // callbacks happen as they would for network requests. | 28 // callbacks happen as they would for network requests. |
| 25 base::MessageLoop::current()->PostTask( | 29 base::MessageLoop::current()->PostTask( |
| 26 FROM_HERE, | 30 FROM_HERE, |
| 27 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr())); | 31 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr())); |
| 28 } | 32 } |
| 29 | 33 |
| 30 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const { | 34 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const { |
| 31 *mime_type = mime_type_; | 35 *mime_type = mime_type_; |
| 32 return true; | 36 return true; |
| 33 } | 37 } |
| 34 | 38 |
| 35 bool URLRequestSimpleJob::GetCharset(std::string* charset) { | 39 bool URLRequestSimpleJob::GetCharset(std::string* charset) { |
| 36 *charset = charset_; | 40 *charset = charset_; |
| 37 return true; | 41 return true; |
| 38 } | 42 } |
| 39 | 43 |
| 40 URLRequestSimpleJob::~URLRequestSimpleJob() {} | 44 URLRequestSimpleJob::~URLRequestSimpleJob() {} |
| 41 | 45 |
| 42 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, | 46 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, |
| 43 int* bytes_read) { | 47 int* bytes_read) { |
| 44 DCHECK(bytes_read); | 48 DCHECK(bytes_read); |
| 45 int remaining = static_cast<int>(data_.size()) - data_offset_; | 49 int remaining = byte_range_.last_byte_position() - data_offset_ + 1; |
| 46 if (buf_size > remaining) | 50 if (buf_size > remaining) |
| 47 buf_size = remaining; | 51 buf_size = remaining; |
| 48 memcpy(buf->data(), data_.data() + data_offset_, buf_size); | 52 memcpy(buf->data(), data_.data() + data_offset_, buf_size); |
| 49 data_offset_ += buf_size; | 53 data_offset_ += buf_size; |
| 50 *bytes_read = buf_size; | 54 *bytes_read = buf_size; |
| 51 return true; | 55 return true; |
| 52 } | 56 } |
| 53 | 57 |
| 54 void URLRequestSimpleJob::StartAsync() { | 58 void URLRequestSimpleJob::StartAsync() { |
| 55 if (!request_) | 59 if (!request_) |
| 56 return; | 60 return; |
| 57 | 61 |
| 62 if (ranges().size() > 1) { | |
| 63 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | |
| 64 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 if (!ranges().empty() && range_parse_result() == net::OK) | |
|
mmenke
2014/01/09 15:49:24
nit: "net::" not needed.
| |
| 69 byte_range_ = ranges().front(); | |
| 70 | |
| 58 int result = GetData(&mime_type_, &charset_, &data_, | 71 int result = GetData(&mime_type_, &charset_, &data_, |
| 59 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, | 72 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, |
| 60 weak_factory_.GetWeakPtr())); | 73 weak_factory_.GetWeakPtr())); |
| 61 if (result != ERR_IO_PENDING) | 74 if (result != ERR_IO_PENDING) |
| 62 OnGetDataCompleted(result); | 75 OnGetDataCompleted(result); |
| 63 } | 76 } |
| 64 | 77 |
| 65 void URLRequestSimpleJob::OnGetDataCompleted(int result) { | 78 void URLRequestSimpleJob::OnGetDataCompleted(int result) { |
| 66 if (result == OK) { | 79 if (result == OK) { |
| 67 // Notify that the headers are complete | 80 // Notify that the headers are complete |
| 81 if (!byte_range_.ComputeBounds(data_.size())) { | |
| 82 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | |
| 83 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 data_offset_ = byte_range_.first_byte_position(); | |
| 88 int remaining_bytes = byte_range_.last_byte_position() - | |
| 89 byte_range_.first_byte_position() + 1; | |
| 90 set_expected_content_size(remaining_bytes); | |
| 68 NotifyHeadersComplete(); | 91 NotifyHeadersComplete(); |
| 69 } else { | 92 } else { |
| 70 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); | 93 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 71 } | 94 } |
| 72 } | 95 } |
| 73 | 96 |
| 74 } // namespace net | 97 } // namespace net |
| OLD | NEW |