[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ |
| 6 | #define NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 10 | #include <memory> |
[email protected] | 6a60c8c | 2011-11-11 05:27:26 | [diff] [blame] | 11 | #include <string> |
| 12 | |
[email protected] | c4c1b48 | 2011-07-22 17:24:26 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 17 | #include "base/strings/string16.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 18 | #include "base/time/time.h" |
bnc | 81c46c1f | 2016-10-04 16:25:59 | [diff] [blame] | 19 | #include "net/base/net_export.h" |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 20 | #include "net/proxy/proxy_script_fetcher.h" |
| 21 | #include "net/url_request/url_request.h" |
| 22 | |
| 23 | class GURL; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 24 | |
| 25 | namespace net { |
| 26 | |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 27 | class URLRequestContext; |
| 28 | |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 29 | // Implementation of ProxyScriptFetcher that downloads scripts using the |
| 30 | // specified request context. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 31 | class NET_EXPORT ProxyScriptFetcherImpl : public ProxyScriptFetcher, |
| 32 | public URLRequest::Delegate { |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 33 | public: |
| 34 | // Creates a ProxyScriptFetcher that issues requests through |
| 35 | // |url_request_context|. |url_request_context| must remain valid for the |
| 36 | // lifetime of ProxyScriptFetcherImpl. |
| 37 | // Note that while a request is in progress, we will be holding a reference |
| 38 | // to |url_request_context|. Be careful not to create cycles between the |
| 39 | // fetcher and the context; you can break such cycles by calling Cancel(). |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 40 | explicit ProxyScriptFetcherImpl(URLRequestContext* url_request_context); |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 41 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 42 | ~ProxyScriptFetcherImpl() override; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 43 | |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 44 | // Used by unit-tests to modify the default limits. |
| 45 | base::TimeDelta SetTimeoutConstraint(base::TimeDelta timeout); |
| 46 | size_t SetSizeConstraint(size_t size_bytes); |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 47 | |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 48 | void OnResponseCompleted(URLRequest* request, int net_error); |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 49 | |
| 50 | // ProxyScriptFetcher methods: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 51 | int Fetch(const GURL& url, |
| 52 | base::string16* text, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 53 | const CompletionCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 54 | void Cancel() override; |
| 55 | URLRequestContext* GetRequestContext() const override; |
mmenke | ed8d7e43 | 2017-05-03 16:48:33 | [diff] [blame] | 56 | void OnShutdown() override; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 57 | |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 58 | // URLRequest::Delegate methods: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 59 | void OnAuthRequired(URLRequest* request, |
| 60 | AuthChallengeInfo* auth_info) override; |
| 61 | void OnSSLCertificateError(URLRequest* request, |
| 62 | const SSLInfo& ssl_info, |
| 63 | bool is_hsts_ok) override; |
maksim.sisov | bf794e2 | 2016-09-13 09:20:34 | [diff] [blame] | 64 | void OnResponseStarted(URLRequest* request, int net_error) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 65 | void OnReadCompleted(URLRequest* request, int num_bytes) override; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 66 | |
| 67 | private: |
[email protected] | 7aefb15 | 2011-01-21 23:46:49 | [diff] [blame] | 68 | enum { kBufSize = 4096 }; |
| 69 | |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 70 | // Read more bytes from the response. |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 71 | void ReadBody(URLRequest* request); |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 72 | |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 73 | // Handles a response from Read(). Returns true if we should continue trying |
| 74 | // to read. |num_bytes| is 0 for EOF, and < 0 on errors. |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 75 | bool ConsumeBytesRead(URLRequest* request, int num_bytes); |
[email protected] | 096724916 | 2010-11-20 00:13:32 | [diff] [blame] | 76 | |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 77 | // Called once the request has completed to notify the caller of |
| 78 | // |response_code_| and |response_text_|. |
| 79 | void FetchCompleted(); |
| 80 | |
| 81 | // Clear out the state for the current request. |
| 82 | void ResetCurRequestState(); |
| 83 | |
| 84 | // Callback for time-out task of request with id |id|. |
| 85 | void OnTimeout(int id); |
| 86 | |
mmenke | ed8d7e43 | 2017-05-03 16:48:33 | [diff] [blame] | 87 | // The context used for making network requests. Set to nullptr by |
| 88 | // OnShutdown. |
| 89 | URLRequestContext* url_request_context_; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 90 | |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 91 | // Buffer that URLRequest writes into. |
[email protected] | da968bc | 2011-01-19 11:48:19 | [diff] [blame] | 92 | scoped_refptr<IOBuffer> buf_; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 93 | |
| 94 | // The next ID to use for |cur_request_| (monotonically increasing). |
| 95 | int next_id_; |
| 96 | |
| 97 | // The current (in progress) request, or NULL. |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 98 | std::unique_ptr<URLRequest> cur_request_; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 99 | |
| 100 | // State for current request (only valid when |cur_request_| is not NULL): |
| 101 | |
| 102 | // Unique ID for the current request. |
| 103 | int cur_request_id_; |
| 104 | |
| 105 | // Callback to invoke on completion of the fetch. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 106 | CompletionCallback callback_; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 107 | |
| 108 | // Holds the error condition that was hit on the current request, or OK. |
| 109 | int result_code_; |
| 110 | |
| 111 | // Holds the bytes read so far. Will not exceed |max_response_bytes|. |
| 112 | std::string bytes_read_so_far_; |
| 113 | |
| 114 | // This buffer is owned by the owner of |callback|, and will be filled with |
| 115 | // UTF16 response on completion. |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 116 | base::string16* result_text_; |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 117 | |
| 118 | // The maximum number of bytes to allow in responses. |
| 119 | size_t max_response_bytes_; |
| 120 | |
| 121 | // The maximum amount of time to wait for download to complete. |
| 122 | base::TimeDelta max_duration_; |
| 123 | |
cbentzel | 27d2e5e5 | 2015-07-10 17:39:56 | [diff] [blame] | 124 | // The time that the fetch started. |
cbentzel | e45ccba | 2015-08-21 18:23:14 | [diff] [blame] | 125 | base::TimeTicks fetch_start_time_; |
| 126 | |
| 127 | // The time that the first byte was received. |
| 128 | base::TimeTicks fetch_time_to_first_byte_; |
cbentzel | 27d2e5e5 | 2015-07-10 17:39:56 | [diff] [blame] | 129 | |
kulkarni.a | cd7b446 | 2014-08-28 07:41:34 | [diff] [blame] | 130 | // Factory for creating the time-out task. This takes care of revoking |
| 131 | // outstanding tasks when |this| is deleted. |
| 132 | base::WeakPtrFactory<ProxyScriptFetcherImpl> weak_factory_; |
| 133 | |
[email protected] | 8693361 | 2010-10-16 23:10:33 | [diff] [blame] | 134 | DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl); |
| 135 | }; |
| 136 | |
| 137 | } // namespace net |
| 138 | |
| 139 | #endif // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ |