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