blob: e9bf978f1c8b1e0468dc47e4f0e1f881f87e8254 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018class PrefService;
19template<class T> class PrefMember;
20
21typedef PrefMember<bool> BooleanPrefMember;
22
23namespace base {
24class Value;
25}
26
27namespace chrome_browser_net {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000028class ConnectInterceptor;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029class LoadTimeStats;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000030class Predictor;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031}
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:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000049 // |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);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000054 virtual ~ChromeNetworkDelegate();
55
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000056 // 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
Torne (Richard Coles)58218062012-11-14 11:43:16 +000093 // 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.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000098 // |enable_referrers| cannot be NULL, the others can.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000099 // 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,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000157 const base::FilePath& path) const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000158 virtual bool OnCanThrottleRequest(
159 const net::URLRequest& request) const OVERRIDE;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100160 virtual bool OnCanEnablePrivacyMode(
161 const GURL& url,
162 const GURL& first_party_for_cookies) const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000163 virtual int OnBeforeSocketStreamConnect(
164 net::SocketStream* stream,
165 const net::CompletionCallback& callback) OVERRIDE;
166 virtual void OnRequestWaitStateChange(const net::URLRequest& request,
167 RequestWaitState state) OVERRIDE;
168
169 void AccumulateContentLength(
Ben Murdochbb1529c2013-08-08 10:24:53 +0100170 int64 received_payload_byte_count, int64 original_payload_byte_count,
171 bool data_reduction_proxy_was_used);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000172
173 scoped_refptr<extensions::EventRouterForwarder> event_router_;
174 void* profile_;
175 scoped_refptr<CookieSettings> cookie_settings_;
176
177 scoped_refptr<ExtensionInfoMap> extension_info_map_;
178
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000179 scoped_ptr<chrome_browser_net::ConnectInterceptor> connect_interceptor_;
180
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000181 // Weak, owned by our owner.
182 BooleanPrefMember* enable_referrers_;
183 BooleanPrefMember* enable_do_not_track_;
184 BooleanPrefMember* force_google_safe_search_;
185
186 // Weak, owned by our owner.
187 const policy::URLBlacklistManager* url_blacklist_manager_;
188
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000189 // When true, allow access to all file:// URLs.
190 static bool g_allow_file_access_;
191
192 // True if OnCanThrottleRequest should always return false.
193 //
194 // Note: This needs to be static as the instance of
195 // ChromeNetworkDelegate used may change over time, and we need to
196 // set this variable once at start-up time. It is effectively
197 // static anyway since it is based on a command-line flag.
198 static bool g_never_throttle_requests_;
199
200 // Pointer to IOThread global, should outlive ChromeNetworkDelegate.
201 chrome_browser_net::LoadTimeStats* load_time_stats_;
202
203 // Total size of all content (excluding headers) that has been received
204 // over the network.
205 int64 received_content_length_;
206
207 // Total original size of all content before it was transferred.
208 int64 original_content_length_;
209
210 DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
211};
212
213#endif // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_