Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / net / url_request / url_request_test_job.h
blobfbdbe0fe06d3f0fcf1590f83fae8d0a976932e8e
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.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_
8 #include <string>
10 #include "base/memory/weak_ptr.h"
11 #include "net/base/load_timing_info.h"
12 #include "net/url_request/url_request.h"
13 #include "net/url_request/url_request_job.h"
14 #include "net/url_request/url_request_job_factory.h"
16 namespace net {
18 // This job type is designed to help with simple unit tests. To use, you
19 // probably want to inherit from it to set up the state you want. Then install
20 // it as the protocol handler for the "test" scheme.
22 // It will respond to several URLs, which you can retrieve using the test_url*
23 // getters, which will in turn respond with the corresponding responses returned
24 // by test_data*. Any other URLs that begin with "test:" will return an error,
25 // which might also be useful, you can use test_url_error() to retreive a
26 // standard one.
28 // You can override the known URLs or the response data by overriding Start().
30 // Optionally, you can also construct test jobs to return a headers and data
31 // provided to the contstructor in response to any request url.
33 // When a job is created, it gets put on a queue of pending test jobs. To
34 // process jobs on this queue, use ProcessOnePendingMessage, which will process
35 // one step of the next job. If the job is incomplete, it will be added to the
36 // end of the queue.
38 // Optionally, you can also construct test jobs that advance automatically
39 // without having to call ProcessOnePendingMessage.
40 class NET_EXPORT_PRIVATE URLRequestTestJob : public URLRequestJob {
41 public:
42 // Constructs a job to return one of the canned responses depending on the
43 // request url, with auto advance disabled.
44 URLRequestTestJob(URLRequest* request, NetworkDelegate* network_delegate);
46 // Constructs a job to return one of the canned responses depending on the
47 // request url, optionally with auto advance enabled.
48 URLRequestTestJob(URLRequest* request,
49 NetworkDelegate* network_delegate,
50 bool auto_advance);
52 // Constructs a job to return the given response regardless of the request
53 // url. The headers should include the HTTP status line and be formatted as
54 // expected by HttpResponseHeaders.
55 URLRequestTestJob(URLRequest* request,
56 net::NetworkDelegate* network_delegate,
57 const std::string& response_headers,
58 const std::string& response_data,
59 bool auto_advance);
61 // The canned URLs this handler will respond to without having been
62 // explicitly initialized with response headers and data.
63 // FIXME(brettw): we should probably also have a redirect one
64 static GURL test_url_1();
65 static GURL test_url_2();
66 static GURL test_url_3();
67 static GURL test_url_4();
68 static GURL test_url_error();
69 static GURL test_url_redirect_to_url_2();
71 // The data that corresponds to each of the URLs above
72 static std::string test_data_1();
73 static std::string test_data_2();
74 static std::string test_data_3();
75 static std::string test_data_4();
77 // The headers that correspond to each of the URLs above
78 static std::string test_headers();
80 // The headers for a redirect response
81 static std::string test_redirect_headers();
83 // The headers for a redirect response to the second test url.
84 static std::string test_redirect_to_url_2_headers();
86 // The headers for a server error response
87 static std::string test_error_headers();
89 // Processes one pending message from the stack, returning true if any
90 // message was processed, or false if there are no more pending request
91 // notifications to send. This is not applicable when using auto_advance.
92 static bool ProcessOnePendingMessage();
94 // With auto advance enabled, the job will advance thru the stages without
95 // the caller having to call ProcessOnePendingMessage. Auto advance depends
96 // on having a message loop running. The default is to not auto advance.
97 // Should not be altered after the job has started.
98 bool auto_advance() { return auto_advance_; }
99 void set_auto_advance(bool auto_advance) { auto_advance_ = auto_advance; }
101 void set_load_timing_info(const LoadTimingInfo& load_timing_info) {
102 load_timing_info_ = load_timing_info;
105 RequestPriority priority() const { return priority_; }
107 // Create a protocol handler for callers that don't subclass.
108 static URLRequestJobFactory::ProtocolHandler* CreateProtocolHandler();
110 // Job functions
111 void SetPriority(RequestPriority priority) override;
112 void Start() override;
113 bool ReadRawData(IOBuffer* buf, int buf_size, int* bytes_read) override;
114 void Kill() override;
115 bool GetMimeType(std::string* mime_type) const override;
116 void GetResponseInfo(HttpResponseInfo* info) override;
117 void GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
118 int GetResponseCode() const override;
119 bool IsRedirectResponse(GURL* location, int* http_status_code) override;
121 protected:
122 // Override to specify whether the next read done from this job will
123 // return IO pending. This controls whether or not the WAITING state will
124 // transition back to WAITING or to DATA_AVAILABLE after an asynchronous
125 // read is processed.
126 virtual bool NextReadAsync();
128 // This is what operation we are going to do next when this job is handled.
129 // When the stage is DONE, this job will not be put on the queue.
130 enum Stage { WAITING, DATA_AVAILABLE, ALL_DATA, DONE };
132 ~URLRequestTestJob() override;
134 // Call to process the next opeation, usually sending a notification, and
135 // advancing the stage if necessary. THIS MAY DELETE THE OBJECT.
136 void ProcessNextOperation();
138 // Call to move the job along to the next operation.
139 void AdvanceJob();
141 // Called via InvokeLater to cause callbacks to occur after Start() returns.
142 virtual void StartAsync();
144 bool auto_advance_;
146 Stage stage_;
148 RequestPriority priority_;
150 // The headers the job should return, will be set in Start() if not provided
151 // in the explicit ctor.
152 scoped_refptr<HttpResponseHeaders> response_headers_;
154 // The data to send, will be set in Start() if not provided in the explicit
155 // ctor.
156 std::string response_data_;
158 // current offset within response_data_
159 int offset_;
161 // Holds the buffer for an asynchronous ReadRawData call
162 IOBuffer* async_buf_;
163 int async_buf_size_;
165 LoadTimingInfo load_timing_info_;
167 base::WeakPtrFactory<URLRequestTestJob> weak_factory_;
170 } // namespace net
172 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_