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_
15 #include "base/lazy_instance.h"
16 #include "base/memory/weak_ptr.h"
17 #include "net/url_request/url_request_job.h"
21 class URLRequestSlowDownloadJob
: public net::URLRequestJob
{
24 static const char kUnknownSizeUrl
[];
25 static const char kKnownSizeUrl
[];
26 static const char kFinishDownloadUrl
[];
27 static const char kErrorDownloadUrl
[];
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
49 static size_t NumberOutstandingRequests();
51 // Adds the testing URLs to the net::URLRequestFilter.
52 static void AddUrlHandler();
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
62 // The buffer was filled with data and may be returned.
65 // No data was added to the buffer because kFinishDownloadUrl has
66 // not yet been seen and we've already returned the first chunk.
69 // No data was added to the buffer because we've already returned
73 ReadStatus
FillBufferHelper(
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_
;
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_
;
98 base::WeakPtrFactory
<URLRequestSlowDownloadJob
> weak_factory_
;
101 } // namespace content
103 #endif // CONTENT_TEST_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_