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. Requests to |kUnknownSizeUrl| and
5 // |kKnownSizeUrl| start downloads that pause after the first N bytes, to be
6 // completed by sending a request to |kFinishDownloadUrl|.
8 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_
9 #define NET_TEST_URL_REQUEST_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_
14 #include "base/lazy_instance.h"
15 #include "base/memory/weak_ptr.h"
16 #include "net/url_request/url_request_job.h"
20 class URLRequestSlowDownloadJob
: public URLRequestJob
{
23 static const char kUnknownSizeUrl
[];
24 static const char kKnownSizeUrl
[];
25 static const char kFinishDownloadUrl
[];
26 static const char kErrorDownloadUrl
[];
29 static const int kFirstDownloadSize
;
30 static const int kSecondDownloadSize
;
32 // Timer callback, used to check to see if we should finish our download and
33 // send the second chunk.
34 void CheckDoneStatus();
36 // URLRequestJob methods
37 void Start() override
;
38 bool GetMimeType(std::string
* mime_type
) const override
;
39 void GetResponseInfo(HttpResponseInfo
* info
) override
;
40 bool ReadRawData(IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
42 static URLRequestJob
* Factory(URLRequest
* request
,
43 NetworkDelegate
* network_delegate
,
44 const std::string
& scheme
);
46 // Returns the current number of URLRequestSlowDownloadJobs that have
48 static size_t NumberOutstandingRequests();
50 // Adds the testing URLs to the URLRequestFilter.
51 static void AddUrlHandler();
54 URLRequestSlowDownloadJob(URLRequest
* request
,
55 NetworkDelegate
* network_delegate
);
56 ~URLRequestSlowDownloadJob() override
;
58 // Enum indicating where we are in the read after a call to
61 // The buffer was filled with data and may be returned.
64 // No data was added to the buffer because kFinishDownloadUrl has
65 // not yet been seen and we've already returned the first chunk.
68 // No data was added to the buffer because we've already returned
72 ReadStatus
FillBufferHelper(IOBuffer
* buf
, int buf_size
, int* bytes_written
);
74 void GetResponseInfoConst(HttpResponseInfo
* info
) const;
76 // Mark all pending requests to be finished. We keep track of pending
77 // requests in |pending_requests_|.
78 static void FinishPendingRequests();
79 static void ErrorPendingRequests();
80 typedef std::set
<URLRequestSlowDownloadJob
*> SlowJobsSet
;
81 static base::LazyInstance
<SlowJobsSet
>::Leaky pending_requests_
;
85 void set_should_finish_download() { should_finish_download_
= true; }
86 void set_should_error_download() { should_error_download_
= true; }
88 int bytes_already_sent_
;
89 bool should_error_download_
;
90 bool should_finish_download_
;
91 scoped_refptr
<IOBuffer
> buffer_
;
94 base::WeakPtrFactory
<URLRequestSlowDownloadJob
> weak_factory_
;
97 } // namespace content
99 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_