| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/url_request/url_request_simple_job.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/memory/ref_counted_memory.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/threading/worker_pool.h" | |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 #include "net/http/http_request_headers.h" | |
| 17 #include "net/http/http_util.h" | |
| 18 #include "net/url_request/url_request_status.h" | |
| 19 | |
| 20 namespace net { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 void CopyData(const scoped_refptr<IOBuffer>& buf, | |
| 25 int buf_size, | |
| 26 const scoped_refptr<base::RefCountedMemory>& data, | |
| 27 int64 data_offset) { | |
| 28 memcpy(buf->data(), data->front() + data_offset, buf_size); | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 URLRequestSimpleJob::URLRequestSimpleJob(URLRequest* request, | |
| 34 NetworkDelegate* network_delegate) | |
| 35 : URLRangeRequestJob(request, network_delegate), | |
| 36 next_data_offset_(0), | |
| 37 task_runner_(base::WorkerPool::GetTaskRunner(false)), | |
| 38 weak_factory_(this) { | |
| 39 } | |
| 40 | |
| 41 void URLRequestSimpleJob::Start() { | |
| 42 // Start reading asynchronously so that all error reporting and data | |
| 43 // callbacks happen as they would for network requests. | |
| 44 base::MessageLoop::current()->PostTask( | |
| 45 FROM_HERE, | |
| 46 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr())); | |
| 47 } | |
| 48 | |
| 49 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const { | |
| 50 *mime_type = mime_type_; | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 bool URLRequestSimpleJob::GetCharset(std::string* charset) { | |
| 55 *charset = charset_; | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 URLRequestSimpleJob::~URLRequestSimpleJob() {} | |
| 60 | |
| 61 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, | |
| 62 int* bytes_read) { | |
| 63 DCHECK(bytes_read); | |
| 64 buf_size = static_cast<int>( | |
| 65 std::min(static_cast<int64>(buf_size), | |
| 66 byte_range_.last_byte_position() - next_data_offset_ + 1)); | |
| 67 DCHECK_GE(buf_size, 0); | |
| 68 if (buf_size == 0) { | |
| 69 *bytes_read = 0; | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 73 // Do memory copy on a background thread. See crbug.com/422489. | |
| 74 GetTaskRunner()->PostTaskAndReply( | |
| 75 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_, | |
| 76 next_data_offset_), | |
| 77 base::Bind(&URLRequestSimpleJob::OnReadCompleted, | |
| 78 weak_factory_.GetWeakPtr(), buf_size)); | |
| 79 next_data_offset_ += buf_size; | |
| 80 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 void URLRequestSimpleJob::OnReadCompleted(int bytes_read) { | |
| 85 SetStatus(net::URLRequestStatus()); | |
| 86 NotifyReadComplete(bytes_read); | |
| 87 } | |
| 88 | |
| 89 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const { | |
| 90 return task_runner_.get(); | |
| 91 } | |
| 92 | |
| 93 int URLRequestSimpleJob::GetData(std::string* mime_type, | |
| 94 std::string* charset, | |
| 95 std::string* data, | |
| 96 const CompletionCallback& callback) const { | |
| 97 NOTREACHED(); | |
| 98 return ERR_UNEXPECTED; | |
| 99 } | |
| 100 | |
| 101 int URLRequestSimpleJob::GetRefCountedData( | |
| 102 std::string* mime_type, | |
| 103 std::string* charset, | |
| 104 scoped_refptr<base::RefCountedMemory>* data, | |
| 105 const CompletionCallback& callback) const { | |
| 106 scoped_refptr<base::RefCountedString> str_data(new base::RefCountedString()); | |
| 107 int result = GetData(mime_type, charset, &str_data->data(), callback); | |
| 108 *data = str_data; | |
| 109 return result; | |
| 110 } | |
| 111 | |
| 112 void URLRequestSimpleJob::StartAsync() { | |
| 113 if (!request_) | |
| 114 return; | |
| 115 | |
| 116 if (ranges().size() > 1) { | |
| 117 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | |
| 118 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 if (!ranges().empty() && range_parse_result() == OK) | |
| 123 byte_range_ = ranges().front(); | |
| 124 | |
| 125 const int result = | |
| 126 GetRefCountedData(&mime_type_, &charset_, &data_, | |
| 127 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, | |
| 128 weak_factory_.GetWeakPtr())); | |
| 129 | |
| 130 if (result != ERR_IO_PENDING) | |
| 131 OnGetDataCompleted(result); | |
| 132 } | |
| 133 | |
| 134 void URLRequestSimpleJob::OnGetDataCompleted(int result) { | |
| 135 if (result == OK) { | |
| 136 // Notify that the headers are complete | |
| 137 if (!byte_range_.ComputeBounds(data_->size())) { | |
| 138 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | |
| 139 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 next_data_offset_ = byte_range_.first_byte_position(); | |
| 144 set_expected_content_size(byte_range_.last_byte_position() - | |
| 145 next_data_offset_ + 1); | |
| 146 NotifyHeadersComplete(); | |
| 147 } else { | |
| 148 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 } // namespace net | |
| OLD | NEW |