[email protected] | 67f92bc3 | 2012-01-26 01:56:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5 | #include "net/url_request/url_request_test_job.h" |
| 6 | |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <list> |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 9 | #include <memory> |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 10 | |
[email protected] | 098b2911 | 2011-12-20 21:12:34 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
Hans Wennborg | 1071fc2a | 2021-06-30 12:29:31 | [diff] [blame] | 13 | #include "base/containers/cxx20_erase_list.h" |
[email protected] | 44281e0 | 2011-11-09 22:26:04 | [diff] [blame] | 14 | #include "base/lazy_instance.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 15 | #include "base/location.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 17 | #include "base/task/single_thread_task_runner.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 18 | #include "base/threading/thread_task_runner_handle.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 19 | #include "base/time/time.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 20 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | #include "net/base/net_errors.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 22 | #include "net/http/http_response_headers.h" |
martijn | f96ddc73 | 2015-10-28 19:41:16 | [diff] [blame] | 23 | #include "net/http/http_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 24 | |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 25 | namespace net { |
| 26 | |
[email protected] | 44281e0 | 2011-11-09 22:26:04 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | typedef std::list<URLRequestTestJob*> URLRequestJobList; |
[email protected] | 67f92bc3 | 2012-01-26 01:56:19 | [diff] [blame] | 30 | base::LazyInstance<URLRequestJobList>::Leaky |
[email protected] | 6de0fd1d | 2011-11-15 13:31:49 | [diff] [blame] | 31 | g_pending_jobs = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 44281e0 | 2011-11-09 22:26:04 | [diff] [blame] | 32 | |
| 33 | } // namespace |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 34 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | // static getters for known URLs |
| 36 | GURL URLRequestTestJob::test_url_1() { |
| 37 | return GURL("test:url1"); |
| 38 | } |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 39 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 40 | GURL URLRequestTestJob::test_url_2() { |
| 41 | return GURL("test:url2"); |
| 42 | } |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 43 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 44 | GURL URLRequestTestJob::test_url_3() { |
| 45 | return GURL("test:url3"); |
| 46 | } |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 47 | |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 48 | GURL URLRequestTestJob::test_url_4() { |
| 49 | return GURL("test:url4"); |
| 50 | } |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 51 | |
| 52 | GURL URLRequestTestJob::test_url_auto_advance_async_reads_1() { |
| 53 | return GURL("test:url_auto_advance_async_reads_1"); |
| 54 | } |
| 55 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 56 | GURL URLRequestTestJob::test_url_error() { |
| 57 | return GURL("test:error"); |
| 58 | } |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 59 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 60 | GURL URLRequestTestJob::test_url_redirect_to_url_1() { |
| 61 | return GURL("test:redirect_to_1"); |
| 62 | } |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 63 | |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 64 | GURL URLRequestTestJob::test_url_redirect_to_url_2() { |
| 65 | return GURL("test:redirect_to_2"); |
| 66 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | |
| 68 | // static getters for known URL responses |
| 69 | std::string URLRequestTestJob::test_data_1() { |
| 70 | return std::string("<html><title>Test One</title></html>"); |
| 71 | } |
| 72 | std::string URLRequestTestJob::test_data_2() { |
| 73 | return std::string("<html><title>Test Two Two</title></html>"); |
| 74 | } |
| 75 | std::string URLRequestTestJob::test_data_3() { |
| 76 | return std::string("<html><title>Test Three Three Three</title></html>"); |
| 77 | } |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 78 | std::string URLRequestTestJob::test_data_4() { |
| 79 | return std::string("<html><title>Test Four Four Four Four</title></html>"); |
| 80 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 82 | // static getter for simple response headers |
| 83 | std::string URLRequestTestJob::test_headers() { |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 84 | static const char kHeaders[] = |
martijn | f96ddc73 | 2015-10-28 19:41:16 | [diff] [blame] | 85 | "HTTP/1.1 200 OK\n" |
| 86 | "Content-type: text/html\n" |
| 87 | "\n"; |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 88 | return std::string(kHeaders, std::size(kHeaders)); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // static getter for redirect response headers |
| 92 | std::string URLRequestTestJob::test_redirect_headers() { |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 93 | static const char kHeaders[] = |
martijn | f96ddc73 | 2015-10-28 19:41:16 | [diff] [blame] | 94 | "HTTP/1.1 302 MOVED\n" |
| 95 | "Location: somewhere\n" |
| 96 | "\n"; |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 97 | return std::string(kHeaders, std::size(kHeaders)); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 100 | // static getter for redirect response headers |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 101 | std::string URLRequestTestJob::test_redirect_to_url_1_headers() { |
| 102 | std::string headers = "HTTP/1.1 302 MOVED"; |
| 103 | headers.push_back('\n'); |
| 104 | headers += "Location: "; |
| 105 | headers += test_url_1().spec(); |
| 106 | headers.push_back('\n'); |
| 107 | headers.push_back('\n'); |
| 108 | return headers; |
| 109 | } |
| 110 | |
| 111 | // static getter for redirect response headers |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 112 | std::string URLRequestTestJob::test_redirect_to_url_2_headers() { |
| 113 | std::string headers = "HTTP/1.1 302 MOVED"; |
martijn | f96ddc73 | 2015-10-28 19:41:16 | [diff] [blame] | 114 | headers.push_back('\n'); |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 115 | headers += "Location: "; |
| 116 | headers += test_url_2().spec(); |
martijn | f96ddc73 | 2015-10-28 19:41:16 | [diff] [blame] | 117 | headers.push_back('\n'); |
| 118 | headers.push_back('\n'); |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 119 | return headers; |
| 120 | } |
| 121 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 122 | // static getter for error response headers |
| 123 | std::string URLRequestTestJob::test_error_headers() { |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 124 | static const char kHeaders[] = |
martijn | f96ddc73 | 2015-10-28 19:41:16 | [diff] [blame] | 125 | "HTTP/1.1 500 BOO HOO\n" |
| 126 | "\n"; |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 127 | return std::string(kHeaders, std::size(kHeaders)); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 128 | } |
| 129 | |
Matt Menke | fd97885 | 2020-09-15 16:00:57 | [diff] [blame] | 130 | URLRequestTestJob::URLRequestTestJob(URLRequest* request, bool auto_advance) |
| 131 | : URLRequestJob(request), |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 132 | auto_advance_(auto_advance), |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame^] | 133 | response_headers_length_(0) {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 134 | |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 135 | URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 136 | const std::string& response_headers, |
| 137 | const std::string& response_data, |
| 138 | bool auto_advance) |
Matt Menke | fd97885 | 2020-09-15 16:00:57 | [diff] [blame] | 139 | : URLRequestJob(request), |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 140 | auto_advance_(auto_advance), |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 141 | response_data_(response_data), |
David Benjamin | 1384c040 | 2019-04-29 18:55:52 | [diff] [blame] | 142 | response_headers_(base::MakeRefCounted<net::HttpResponseHeaders>( |
| 143 | net::HttpUtil::AssembleRawHeaders(response_headers))), |
Tsuyoshi Horo | 432981d5 | 2022-06-09 09:50:13 | [diff] [blame^] | 144 | response_headers_length_(response_headers.size()) {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 145 | |
| 146 | URLRequestTestJob::~URLRequestTestJob() { |
Eunseok Oh | 26ee5ee | 2018-09-08 03:06:08 | [diff] [blame] | 147 | base::Erase(g_pending_jobs.Get(), this); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 60c413c9 | 2009-03-09 16:53:31 | [diff] [blame] | 150 | bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 151 | DCHECK(mime_type); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 152 | if (!response_headers_.get()) |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 153 | return false; |
| 154 | return response_headers_->GetMimeType(mime_type); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 157 | void URLRequestTestJob::SetPriority(RequestPriority priority) { |
| 158 | priority_ = priority; |
| 159 | } |
| 160 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 161 | void URLRequestTestJob::Start() { |
| 162 | // Start reading asynchronously so that all error reporting and data |
| 163 | // callbacks happen as they would for network requests. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 164 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 165 | FROM_HERE, base::BindOnce(&URLRequestTestJob::StartAsync, |
| 166 | weak_factory_.GetWeakPtr())); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void URLRequestTestJob::StartAsync() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 170 | if (!response_headers_.get()) { |
ricea | 49369a3 | 2016-07-19 03:43:02 | [diff] [blame] | 171 | SetResponseHeaders(test_headers()); |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 172 | if (request_->url() == test_url_1()) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 173 | response_data_ = test_data_1(); |
| 174 | stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 175 | } else if (request_->url() == test_url_2()) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 176 | response_data_ = test_data_2(); |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 177 | } else if (request_->url() == test_url_3()) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 178 | response_data_ = test_data_3(); |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 179 | } else if (request_->url() == test_url_4()) { |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 180 | response_data_ = test_data_4(); |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 181 | } else if (request_->url() == test_url_auto_advance_async_reads_1()) { |
| 182 | response_data_ = test_data_1(); |
| 183 | stage_ = DATA_AVAILABLE; // Data is available immediately. |
| 184 | async_reads_ = true; // All reads complete asynchronously. |
| 185 | } else if (request_->url() == test_url_redirect_to_url_1()) { |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 186 | SetResponseHeaders(test_redirect_to_url_1_headers()); |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 187 | } else if (request_->url() == test_url_redirect_to_url_2()) { |
ricea | 49369a3 | 2016-07-19 03:43:02 | [diff] [blame] | 188 | SetResponseHeaders(test_redirect_to_url_2_headers()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 189 | } else { |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 190 | AdvanceJob(); |
| 191 | |
Matt Menke | 2b4d7c8b | 2020-04-27 23:35:07 | [diff] [blame] | 192 | // Return an error on unexpected urls. |
| 193 | NotifyStartError(ERR_INVALID_URL); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 194 | return; |
| 195 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 198 | AdvanceJob(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 199 | |
| 200 | this->NotifyHeadersComplete(); |
| 201 | } |
| 202 | |
ricea | 49369a3 | 2016-07-19 03:43:02 | [diff] [blame] | 203 | void URLRequestTestJob::SetResponseHeaders( |
| 204 | const std::string& response_headers) { |
David Benjamin | 1384c040 | 2019-04-29 18:55:52 | [diff] [blame] | 205 | response_headers_ = base::MakeRefCounted<HttpResponseHeaders>( |
| 206 | net::HttpUtil::AssembleRawHeaders(response_headers)); |
ricea | 49369a3 | 2016-07-19 03:43:02 | [diff] [blame] | 207 | response_headers_length_ = response_headers.size(); |
| 208 | } |
| 209 | |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 210 | int URLRequestTestJob::CopyDataForRead(IOBuffer* buf, int buf_size) { |
| 211 | int bytes_read = 0; |
| 212 | if (offset_ < static_cast<int>(response_data_.length())) { |
| 213 | bytes_read = buf_size; |
| 214 | if (bytes_read + offset_ > static_cast<int>(response_data_.length())) |
| 215 | bytes_read = static_cast<int>(response_data_.length()) - offset_; |
| 216 | |
| 217 | memcpy(buf->data(), &response_data_.c_str()[offset_], bytes_read); |
| 218 | offset_ += bytes_read; |
| 219 | } |
| 220 | return bytes_read; |
| 221 | } |
| 222 | |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 223 | int URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size) { |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 224 | if (stage_ == WAITING || async_reads_) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 225 | async_buf_ = buf; |
| 226 | async_buf_size_ = buf_size; |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 227 | if (stage_ != WAITING) { |
| 228 | stage_ = WAITING; |
| 229 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 230 | FROM_HERE, base::BindOnce(&URLRequestTestJob::ProcessNextOperation, |
| 231 | weak_factory_.GetWeakPtr())); |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 232 | } |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 233 | return ERR_IO_PENDING; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 234 | } |
| 235 | |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 236 | return CopyDataForRead(buf, buf_size); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 239 | void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 240 | if (response_headers_.get()) |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 241 | info->headers = response_headers_; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 244 | void URLRequestTestJob::GetLoadTimingInfo( |
| 245 | LoadTimingInfo* load_timing_info) const { |
| 246 | // Preserve the times the URLRequest is responsible for, but overwrite all |
| 247 | // the others. |
| 248 | base::TimeTicks request_start = load_timing_info->request_start; |
| 249 | base::Time request_start_time = load_timing_info->request_start_time; |
| 250 | *load_timing_info = load_timing_info_; |
| 251 | load_timing_info->request_start = request_start; |
| 252 | load_timing_info->request_start_time = request_start_time; |
| 253 | } |
| 254 | |
ricea | 49369a3 | 2016-07-19 03:43:02 | [diff] [blame] | 255 | int64_t URLRequestTestJob::GetTotalReceivedBytes() const { |
| 256 | return response_headers_length_ + offset_; |
| 257 | } |
| 258 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 259 | bool URLRequestTestJob::IsRedirectResponse(GURL* location, |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 260 | int* http_status_code, |
| 261 | bool* insecure_scheme_was_upgraded) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 262 | if (!response_headers_.get()) |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 263 | return false; |
| 264 | |
| 265 | std::string value; |
| 266 | if (!response_headers_->IsRedirect(&value)) |
| 267 | return false; |
| 268 | |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 269 | *insecure_scheme_was_upgraded = false; |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 270 | *location = request_->url().Resolve(value); |
| 271 | *http_status_code = response_headers_->response_code(); |
| 272 | return true; |
| 273 | } |
| 274 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 275 | void URLRequestTestJob::Kill() { |
[email protected] | 083b0a6 | 2009-02-06 01:13:22 | [diff] [blame] | 276 | stage_ = DONE; |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 277 | URLRequestJob::Kill(); |
[email protected] | 098b2911 | 2011-12-20 21:12:34 | [diff] [blame] | 278 | weak_factory_.InvalidateWeakPtrs(); |
Eunseok Oh | 26ee5ee | 2018-09-08 03:06:08 | [diff] [blame] | 279 | base::Erase(g_pending_jobs.Get(), this); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 280 | } |
| 281 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 282 | void URLRequestTestJob::ProcessNextOperation() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 283 | switch (stage_) { |
| 284 | case WAITING: |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 285 | // Must call AdvanceJob() prior to NotifyReadComplete() since that may |
| 286 | // delete |this|. |
| 287 | AdvanceJob(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 288 | stage_ = DATA_AVAILABLE; |
| 289 | // OK if ReadRawData wasn't called yet. |
| 290 | if (async_buf_) { |
mmenke | c8b5fe1 | 2016-12-09 18:12:04 | [diff] [blame] | 291 | int result = CopyDataForRead(async_buf_, async_buf_size_); |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 292 | if (result < 0) |
| 293 | NOTREACHED() << "Reads should not fail in DATA_AVAILABLE."; |
[email protected] | d7db4f62 | 2012-06-04 18:20:56 | [diff] [blame] | 294 | if (NextReadAsync()) { |
| 295 | // Make all future reads return io pending until the next |
| 296 | // ProcessNextOperation(). |
| 297 | stage_ = WAITING; |
| 298 | } |
xunjieli | 26ede96 | 2015-11-23 19:39:13 | [diff] [blame] | 299 | ReadRawDataComplete(result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 300 | } |
| 301 | break; |
| 302 | case DATA_AVAILABLE: |
[email protected] | b9c806b | 2011-04-30 00:44:48 | [diff] [blame] | 303 | AdvanceJob(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 304 | stage_ = ALL_DATA; // done sending data |
| 305 | break; |
| 306 | case ALL_DATA: |
| 307 | stage_ = DONE; |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 308 | return; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 309 | case DONE: |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 310 | return; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 311 | default: |
| 312 | NOTREACHED() << "Invalid stage"; |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 313 | return; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 314 | } |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 315 | } |
| 316 | |
[email protected] | d7db4f62 | 2012-06-04 18:20:56 | [diff] [blame] | 317 | bool URLRequestTestJob::NextReadAsync() { |
| 318 | return false; |
| 319 | } |
| 320 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 321 | void URLRequestTestJob::AdvanceJob() { |
| 322 | if (auto_advance_) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 323 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 324 | FROM_HERE, base::BindOnce(&URLRequestTestJob::ProcessNextOperation, |
| 325 | weak_factory_.GetWeakPtr())); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 326 | return; |
| 327 | } |
[email protected] | 44281e0 | 2011-11-09 22:26:04 | [diff] [blame] | 328 | g_pending_jobs.Get().push_back(this); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // static |
| 332 | bool URLRequestTestJob::ProcessOnePendingMessage() { |
[email protected] | 44281e0 | 2011-11-09 22:26:04 | [diff] [blame] | 333 | if (g_pending_jobs.Get().empty()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 334 | return false; |
| 335 | |
[email protected] | 44281e0 | 2011-11-09 22:26:04 | [diff] [blame] | 336 | URLRequestTestJob* next_job(g_pending_jobs.Get().front()); |
| 337 | g_pending_jobs.Get().pop_front(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 338 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 339 | DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q |
| 340 | next_job->ProcessNextOperation(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 341 | return true; |
| 342 | } |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 343 | |
| 344 | } // namespace net |