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