OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_PROXY_PROXY_SERVICE_H_ | 5 #ifndef NET_PROXY_PROXY_SERVICE_H_ |
6 #define NET_PROXY_PROXY_SERVICE_H_ | 6 #define NET_PROXY_PROXY_SERVICE_H_ |
7 | 7 |
8 #include <deque> | |
9 #include <string> | 8 #include <string> |
| 9 #include <vector> |
10 | 10 |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
12 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
| 13 // TODO(eroman): remove this unused header; other callers are depending on it! |
13 #include "base/thread.h" | 14 #include "base/thread.h" |
14 #include "base/waitable_event.h" | 15 #include "base/waitable_event.h" |
15 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
16 #include "net/proxy/proxy_server.h" | 17 #include "net/proxy/proxy_server.h" |
17 #include "net/proxy/proxy_info.h" | 18 #include "net/proxy/proxy_info.h" |
18 | 19 |
19 class GURL; | 20 class GURL; |
| 21 class MessageLoop; |
20 class URLRequestContext; | 22 class URLRequestContext; |
21 | 23 |
22 namespace net { | 24 namespace net { |
23 | 25 |
24 class ProxyScriptFetcher; | 26 class ProxyScriptFetcher; |
25 class ProxyConfigService; | 27 class ProxyConfigService; |
26 class ProxyResolver; | 28 class ProxyResolver; |
27 | 29 |
28 // This class can be used to resolve the proxy server to use when loading a | 30 // This class can be used to resolve the proxy server to use when loading a |
29 // HTTP(S) URL. It uses the given ProxyResolver to handle the actual proxy | 31 // HTTP(S) URL. It uses the given ProxyResolver to handle the actual proxy |
30 // resolution. See ProxyResolverWinHttp for example. | 32 // resolution. See ProxyResolverV8 for example. |
31 class ProxyService { | 33 class ProxyService { |
32 public: | 34 public: |
33 // The instance takes ownership of |config_service| and |resolver|. | 35 // The instance takes ownership of |config_service| and |resolver|. |
34 ProxyService(ProxyConfigService* config_service, | 36 ProxyService(ProxyConfigService* config_service, |
35 ProxyResolver* resolver); | 37 ProxyResolver* resolver); |
36 | 38 |
37 ~ProxyService(); | 39 ~ProxyService(); |
38 | 40 |
39 // Used internally to handle PAC queries. | 41 // Used internally to handle PAC queries. |
| 42 // TODO(eroman): consider naming this simply "Request". |
40 class PacRequest; | 43 class PacRequest; |
41 | 44 |
42 // Returns ERR_IO_PENDING if the proxy information could not be provided | 45 // Returns ERR_IO_PENDING if the proxy information could not be provided |
43 // synchronously, to indicate that the result will be available when the | 46 // synchronously, to indicate that the result will be available when the |
44 // callback is run. The callback is run on the thread that calls | 47 // callback is run. The callback is run on the thread that calls |
45 // ResolveProxy. | 48 // ResolveProxy. |
46 // | 49 // |
47 // The caller is responsible for ensuring that |results| and |callback| | 50 // The caller is responsible for ensuring that |results| and |callback| |
48 // remain valid until the callback is run or until |pac_request| is cancelled | 51 // remain valid until the callback is run or until |pac_request| is cancelled |
49 // via CancelPacRequest. |pac_request| is only valid while the completion | 52 // via CancelPacRequest. |pac_request| is only valid while the completion |
50 // callback is still pending. NULL can be passed for |pac_request| if | 53 // callback is still pending. NULL can be passed for |pac_request| if |
51 // the caller will not need to cancel the request. | 54 // the caller will not need to cancel the request. |
52 // | 55 // |
53 // We use the three possible proxy access types in the following order, and | 56 // We use the three possible proxy access types in the following order, and |
54 // we only use one of them (no falling back to other access types if the | 57 // we only use one of them (no falling back to other access types if the |
55 // chosen one doesn't work). | 58 // chosen one doesn't work). |
56 // 1. named proxy | 59 // 1. named proxy |
57 // 2. PAC URL | 60 // 2. PAC URL |
58 // 3. WPAD auto-detection | 61 // 3. WPAD auto-detection |
59 // | 62 // |
| 63 // TODO(eroman): see http://crbug.com/9985; the outline above is too simple. |
60 int ResolveProxy(const GURL& url, | 64 int ResolveProxy(const GURL& url, |
61 ProxyInfo* results, | 65 ProxyInfo* results, |
62 CompletionCallback* callback, | 66 CompletionCallback* callback, |
63 PacRequest** pac_request); | 67 PacRequest** pac_request); |
64 | 68 |
65 // This method is called after a failure to connect or resolve a host name. | 69 // This method is called after a failure to connect or resolve a host name. |
66 // It gives the proxy service an opportunity to reconsider the proxy to use. | 70 // It gives the proxy service an opportunity to reconsider the proxy to use. |
67 // The |results| parameter contains the results returned by an earlier call | 71 // The |results| parameter contains the results returned by an earlier call |
68 // to ResolveProxy. The semantics of this call are otherwise similar to | 72 // to ResolveProxy. The semantics of this call are otherwise similar to |
69 // ResolveProxy. | 73 // ResolveProxy. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // Convenience method that creates a proxy service using the | 123 // Convenience method that creates a proxy service using the |
120 // specified fixed settings. |pc| must not be NULL. | 124 // specified fixed settings. |pc| must not be NULL. |
121 static ProxyService* CreateFixed(const ProxyConfig& pc); | 125 static ProxyService* CreateFixed(const ProxyConfig& pc); |
122 | 126 |
123 // Creates a proxy service that always fails to fetch the proxy configuration, | 127 // Creates a proxy service that always fails to fetch the proxy configuration, |
124 // so it falls back to direct connect. | 128 // so it falls back to direct connect. |
125 static ProxyService* CreateNull(); | 129 static ProxyService* CreateNull(); |
126 | 130 |
127 private: | 131 private: |
128 friend class PacRequest; | 132 friend class PacRequest; |
| 133 // TODO(eroman): change this to a std::set. Note that this requires updating |
| 134 // some tests in proxy_service_unittest.cc such as: |
| 135 // ProxyServiceTest.InitialPACScriptDownload |
| 136 // which expects requests to finish in the order they were added. |
| 137 typedef std::vector<scoped_refptr<PacRequest> > PendingRequests; |
129 | 138 |
130 // Creates a config service appropriate for this platform that fetches the | 139 // Creates a config service appropriate for this platform that fetches the |
131 // system proxy settings. | 140 // system proxy settings. |
132 static ProxyConfigService* CreateSystemProxyConfigService( | 141 static ProxyConfigService* CreateSystemProxyConfigService( |
133 MessageLoop* io_loop); | 142 MessageLoop* io_loop); |
134 | 143 |
135 // Creates a proxy resolver appropriate for this platform that doesn't rely | 144 // Creates a proxy resolver appropriate for this platform that doesn't rely |
136 // on V8. | 145 // on V8. |
137 static ProxyResolver* CreateNonV8ProxyResolver(); | 146 static ProxyResolver* CreateNonV8ProxyResolver(); |
138 | 147 |
139 ProxyResolver* resolver() { return resolver_.get(); } | |
140 base::Thread* pac_thread() { return pac_thread_.get(); } | |
141 | |
142 // Identifies the proxy configuration. | 148 // Identifies the proxy configuration. |
143 ProxyConfig::ID config_id() const { return config_.id(); } | 149 ProxyConfig::ID config_id() const { return config_.id(); } |
144 | 150 |
145 // Returns true if we have called UpdateConfig() at least once. | 151 // Returns true if we have called UpdateConfig() at least once. |
146 bool config_has_been_initialized() const { | 152 bool config_has_been_initialized() const { |
147 return config_.id() != ProxyConfig::INVALID_ID; | 153 return config_.id() != ProxyConfig::INVALID_ID; |
148 } | 154 } |
149 | 155 |
150 // Checks to see if the proxy configuration changed, and then updates config_ | 156 // Checks to see if the proxy configuration changed, and then updates config_ |
151 // to reference the new configuration. | 157 // to reference the new configuration. |
(...skipping 11 matching lines...) Expand all Loading... |
163 bool IsFetchingPacScript() const { | 169 bool IsFetchingPacScript() const { |
164 return in_progress_fetch_config_id_ != ProxyConfig::INVALID_ID; | 170 return in_progress_fetch_config_id_ != ProxyConfig::INVALID_ID; |
165 } | 171 } |
166 | 172 |
167 // Callback for when the PAC script has finished downloading. | 173 // Callback for when the PAC script has finished downloading. |
168 void OnScriptFetchCompletion(int result); | 174 void OnScriptFetchCompletion(int result); |
169 | 175 |
170 // Returns ERR_IO_PENDING if the request cannot be completed synchronously. | 176 // Returns ERR_IO_PENDING if the request cannot be completed synchronously. |
171 // Otherwise it fills |result| with the proxy information for |url|. | 177 // Otherwise it fills |result| with the proxy information for |url|. |
172 // Completing synchronously means we don't need to query ProxyResolver. | 178 // Completing synchronously means we don't need to query ProxyResolver. |
173 // (ProxyResolver runs on PAC thread.) | |
174 int TryToCompleteSynchronously(const GURL& url, ProxyInfo* result); | 179 int TryToCompleteSynchronously(const GURL& url, ProxyInfo* result); |
175 | 180 |
176 // Set |result| with the proxy to use for |url|, based on |rules|. | 181 // Set |result| with the proxy to use for |url|, based on |rules|. |
177 void ApplyProxyRules(const GURL& url, | 182 void ApplyProxyRules(const GURL& url, |
178 const ProxyConfig::ProxyRules& rules, | 183 const ProxyConfig::ProxyRules& rules, |
179 ProxyInfo* result); | 184 ProxyInfo* result); |
180 | 185 |
181 // Starts the PAC thread if it isn't already running. | 186 // Sends all the unstarted pending requests off to the resolver. |
182 void InitPacThread(); | 187 void ResumeAllPendingRequests(); |
183 | 188 |
184 // Starts the next request from |pending_requests_| is possible. | 189 // Returns true if |pending_requests_| contains |req|. |
185 // |recent_req| is the request that just got added, or NULL. | 190 bool ContainsPendingRequest(PacRequest* req); |
186 void ProcessPendingRequests(PacRequest* recent_req); | |
187 | 191 |
188 // Removes the front entry of the requests queue. |expected_req| is our | 192 // Removes |req| from the list of pending requests. |
189 // expectation of what the front of the request queue is; it is only used by | 193 void RemovePendingRequest(PacRequest* req); |
190 // DCHECK for verification purposes. | 194 |
191 void RemoveFrontOfRequestQueue(PacRequest* expected_req); | 195 // Returns true if the resolver is all set-up and ready to accept requests. |
| 196 // Returns false if requests are blocked (because the PAC script is being |
| 197 // downloaded). May have the side-effect of starting the PAC script |
| 198 // download. |
| 199 bool PrepareResolverForRequests(); |
192 | 200 |
193 // Called to indicate that a PacRequest completed. The |config_id| parameter | 201 // Called to indicate that a PacRequest completed. The |config_id| parameter |
194 // indicates the proxy configuration that was queried. |result_code| is OK | 202 // indicates the proxy configuration that was queried. |result_code| is OK |
195 // if the PAC file could be downloaded and executed. Otherwise, it is an | 203 // if the PAC file could be downloaded and executed. Otherwise, it is an |
196 // error code, indicating a bad proxy configuration. | 204 // error code, indicating a bad proxy configuration. |
197 void DidCompletePacRequest(int config_id, int result_code); | 205 void DidCompletePacRequest(int config_id, int result_code); |
198 | 206 |
199 // Returns true if the URL passed in should not go through the proxy server. | 207 // Returns true if the URL passed in should not go through the proxy server. |
200 // 1. If the bypass proxy list contains the string <local> and the URL | 208 // 1. If the bypass proxy list contains the string <local> and the URL |
201 // passed in is a local URL, i.e. a URL without a DOT (.) | 209 // passed in is a local URL, i.e. a URL without a DOT (.) |
202 // 2. The URL matches one of the entities in the proxy bypass list. | 210 // 2. The URL matches one of the entities in the proxy bypass list. |
203 bool ShouldBypassProxyForURL(const GURL& url); | 211 bool ShouldBypassProxyForURL(const GURL& url); |
204 | 212 |
205 scoped_ptr<ProxyConfigService> config_service_; | 213 scoped_ptr<ProxyConfigService> config_service_; |
206 scoped_ptr<ProxyResolver> resolver_; | 214 scoped_ptr<ProxyResolver> resolver_; |
207 scoped_ptr<base::Thread> pac_thread_; | |
208 | 215 |
209 // We store the proxy config and a counter (ID) that is incremented each time | 216 // We store the proxy config and a counter (ID) that is incremented each time |
210 // the config changes. | 217 // the config changes. |
211 ProxyConfig config_; | 218 ProxyConfig config_; |
212 | 219 |
213 // Increasing ID to give to the next ProxyConfig that we set. | 220 // Increasing ID to give to the next ProxyConfig that we set. |
214 int next_config_id_; | 221 int next_config_id_; |
215 | 222 |
216 // Indicates that the configuration is bad and should be ignored. | 223 // Indicates that the configuration is bad and should be ignored. |
217 bool config_is_bad_; | 224 bool config_is_bad_; |
218 | 225 |
219 // The time when the proxy configuration was last read from the system. | 226 // The time when the proxy configuration was last read from the system. |
220 base::TimeTicks config_last_update_time_; | 227 base::TimeTicks config_last_update_time_; |
221 | 228 |
222 // Map of the known bad proxies and the information about the retry time. | 229 // Map of the known bad proxies and the information about the retry time. |
223 ProxyRetryInfoMap proxy_retry_info_; | 230 ProxyRetryInfoMap proxy_retry_info_; |
224 | 231 |
225 // FIFO queue of pending/inprogress requests. | 232 // Set of pending/inprogress requests. |
226 typedef std::deque<scoped_refptr<PacRequest> > PendingRequestsQueue; | 233 PendingRequests pending_requests_; |
227 PendingRequestsQueue pending_requests_; | |
228 | 234 |
229 // The fetcher to use when downloading PAC scripts for the ProxyResolver. | 235 // The fetcher to use when downloading PAC scripts for the ProxyResolver. |
230 // This dependency can be NULL if our ProxyResolver has no need for | 236 // This dependency can be NULL if our ProxyResolver has no need for |
231 // external PAC script fetching. | 237 // external PAC script fetching. |
232 scoped_ptr<ProxyScriptFetcher> proxy_script_fetcher_; | 238 scoped_ptr<ProxyScriptFetcher> proxy_script_fetcher_; |
233 | 239 |
234 // Callback for when |proxy_script_fetcher_| is done. | 240 // Callback for when |proxy_script_fetcher_| is done. |
235 CompletionCallbackImpl<ProxyService> proxy_script_fetcher_callback_; | 241 CompletionCallbackImpl<ProxyService> proxy_script_fetcher_callback_; |
236 | 242 |
237 // The ID of the configuration for which we last downloaded a PAC script. | 243 // The ID of the configuration for which we last downloaded a PAC script. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 278 |
273 base::WaitableEvent event_; | 279 base::WaitableEvent event_; |
274 CompletionCallbackImpl<SyncProxyServiceHelper> callback_; | 280 CompletionCallbackImpl<SyncProxyServiceHelper> callback_; |
275 ProxyInfo proxy_info_; | 281 ProxyInfo proxy_info_; |
276 int result_; | 282 int result_; |
277 }; | 283 }; |
278 | 284 |
279 } // namespace net | 285 } // namespace net |
280 | 286 |
281 #endif // NET_PROXY_PROXY_SERVICE_H_ | 287 #endif // NET_PROXY_PROXY_SERVICE_H_ |
OLD | NEW |