Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / test / net / url_request_slow_download_job.h
blob02703b37cc86a7e8c9d0b683a38aecff72913520
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 // 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 N bytes, to be completed by sending a
7 // request to |kFinishDownloadUrl|.
9 #ifndef CONTENT_TEST_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_
10 #define CONTENT_TEST_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_
12 #include <set>
13 #include <string>
15 #include "base/lazy_instance.h"
16 #include "base/memory/weak_ptr.h"
17 #include "net/url_request/url_request_job.h"
19 namespace content {
21 class URLRequestSlowDownloadJob : public net::URLRequestJob {
22 public:
23 // Test URLs.
24 static const char kUnknownSizeUrl[];
25 static const char kKnownSizeUrl[];
26 static const char kFinishDownloadUrl[];
27 static const char kErrorDownloadUrl[];
29 // Download sizes.
30 static const int kFirstDownloadSize;
31 static const int kSecondDownloadSize;
33 // Timer callback, used to check to see if we should finish our download and
34 // send the second chunk.
35 void CheckDoneStatus();
37 // net::URLRequestJob methods
38 virtual void Start() OVERRIDE;
39 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
40 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE;
41 virtual bool ReadRawData(net::IOBuffer* buf,
42 int buf_size,
43 int *bytes_read) OVERRIDE;
45 static net::URLRequestJob* Factory(net::URLRequest* request,
46 net::NetworkDelegate* network_delegate,
47 const std::string& scheme);
49 // Returns the current number of URLRequestSlowDownloadJobs that have
50 // not yet completed.
51 static size_t NumberOutstandingRequests();
53 // Adds the testing URLs to the net::URLRequestFilter.
54 static void AddUrlHandler();
56 private:
57 URLRequestSlowDownloadJob(net::URLRequest* request,
58 net::NetworkDelegate* network_delegate);
59 virtual ~URLRequestSlowDownloadJob();
61 // Enum indicating where we are in the read after a call to
62 // FillBufferHelper.
63 enum ReadStatus {
64 // The buffer was filled with data and may be returned.
65 BUFFER_FILLED,
67 // No data was added to the buffer because kFinishDownloadUrl has
68 // not yet been seen and we've already returned the first chunk.
69 REQUEST_BLOCKED,
71 // No data was added to the buffer because we've already returned
72 // all the data.
73 REQUEST_COMPLETE
75 ReadStatus FillBufferHelper(
76 net::IOBuffer* buf,
77 int buf_size,
78 int* bytes_written);
80 void GetResponseInfoConst(net::HttpResponseInfo* info) const;
82 // Mark all pending requests to be finished. We keep track of pending
83 // requests in |pending_requests_|.
84 static void FinishPendingRequests();
85 static void ErrorPendingRequests();
86 typedef std::set<URLRequestSlowDownloadJob*> SlowJobsSet;
87 static base::LazyInstance<SlowJobsSet>::Leaky pending_requests_;
89 void StartAsync();
91 void set_should_finish_download() { should_finish_download_ = true; }
92 void set_should_error_download() { should_error_download_ = true; }
94 int bytes_already_sent_;
95 bool should_error_download_;
96 bool should_finish_download_;
97 scoped_refptr<net::IOBuffer> buffer_;
98 int buffer_size_;
100 base::WeakPtrFactory<URLRequestSlowDownloadJob> weak_factory_;
103 } // namespace content
105 #endif // CONTENT_TEST_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_