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" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 return true; | 58 return true; |
59 } | 59 } |
60 | 60 |
61 bool URLRequestSimpleJob::GetCharset(std::string* charset) { | 61 bool URLRequestSimpleJob::GetCharset(std::string* charset) { |
62 *charset = charset_; | 62 *charset = charset_; |
63 return true; | 63 return true; |
64 } | 64 } |
65 | 65 |
66 URLRequestSimpleJob::~URLRequestSimpleJob() {} | 66 URLRequestSimpleJob::~URLRequestSimpleJob() {} |
67 | 67 |
68 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, | 68 int URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size) { |
69 int buf_size, | 69 buf_size = std::min(static_cast<int64>(buf_size), |
70 int* bytes_read) { | 70 byte_range_.last_byte_position() - next_data_offset_ + 1); |
71 DCHECK(bytes_read); | 71 if (buf_size == 0) |
72 buf_size = static_cast<int>( | 72 return 0; |
73 std::min(static_cast<int64>(buf_size), | |
74 byte_range_.last_byte_position() - next_data_offset_ + 1)); | |
75 DCHECK_GE(buf_size, 0); | |
76 if (buf_size == 0) { | |
77 *bytes_read = 0; | |
78 return true; | |
79 } | |
80 | 73 |
81 // Do memory copy on a background thread. See crbug.com/422489. | 74 // Do memory copy on a background thread. See crbug.com/422489. |
82 GetTaskRunner()->PostTaskAndReply( | 75 GetTaskRunner()->PostTaskAndReply( |
83 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_, | 76 FROM_HERE, base::Bind(&CopyData, make_scoped_refptr(buf), buf_size, data_, |
84 next_data_offset_), | 77 next_data_offset_), |
85 base::Bind(&URLRequestSimpleJob::OnReadCompleted, | 78 base::Bind(&URLRequestSimpleJob::ReadRawDataComplete, |
86 weak_factory_.GetWeakPtr(), buf_size)); | 79 weak_factory_.GetWeakPtr(), buf_size)); |
87 next_data_offset_ += buf_size; | 80 next_data_offset_ += buf_size; |
88 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 81 return ERR_IO_PENDING; |
89 return false; | |
90 } | |
91 | |
92 void URLRequestSimpleJob::OnReadCompleted(int bytes_read) { | |
93 SetStatus(URLRequestStatus()); | |
94 NotifyReadComplete(bytes_read); | |
95 } | 82 } |
96 | 83 |
97 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const { | 84 base::TaskRunner* URLRequestSimpleJob::GetTaskRunner() const { |
98 return task_runner_.get(); | 85 return task_runner_.get(); |
99 } | 86 } |
100 | 87 |
101 int URLRequestSimpleJob::GetData(std::string* mime_type, | 88 int URLRequestSimpleJob::GetData(std::string* mime_type, |
102 std::string* charset, | 89 std::string* charset, |
103 std::string* data, | 90 std::string* data, |
104 const CompletionCallback& callback) const { | 91 const CompletionCallback& callback) const { |
(...skipping 10 matching lines...) Expand all Loading... |
115 int result = GetData(mime_type, charset, &str_data->data(), callback); | 102 int result = GetData(mime_type, charset, &str_data->data(), callback); |
116 *data = str_data; | 103 *data = str_data; |
117 return result; | 104 return result; |
118 } | 105 } |
119 | 106 |
120 void URLRequestSimpleJob::StartAsync() { | 107 void URLRequestSimpleJob::StartAsync() { |
121 if (!request_) | 108 if (!request_) |
122 return; | 109 return; |
123 | 110 |
124 if (ranges().size() > 1) { | 111 if (ranges().size() > 1) { |
125 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 112 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
126 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 113 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
127 return; | 114 return; |
128 } | 115 } |
129 | 116 |
130 if (!ranges().empty() && range_parse_result() == OK) | 117 if (!ranges().empty() && range_parse_result() == OK) |
131 byte_range_ = ranges().front(); | 118 byte_range_ = ranges().front(); |
132 | 119 |
133 const int result = | 120 const int result = |
134 GetRefCountedData(&mime_type_, &charset_, &data_, | 121 GetRefCountedData(&mime_type_, &charset_, &data_, |
135 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, | 122 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted, |
136 weak_factory_.GetWeakPtr())); | 123 weak_factory_.GetWeakPtr())); |
137 | 124 |
138 if (result != ERR_IO_PENDING) | 125 if (result != ERR_IO_PENDING) |
139 OnGetDataCompleted(result); | 126 OnGetDataCompleted(result); |
140 } | 127 } |
141 | 128 |
142 void URLRequestSimpleJob::OnGetDataCompleted(int result) { | 129 void URLRequestSimpleJob::OnGetDataCompleted(int result) { |
143 if (result == OK) { | 130 if (result == OK) { |
144 // Notify that the headers are complete | 131 // Notify that the headers are complete |
145 if (!byte_range_.ComputeBounds(data_->size())) { | 132 if (!byte_range_.ComputeBounds(data_->size())) { |
146 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 133 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, |
147 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); | 134 ERR_REQUEST_RANGE_NOT_SATISFIABLE)); |
148 return; | 135 return; |
149 } | 136 } |
150 | 137 |
151 next_data_offset_ = byte_range_.first_byte_position(); | 138 next_data_offset_ = byte_range_.first_byte_position(); |
152 set_expected_content_size(byte_range_.last_byte_position() - | 139 set_expected_content_size(byte_range_.last_byte_position() - |
153 next_data_offset_ + 1); | 140 next_data_offset_ + 1); |
154 NotifyHeadersComplete(); | 141 NotifyHeadersComplete(); |
155 } else { | 142 } else { |
156 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); | 143 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
157 } | 144 } |
158 } | 145 } |
159 | 146 |
160 } // namespace net | 147 } // namespace net |
OLD | NEW |