| 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/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/profiler/scoped_tracker.h" | 13 #include "base/profiler/scoped_tracker.h" |
| 14 #include "base/threading/worker_pool.h" |
| 14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 #include "net/http/http_request_headers.h" | 17 #include "net/http/http_request_headers.h" |
| 17 #include "net/http/http_util.h" | 18 #include "net/http/http_util.h" |
| 18 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 | 22 |
| 22 URLRequestSimpleJob::URLRequestSimpleJob( | 23 namespace { |
| 23 URLRequest* request, NetworkDelegate* network_delegate) | 24 |
| 25 void CopyData(const scoped_refptr<IOBuffer>& buf, |
| 26 int buf_size, |
| 27 const scoped_refptr<base::RefCountedMemory>& data, |
| 28 int64 data_offset) { |
| 29 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. |
| 30 tracked_objects::ScopedTracker tracking_profile( |
| 31 FROM_HERE_WITH_EXPLICIT_FUNCTION("422489 CopyData")); |
| 32 |
| 33 memcpy(buf->data(), data->front() + data_offset, buf_size); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 38 URLRequestSimpleJob::URLRequestSimpleJob(URLRequest* request, |
| 39 NetworkDelegate* network_delegate) |
| 24 : URLRangeRequestJob(request, network_delegate), | 40 : URLRangeRequestJob(request, network_delegate), |
| 25 data_offset_(0), | 41 next_data_offset_(0), |
| 26 weak_factory_(this) {} | 42 task_runner_(base::WorkerPool::GetTaskRunner(false)), |
| 43 weak_factory_(this) { |
| 44 } |
| 27 | 45 |
| 28 void URLRequestSimpleJob::Start() { | 46 void URLRequestSimpleJob::Start() { |
| 29 // Start reading asynchronously so that all error reporting and data | 47 // Start reading asynchronously so that all error reporting and data |
| 30 // callbacks happen as they would for network requests. | 48 // callbacks happen as they would for network requests. |
| 31 base::MessageLoop::current()->PostTask( | 49 base::MessageLoop::current()->PostTask( |
| 32 FROM_HERE, | 50 FROM_HERE, |
| 33 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr())); | 51 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr())); |
| 34 } | 52 } |
| 35 | 53 |
| 36 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const { | 54 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const { |
| 37 *mime_type = mime_type_; | 55 *mime_type = mime_type_; |
| 38 return true; | 56 return true; |
| 39 } | 57 } |
| 40 | 58 |
| 41 bool URLRequestSimpleJob::GetCharset(std::string* charset) { | 59 bool URLRequestSimpleJob::GetCharset(std::string* charset) { |
| 42 *charset = charset_; | 60 *charset = charset_; |
| 43 return true; | 61 return true; |
| 44 } | 62 } |
| 45 | 63 |
| 46 URLRequestSimpleJob::~URLRequestSimpleJob() {} | 64 URLRequestSimpleJob::~URLRequestSimpleJob() {} |
| 47 | 65 |
| 48 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, | 66 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, |
| 49 int* bytes_read) { | 67 int* bytes_read) { |
| 50 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. | 68 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. |
| 51 tracked_objects::ScopedTracker tracking_profile( | 69 tracked_objects::ScopedTracker tracking_profile( |
| 52 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 70 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 53 "422489 URLRequestSimpleJob::ReadRawData")); | 71 "422489 URLRequestSimpleJob::ReadRawData")); |
| 54 | 72 |
| 55 DCHECK(bytes_read); | 73 DCHECK(bytes_read); |
| 56 buf_size = static_cast<int>(std::min( | 74 buf_size = static_cast<int>( |
| 57 static_cast<int64>(buf_size), | 75 std::min(static_cast<int64>(buf_size), |
| 58 byte_range_.last_byte_position() - data_offset_ + 1)); | 76 byte_range_.last_byte_position() - next_data_offset_ + 1)); |
| 59 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 77 DCHECK_GE(buf_size, 0); |
| 60 data_offset_ += buf_size; | 78 if (buf_size == 0) { |
| 61 *bytes_read = buf_size; | 79 *bytes_read = 0; |
| 62 return true; | 80 return true; |
| 81 } |
| 82 |
| 83 // Do memory copy on a background thread. See crbug.com/422489. |
| 84 GetTaskRunner()->PostTaskAndReply( |
| 85 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_, |
| 86 next_data_offset_), |
| 87 base::Bind(&URLRequestSimpleJob::OnReadCompleted, |
| 88 weak_factory_.GetWeakPtr(), buf_size)); |
| 89 next_data_offset_ += buf_size; |
| 90 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 91 return false; |
| 92 } |
| 93 |
| 94 void URLRequestSimpleJob::OnReadCompleted(int bytes_read) { |
| 95 SetStatus(net::URLRequestStatus()); |
| 96 NotifyReadComplete(bytes_read); |
| 97 } |
| 98 |
| 99 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const { |
| 100 return task_runner_.get(); |
| 63 } | 101 } |
| 64 | 102 |
| 65 int URLRequestSimpleJob::GetData(std::string* mime_type, | 103 int URLRequestSimpleJob::GetData(std::string* mime_type, |
| 66 std::string* charset, | 104 std::string* charset, |
| 67 std::string* data, | 105 std::string* data, |
| 68 const CompletionCallback& callback) const { | 106 const CompletionCallback& callback) const { |
| 69 NOTREACHED(); | 107 NOTREACHED(); |
| 70 return ERR_UNEXPECTED; | 108 return ERR_UNEXPECTED; |
| 71 } | 109 } |
| 72 | 110 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 "422489 URLRequestSimpleJob::OnGetDataCompleted")); | 168 "422489 URLRequestSimpleJob::OnGetDataCompleted")); |
| 131 | 169 |
| 132 if (result == OK) { | 170 if (result == OK) { |
| 133 // Notify that the headers are complete | 171 // Notify that the headers are complete |
| 134 if (!byte_range_.ComputeBounds(data_->size())) { | 172 if (!byte_range_.ComputeBounds(data_->size())) { |
| 135 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 173 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 136 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 174 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
| 137 return; | 175 return; |
| 138 } | 176 } |
| 139 | 177 |
| 140 data_offset_ = byte_range_.first_byte_position(); | 178 next_data_offset_ = byte_range_.first_byte_position(); |
| 141 set_expected_content_size( | 179 set_expected_content_size(byte_range_.last_byte_position() - |
| 142 byte_range_.last_byte_position() - data_offset_ + 1); | 180 next_data_offset_ + 1); |
| 143 NotifyHeadersComplete(); | 181 NotifyHeadersComplete(); |
| 144 } else { | 182 } else { |
| 145 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); | 183 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 146 } | 184 } |
| 147 } | 185 } |
| 148 | 186 |
| 149 } // namespace net | 187 } // namespace net |
| OLD | NEW |