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"
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"
26 void CopyData(const scoped_refptr
<IOBuffer
>& buf
,
28 const scoped_refptr
<base::RefCountedMemory
>& data
,
30 memcpy(buf
->data(), data
->front() + data_offset
, buf_size
);
35 URLRequestSimpleJob::URLRequestSimpleJob(URLRequest
* request
,
36 NetworkDelegate
* network_delegate
)
37 : URLRangeRequestJob(request
, network_delegate
),
39 task_runner_(base::WorkerPool::GetTaskRunner(false)),
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(
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_
;
61 bool URLRequestSimpleJob::GetCharset(std::string
* charset
) {
66 URLRequestSimpleJob::~URLRequestSimpleJob() {}
68 bool URLRequestSimpleJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
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);
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_
,
84 base::Bind(&URLRequestSimpleJob::OnReadCompleted
,
85 weak_factory_
.GetWeakPtr(), buf_size
));
86 next_data_offset_
+= buf_size
;
87 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
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
,
103 const CompletionCallback
& callback
) const {
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
);
119 void URLRequestSimpleJob::StartAsync() {
123 if (ranges().size() > 1) {
124 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
125 ERR_REQUEST_RANGE_NOT_SATISFIABLE
));
129 if (!ranges().empty() && range_parse_result() == OK
)
130 byte_range_
= ranges().front();
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
) {
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
));
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();
155 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED
, result
));