Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / test / url_request / url_request_slow_download_job.cc
blobf344feb1c4d8f69f22008c6ef9fee22cf70f0997
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/test/url_request/url_request_slow_download_job.h"
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h"
17 #include "net/http/http_response_headers.h"
18 #include "net/url_request/url_request.h"
19 #include "net/url_request/url_request_filter.h"
20 #include "net/url_request/url_request_interceptor.h"
21 #include "url/gurl.h"
23 namespace net {
25 const char URLRequestSlowDownloadJob::kUnknownSizeUrl[] =
26 "http://url.handled.by.slow.download/download-unknown-size";
27 const char URLRequestSlowDownloadJob::kKnownSizeUrl[] =
28 "http://url.handled.by.slow.download/download-known-size";
29 const char URLRequestSlowDownloadJob::kFinishDownloadUrl[] =
30 "http://url.handled.by.slow.download/download-finish";
31 const char URLRequestSlowDownloadJob::kErrorDownloadUrl[] =
32 "http://url.handled.by.slow.download/download-error";
34 const int URLRequestSlowDownloadJob::kFirstDownloadSize = 1024 * 35;
35 const int URLRequestSlowDownloadJob::kSecondDownloadSize = 1024 * 10;
37 class URLRequestSlowDownloadJob::Interceptor : public URLRequestInterceptor {
38 public:
39 Interceptor() {}
40 ~Interceptor() override {}
42 // URLRequestInterceptor implementation:
43 URLRequestJob* MaybeInterceptRequest(
44 URLRequest* request,
45 NetworkDelegate* network_delegate) const override {
46 URLRequestSlowDownloadJob* job =
47 new URLRequestSlowDownloadJob(request, network_delegate);
48 if (request->url().spec() != kFinishDownloadUrl &&
49 request->url().spec() != kErrorDownloadUrl) {
50 pending_requests_.Get().insert(job);
52 return job;
55 private:
56 DISALLOW_COPY_AND_ASSIGN(Interceptor);
59 // static
60 base::LazyInstance<URLRequestSlowDownloadJob::SlowJobsSet>::Leaky
61 URLRequestSlowDownloadJob::pending_requests_ = LAZY_INSTANCE_INITIALIZER;
63 void URLRequestSlowDownloadJob::Start() {
64 base::ThreadTaskRunnerHandle::Get()->PostTask(
65 FROM_HERE, base::Bind(&URLRequestSlowDownloadJob::StartAsync,
66 weak_factory_.GetWeakPtr()));
69 // static
70 void URLRequestSlowDownloadJob::AddUrlHandler() {
71 URLRequestFilter* filter = URLRequestFilter::GetInstance();
72 filter->AddUrlInterceptor(
73 GURL(kUnknownSizeUrl),
74 scoped_ptr<URLRequestInterceptor>(new Interceptor()));
75 filter->AddUrlInterceptor(
76 GURL(kKnownSizeUrl),
77 scoped_ptr<URLRequestInterceptor>(new Interceptor()));
78 filter->AddUrlInterceptor(
79 GURL(kFinishDownloadUrl),
80 scoped_ptr<URLRequestInterceptor>(new Interceptor()));
81 filter->AddUrlInterceptor(
82 GURL(kErrorDownloadUrl),
83 scoped_ptr<URLRequestInterceptor>(new Interceptor()));
86 // static
87 size_t URLRequestSlowDownloadJob::NumberOutstandingRequests() {
88 return pending_requests_.Get().size();
91 // static
92 void URLRequestSlowDownloadJob::FinishPendingRequests() {
93 typedef std::set<URLRequestSlowDownloadJob*> JobList;
94 for (JobList::iterator it = pending_requests_.Get().begin();
95 it != pending_requests_.Get().end(); ++it) {
96 (*it)->set_should_finish_download();
100 void URLRequestSlowDownloadJob::ErrorPendingRequests() {
101 typedef std::set<URLRequestSlowDownloadJob*> JobList;
102 for (JobList::iterator it = pending_requests_.Get().begin();
103 it != pending_requests_.Get().end(); ++it) {
104 (*it)->set_should_error_download();
108 URLRequestSlowDownloadJob::URLRequestSlowDownloadJob(
109 URLRequest* request,
110 NetworkDelegate* network_delegate)
111 : URLRequestJob(request, network_delegate),
112 bytes_already_sent_(0),
113 should_error_download_(false),
114 should_finish_download_(false),
115 buffer_size_(0),
116 weak_factory_(this) {
119 void URLRequestSlowDownloadJob::StartAsync() {
120 if (base::LowerCaseEqualsASCII(kFinishDownloadUrl,
121 request_->url().spec().c_str()))
122 URLRequestSlowDownloadJob::FinishPendingRequests();
123 if (base::LowerCaseEqualsASCII(kErrorDownloadUrl,
124 request_->url().spec().c_str()))
125 URLRequestSlowDownloadJob::ErrorPendingRequests();
127 NotifyHeadersComplete();
130 // ReadRawData and CheckDoneStatus together implement a state
131 // machine. ReadRawData may be called arbitrarily by the network stack.
132 // It responds by:
133 // * If there are bytes remaining in the first chunk, they are
134 // returned.
135 // [No bytes remaining in first chunk. ]
136 // * If should_finish_download_ is not set, it returns IO_PENDING,
137 // and starts calling CheckDoneStatus on a regular timer.
138 // [should_finish_download_ set.]
139 // * If there are bytes remaining in the second chunk, they are filled.
140 // * Otherwise, return *bytes_read = 0 to indicate end of request.
141 // CheckDoneStatus is called on a regular basis, in the specific
142 // case where we have transmitted all of the first chunk and none of the
143 // second. If should_finish_download_ becomes set, it will "complete"
144 // the ReadRawData call that spawned off the CheckDoneStatus() repeated call.
146 // FillBufferHelper is a helper function that does the actual work of figuring
147 // out where in the state machine we are and how we should fill the buffer.
148 // It returns an enum indicating the state of the read.
149 URLRequestSlowDownloadJob::ReadStatus
150 URLRequestSlowDownloadJob::FillBufferHelper(IOBuffer* buf,
151 int buf_size,
152 int* bytes_written) {
153 if (bytes_already_sent_ < kFirstDownloadSize) {
154 int bytes_to_write =
155 std::min(kFirstDownloadSize - bytes_already_sent_, buf_size);
156 for (int i = 0; i < bytes_to_write; ++i) {
157 buf->data()[i] = '*';
159 *bytes_written = bytes_to_write;
160 bytes_already_sent_ += bytes_to_write;
161 return BUFFER_FILLED;
164 if (!should_finish_download_)
165 return REQUEST_BLOCKED;
167 if (bytes_already_sent_ < kFirstDownloadSize + kSecondDownloadSize) {
168 int bytes_to_write =
169 std::min(kFirstDownloadSize + kSecondDownloadSize - bytes_already_sent_,
170 buf_size);
171 for (int i = 0; i < bytes_to_write; ++i) {
172 buf->data()[i] = '*';
174 *bytes_written = bytes_to_write;
175 bytes_already_sent_ += bytes_to_write;
176 return BUFFER_FILLED;
179 return REQUEST_COMPLETE;
182 bool URLRequestSlowDownloadJob::ReadRawData(IOBuffer* buf,
183 int buf_size,
184 int* bytes_read) {
185 if (base::LowerCaseEqualsASCII(kFinishDownloadUrl,
186 request_->url().spec().c_str()) ||
187 base::LowerCaseEqualsASCII(kErrorDownloadUrl,
188 request_->url().spec().c_str())) {
189 VLOG(10) << __FUNCTION__ << " called w/ kFinish/ErrorDownloadUrl.";
190 *bytes_read = 0;
191 return true;
194 VLOG(10) << __FUNCTION__ << " called at position " << bytes_already_sent_
195 << " in the stream.";
196 ReadStatus status = FillBufferHelper(buf, buf_size, bytes_read);
197 switch (status) {
198 case BUFFER_FILLED:
199 return true;
200 case REQUEST_BLOCKED:
201 buffer_ = buf;
202 buffer_size_ = buf_size;
203 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
204 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
205 FROM_HERE, base::Bind(&URLRequestSlowDownloadJob::CheckDoneStatus,
206 weak_factory_.GetWeakPtr()),
207 base::TimeDelta::FromMilliseconds(100));
208 return false;
209 case REQUEST_COMPLETE:
210 *bytes_read = 0;
211 return true;
213 NOTREACHED();
214 return true;
217 void URLRequestSlowDownloadJob::CheckDoneStatus() {
218 if (should_finish_download_) {
219 VLOG(10) << __FUNCTION__ << " called w/ should_finish_download_ set.";
220 DCHECK(NULL != buffer_.get());
221 int bytes_written = 0;
222 ReadStatus status =
223 FillBufferHelper(buffer_.get(), buffer_size_, &bytes_written);
224 DCHECK_EQ(BUFFER_FILLED, status);
225 buffer_ = NULL; // Release the reference.
226 SetStatus(URLRequestStatus());
227 NotifyReadComplete(bytes_written);
228 } else if (should_error_download_) {
229 VLOG(10) << __FUNCTION__ << " called w/ should_finish_ownload_ set.";
230 NotifyDone(
231 URLRequestStatus(URLRequestStatus::FAILED, ERR_CONNECTION_RESET));
232 } else {
233 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
234 FROM_HERE, base::Bind(&URLRequestSlowDownloadJob::CheckDoneStatus,
235 weak_factory_.GetWeakPtr()),
236 base::TimeDelta::FromMilliseconds(100));
240 // Public virtual version.
241 void URLRequestSlowDownloadJob::GetResponseInfo(HttpResponseInfo* info) {
242 // Forward to private const version.
243 GetResponseInfoConst(info);
246 URLRequestSlowDownloadJob::~URLRequestSlowDownloadJob() {
247 pending_requests_.Get().erase(this);
250 // Private const version.
251 void URLRequestSlowDownloadJob::GetResponseInfoConst(
252 HttpResponseInfo* info) const {
253 // Send back mock headers.
254 std::string raw_headers;
255 if (base::LowerCaseEqualsASCII(kFinishDownloadUrl,
256 request_->url().spec().c_str()) ||
257 base::LowerCaseEqualsASCII(kErrorDownloadUrl,
258 request_->url().spec().c_str())) {
259 raw_headers.append(
260 "HTTP/1.1 200 OK\n"
261 "Content-type: text/plain\n");
262 } else {
263 raw_headers.append(
264 "HTTP/1.1 200 OK\n"
265 "Content-type: application/octet-stream\n"
266 "Cache-Control: max-age=0\n");
268 if (base::LowerCaseEqualsASCII(kKnownSizeUrl,
269 request_->url().spec().c_str())) {
270 raw_headers.append(base::StringPrintf(
271 "Content-Length: %d\n", kFirstDownloadSize + kSecondDownloadSize));
275 // ParseRawHeaders expects \0 to end each header line.
276 base::ReplaceSubstringsAfterOffset(
277 &raw_headers, 0, "\n", base::StringPiece("\0", 1));
278 info->headers = new HttpResponseHeaders(raw_headers);
281 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const {
282 HttpResponseInfo info;
283 GetResponseInfoConst(&info);
284 return info.headers.get() && info.headers->GetMimeType(mime_type);
287 } // namespace net