base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / content / test / net / url_request_slow_download_job.h
blob08b6dc08c1daff63de5cce8dce447724572f0f4e
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 void Start() override;
39 bool GetMimeType(std::string* mime_type) const override;
40 void GetResponseInfo(net::HttpResponseInfo* info) override;
41 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override;
43 static net::URLRequestJob* Factory(net::URLRequest* request,
44 net::NetworkDelegate* network_delegate,
45 const std::string& scheme);
47 // Returns the current number of URLRequestSlowDownloadJobs that have
48 // not yet completed.
49 static size_t NumberOutstandingRequests();
51 // Adds the testing URLs to the net::URLRequestFilter.
52 static void AddUrlHandler();
54 private:
55 URLRequestSlowDownloadJob(net::URLRequest* request,
56 net::NetworkDelegate* network_delegate);
57 ~URLRequestSlowDownloadJob() override;
59 // Enum indicating where we are in the read after a call to
60 // FillBufferHelper.
61 enum ReadStatus {
62 // The buffer was filled with data and may be returned.
63 BUFFER_FILLED,
65 // No data was added to the buffer because kFinishDownloadUrl has
66 // not yet been seen and we've already returned the first chunk.
67 REQUEST_BLOCKED,
69 // No data was added to the buffer because we've already returned
70 // all the data.
71 REQUEST_COMPLETE
73 ReadStatus FillBufferHelper(
74 net::IOBuffer* buf,
75 int buf_size,
76 int* bytes_written);
78 void GetResponseInfoConst(net::HttpResponseInfo* info) const;
80 // Mark all pending requests to be finished. We keep track of pending
81 // requests in |pending_requests_|.
82 static void FinishPendingRequests();
83 static void ErrorPendingRequests();
84 typedef std::set<URLRequestSlowDownloadJob*> SlowJobsSet;
85 static base::LazyInstance<SlowJobsSet>::Leaky pending_requests_;
87 void StartAsync();
89 void set_should_finish_download() { should_finish_download_ = true; }
90 void set_should_error_download() { should_error_download_ = true; }
92 int bytes_already_sent_;
93 bool should_error_download_;
94 bool should_finish_download_;
95 scoped_refptr<net::IOBuffer> buffer_;
96 int buffer_size_;
98 base::WeakPtrFactory<URLRequestSlowDownloadJob> weak_factory_;
101 } // namespace content
103 #endif // CONTENT_TEST_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_