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 // Returns the current number of URLRequestSlowDownloadJobs that have
44 static size_t NumberOutstandingRequests();
46 // Adds the testing URLs to the URLRequestFilter.
47 static void AddUrlHandler();
52 // Enum indicating where we are in the read after a call to
55 // The buffer was filled with data and may be returned.
58 // No data was added to the buffer because kFinishDownloadUrl has
59 // not yet been seen and we've already returned the first chunk.
62 // No data was added to the buffer because we've already returned
67 URLRequestSlowDownloadJob(URLRequest
* request
,
68 NetworkDelegate
* network_delegate
);
69 ~URLRequestSlowDownloadJob() override
;
71 ReadStatus
FillBufferHelper(IOBuffer
* buf
, int buf_size
, int* bytes_written
);
73 void GetResponseInfoConst(HttpResponseInfo
* info
) const;
75 // Mark all pending requests to be finished. We keep track of pending
76 // requests in |pending_requests_|.
77 static void FinishPendingRequests();
78 static void ErrorPendingRequests();
79 typedef std::set
<URLRequestSlowDownloadJob
*> SlowJobsSet
;
80 static base::LazyInstance
<SlowJobsSet
>::Leaky pending_requests_
;
84 void set_should_finish_download() { should_finish_download_
= true; }
85 void set_should_error_download() { should_error_download_
= true; }
87 int bytes_already_sent_
;
88 bool should_error_download_
;
89 bool should_finish_download_
;
90 scoped_refptr
<IOBuffer
> buffer_
;
93 base::WeakPtrFactory
<URLRequestSlowDownloadJob
> weak_factory_
;
96 } // namespace content
98 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_