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