[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / url_request / url_request_simple_job.cc
blob2f2fb875a2c7668f827d5e2e98436e344bd2951b
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 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
52 *mime_type = mime_type_;
53 return true;
56 bool URLRequestSimpleJob::GetCharset(std::string* charset) {
57 *charset = charset_;
58 return true;
61 URLRequestSimpleJob::~URLRequestSimpleJob() {}
63 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
64 int* bytes_read) {
65 DCHECK(bytes_read);
66 buf_size = static_cast<int>(
67 std::min(static_cast<int64>(buf_size),
68 byte_range_.last_byte_position() - next_data_offset_ + 1));
69 DCHECK_GE(buf_size, 0);
70 if (buf_size == 0) {
71 *bytes_read = 0;
72 return true;
75 // Do memory copy on a background thread. See crbug.com/422489.
76 GetTaskRunner()->PostTaskAndReply(
77 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_,
78 next_data_offset_),
79 base::Bind(&URLRequestSimpleJob::OnReadCompleted,
80 weak_factory_.GetWeakPtr(), buf_size));
81 next_data_offset_ += buf_size;
82 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
83 return false;
86 void URLRequestSimpleJob::OnReadCompleted(int bytes_read) {
87 SetStatus(URLRequestStatus());
88 NotifyReadComplete(bytes_read);
91 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const {
92 return task_runner_.get();
95 int URLRequestSimpleJob::GetData(std::string* mime_type,
96 std::string* charset,
97 std::string* data,
98 const CompletionCallback& callback) const {
99 NOTREACHED();
100 return ERR_UNEXPECTED;
103 int URLRequestSimpleJob::GetRefCountedData(
104 std::string* mime_type,
105 std::string* charset,
106 scoped_refptr<base::RefCountedMemory>* data,
107 const CompletionCallback& callback) const {
108 scoped_refptr<base::RefCountedString> str_data(new base::RefCountedString());
109 int result = GetData(mime_type, charset, &str_data->data(), callback);
110 *data = str_data;
111 return result;
114 void URLRequestSimpleJob::StartAsync() {
115 if (!request_)
116 return;
118 if (ranges().size() > 1) {
119 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
120 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
121 return;
124 if (!ranges().empty() && range_parse_result() == OK)
125 byte_range_ = ranges().front();
127 const int result =
128 GetRefCountedData(&mime_type_, &charset_, &data_,
129 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
130 weak_factory_.GetWeakPtr()));
132 if (result != ERR_IO_PENDING)
133 OnGetDataCompleted(result);
136 void URLRequestSimpleJob::OnGetDataCompleted(int result) {
137 if (result == OK) {
138 // Notify that the headers are complete
139 if (!byte_range_.ComputeBounds(data_->size())) {
140 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
141 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
142 return;
145 next_data_offset_ = byte_range_.first_byte_position();
146 set_expected_content_size(byte_range_.last_byte_position() -
147 next_data_offset_ + 1);
148 NotifyHeadersComplete();
149 } else {
150 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
154 } // namespace net