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