Add wfh@ to chrome/browser/resources/plugin_metadata OWNERS.
[chromium-blink-merge.git] / net / url_request / url_request_simple_job.cc
blob46d9c38a9440500710f9486db003d6bd06ea514c
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/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"
20 namespace net {
22 namespace {
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);
31 } // namespace
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) {
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()));
49 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
50 *mime_type = mime_type_;
51 return true;
54 bool URLRequestSimpleJob::GetCharset(std::string* charset) {
55 *charset = charset_;
56 return true;
59 URLRequestSimpleJob::~URLRequestSimpleJob() {}
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;
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;
84 void URLRequestSimpleJob::OnReadCompleted(int bytes_read) {
85 SetStatus(net::URLRequestStatus());
86 NotifyReadComplete(bytes_read);
89 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const {
90 return task_runner_.get();
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;
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;
112 void URLRequestSimpleJob::StartAsync() {
113 if (!request_)
114 return;
116 if (ranges().size() > 1) {
117 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
118 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
119 return;
122 if (!ranges().empty() && range_parse_result() == OK)
123 byte_range_ = ranges().front();
125 const int result =
126 GetRefCountedData(&mime_type_, &charset_, &data_,
127 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
128 weak_factory_.GetWeakPtr()));
130 if (result != ERR_IO_PENDING)
131 OnGetDataCompleted(result);
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;
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));
152 } // namespace net