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