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