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/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"
24 void CopyData(const scoped_refptr
<IOBuffer
>& buf
,
26 const scoped_refptr
<base::RefCountedMemory
>& data
,
28 memcpy(buf
->data(), data
->front() + data_offset
, buf_size
);
33 URLRequestSimpleJob::URLRequestSimpleJob(URLRequest
* request
,
34 NetworkDelegate
* network_delegate
)
35 : URLRangeRequestJob(request
, network_delegate
),
37 task_runner_(base::WorkerPool::GetTaskRunner(false)),
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(
46 base::Bind(&URLRequestSimpleJob::StartAsync
, weak_factory_
.GetWeakPtr()));
49 bool URLRequestSimpleJob::GetMimeType(std::string
* mime_type
) const {
50 *mime_type
= mime_type_
;
54 bool URLRequestSimpleJob::GetCharset(std::string
* charset
) {
59 URLRequestSimpleJob::~URLRequestSimpleJob() {}
61 bool URLRequestSimpleJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
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);
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_
,
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));
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
,
96 const CompletionCallback
& callback
) const {
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
);
112 void URLRequestSimpleJob::StartAsync() {
116 if (ranges().size() > 1) {
117 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
118 ERR_REQUEST_RANGE_NOT_SATISFIABLE
));
122 if (!ranges().empty() && range_parse_result() == OK
)
123 byte_range_
= ranges().front();
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
) {
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
));
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();
148 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED
, result
));