OLD | NEW |
| (Empty) |
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|. | |
7 | |
8 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_ | |
9 #define NET_TEST_URL_REQUEST_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_ | |
10 | |
11 #include <set> | |
12 #include <string> | |
13 | |
14 #include "base/lazy_instance.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "net/url_request/url_request_job.h" | |
17 | |
18 namespace net { | |
19 | |
20 class URLRequestSlowDownloadJob : public URLRequestJob { | |
21 public: | |
22 // Test URLs. | |
23 static const char kUnknownSizeUrl[]; | |
24 static const char kKnownSizeUrl[]; | |
25 static const char kFinishDownloadUrl[]; | |
26 static const char kErrorDownloadUrl[]; | |
27 | |
28 // Download sizes. | |
29 static const int kFirstDownloadSize; | |
30 static const int kSecondDownloadSize; | |
31 | |
32 // Timer callback, used to check to see if we should finish our download and | |
33 // send the second chunk. | |
34 void CheckDoneStatus(); | |
35 | |
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; | |
41 | |
42 static URLRequestJob* Factory(URLRequest* request, | |
43 NetworkDelegate* network_delegate, | |
44 const std::string& scheme); | |
45 | |
46 // Returns the current number of URLRequestSlowDownloadJobs that have | |
47 // not yet completed. | |
48 static size_t NumberOutstandingRequests(); | |
49 | |
50 // Adds the testing URLs to the URLRequestFilter. | |
51 static void AddUrlHandler(); | |
52 | |
53 private: | |
54 URLRequestSlowDownloadJob(URLRequest* request, | |
55 NetworkDelegate* network_delegate); | |
56 ~URLRequestSlowDownloadJob() override; | |
57 | |
58 // Enum indicating where we are in the read after a call to | |
59 // FillBufferHelper. | |
60 enum ReadStatus { | |
61 // The buffer was filled with data and may be returned. | |
62 BUFFER_FILLED, | |
63 | |
64 // No data was added to the buffer because kFinishDownloadUrl has | |
65 // not yet been seen and we've already returned the first chunk. | |
66 REQUEST_BLOCKED, | |
67 | |
68 // No data was added to the buffer because we've already returned | |
69 // all the data. | |
70 REQUEST_COMPLETE | |
71 }; | |
72 ReadStatus FillBufferHelper(IOBuffer* buf, int buf_size, int* bytes_written); | |
73 | |
74 void GetResponseInfoConst(HttpResponseInfo* info) const; | |
75 | |
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_; | |
82 | |
83 void StartAsync(); | |
84 | |
85 void set_should_finish_download() { should_finish_download_ = true; } | |
86 void set_should_error_download() { should_error_download_ = true; } | |
87 | |
88 int bytes_already_sent_; | |
89 bool should_error_download_; | |
90 bool should_finish_download_; | |
91 scoped_refptr<IOBuffer> buffer_; | |
92 int buffer_size_; | |
93 | |
94 base::WeakPtrFactory<URLRequestSlowDownloadJob> weak_factory_; | |
95 }; | |
96 | |
97 } // namespace content | |
98 | |
99 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_SLOW_DOWNLOAD_JOB_H_ | |
OLD | NEW |