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