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 bool URLRequestSimpleJob::GetMimeType(std::string
* mime_type
) const {
52 *mime_type
= mime_type_
;
56 bool URLRequestSimpleJob::GetCharset(std::string
* charset
) {
61 URLRequestSimpleJob::~URLRequestSimpleJob() {}
63 bool URLRequestSimpleJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
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);
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_
,
79 base::Bind(&URLRequestSimpleJob::OnReadCompleted
,
80 weak_factory_
.GetWeakPtr(), buf_size
));
81 next_data_offset_
+= buf_size
;
82 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
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
,
98 const CompletionCallback
& callback
) const {
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
);
114 void URLRequestSimpleJob::StartAsync() {
118 if (ranges().size() > 1) {
119 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
120 ERR_REQUEST_RANGE_NOT_SATISFIABLE
));
124 if (!ranges().empty() && range_parse_result() == OK
)
125 byte_range_
= ranges().front();
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
) {
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
));
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();
150 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED
, result
));