Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / url_request / url_request_simple_job.cc
blob2fdbef4049ad0db86e25562b041ace700c20a0f5
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.
5 #include "net/url_request/url_request_simple_job.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "base/location.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/threading/worker_pool.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/net_errors.h"
18 #include "net/http/http_request_headers.h"
19 #include "net/http/http_util.h"
20 #include "net/url_request/url_request_status.h"
22 namespace net {
24 namespace {
26 void CopyData(const scoped_refptr<IOBuffer>& buf,
27 int buf_size,
28 const scoped_refptr<base::RefCountedMemory>& data,
29 int64 data_offset) {
30 memcpy(buf->data(), data->front() + data_offset, buf_size);
33 } // namespace
35 URLRequestSimpleJob::URLRequestSimpleJob(URLRequest* request,
36 NetworkDelegate* network_delegate)
37 : URLRangeRequestJob(request, network_delegate),
38 next_data_offset_(0),
39 task_runner_(base::WorkerPool::GetTaskRunner(false)),
40 weak_factory_(this) {
43 void URLRequestSimpleJob::Start() {
44 // Start reading asynchronously so that all error reporting and data
45 // callbacks happen as they would for network requests.
46 base::ThreadTaskRunnerHandle::Get()->PostTask(
47 FROM_HERE,
48 base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr()));
51 void URLRequestSimpleJob::Kill() {
52 weak_factory_.InvalidateWeakPtrs();
53 URLRangeRequestJob::Kill();
56 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
57 *mime_type = mime_type_;
58 return true;
61 bool URLRequestSimpleJob::GetCharset(std::string* charset) {
62 *charset = charset_;
63 return true;
66 URLRequestSimpleJob::~URLRequestSimpleJob() {}
68 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
69 int* bytes_read) {
70 DCHECK(bytes_read);
71 buf_size = static_cast<int>(
72 std::min(static_cast<int64>(buf_size),
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;
80 // Do memory copy on a background thread. See crbug.com/422489.
81 GetTaskRunner()->PostTaskAndReply(
82 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_,
83 next_data_offset_),
84 base::Bind(&URLRequestSimpleJob::OnReadCompleted,
85 weak_factory_.GetWeakPtr(), buf_size));
86 next_data_offset_ += buf_size;
87 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
88 return false;
91 void URLRequestSimpleJob::OnReadCompleted(int bytes_read) {
92 SetStatus(URLRequestStatus());
93 NotifyReadComplete(bytes_read);
96 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const {
97 return task_runner_.get();
100 int URLRequestSimpleJob::GetData(std::string* mime_type,
101 std::string* charset,
102 std::string* data,
103 const CompletionCallback& callback) const {
104 NOTREACHED();
105 return ERR_UNEXPECTED;
108 int URLRequestSimpleJob::GetRefCountedData(
109 std::string* mime_type,
110 std::string* charset,
111 scoped_refptr<base::RefCountedMemory>* data,
112 const CompletionCallback& callback) const {
113 scoped_refptr<base::RefCountedString> str_data(new base::RefCountedString());
114 int result = GetData(mime_type, charset, &str_data->data(), callback);
115 *data = str_data;
116 return result;
119 void URLRequestSimpleJob::StartAsync() {
120 if (!request_)
121 return;
123 if (ranges().size() > 1) {
124 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
125 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
126 return;
129 if (!ranges().empty() && range_parse_result() == OK)
130 byte_range_ = ranges().front();
132 const int result =
133 GetRefCountedData(&mime_type_, &charset_, &data_,
134 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
135 weak_factory_.GetWeakPtr()));
137 if (result != ERR_IO_PENDING)
138 OnGetDataCompleted(result);
141 void URLRequestSimpleJob::OnGetDataCompleted(int result) {
142 if (result == OK) {
143 // Notify that the headers are complete
144 if (!byte_range_.ComputeBounds(data_->size())) {
145 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
146 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
147 return;
150 next_data_offset_ = byte_range_.first_byte_position();
151 set_expected_content_size(byte_range_.last_byte_position() -
152 next_data_offset_ + 1);
153 NotifyHeadersComplete();
154 } else {
155 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
159 } // namespace net