chrome_network_delegate.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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) OVERRIDE;
220  virtual int OnBeforeSendHeaders(net::URLRequest* request,
221                                  const net::CompletionCallback& callback,
222                                  net::HttpRequestHeaders* headers) OVERRIDE;
223  virtual void OnBeforeSendProxyHeaders(
224      net::URLRequest* request,
225      const net::ProxyInfo& proxy_info,
226      net::HttpRequestHeaders* headers) OVERRIDE;
227  virtual void OnSendHeaders(net::URLRequest* request,
228                             const net::HttpRequestHeaders& headers) OVERRIDE;
229  virtual int OnHeadersReceived(
230      net::URLRequest* request,
231      const net::CompletionCallback& callback,
232      const net::HttpResponseHeaders* original_response_headers,
233      scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
234      GURL* allowed_unsafe_redirect_url) OVERRIDE;
235  virtual void OnBeforeRedirect(net::URLRequest* request,
236                                const GURL& new_location) OVERRIDE;
237  virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
238  virtual void OnRawBytesRead(const net::URLRequest& request,
239                              int bytes_read) OVERRIDE;
240  virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE;
241  virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE;
242  virtual void OnPACScriptError(int line_number,
243                                const base::string16& error) OVERRIDE;
244  virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired(
245      net::URLRequest* request,
246      const net::AuthChallengeInfo& auth_info,
247      const AuthCallback& callback,
248      net::AuthCredentials* credentials) OVERRIDE;
249  virtual bool OnCanGetCookies(const net::URLRequest& request,
250                               const net::CookieList& cookie_list) OVERRIDE;
251  virtual bool OnCanSetCookie(const net::URLRequest& request,
252                              const std::string& cookie_line,
253                              net::CookieOptions* options) OVERRIDE;
254  virtual bool OnCanAccessFile(const net::URLRequest& request,
255                               const base::FilePath& path) const OVERRIDE;
256  virtual bool OnCanThrottleRequest(
257      const net::URLRequest& request) const OVERRIDE;
258  virtual bool OnCanEnablePrivacyMode(
259      const GURL& url,
260      const GURL& first_party_for_cookies) const OVERRIDE;
261  virtual int OnBeforeSocketStreamConnect(
262      net::SocketStream* stream,
263      const net::CompletionCallback& callback) OVERRIDE;
264
265  void AccumulateContentLength(
266      int64 received_payload_byte_count,
267      int64 original_payload_byte_count,
268      data_reduction_proxy::DataReductionProxyRequestType request_type);
269
270  scoped_ptr<ChromeExtensionsNetworkDelegate> extensions_delegate_;
271
272  void* profile_;
273  base::FilePath profile_path_;
274  scoped_refptr<CookieSettings> cookie_settings_;
275
276  scoped_ptr<chrome_browser_net::ConnectInterceptor> connect_interceptor_;
277
278  // Weak, owned by our owner.
279  BooleanPrefMember* enable_referrers_;
280  BooleanPrefMember* enable_do_not_track_;
281  BooleanPrefMember* force_google_safe_search_;
282  BooleanPrefMember* data_reduction_proxy_enabled_;
283
284  // Weak, owned by our owner.
285#if defined(ENABLE_CONFIGURATION_POLICY)
286  const policy::URLBlacklistManager* url_blacklist_manager_;
287#endif
288  domain_reliability::DomainReliabilityMonitor* domain_reliability_monitor_;
289
290  // When true, allow access to all file:// URLs.
291  static bool g_allow_file_access_;
292
293  // True if OnCanThrottleRequest should always return false.
294  //
295  // Note: This needs to be static as the instance of
296  // ChromeNetworkDelegate used may change over time, and we need to
297  // set this variable once at start-up time.  It is effectively
298  // static anyway since it is based on a command-line flag.
299  static bool g_never_throttle_requests_;
300
301  // Total size of all content (excluding headers) that has been received
302  // over the network.
303  int64 received_content_length_;
304
305  // Total original size of all content before it was transferred.
306  int64 original_content_length_;
307
308  scoped_ptr<ClientHints> client_hints_;
309
310  bool first_request_;
311
312  prerender::PrerenderTracker* prerender_tracker_;
313
314  // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
315  data_reduction_proxy::DataReductionProxyParams* data_reduction_proxy_params_;
316  // |data_reduction_proxy_usage_stats_| must outlive this
317  // ChromeNetworkDelegate.
318  data_reduction_proxy::DataReductionProxyUsageStats*
319      data_reduction_proxy_usage_stats_;
320  data_reduction_proxy::DataReductionProxyAuthRequestHandler*
321  data_reduction_proxy_auth_request_handler_;
322
323  OnResolveProxyHandler on_resolve_proxy_handler_;
324  ProxyConfigGetter proxy_config_getter_;
325
326  DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
327};
328
329#endif  // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
330