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 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
,
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
51 static size_t NumberOutstandingRequests();
53 // Adds the testing URLs to the net::URLRequestFilter.
54 static void AddUrlHandler();
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
64 // The buffer was filled with data and may be returned.
67 // No data was added to the buffer because kFinishDownloadUrl has
68 // not yet been seen and we've already returned the first chunk.
71 // No data was added to the buffer because we've already returned
75 ReadStatus
FillBufferHelper(
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_
;
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_
;
100 base::WeakPtrFactory
<URLRequestSlowDownloadJob
> weak_factory_
;
103 } // namespace content
105 #endif // CONTENT_TEST_NET_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_