chrome_network_delegate.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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_data_reduction_proxy_enabled_pref(
126      BooleanPrefMember* data_reduction_proxy_enabled) {
127    data_reduction_proxy_enabled_ = data_reduction_proxy_enabled;
128  }
129
130  void set_domain_reliability_monitor(
131      domain_reliability::DomainReliabilityMonitor* monitor) {
132    domain_reliability_monitor_ = monitor;
133  }
134
135  void set_prerender_tracker(prerender::PrerenderTracker* prerender_tracker) {
136    prerender_tracker_ = prerender_tracker;
137  }
138
139  // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
140  void set_data_reduction_proxy_params(
141      data_reduction_proxy::DataReductionProxyParams* params) {
142    data_reduction_proxy_params_ = params;
143  }
144
145  // |data_reduction_proxy_usage_stats_| must outlive this
146  // ChromeNetworkDelegate.
147  void set_data_reduction_proxy_usage_stats(
148      data_reduction_proxy::DataReductionProxyUsageStats* usage_stats) {
149    data_reduction_proxy_usage_stats_ = usage_stats;
150  }
151
152  // |data_reduction_proxy_auth_request_handler_| must outlive this
153  // ChromeNetworkDelegate.
154  void set_data_reduction_proxy_auth_request_handler(
155      data_reduction_proxy::DataReductionProxyAuthRequestHandler* handler) {
156    data_reduction_proxy_auth_request_handler_ = handler;
157  }
158
159  void set_on_resolve_proxy_handler(OnResolveProxyHandler handler) {
160    on_resolve_proxy_handler_ = handler;
161  }
162
163  // Adds the Client Hints header to HTTP requests.
164  void SetEnableClientHints();
165
166  // Causes |OnCanThrottleRequest| to always return false, for all
167  // instances of this object.
168  static void NeverThrottleRequests();
169
170  // Binds the pref members to |pref_service| and moves them to the IO thread.
171  // |enable_referrers| cannot be NULL, the others can.
172  // This method should be called on the UI thread.
173  static void InitializePrefsOnUIThread(
174      BooleanPrefMember* enable_referrers,
175      BooleanPrefMember* enable_do_not_track,
176      BooleanPrefMember* force_google_safe_search,
177      PrefService* pref_service);
178
179  // When called, all file:// URLs will now be accessible.  If this is not
180  // called, then some platforms restrict access to file:// paths.
181  static void AllowAccessToAllFiles();
182
183  // Creates a Value summary of the persistent state of the network session.
184  // The caller is responsible for deleting the returned value.
185  // Must be called on the UI thread.
186  static base::Value* HistoricNetworkStatsInfoToValue();
187
188  // Creates a Value summary of the state of the network session. The caller is
189  // responsible for deleting the returned value.
190  base::Value* SessionNetworkStatsInfoToValue() const;
191
192 private:
193  friend class ChromeNetworkDelegateTest;
194
195  // NetworkDelegate implementation.
196  virtual int OnBeforeURLRequest(net::URLRequest* request,
197                                 const net::CompletionCallback& callback,
198                                 GURL* new_url) OVERRIDE;
199  virtual void OnResolveProxy(
200      const GURL& url, int load_flags, net::ProxyInfo* result) OVERRIDE;
201  virtual int OnBeforeSendHeaders(net::URLRequest* request,
202                                  const net::CompletionCallback& callback,
203                                  net::HttpRequestHeaders* headers) OVERRIDE;
204  virtual void OnBeforeSendProxyHeaders(
205      net::URLRequest* request,
206      const net::ProxyInfo& proxy_info,
207      net::HttpRequestHeaders* headers) OVERRIDE;
208  virtual void OnSendHeaders(net::URLRequest* request,
209                             const net::HttpRequestHeaders& headers) OVERRIDE;
210  virtual int OnHeadersReceived(
211      net::URLRequest* request,
212      const net::CompletionCallback& callback,
213      const net::HttpResponseHeaders* original_response_headers,
214      scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
215      GURL* allowed_unsafe_redirect_url) OVERRIDE;
216  virtual void OnBeforeRedirect(net::URLRequest* request,
217                                const GURL& new_location) OVERRIDE;
218  virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE;
219  virtual void OnRawBytesRead(const net::URLRequest& request,
220                              int bytes_read) OVERRIDE;
221  virtual void OnCompleted(net::URLRequest* request, bool started) OVERRIDE;
222  virtual void OnURLRequestDestroyed(net::URLRequest* request) OVERRIDE;
223  virtual void OnPACScriptError(int line_number,
224                                const base::string16& error) OVERRIDE;
225  virtual net::NetworkDelegate::AuthRequiredResponse OnAuthRequired(
226      net::URLRequest* request,
227      const net::AuthChallengeInfo& auth_info,
228      const AuthCallback& callback,
229      net::AuthCredentials* credentials) OVERRIDE;
230  virtual bool OnCanGetCookies(const net::URLRequest& request,
231                               const net::CookieList& cookie_list) OVERRIDE;
232  virtual bool OnCanSetCookie(const net::URLRequest& request,
233                              const std::string& cookie_line,
234                              net::CookieOptions* options) OVERRIDE;
235  virtual bool OnCanAccessFile(const net::URLRequest& request,
236                               const base::FilePath& path) const OVERRIDE;
237  virtual bool OnCanThrottleRequest(
238      const net::URLRequest& request) const OVERRIDE;
239  virtual bool OnCanEnablePrivacyMode(
240      const GURL& url,
241      const GURL& first_party_for_cookies) const OVERRIDE;
242  virtual int OnBeforeSocketStreamConnect(
243      net::SocketStream* stream,
244      const net::CompletionCallback& callback) OVERRIDE;
245
246  void AccumulateContentLength(
247      int64 received_payload_byte_count,
248      int64 original_payload_byte_count,
249      data_reduction_proxy::DataReductionProxyRequestType request_type);
250
251  scoped_ptr<ChromeExtensionsNetworkDelegate> extensions_delegate_;
252
253  void* profile_;
254  base::FilePath profile_path_;
255  scoped_refptr<CookieSettings> cookie_settings_;
256
257  scoped_ptr<chrome_browser_net::ConnectInterceptor> connect_interceptor_;
258
259  // Weak, owned by our owner.
260  BooleanPrefMember* enable_referrers_;
261  BooleanPrefMember* enable_do_not_track_;
262  BooleanPrefMember* force_google_safe_search_;
263  BooleanPrefMember* data_reduction_proxy_enabled_;
264
265  // Weak, owned by our owner.
266#if defined(ENABLE_CONFIGURATION_POLICY)
267  const policy::URLBlacklistManager* url_blacklist_manager_;
268#endif
269  domain_reliability::DomainReliabilityMonitor* domain_reliability_monitor_;
270
271  // When true, allow access to all file:// URLs.
272  static bool g_allow_file_access_;
273
274  // True if OnCanThrottleRequest should always return false.
275  //
276  // Note: This needs to be static as the instance of
277  // ChromeNetworkDelegate used may change over time, and we need to
278  // set this variable once at start-up time.  It is effectively
279  // static anyway since it is based on a command-line flag.
280  static bool g_never_throttle_requests_;
281
282  // Total size of all content (excluding headers) that has been received
283  // over the network.
284  int64 received_content_length_;
285
286  // Total original size of all content before it was transferred.
287  int64 original_content_length_;
288
289  scoped_ptr<ClientHints> client_hints_;
290
291  bool first_request_;
292
293  prerender::PrerenderTracker* prerender_tracker_;
294
295  // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
296  data_reduction_proxy::DataReductionProxyParams* data_reduction_proxy_params_;
297  // |data_reduction_proxy_usage_stats_| must outlive this
298  // ChromeNetworkDelegate.
299  data_reduction_proxy::DataReductionProxyUsageStats*
300      data_reduction_proxy_usage_stats_;
301  data_reduction_proxy::DataReductionProxyAuthRequestHandler*
302  data_reduction_proxy_auth_request_handler_;
303
304  OnResolveProxyHandler on_resolve_proxy_handler_;
305
306  DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
307};
308
309#endif  // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
310