| 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/location.h" | 11 #include "base/location.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/task_scheduler/post_task.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "base/threading/worker_pool.h" | 16 #include "base/threading/worker_pool.h" |
| 16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 18 #include "net/http/http_request_headers.h" | 19 #include "net/http/http_request_headers.h" |
| 19 #include "net/http/http_util.h" | 20 #include "net/http/http_util.h" |
| 20 #include "net/url_request/url_request_status.h" | 21 #include "net/url_request/url_request_status.h" |
| 21 | 22 |
| 22 namespace net { | 23 namespace net { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 void CopyData(const scoped_refptr<IOBuffer>& buf, | 27 void CopyData(const scoped_refptr<IOBuffer>& buf, |
| 27 int buf_size, | 28 int buf_size, |
| 28 const scoped_refptr<base::RefCountedMemory>& data, | 29 const scoped_refptr<base::RefCountedMemory>& data, |
| 29 int64_t data_offset) { | 30 int64_t data_offset) { |
| 30 memcpy(buf->data(), data->front() + data_offset, buf_size); | 31 memcpy(buf->data(), data->front() + data_offset, buf_size); |
| 31 } | 32 } |
| 32 | 33 |
| 33 } // namespace | 34 } // namespace |
| 34 | 35 |
| 35 URLRequestSimpleJob::URLRequestSimpleJob(URLRequest* request, | 36 URLRequestSimpleJob::URLRequestSimpleJob(URLRequest* request, |
| 36 NetworkDelegate* network_delegate) | 37 NetworkDelegate* network_delegate) |
| 37 : URLRangeRequestJob(request, network_delegate), | 38 : URLRangeRequestJob(request, network_delegate), |
| 38 next_data_offset_(0), | 39 next_data_offset_(0), |
| 39 task_runner_(base::WorkerPool::GetTaskRunner(false)), | |
| 40 weak_factory_(this) { | 40 weak_factory_(this) { |
| 41 } | 41 } |
| 42 | 42 |
| 43 void URLRequestSimpleJob::Start() { | 43 void URLRequestSimpleJob::Start() { |
| 44 // Start reading asynchronously so that all error reporting and data | 44 // Start reading asynchronously so that all error reporting and data |
| 45 // callbacks happen as they would for network requests. | 45 // callbacks happen as they would for network requests. |
| 46 base::ThreadTaskRunnerHandle::Get()->PostTask( | 46 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 47 FROM_HERE, | 47 FROM_HERE, |
| 48 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr())); | 48 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr())); |
| 49 } | 49 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 64 } | 64 } |
| 65 | 65 |
| 66 URLRequestSimpleJob::~URLRequestSimpleJob() {} | 66 URLRequestSimpleJob::~URLRequestSimpleJob() {} |
| 67 | 67 |
| 68 int URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size) { | 68 int URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size) { |
| 69 buf_size = std::min(static_cast<int64_t>(buf_size), | 69 buf_size = std::min(static_cast<int64_t>(buf_size), |
| 70 byte_range_.last_byte_position() - next_data_offset_ + 1); | 70 byte_range_.last_byte_position() - next_data_offset_ + 1); |
| 71 if (buf_size == 0) | 71 if (buf_size == 0) |
| 72 return 0; | 72 return 0; |
| 73 | 73 |
| 74 // Do memory copy on a background thread. See crbug.com/422489. | 74 // Do memory copy asynchronously on a thread that is not the network thread. |
| 75 GetTaskRunner()->PostTaskAndReply( | 75 // See crbug.com/422489. |
| 76 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_, | 76 base::PostTaskWithTraitsAndReply( |
| 77 next_data_offset_), | 77 FROM_HERE, base::TaskTraits().WithShutdownBehavior( |
| 78 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), |
| 79 base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_, |
| 80 next_data_offset_), |
| 78 base::Bind(&URLRequestSimpleJob::ReadRawDataComplete, | 81 base::Bind(&URLRequestSimpleJob::ReadRawDataComplete, |
| 79 weak_factory_.GetWeakPtr(), buf_size)); | 82 weak_factory_.GetWeakPtr(), buf_size)); |
| 80 next_data_offset_ += buf_size; | 83 next_data_offset_ += buf_size; |
| 81 return ERR_IO_PENDING; | 84 return ERR_IO_PENDING; |
| 82 } | 85 } |
| 83 | 86 |
| 84 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const { | |
| 85 return task_runner_.get(); | |
| 86 } | |
| 87 | |
| 88 int URLRequestSimpleJob::GetData(std::string* mime_type, | 87 int URLRequestSimpleJob::GetData(std::string* mime_type, |
| 89 std::string* charset, | 88 std::string* charset, |
| 90 std::string* data, | 89 std::string* data, |
| 91 const CompletionCallback& callback) const { | 90 const CompletionCallback& callback) const { |
| 92 NOTREACHED(); | 91 NOTREACHED(); |
| 93 return ERR_UNEXPECTED; | 92 return ERR_UNEXPECTED; |
| 94 } | 93 } |
| 95 | 94 |
| 96 int URLRequestSimpleJob::GetRefCountedData( | 95 int URLRequestSimpleJob::GetRefCountedData( |
| 97 std::string* mime_type, | 96 std::string* mime_type, |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 next_data_offset_ = byte_range_.first_byte_position(); | 137 next_data_offset_ = byte_range_.first_byte_position(); |
| 139 set_expected_content_size(byte_range_.last_byte_position() - | 138 set_expected_content_size(byte_range_.last_byte_position() - |
| 140 next_data_offset_ + 1); | 139 next_data_offset_ + 1); |
| 141 NotifyHeadersComplete(); | 140 NotifyHeadersComplete(); |
| 142 } else { | 141 } else { |
| 143 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); | 142 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 144 } | 143 } |
| 145 } | 144 } |
| 146 | 145 |
| 147 } // namespace net | 146 } // namespace net |
| OLD | NEW |