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