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.
5 #ifndef CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
6 #define CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
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/core/browser/data_reduction_proxy_metrics.h"
17 #include "net/base/network_delegate.h"
18 #include "net/proxy/proxy_retry_info.h"
20 class ChromeExtensionsNetworkDelegate
;
25 template<class T
> class PrefMember
;
27 typedef PrefMember
<bool> BooleanPrefMember
;
33 namespace chrome_browser_net
{
34 class ConnectInterceptor
;
38 namespace data_reduction_proxy
{
39 class DataReductionProxyAuthRequestHandler
;
40 class DataReductionProxyParams
;
41 class DataReductionProxyStatisticsPrefs
;
42 class DataReductionProxyUsageStats
;
45 namespace domain_reliability
{
46 class DomainReliabilityMonitor
;
49 namespace extensions
{
50 class EventRouterForwarder
;
63 class URLBlacklistManager
;
67 class PrerenderTracker
;
70 // ChromeNetworkDelegate is the central point from within the chrome code to
71 // add hooks into the network stack.
72 class ChromeNetworkDelegate
: public net::NetworkDelegate
{
74 // Provides an opportunity to interpose on proxy resolution. Called before
75 // ProxyService.ResolveProxy() returns. |proxy_info| contains information
76 // about the proxy being used, and may be modified by this callback.
77 typedef base::Callback
<void(
80 const net::ProxyConfig
& data_reduction_proxy_config
,
81 const net::ProxyRetryInfoMap
& proxy_retry_info_map
,
82 const data_reduction_proxy::DataReductionProxyParams
* params
,
83 net::ProxyInfo
* result
)> OnResolveProxyHandler
;
85 // Provides an additional proxy configuration that can be consulted after
87 typedef base::Callback
<const net::ProxyConfig
&()> ProxyConfigGetter
;
89 // |enable_referrers| (and all of the other optional PrefMembers) should be
90 // initialized on the UI thread (see below) beforehand. This object's owner is
91 // responsible for cleaning them up at shutdown.
92 ChromeNetworkDelegate(extensions::EventRouterForwarder
* event_router
,
93 BooleanPrefMember
* enable_referrers
);
94 virtual ~ChromeNetworkDelegate();
96 // Pass through to ChromeExtensionsNetworkDelegate::set_extension_info_map().
97 void set_extension_info_map(extensions::InfoMap
* extension_info_map
);
99 #if defined(ENABLE_CONFIGURATION_POLICY)
100 void set_url_blacklist_manager(
101 const policy::URLBlacklistManager
* url_blacklist_manager
) {
102 url_blacklist_manager_
= url_blacklist_manager
;
106 // If |profile| is NULL or not set, events will be broadcast to all profiles,
107 // otherwise they will only be sent to the specified profile.
108 // Also pass through to ChromeExtensionsNetworkDelegate::set_profile().
109 void set_profile(void* profile
);
111 // |profile_path| is used to locate the "Downloads" folder on Chrome OS. If it
112 // is set, the location of the Downloads folder for the profile is added to
113 // the whitelist for accesses via file: scheme.
114 void set_profile_path(const base::FilePath
& profile_path
) {
115 profile_path_
= profile_path
;
118 // If |cookie_settings| is NULL or not set, all cookies are enabled,
119 // otherwise the settings are enforced on all observed network requests.
120 // Not inlined because we assign a scoped_refptr, which requires us to include
121 // the header file. Here we just forward-declare it.
122 void set_cookie_settings(CookieSettings
* cookie_settings
);
124 // Causes requested URLs to be fed to |predictor| via ConnectInterceptor.
125 void set_predictor(chrome_browser_net::Predictor
* predictor
);
127 void set_enable_do_not_track(BooleanPrefMember
* enable_do_not_track
) {
128 enable_do_not_track_
= enable_do_not_track
;
131 void set_force_google_safe_search(
132 BooleanPrefMember
* force_google_safe_search
) {
133 force_google_safe_search_
= force_google_safe_search
;
136 void set_data_reduction_proxy_enabled_pref(
137 BooleanPrefMember
* data_reduction_proxy_enabled
) {
138 data_reduction_proxy_enabled_
= data_reduction_proxy_enabled
;
141 void set_domain_reliability_monitor(
142 domain_reliability::DomainReliabilityMonitor
* monitor
) {
143 domain_reliability_monitor_
= monitor
;
146 void set_prerender_tracker(prerender::PrerenderTracker
* prerender_tracker
) {
147 prerender_tracker_
= prerender_tracker
;
150 // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
151 void set_data_reduction_proxy_params(
152 data_reduction_proxy::DataReductionProxyParams
* params
) {
153 data_reduction_proxy_params_
= params
;
156 // |data_reduction_proxy_usage_stats_| must outlive this
157 // ChromeNetworkDelegate.
158 void set_data_reduction_proxy_usage_stats(
159 data_reduction_proxy::DataReductionProxyUsageStats
* usage_stats
) {
160 data_reduction_proxy_usage_stats_
= usage_stats
;
163 // |data_reduction_proxy_auth_request_handler_| must outlive this
164 // ChromeNetworkDelegate.
165 void set_data_reduction_proxy_auth_request_handler(
166 data_reduction_proxy::DataReductionProxyAuthRequestHandler
* handler
) {
167 data_reduction_proxy_auth_request_handler_
= handler
;
170 // |data_reduction_proxy_statistics_prefs_| must outlive this
171 // ChromeNetworkDelegate.
172 void set_data_reduction_proxy_statistics_prefs(
173 data_reduction_proxy::DataReductionProxyStatisticsPrefs
*
175 data_reduction_proxy_statistics_prefs_
= statistics_prefs
;
178 void set_on_resolve_proxy_handler(OnResolveProxyHandler handler
) {
179 on_resolve_proxy_handler_
= handler
;
182 void set_proxy_config_getter(const ProxyConfigGetter
& getter
) {
183 proxy_config_getter_
= getter
;
186 // Adds the Client Hints header to HTTP requests.
187 void SetEnableClientHints();
189 // Causes |OnCanThrottleRequest| to always return false, for all
190 // instances of this object.
191 static void NeverThrottleRequests();
193 // Binds the pref members to |pref_service| and moves them to the IO thread.
194 // |enable_referrers| cannot be NULL, the others can.
195 // This method should be called on the UI thread.
196 static void InitializePrefsOnUIThread(
197 BooleanPrefMember
* enable_referrers
,
198 BooleanPrefMember
* enable_do_not_track
,
199 BooleanPrefMember
* force_google_safe_search
,
200 PrefService
* pref_service
);
202 // When called, all file:// URLs will now be accessible. If this is not
203 // called, then some platforms restrict access to file:// paths.
204 static void AllowAccessToAllFiles();
206 // Creates a Value summary of the persistent state of the network session.
207 // The caller is responsible for deleting the returned value.
208 // Must be called on the UI thread.
209 static base::Value
* HistoricNetworkStatsInfoToValue();
211 // Creates a Value summary of the state of the network session. The caller is
212 // responsible for deleting the returned value.
213 base::Value
* SessionNetworkStatsInfoToValue() const;
216 friend class ChromeNetworkDelegateTest
;
218 // NetworkDelegate implementation.
219 virtual int OnBeforeURLRequest(net::URLRequest
* request
,
220 const net::CompletionCallback
& callback
,
221 GURL
* new_url
) override
;
222 virtual void OnResolveProxy(
225 const net::ProxyService
& proxy_service
,
226 net::ProxyInfo
* result
) override
;
227 virtual void OnProxyFallback(const net::ProxyServer
& bad_proxy
,
228 int net_error
) override
;
229 virtual int OnBeforeSendHeaders(net::URLRequest
* request
,
230 const net::CompletionCallback
& callback
,
231 net::HttpRequestHeaders
* headers
) override
;
232 virtual void OnBeforeSendProxyHeaders(
233 net::URLRequest
* request
,
234 const net::ProxyInfo
& proxy_info
,
235 net::HttpRequestHeaders
* headers
) override
;
236 virtual void OnSendHeaders(net::URLRequest
* request
,
237 const net::HttpRequestHeaders
& headers
) override
;
238 virtual int OnHeadersReceived(
239 net::URLRequest
* request
,
240 const net::CompletionCallback
& callback
,
241 const net::HttpResponseHeaders
* original_response_headers
,
242 scoped_refptr
<net::HttpResponseHeaders
>* override_response_headers
,
243 GURL
* allowed_unsafe_redirect_url
) override
;
244 virtual void OnBeforeRedirect(net::URLRequest
* request
,
245 const GURL
& new_location
) override
;
246 virtual void OnResponseStarted(net::URLRequest
* request
) override
;
247 virtual void OnRawBytesRead(const net::URLRequest
& request
,
248 int bytes_read
) override
;
249 virtual void OnCompleted(net::URLRequest
* request
, bool started
) override
;
250 virtual void OnURLRequestDestroyed(net::URLRequest
* request
) override
;
251 virtual void OnPACScriptError(int line_number
,
252 const base::string16
& error
) override
;
253 virtual net::NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
254 net::URLRequest
* request
,
255 const net::AuthChallengeInfo
& auth_info
,
256 const AuthCallback
& callback
,
257 net::AuthCredentials
* credentials
) override
;
258 virtual bool OnCanGetCookies(const net::URLRequest
& request
,
259 const net::CookieList
& cookie_list
) override
;
260 virtual bool OnCanSetCookie(const net::URLRequest
& request
,
261 const std::string
& cookie_line
,
262 net::CookieOptions
* options
) override
;
263 virtual bool OnCanAccessFile(const net::URLRequest
& request
,
264 const base::FilePath
& path
) const override
;
265 virtual bool OnCanThrottleRequest(
266 const net::URLRequest
& request
) const override
;
267 virtual bool OnCanEnablePrivacyMode(
269 const GURL
& first_party_for_cookies
) const override
;
270 virtual int OnBeforeSocketStreamConnect(
271 net::SocketStream
* stream
,
272 const net::CompletionCallback
& callback
) override
;
273 virtual bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
274 const net::URLRequest
& request
,
275 const GURL
& target_url
,
276 const GURL
& referrer_url
) const override
;
278 void AccumulateContentLength(
279 int64 received_payload_byte_count
,
280 int64 original_payload_byte_count
,
281 data_reduction_proxy::DataReductionProxyRequestType request_type
);
283 scoped_ptr
<ChromeExtensionsNetworkDelegate
> extensions_delegate_
;
286 base::FilePath profile_path_
;
287 scoped_refptr
<CookieSettings
> cookie_settings_
;
289 scoped_ptr
<chrome_browser_net::ConnectInterceptor
> connect_interceptor_
;
291 // Weak, owned by our owner.
292 BooleanPrefMember
* enable_referrers_
;
293 BooleanPrefMember
* enable_do_not_track_
;
294 BooleanPrefMember
* force_google_safe_search_
;
295 BooleanPrefMember
* data_reduction_proxy_enabled_
;
297 // Weak, owned by our owner.
298 #if defined(ENABLE_CONFIGURATION_POLICY)
299 const policy::URLBlacklistManager
* url_blacklist_manager_
;
301 domain_reliability::DomainReliabilityMonitor
* domain_reliability_monitor_
;
303 // When true, allow access to all file:// URLs.
304 static bool g_allow_file_access_
;
306 // True if OnCanThrottleRequest should always return false.
308 // Note: This needs to be static as the instance of
309 // ChromeNetworkDelegate used may change over time, and we need to
310 // set this variable once at start-up time. It is effectively
311 // static anyway since it is based on a command-line flag.
312 static bool g_never_throttle_requests_
;
314 // Total size of all content (excluding headers) that has been received
316 int64 received_content_length_
;
318 // Total original size of all content before it was transferred.
319 int64 original_content_length_
;
321 scoped_ptr
<ClientHints
> client_hints_
;
325 prerender::PrerenderTracker
* prerender_tracker_
;
327 // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
328 data_reduction_proxy::DataReductionProxyParams
* data_reduction_proxy_params_
;
329 // |data_reduction_proxy_usage_stats_| must outlive this
330 // ChromeNetworkDelegate.
331 data_reduction_proxy::DataReductionProxyUsageStats
*
332 data_reduction_proxy_usage_stats_
;
333 data_reduction_proxy::DataReductionProxyAuthRequestHandler
*
334 data_reduction_proxy_auth_request_handler_
;
335 data_reduction_proxy::DataReductionProxyStatisticsPrefs
*
336 data_reduction_proxy_statistics_prefs_
;
338 OnResolveProxyHandler on_resolve_proxy_handler_
;
339 ProxyConfigGetter proxy_config_getter_
;
341 DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate
);
344 #endif // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_