chrome_network_delegate.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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.
4
5#ifndef CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
6#define CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/files/file_path.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/values.h"
16#include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h"
17#include "net/base/network_delegate.h"
18#include "net/proxy/proxy_retry_info.h"
19
20class ChromeExtensionsNetworkDelegate;
21class ClientHints;
22class CookieSettings;
23class PrefService;
24
25template<class T> class PrefMember;
26
27typedef PrefMember<bool> BooleanPrefMember;
28
29namespace base {
30class Value;
31}
32
33namespace chrome_browser_net {
34class ConnectInterceptor;
35class Predictor;
36}
37
38namespace data_reduction_proxy {
39class DataReductionProxyAuthRequestHandler;
40class DataReductionProxyParams;
41class DataReductionProxyUsageStats;
42}
43
44namespace domain_reliability {
45class DomainReliabilityMonitor;
46}  // namespace domain_reliability
47
48namespace extensions {
49class EventRouterForwarder;
50class InfoMap;
51}
52
53namespace net {
54class ProxyConfig;
55class ProxyInfo;
56class ProxyServer;
57class ProxyService;
58class URLRequest;
59}
60
61namespace policy {
62class URLBlacklistManager;
63}
64
65namespace prerender {
66class PrerenderTracker;
67}
68
69// ChromeNetworkDelegate is the central point from within the chrome code to
70// add hooks into the network stack.
71class ChromeNetworkDelegate : public net::NetworkDelegate {
72 public:
73  // Provides an opportunity to interpose on proxy resolution. Called before
74  // ProxyService.ResolveProxy() returns. |proxy_info| contains information
75  // about the proxy being used, and may be modified by this callback.
76  typedef base::Callback<void(
77      const GURL& url,
78      int load_flags,
79      const net::ProxyConfig& data_reduction_proxy_config,
80      const net::ProxyRetryInfoMap& proxy_retry_info_map,
81      const data_reduction_proxy::DataReductionProxyParams* params,
82      net::ProxyInfo* result)> OnResolveProxyHandler;
83
84  // Provides an additional proxy configuration that can be consulted after
85  // proxy resolution.
86  typedef base::Callback<const net::ProxyConfig&()> ProxyConfigGetter;
87
88  // |enable_referrers| (and all of the other optional PrefMembers) should be
89  // initialized on the UI thread (see below) beforehand. This object's owner is
90  // responsible for cleaning them up at shutdown.
91  ChromeNetworkDelegate(extensions::EventRouterForwarder* event_router,
92                        BooleanPrefMember* enable_referrers);
93  virtual ~ChromeNetworkDelegate();
94
95  // Pass through to ChromeExtensionsNetworkDelegate::set_extension_info_map().
96  void set_extension_info_map(extensions::InfoMap* extension_info_map);
97
98#if defined(ENABLE_CONFIGURATION_POLICY)
99  void set_url_blacklist_manager(
100      const policy::URLBlacklistManager* url_blacklist_manager) {
101    url_blacklist_manager_ = url_blacklist_manager;
102  }
103#endif
104
105  // If |profile| is NULL or not set, events will be broadcast to all profiles,
106  // otherwise they will only be sent to the specified profile.
107  // Also pass through to ChromeExtensionsNetworkDelegate::set_profile().
108  void set_profile(void* profile);
109
110  // |profile_path| is used to locate the "Downloads" folder on Chrome OS. If it
111  // is set, the location of the Downloads folder for the profile is added to
112  // the whitelist for accesses via file: scheme.
113  void set_profile_path(const base::FilePath& profile_path) {
114    profile_path_ = profile_path;
115  }
116
117  // If |cookie_settings| is NULL or not set, all cookies are enabled,
118  // otherwise the settings are enforced on all observed network requests.
119  // Not inlined because we assign a scoped_refptr, which requires us to include
120  // the header file. Here we just forward-declare it.
121  void set_cookie_settings(CookieSettings* cookie_settings);
122
123  // Causes requested URLs to be fed to |predictor| via ConnectInterceptor.
124  void set_predictor(chrome_browser_net::Predictor* predictor);
125
126  void set_enable_do_not_track(BooleanPrefMember* enable_do_not_track) {
127    enable_do_not_track_ = enable_do_not_track;
128  }
129
130  void set_force_google_safe_search(
131      BooleanPrefMember* force_google_safe_search) {
132    force_google_safe_search_ = force_google_safe_search;
133  }
134
135  void set_data_reduction_proxy_enabled_pref(
136      BooleanPrefMember* data_reduction_proxy_enabled) {
137    data_reduction_proxy_enabled_ = data_reduction_proxy_enabled;
138  }
139
140  void set_domain_reliability_monitor(
141      domain_reliability::DomainReliabilityMonitor* monitor) {
142    domain_reliability_monitor_ = monitor;
143  }
144
145  void set_prerender_tracker(prerender::PrerenderTracker* prerender_tracker) {
146    prerender_tracker_ = prerender_tracker;
147  }
148
149  // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
150  void set_data_reduction_proxy_params(
151      data_reduction_proxy::DataReductionProxyParams* params) {
152    data_reduction_proxy_params_ = params;
153  }
154
155  // |data_reduction_proxy_usage_stats_| must outlive this
156  // ChromeNetworkDelegate.
157  void set_data_reduction_proxy_usage_stats(
158      data_reduction_proxy::DataReductionProxyUsageStats* usage_stats) {
159    data_reduction_proxy_usage_stats_ = usage_stats;
160  }
161
162  // |data_reduction_proxy_auth_request_handler_| must outlive this
163  // ChromeNetworkDelegate.
164  void set_data_reduction_proxy_auth_request_handler(
165      data_reduction_proxy::DataReductionProxyAuthRequestHandler* handler) {
166    data_reduction_proxy_auth_request_handler_ = handler;
167  }
168
169  void set_on_resolve_proxy_handler(OnResolveProxyHandler handler) {
170    on_resolve_proxy_handler_ = handler;
171  }
172
173  void set_proxy_config_getter(const ProxyConfigGetter& getter) {
174    proxy_config_getter_ = getter;
175  }
176
177  // Adds the Client Hints header to HTTP requests.
178  void SetEnableClientHints();
179
180  // Causes |OnCanThrottleRequest| to always return false, for all
181  // instances of this object.
182  static void NeverThrottleRequests();
183
184  // Binds the pref members to |pref_service| and moves them to the IO thread.
185  // |enable_referrers| cannot be NULL, the others can.
186  // This method should be called on the UI thread.
187  static void InitializePrefsOnUIThread(
188      BooleanPrefMember* enable_referrers,
189      BooleanPrefMember* enable_do_not_track,
190      BooleanPrefMember* force_google_safe_search,
191      PrefService* pref_service);
192
193  // When called, all file:// URLs will now be accessible.  If this is not
194  // called, then some platforms restrict access to file:// paths.
195  static void AllowAccessToAllFiles();
196
197  // Creates a Value summary of the persistent state of the network session.
198  // The caller is responsible for deleting the returned value.
199  // Must be called on the UI thread.
200  static base::Value* HistoricNetworkStatsInfoToValue();
201
202  // Creates a Value summary of the state of the network session. The caller is
203  // responsible for deleting the returned value.
204  base::Value* SessionNetworkStatsInfoToValue() const;
205
206 private:
207  friend class ChromeNetworkDelegateTest;
208
209  // NetworkDelegate implementation.
210  virtual int OnBeforeURLRequest(net::URLRequest* request,
211                                 const net::CompletionCallback& callback,
212                                 GURL* new_url) OVERRIDE;
213  virtual void OnResolveProxy(
214      const GURL& url,
215      int load_flags,
216      const net::ProxyService& proxy_service,
217      net::ProxyInfo* result) OVERRIDE;
218  virtual void OnProxyFallback(const net::ProxyServer& bad_proxy,
219                               int net_error,
220                               bool did_fallback) OVERRIDE;
221  virtual int OnBeforeSendHeaders(net::URLRequest* request,
222                                  const net::CompletionCallback& callback,
223                                  net::HttpRequestHeaders* headers) OVERRIDE;
224  virtual void OnBeforeSendProxyHeaders(
225      net::URLRequest* request,
226      const net::ProxyInfo& proxy_info,
227      net::HttpRequestHeaders* headers) OVERRIDE;
228  virtual void OnSendHeaders(net::URLRequest* request,
229                             const net::HttpRequestHeaders& headers) OVERRIDE;
230  virtual int OnHeadersReceived(
231      net::URLRequest* request,
232      const net::CompletionCallback& callback,
233      const net::HttpResponseHeaders* original_response_headers,
234      scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
235      GURL* allowed_unsafe_redirect_url) OVERRIDE;
236  virtual void OnBeforeRedirect(net::URLRequest* request,
237                                const GURL& new_location) OVERRIDE;
238  virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
239  virtual void OnRawBytesRead(const net::URLRequest& request,
240                              int bytes_read) OVERRIDE;
241  virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE;
242  virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE;
243  virtual void OnPACScriptError(int line_number,
244                                const base::string16& error) OVERRIDE;
245  virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired(
246      net::URLRequest* request,
247      const net::AuthChallengeInfo& auth_info,
248      const AuthCallback& callback,
249      net::AuthCredentials* credentials) OVERRIDE;
250  virtual bool OnCanGetCookies(const net::URLRequest& request,
251                               const net::CookieList& cookie_list) OVERRIDE;
252  virtual bool OnCanSetCookie(const net::URLRequest& request,
253                              const std::string& cookie_line,
254                              net::CookieOptions* options) OVERRIDE;
255  virtual bool OnCanAccessFile(const net::URLRequest& request,
256                               const base::FilePath& path) const OVERRIDE;
257  virtual bool OnCanThrottleRequest(
258      const net::URLRequest& request) const OVERRIDE;
259  virtual bool OnCanEnablePrivacyMode(
260      const GURL& url,
261      const GURL& first_party_for_cookies) const OVERRIDE;
262  virtual int OnBeforeSocketStreamConnect(
263      net::SocketStream* stream,
264      const net::CompletionCallback& callback) OVERRIDE;
265
266  void AccumulateContentLength(
267      int64 received_payload_byte_count,
268      int64 original_payload_byte_count,
269      data_reduction_proxy::DataReductionProxyRequestType request_type);
270
271  scoped_ptr<ChromeExtensionsNetworkDelegate> extensions_delegate_;
272
273  void* profile_;
274  base::FilePath profile_path_;
275  scoped_refptr<CookieSettings> cookie_settings_;
276
277  scoped_ptr<chrome_browser_net::ConnectInterceptor> connect_interceptor_;
278
279  // Weak, owned by our owner.
280  BooleanPrefMember* enable_referrers_;
281  BooleanPrefMember* enable_do_not_track_;
282  BooleanPrefMember* force_google_safe_search_;
283  BooleanPrefMember* data_reduction_proxy_enabled_;
284
285  // Weak, owned by our owner.
286#if defined(ENABLE_CONFIGURATION_POLICY)
287  const policy::URLBlacklistManager* url_blacklist_manager_;
288#endif
289  domain_reliability::DomainReliabilityMonitor* domain_reliability_monitor_;
290
291  // When true, allow access to all file:// URLs.
292  static bool g_allow_file_access_;
293
294  // True if OnCanThrottleRequest should always return false.
295  //
296  // Note: This needs to be static as the instance of
297  // ChromeNetworkDelegate used may change over time, and we need to
298  // set this variable once at start-up time.  It is effectively
299  // static anyway since it is based on a command-line flag.
300  static bool g_never_throttle_requests_;
301
302  // Total size of all content (excluding headers) that has been received
303  // over the network.
304  int64 received_content_length_;
305
306  // Total original size of all content before it was transferred.
307  int64 original_content_length_;
308
309  scoped_ptr<ClientHints> client_hints_;
310
311  bool first_request_;
312
313  prerender::PrerenderTracker* prerender_tracker_;
314
315  // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
316  data_reduction_proxy::DataReductionProxyParams* data_reduction_proxy_params_;
317  // |data_reduction_proxy_usage_stats_| must outlive this
318  // ChromeNetworkDelegate.
319  data_reduction_proxy::DataReductionProxyUsageStats*
320      data_reduction_proxy_usage_stats_;
321  data_reduction_proxy::DataReductionProxyAuthRequestHandler*
322  data_reduction_proxy_auth_request_handler_;
323
324  OnResolveProxyHandler on_resolve_proxy_handler_;
325  ProxyConfigGetter proxy_config_getter_;
326
327  DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
328};
329
330#endif  // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
331