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