gn: More helpful default args file
[chromium-blink-merge.git] / net / proxy / proxy_service.h
blobc4e0fea72b034e3d0c25c12fe01bec8bd00e13c6
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_PROXY_PROXY_SERVICE_H_
6 #define NET_PROXY_PROXY_SERVICE_H_
8 #include <string>
9 #include <vector>
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/load_states.h"
18 #include "net/base/net_export.h"
19 #include "net/base/net_log.h"
20 #include "net/base/network_change_notifier.h"
21 #include "net/proxy/proxy_config_service.h"
22 #include "net/proxy/proxy_info.h"
23 #include "net/proxy/proxy_server.h"
25 class GURL;
27 namespace base {
28 class SingleThreadTaskRunner;
29 class TimeDelta;
30 } // namespace base
32 namespace net {
34 class DhcpProxyScriptFetcher;
35 class HostResolver;
36 class NetworkDelegate;
37 class ProxyResolver;
38 class ProxyResolverScriptData;
39 class ProxyScriptDecider;
40 class ProxyScriptFetcher;
42 // This class can be used to resolve the proxy server to use when loading a
43 // HTTP(S) URL. It uses the given ProxyResolver to handle the actual proxy
44 // resolution. See ProxyResolverV8 for example.
45 class NET_EXPORT ProxyService : public NetworkChangeNotifier::IPAddressObserver,
46 public NetworkChangeNotifier::DNSObserver,
47 public ProxyConfigService::Observer,
48 NON_EXPORTED_BASE(public base::NonThreadSafe) {
49 public:
50 static const size_t kDefaultNumPacThreads = 4;
52 // This interface defines the set of policies for when to poll the PAC
53 // script for changes.
55 // The polling policy decides what the next poll delay should be in
56 // milliseconds. It also decides how to wait for this delay -- either
57 // by starting a timer to do the poll at exactly |next_delay_ms|
58 // (MODE_USE_TIMER) or by waiting for the first network request issued after
59 // |next_delay_ms| (MODE_START_AFTER_ACTIVITY).
61 // The timer method is more precise and guarantees that polling happens when
62 // it was requested. However it has the disadvantage of causing spurious CPU
63 // and network activity. It is a reasonable choice to use for short poll
64 // intervals which only happen a couple times.
66 // However for repeated timers this will prevent the browser from going
67 // idle. MODE_START_AFTER_ACTIVITY solves this problem by only polling in
68 // direct response to network activity. The drawback to
69 // MODE_START_AFTER_ACTIVITY is since the poll is initiated only after the
70 // request is received, the first couple requests initiated after a long
71 // period of inactivity will likely see a stale version of the PAC script
72 // until the background polling gets a chance to update things.
73 class NET_EXPORT_PRIVATE PacPollPolicy {
74 public:
75 enum Mode {
76 MODE_USE_TIMER,
77 MODE_START_AFTER_ACTIVITY,
80 virtual ~PacPollPolicy() {}
82 // Decides the next poll delay. |current_delay| is the delay used
83 // by the preceding poll, or a negative TimeDelta value if determining
84 // the delay for the initial poll. |initial_error| is the network error
85 // code that the last PAC fetch (or WPAD initialization) failed with,
86 // or OK if it completed successfully. Implementations must set
87 // |next_delay| to a non-negative value.
88 virtual Mode GetNextDelay(int initial_error,
89 base::TimeDelta current_delay,
90 base::TimeDelta* next_delay) const = 0;
93 // The instance takes ownership of |config_service| and |resolver|.
94 // |net_log| is a possibly NULL destination to send log events to. It must
95 // remain alive for the lifetime of this ProxyService.
96 ProxyService(ProxyConfigService* config_service,
97 ProxyResolver* resolver,
98 NetLog* net_log);
100 ~ProxyService() override;
102 // Used internally to handle PAC queries.
103 // TODO(eroman): consider naming this simply "Request".
104 class PacRequest;
106 // Returns ERR_IO_PENDING if the proxy information could not be provided
107 // synchronously, to indicate that the result will be available when the
108 // callback is run. The callback is run on the thread that calls
109 // ResolveProxy.
111 // The caller is responsible for ensuring that |results| and |callback|
112 // remain valid until the callback is run or until |pac_request| is cancelled
113 // via CancelPacRequest. |pac_request| is only valid while the completion
114 // callback is still pending. NULL can be passed for |pac_request| if
115 // the caller will not need to cancel the request.
117 // We use the three possible proxy access types in the following order,
118 // doing fallback if one doesn't work. See "pac_script_decider.h"
119 // for the specifics.
120 // 1. WPAD auto-detection
121 // 2. PAC URL
122 // 3. named proxy
124 // Profiling information for the request is saved to |net_log| if non-NULL.
125 int ResolveProxy(const GURL& url,
126 int load_flags,
127 ProxyInfo* results,
128 const net::CompletionCallback& callback,
129 PacRequest** pac_request,
130 NetworkDelegate* network_delegate,
131 const BoundNetLog& net_log);
133 // Returns true if the proxy information could be determined without spawning
134 // an asynchronous task. Otherwise, |result| is unmodified.
135 bool TryResolveProxySynchronously(const GURL& raw_url,
136 int load_flags,
137 ProxyInfo* result,
138 NetworkDelegate* network_delegate,
139 const BoundNetLog& net_log);
141 // This method is called after a failure to connect or resolve a host name.
142 // It gives the proxy service an opportunity to reconsider the proxy to use.
143 // The |results| parameter contains the results returned by an earlier call
144 // to ResolveProxy. The |net_error| parameter contains the network error
145 // code associated with the failure. See "net/base/net_error_list.h" for a
146 // list of possible values. The semantics of this call are otherwise
147 // similar to ResolveProxy.
149 // NULL can be passed for |pac_request| if the caller will not need to
150 // cancel the request.
152 // Returns ERR_FAILED if there is not another proxy config to try.
154 // Profiling information for the request is saved to |net_log| if non-NULL.
155 int ReconsiderProxyAfterError(const GURL& url,
156 int load_flags,
157 int net_error,
158 ProxyInfo* results,
159 const CompletionCallback& callback,
160 PacRequest** pac_request,
161 NetworkDelegate* network_delegate,
162 const BoundNetLog& net_log);
164 // Explicitly trigger proxy fallback for the given |results| by updating our
165 // list of bad proxies to include the first entry of |results|, and,
166 // optionally, another bad proxy. Will retry after |retry_delay| if positive,
167 // and will use the default proxy retry duration otherwise. Proxies marked as
168 // bad will not be retried until |retry_delay| has passed. Returns true if
169 // there will be at least one proxy remaining in the list after fallback and
170 // false otherwise. This method should be used to add proxies to the bad
171 // proxy list only for reasons other than a network error. If a proxy needs
172 // to be added to the bad proxy list because a network error was encountered
173 // when trying to connect to it, use |ReconsiderProxyAfterError|.
174 bool MarkProxiesAsBadUntil(const ProxyInfo& results,
175 base::TimeDelta retry_delay,
176 const ProxyServer& another_bad_proxy,
177 const BoundNetLog& net_log);
179 // Called to report that the last proxy connection succeeded. If |proxy_info|
180 // has a non empty proxy_retry_info map, the proxies that have been tried (and
181 // failed) for this request will be marked as bad. |network_delegate| will
182 // be notified of any proxy fallbacks.
183 void ReportSuccess(const ProxyInfo& proxy_info,
184 NetworkDelegate* network_delegate);
186 // Call this method with a non-null |pac_request| to cancel the PAC request.
187 void CancelPacRequest(PacRequest* pac_request);
189 // Returns the LoadState for this |pac_request| which must be non-NULL.
190 LoadState GetLoadState(const PacRequest* pac_request) const;
192 // Sets the ProxyScriptFetcher and DhcpProxyScriptFetcher dependencies. This
193 // is needed if the ProxyResolver is of type ProxyResolverWithoutFetch.
194 // ProxyService takes ownership of both objects.
195 void SetProxyScriptFetchers(
196 ProxyScriptFetcher* proxy_script_fetcher,
197 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher);
198 ProxyScriptFetcher* GetProxyScriptFetcher() const;
200 // Tells this ProxyService to start using a new ProxyConfigService to
201 // retrieve its ProxyConfig from. The new ProxyConfigService will immediately
202 // be queried for new config info which will be used for all subsequent
203 // ResolveProxy calls. ProxyService takes ownership of
204 // |new_proxy_config_service|.
205 void ResetConfigService(ProxyConfigService* new_proxy_config_service);
207 // Returns the last configuration fetched from ProxyConfigService.
208 const ProxyConfig& fetched_config() {
209 return fetched_config_;
212 // Returns the current configuration being used by ProxyConfigService.
213 const ProxyConfig& config() const {
214 return config_;
217 // Returns the map of proxies which have been marked as "bad".
218 const ProxyRetryInfoMap& proxy_retry_info() const {
219 return proxy_retry_info_;
222 // Clears the list of bad proxy servers that has been cached.
223 void ClearBadProxiesCache() {
224 proxy_retry_info_.clear();
227 // Forces refetching the proxy configuration, and applying it.
228 // This re-does everything from fetching the system configuration,
229 // to downloading and testing the PAC files.
230 void ForceReloadProxyConfig();
232 // Same as CreateProxyServiceUsingV8ProxyResolver, except it uses system
233 // libraries for evaluating the PAC script if available, otherwise skips
234 // proxy autoconfig.
235 static ProxyService* CreateUsingSystemProxyResolver(
236 ProxyConfigService* proxy_config_service,
237 size_t num_pac_threads,
238 NetLog* net_log);
240 // Creates a ProxyService without support for proxy autoconfig.
241 static ProxyService* CreateWithoutProxyResolver(
242 ProxyConfigService* proxy_config_service,
243 NetLog* net_log);
245 // Convenience methods that creates a proxy service using the
246 // specified fixed settings.
247 static ProxyService* CreateFixed(const ProxyConfig& pc);
248 static ProxyService* CreateFixed(const std::string& proxy);
250 // Creates a proxy service that uses a DIRECT connection for all requests.
251 static ProxyService* CreateDirect();
252 // |net_log|'s lifetime must exceed ProxyService.
253 static ProxyService* CreateDirectWithNetLog(NetLog* net_log);
255 // This method is used by tests to create a ProxyService that returns a
256 // hardcoded proxy fallback list (|pac_string|) for every URL.
258 // |pac_string| is a list of proxy servers, in the format that a PAC script
259 // would return it. For example, "PROXY foobar:99; SOCKS fml:2; DIRECT"
260 static ProxyService* CreateFixedFromPacResult(const std::string& pac_string);
262 // Creates a config service appropriate for this platform that fetches the
263 // system proxy settings.
264 static ProxyConfigService* CreateSystemProxyConfigService(
265 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
266 const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner);
268 // This method should only be used by unit tests.
269 void set_stall_proxy_auto_config_delay(base::TimeDelta delay) {
270 stall_proxy_auto_config_delay_ = delay;
273 // This method should only be used by unit tests. Returns the previously
274 // active policy.
275 static const PacPollPolicy* set_pac_script_poll_policy(
276 const PacPollPolicy* policy);
278 // This method should only be used by unit tests. Creates an instance
279 // of the default internal PacPollPolicy used by ProxyService.
280 static scoped_ptr<PacPollPolicy> CreateDefaultPacPollPolicy();
282 void set_quick_check_enabled(bool value) {
283 quick_check_enabled_ = value;
286 bool quick_check_enabled() const { return quick_check_enabled_; }
288 private:
289 FRIEND_TEST_ALL_PREFIXES(ProxyServiceTest, UpdateConfigAfterFailedAutodetect);
290 FRIEND_TEST_ALL_PREFIXES(ProxyServiceTest, UpdateConfigFromPACToDirect);
291 friend class PacRequest;
292 class InitProxyResolver;
293 class ProxyScriptDeciderPoller;
295 // TODO(eroman): change this to a std::set. Note that this requires updating
296 // some tests in proxy_service_unittest.cc such as:
297 // ProxyServiceTest.InitialPACScriptDownload
298 // which expects requests to finish in the order they were added.
299 typedef std::vector<scoped_refptr<PacRequest> > PendingRequests;
301 enum State {
302 STATE_NONE,
303 STATE_WAITING_FOR_PROXY_CONFIG,
304 STATE_WAITING_FOR_INIT_PROXY_RESOLVER,
305 STATE_READY,
308 // Resets all the variables associated with the current proxy configuration,
309 // and rewinds the current state to |STATE_NONE|. Returns the previous value
310 // of |current_state_|. If |reset_fetched_config| is true then
311 // |fetched_config_| will also be reset, otherwise it will be left as-is.
312 // Resetting it means that we will have to re-fetch the configuration from
313 // the ProxyConfigService later.
314 State ResetProxyConfig(bool reset_fetched_config);
316 // Retrieves the current proxy configuration from the ProxyConfigService, and
317 // starts initializing for it.
318 void ApplyProxyConfigIfAvailable();
320 // Callback for when the proxy resolver has been initialized with a
321 // PAC script.
322 void OnInitProxyResolverComplete(int result);
324 // Returns ERR_IO_PENDING if the request cannot be completed synchronously.
325 // Otherwise it fills |result| with the proxy information for |url|.
326 // Completing synchronously means we don't need to query ProxyResolver.
327 int TryToCompleteSynchronously(const GURL& url,
328 int load_flags,
329 NetworkDelegate* network_delegate,
330 ProxyInfo* result);
332 // Identical to ResolveProxy, except that |callback| is permitted to be null.
333 // if |callback.is_null()|, this function becomes a thin wrapper around
334 // |TryToCompleteSynchronously|.
335 int ResolveProxyHelper(const GURL& url,
336 int load_flags,
337 ProxyInfo* results,
338 const net::CompletionCallback& callback,
339 PacRequest** pac_request,
340 NetworkDelegate* network_delegate,
341 const BoundNetLog& net_log);
343 // Cancels all of the requests sent to the ProxyResolver. These will be
344 // restarted when calling SetReady().
345 void SuspendAllPendingRequests();
347 // Advances the current state to |STATE_READY|, and resumes any pending
348 // requests which had been stalled waiting for initialization to complete.
349 void SetReady();
351 // Returns true if |pending_requests_| contains |req|.
352 bool ContainsPendingRequest(PacRequest* req);
354 // Removes |req| from the list of pending requests.
355 void RemovePendingRequest(PacRequest* req);
357 // Called when proxy resolution has completed (either synchronously or
358 // asynchronously). Handles logging the result, and cleaning out
359 // bad entries from the results list.
360 int DidFinishResolvingProxy(const GURL& url,
361 int load_flags,
362 NetworkDelegate* network_delegate,
363 ProxyInfo* result,
364 int result_code,
365 const BoundNetLog& net_log);
367 // Start initialization using |fetched_config_|.
368 void InitializeUsingLastFetchedConfig();
370 // Start the initialization skipping past the "decision" phase.
371 void InitializeUsingDecidedConfig(
372 int decider_result,
373 ProxyResolverScriptData* script_data,
374 const ProxyConfig& effective_config);
376 // NetworkChangeNotifier::IPAddressObserver
377 // When this is called, we re-fetch PAC scripts and re-run WPAD.
378 void OnIPAddressChanged() override;
380 // NetworkChangeNotifier::DNSObserver
381 // We respond as above.
382 void OnDNSChanged() override;
384 // ProxyConfigService::Observer
385 void OnProxyConfigChanged(
386 const ProxyConfig& config,
387 ProxyConfigService::ConfigAvailability availability) override;
389 scoped_ptr<ProxyConfigService> config_service_;
390 scoped_ptr<ProxyResolver> resolver_;
392 // We store the proxy configuration that was last fetched from the
393 // ProxyConfigService, as well as the resulting "effective" configuration.
394 // The effective configuration is what we condense the original fetched
395 // settings to after testing the various automatic settings (auto-detect
396 // and custom PAC url).
397 ProxyConfig fetched_config_;
398 ProxyConfig config_;
400 // Increasing ID to give to the next ProxyConfig that we set.
401 int next_config_id_;
403 // The time when the proxy configuration was last read from the system.
404 base::TimeTicks config_last_update_time_;
406 // Map of the known bad proxies and the information about the retry time.
407 ProxyRetryInfoMap proxy_retry_info_;
409 // Set of pending/inprogress requests.
410 PendingRequests pending_requests_;
412 // The fetcher to use when downloading PAC scripts for the ProxyResolver.
413 // This dependency can be NULL if our ProxyResolver has no need for
414 // external PAC script fetching.
415 scoped_ptr<ProxyScriptFetcher> proxy_script_fetcher_;
417 // The fetcher to use when attempting to download the most appropriate PAC
418 // script configured in DHCP, if any. Can be NULL if the ProxyResolver has
419 // no need for DHCP PAC script fetching.
420 scoped_ptr<DhcpProxyScriptFetcher> dhcp_proxy_script_fetcher_;
422 // Helper to download the PAC script (wpad + custom) and apply fallback rules.
424 // Note that the declaration is important here: |proxy_script_fetcher_| and
425 // |proxy_resolver_| must outlive |init_proxy_resolver_|.
426 scoped_ptr<InitProxyResolver> init_proxy_resolver_;
428 // Helper to poll the PAC script for changes.
429 scoped_ptr<ProxyScriptDeciderPoller> script_poller_;
431 State current_state_;
433 // Either OK or an ERR_* value indicating that a permanent error (e.g.
434 // failed to fetch the PAC script) prevents proxy resolution.
435 int permanent_error_;
437 // This is the log where any events generated by |init_proxy_resolver_| are
438 // sent to.
439 NetLog* net_log_;
441 // The earliest time at which we should run any proxy auto-config. (Used to
442 // stall re-configuration following an IP address change).
443 base::TimeTicks stall_proxy_autoconfig_until_;
445 // The amount of time to stall requests following IP address changes.
446 base::TimeDelta stall_proxy_auto_config_delay_;
448 // Whether child ProxyScriptDeciders should use QuickCheck
449 bool quick_check_enabled_;
451 DISALLOW_COPY_AND_ASSIGN(ProxyService);
454 } // namespace net
456 #endif // NET_PROXY_PROXY_SERVICE_H_