OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_simple_job.h" | 5 #include "net/url_request/url_request_simple_job.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/memory/ref_counted_memory.h" | |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/profiler/scoped_tracker.h" | 13 #include "base/profiler/scoped_tracker.h" |
13 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
15 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
16 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
17 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
(...skipping 22 matching lines...) Expand all Loading... | |
43 } | 44 } |
44 | 45 |
45 URLRequestSimpleJob::~URLRequestSimpleJob() {} | 46 URLRequestSimpleJob::~URLRequestSimpleJob() {} |
46 | 47 |
47 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, | 48 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size, |
48 int* bytes_read) { | 49 int* bytes_read) { |
49 DCHECK(bytes_read); | 50 DCHECK(bytes_read); |
50 int remaining = byte_range_.last_byte_position() - data_offset_ + 1; | 51 int remaining = byte_range_.last_byte_position() - data_offset_ + 1; |
51 if (buf_size > remaining) | 52 if (buf_size > remaining) |
52 buf_size = remaining; | 53 buf_size = remaining; |
53 memcpy(buf->data(), data_.data() + data_offset_, buf_size); | 54 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
54 data_offset_ += buf_size; | 55 data_offset_ += buf_size; |
55 *bytes_read = buf_size; | 56 *bytes_read = buf_size; |
56 return true; | 57 return true; |
57 } | 58 } |
58 | 59 |
60 int URLRequestSimpleJob::GetData(std::string* mime_type, | |
61 std::string* charset, | |
62 std::string* data, | |
63 const CompletionCallback& callback) const { | |
64 NOTREACHED(); | |
65 return ERR_UNEXPECTED; | |
66 } | |
67 | |
68 int URLRequestSimpleJob::GetRefCountedData( | |
69 std::string* mime_type, | |
70 std::string* charset, | |
71 scoped_refptr<base::RefCountedMemory>* data, | |
72 const CompletionCallback& callback) const { | |
73 scoped_refptr<base::RefCountedString> str_data(new base::RefCountedString()); | |
mmenke
2014/11/17 15:54:10
Do we get anything from using a temporary here?
Jeffrey Yasskin
2014/11/17 17:16:23
Yeah, I have to use the subclass's member function
| |
74 int result = GetData(mime_type, charset, &str_data->data(), callback); | |
75 *data = str_data; | |
76 return result; | |
77 } | |
78 | |
59 void URLRequestSimpleJob::StartAsync() { | 79 void URLRequestSimpleJob::StartAsync() { |
60 if (!request_) | 80 if (!request_) |
61 return; | 81 return; |
62 | 82 |
63 if (ranges().size() > 1) { | 83 if (ranges().size() > 1) { |
64 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. | 84 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. |
65 tracked_objects::ScopedTracker tracking_profile( | 85 tracked_objects::ScopedTracker tracking_profile( |
66 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 86 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
67 "422489 URLRequestSimpleJob::StartAsync 1")); | 87 "422489 URLRequestSimpleJob::StartAsync 1")); |
68 | 88 |
69 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 89 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
70 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 90 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
71 return; | 91 return; |
72 } | 92 } |
73 | 93 |
74 if (!ranges().empty() && range_parse_result() == OK) | 94 if (!ranges().empty() && range_parse_result() == OK) |
75 byte_range_ = ranges().front(); | 95 byte_range_ = ranges().front(); |
76 | 96 |
77 int result; | 97 int result; |
78 { | 98 { |
79 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. | 99 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. |
80 // Remove the block and assign 'result' in its declaration. | 100 // Remove the block and assign 'result' in its declaration. |
81 tracked_objects::ScopedTracker tracking_profile( | 101 tracked_objects::ScopedTracker tracking_profile( |
82 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 102 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
83 "422489 URLRequestSimpleJob::StartAsync 2")); | 103 "422489 URLRequestSimpleJob::StartAsync 2")); |
84 | 104 |
85 result = GetData(&mime_type_, | 105 result = |
86 &charset_, | 106 GetRefCountedData(&mime_type_, &charset_, &data_, |
87 &data_, | 107 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, |
88 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, | 108 weak_factory_.GetWeakPtr())); |
89 weak_factory_.GetWeakPtr())); | |
90 } | 109 } |
91 | 110 |
92 if (result != ERR_IO_PENDING) { | 111 if (result != ERR_IO_PENDING) { |
93 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. | 112 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. |
94 tracked_objects::ScopedTracker tracking_profile( | 113 tracked_objects::ScopedTracker tracking_profile( |
95 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 114 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
96 "422489 URLRequestSimpleJob::StartAsync 3")); | 115 "422489 URLRequestSimpleJob::StartAsync 3")); |
97 | 116 |
98 OnGetDataCompleted(result); | 117 OnGetDataCompleted(result); |
99 } | 118 } |
100 } | 119 } |
101 | 120 |
102 void URLRequestSimpleJob::OnGetDataCompleted(int result) { | 121 void URLRequestSimpleJob::OnGetDataCompleted(int result) { |
103 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. | 122 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed. |
104 tracked_objects::ScopedTracker tracking_profile( | 123 tracked_objects::ScopedTracker tracking_profile( |
105 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 124 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
106 "422489 URLRequestSimpleJob::OnGetDataCompleted")); | 125 "422489 URLRequestSimpleJob::OnGetDataCompleted")); |
107 | 126 |
108 if (result == OK) { | 127 if (result == OK) { |
109 // Notify that the headers are complete | 128 // Notify that the headers are complete |
110 if (!byte_range_.ComputeBounds(data_.size())) { | 129 if (!byte_range_.ComputeBounds(data_->size())) { |
111 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 130 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
112 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 131 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
113 return; | 132 return; |
114 } | 133 } |
115 | 134 |
116 data_offset_ = byte_range_.first_byte_position(); | 135 data_offset_ = byte_range_.first_byte_position(); |
117 int remaining_bytes = byte_range_.last_byte_position() - | 136 int remaining_bytes = byte_range_.last_byte_position() - |
118 byte_range_.first_byte_position() + 1; | 137 byte_range_.first_byte_position() + 1; |
119 set_expected_content_size(remaining_bytes); | 138 set_expected_content_size(remaining_bytes); |
120 NotifyHeadersComplete(); | 139 NotifyHeadersComplete(); |
121 } else { | 140 } else { |
122 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); | 141 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
123 } | 142 } |
124 } | 143 } |
125 | 144 |
126 } // namespace net | 145 } // namespace net |
OLD | NEW |