1// Copyright (c) 2010 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// This class simulates a slow download.  This used in a UI test to test the
5// download manager.  Requests to |kUnknownSizeUrl| and |kKnownSizeUrl| start
6// downloads that pause after the first
7
8#ifndef CHROME_BROWSER_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_
9#define CHROME_BROWSER_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_
10#pragma once
11
12#include <string>
13#include <vector>
14
15#include "base/task.h"
16#include "net/url_request/url_request_job.h"
17
18class URLRequestSlowDownloadJob : public net::URLRequestJob {
19 public:
20  explicit URLRequestSlowDownloadJob(net::URLRequest* request);
21
22  // Timer callback, used to check to see if we should finish our download and
23  // send the second chunk.
24  void CheckDoneStatus();
25
26  // net::URLRequestJob methods
27  virtual void Start();
28  virtual bool GetMimeType(std::string* mime_type) const;
29  virtual void GetResponseInfo(net::HttpResponseInfo* info);
30  virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read);
31
32  static net::URLRequestJob* Factory(net::URLRequest* request,
33                                     const std::string& scheme);
34
35  // Test URLs.
36  static const char kUnknownSizeUrl[];
37  static const char kKnownSizeUrl[];
38  static const char kFinishDownloadUrl[];
39
40  // Adds the testing URLs to the net::URLRequestFilter.
41  static void AddUrlHandler();
42
43 private:
44  virtual ~URLRequestSlowDownloadJob();
45
46  void GetResponseInfoConst(net::HttpResponseInfo* info) const;
47
48  // Mark all pending requests to be finished.  We keep track of pending
49  // requests in |kPendingRequests|.
50  static void FinishPendingRequests();
51  static std::vector<URLRequestSlowDownloadJob*> kPendingRequests;
52
53  void StartAsync();
54
55  void set_should_finish_download() { should_finish_download_ = true; }
56
57  int first_download_size_remaining_;
58  bool should_finish_download_;
59  bool should_send_second_chunk_;
60
61  ScopedRunnableMethodFactory<URLRequestSlowDownloadJob> method_factory_;
62};
63
64#endif  // CHROME_BROWSER_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_
65