| 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" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 return true; | 58 return true; |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool URLRequestSimpleJob::GetCharset(std::string* charset) { | 61 bool URLRequestSimpleJob::GetCharset(std::string* charset) { |
| 62 *charset = charset_; | 62 *charset = charset_; |
| 63 return true; | 63 return true; |
| 64 } | 64 } |
| 65 | 65 |
| 66 URLRequestSimpleJob::~URLRequestSimpleJob() {} | 66 URLRequestSimpleJob::~URLRequestSimpleJob() {} |
| 67 | 67 |
| 68 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, | 68 int URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size) { |
| 69 int* bytes_read) { | 69 buf_size = std::min(static_cast<int64>(buf_size), |
| 70 DCHECK(bytes_read); | 70 byte_range_.last_byte_position() - next_data_offset_ + 1); |
| 71 buf_size = static_cast<int>( | 71 if (buf_size == 0) |
| 72 std::min(static_cast<int64>(buf_size), | 72 return 0; |
| 73 byte_range_.last_byte_position() - next_data_offset_ + 1)); | |
| 74 DCHECK_GE(buf_size, 0); | |
| 75 if (buf_size == 0) { | |
| 76 *bytes_read = 0; | |
| 77 return true; | |
| 78 } | |
| 79 | 73 |
| 80 // Do memory copy on a background thread. See crbug.com/422489. | 74 // Do memory copy on a background thread. See crbug.com/422489. |
| 81 GetTaskRunner()->PostTaskAndReply( | 75 GetTaskRunner()->PostTaskAndReply( |
| 82 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_, | 76 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_, |
| 83 next_data_offset_), | 77 next_data_offset_), |
| 84 base::Bind(&URLRequestSimpleJob::OnReadCompleted, | 78 base::Bind(&URLRequestSimpleJob::OnReadCompleted, |
| 85 weak_factory_.GetWeakPtr(), buf_size)); | 79 weak_factory_.GetWeakPtr(), buf_size)); |
| 86 next_data_offset_ += buf_size; | 80 next_data_offset_ += buf_size; |
| 87 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 81 return ERR_IO_PENDING; |
| 88 return false; | |
| 89 } | 82 } |
| 90 | 83 |
| 91 void URLRequestSimpleJob::OnReadCompleted(int bytes_read) { | 84 void URLRequestSimpleJob::OnReadCompleted(int bytes_read) { |
| 92 SetStatus(URLRequestStatus()); | 85 ReadRawDataComplete(bytes_read); |
| 93 NotifyReadComplete(bytes_read); | |
| 94 } | 86 } |
| 95 | 87 |
| 96 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const { | 88 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const { |
| 97 return task_runner_.get(); | 89 return task_runner_.get(); |
| 98 } | 90 } |
| 99 | 91 |
| 100 int URLRequestSimpleJob::GetData(std::string* mime_type, | 92 int URLRequestSimpleJob::GetData(std::string* mime_type, |
| 101 std::string* charset, | 93 std::string* charset, |
| 102 std::string* data, | 94 std::string* data, |
| 103 const CompletionCallback& callback) const { | 95 const CompletionCallback& callback) const { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 int result = GetData(mime_type, charset, &str_data->data(), callback); | 106 int result = GetData(mime_type, charset, &str_data->data(), callback); |
| 115 *data = str_data; | 107 *data = str_data; |
| 116 return result; | 108 return result; |
| 117 } | 109 } |
| 118 | 110 |
| 119 void URLRequestSimpleJob::StartAsync() { | 111 void URLRequestSimpleJob::StartAsync() { |
| 120 if (!request_) | 112 if (!request_) |
| 121 return; | 113 return; |
| 122 | 114 |
| 123 if (ranges().size() > 1) { | 115 if (ranges().size() > 1) { |
| 124 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 116 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
| 125 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 117 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 126 return; | 118 return; |
| 127 } | 119 } |
| 128 | 120 |
| 129 if (!ranges().empty() && range_parse_result() == OK) | 121 if (!ranges().empty() && range_parse_result() == OK) |
| 130 byte_range_ = ranges().front(); | 122 byte_range_ = ranges().front(); |
| 131 | 123 |
| 132 const int result = | 124 const int result = |
| 133 GetRefCountedData(&mime_type_, &charset_, &data_, | 125 GetRefCountedData(&mime_type_, &charset_, &data_, |
| 134 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, | 126 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, |
| 135 weak_factory_.GetWeakPtr())); | 127 weak_factory_.GetWeakPtr())); |
| 136 | 128 |
| 137 if (result != ERR_IO_PENDING) | 129 if (result != ERR_IO_PENDING) |
| 138 OnGetDataCompleted(result); | 130 OnGetDataCompleted(result); |
| 139 } | 131 } |
| 140 | 132 |
| 141 void URLRequestSimpleJob::OnGetDataCompleted(int result) { | 133 void URLRequestSimpleJob::OnGetDataCompleted(int result) { |
| 142 if (result == OK) { | 134 if (result == OK) { |
| 143 // Notify that the headers are complete | 135 // Notify that the headers are complete |
| 144 if (!byte_range_.ComputeBounds(data_->size())) { | 136 if (!byte_range_.ComputeBounds(data_->size())) { |
| 145 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 137 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
| 146 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 138 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 147 return; | 139 return; |
| 148 } | 140 } |
| 149 | 141 |
| 150 next_data_offset_ = byte_range_.first_byte_position(); | 142 next_data_offset_ = byte_range_.first_byte_position(); |
| 151 set_expected_content_size(byte_range_.last_byte_position() - | 143 set_expected_content_size(byte_range_.last_byte_position() - |
| 152 next_data_offset_ + 1); | 144 next_data_offset_ + 1); |
| 153 NotifyHeadersComplete(); | 145 NotifyHeadersComplete(); |
| 154 } else { | 146 } else { |
| 155 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); | 147 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 156 } | 148 } |
| 157 } | 149 } |
| 158 | 150 |
| 159 } // namespace net | 151 } // namespace net |
| OLD | NEW |