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/profiler/scoped_tracker.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"
22 URLRequestSimpleJob::URLRequestSimpleJob(
23 URLRequest
* request
, NetworkDelegate
* network_delegate
)
24 : URLRangeRequestJob(request
, network_delegate
),
26 weak_factory_(this) {}
28 void URLRequestSimpleJob::Start() {
29 // Start reading asynchronously so that all error reporting and data
30 // callbacks happen as they would for network requests.
31 base::MessageLoop::current()->PostTask(
33 base::Bind(&URLRequestSimpleJob::StartAsync
, weak_factory_
.GetWeakPtr()));
36 bool URLRequestSimpleJob::GetMimeType(std::string
* mime_type
) const {
37 *mime_type
= mime_type_
;
41 bool URLRequestSimpleJob::GetCharset(std::string
* charset
) {
46 URLRequestSimpleJob::~URLRequestSimpleJob() {}
48 bool URLRequestSimpleJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
50 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
51 tracked_objects::ScopedTracker
tracking_profile(
52 FROM_HERE_WITH_EXPLICIT_FUNCTION(
53 "422489 URLRequestSimpleJob::ReadRawData"));
56 int remaining
= byte_range_
.last_byte_position() - data_offset_
+ 1;
57 if (buf_size
> remaining
)
59 memcpy(buf
->data(), data_
->front() + data_offset_
, buf_size
);
60 data_offset_
+= buf_size
;
61 *bytes_read
= buf_size
;
65 int URLRequestSimpleJob::GetData(std::string
* mime_type
,
68 const CompletionCallback
& callback
) const {
70 return ERR_UNEXPECTED
;
73 int URLRequestSimpleJob::GetRefCountedData(
74 std::string
* mime_type
,
76 scoped_refptr
<base::RefCountedMemory
>* data
,
77 const CompletionCallback
& callback
) const {
78 scoped_refptr
<base::RefCountedString
> str_data(new base::RefCountedString());
79 int result
= GetData(mime_type
, charset
, &str_data
->data(), callback
);
84 void URLRequestSimpleJob::StartAsync() {
88 if (ranges().size() > 1) {
89 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
90 tracked_objects::ScopedTracker
tracking_profile(
91 FROM_HERE_WITH_EXPLICIT_FUNCTION(
92 "422489 URLRequestSimpleJob::StartAsync 1"));
94 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
95 ERR_REQUEST_RANGE_NOT_SATISFIABLE
));
99 if (!ranges().empty() && range_parse_result() == OK
)
100 byte_range_
= ranges().front();
104 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
105 // Remove the block and assign 'result' in its declaration.
106 tracked_objects::ScopedTracker
tracking_profile(
107 FROM_HERE_WITH_EXPLICIT_FUNCTION(
108 "422489 URLRequestSimpleJob::StartAsync 2"));
111 GetRefCountedData(&mime_type_
, &charset_
, &data_
,
112 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted
,
113 weak_factory_
.GetWeakPtr()));
116 if (result
!= ERR_IO_PENDING
) {
117 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
118 tracked_objects::ScopedTracker
tracking_profile(
119 FROM_HERE_WITH_EXPLICIT_FUNCTION(
120 "422489 URLRequestSimpleJob::StartAsync 3"));
122 OnGetDataCompleted(result
);
126 void URLRequestSimpleJob::OnGetDataCompleted(int result
) {
127 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
128 tracked_objects::ScopedTracker
tracking_profile(
129 FROM_HERE_WITH_EXPLICIT_FUNCTION(
130 "422489 URLRequestSimpleJob::OnGetDataCompleted"));
133 // Notify that the headers are complete
134 if (!byte_range_
.ComputeBounds(data_
->size())) {
135 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
136 ERR_REQUEST_RANGE_NOT_SATISFIABLE
));
140 data_offset_
= byte_range_
.first_byte_position();
141 int remaining_bytes
= byte_range_
.last_byte_position() -
142 byte_range_
.first_byte_position() + 1;
143 set_expected_content_size(remaining_bytes
);
144 NotifyHeadersComplete();
146 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED
, result
));