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. Two proxy configurations are provided
76 // that specify the data reduction proxy's configuration and the effective
77 // configuration according to the proxy service, respectively. Retry info is
78 // presumed to be from the proxy service.
79 typedef base::Callback<void(
82 const net::ProxyConfig& data_reduction_proxy_config,
83 const net::ProxyConfig& proxy_service_proxy_config,
84 const net::ProxyRetryInfoMap& proxy_retry_info_map,
85 const data_reduction_proxy::DataReductionProxyParams* params,
86 net::ProxyInfo* result)> OnResolveProxyHandler;
88 // Provides an additional proxy configuration that can be consulted after
90 typedef base::Callback<const net::ProxyConfig&()> ProxyConfigGetter;
92 // |enable_referrers| (and all of the other optional PrefMembers) should be
93 // initialized on the UI thread (see below) beforehand. This object's owner is
94 // responsible for cleaning them up at shutdown.
95 ChromeNetworkDelegate(extensions::EventRouterForwarder* event_router,
96 BooleanPrefMember* enable_referrers);
97 ~ChromeNetworkDelegate() override;
99 // Pass through to ChromeExtensionsNetworkDelegate::set_extension_info_map().
100 void set_extension_info_map(extensions::InfoMap* extension_info_map);
102 #if defined(ENABLE_CONFIGURATION_POLICY)
103 void set_url_blacklist_manager(
104 const policy::URLBlacklistManager* url_blacklist_manager) {
105 url_blacklist_manager_ = url_blacklist_manager;
109 // If |profile| is NULL or not set, events will be broadcast to all profiles,
110 // otherwise they will only be sent to the specified profile.
111 // Also pass through to ChromeExtensionsNetworkDelegate::set_profile().
112 void set_profile(void* profile);
114 // |profile_path| is used to locate the "Downloads" folder on Chrome OS. If it
115 // is set, the location of the Downloads folder for the profile is added to
116 // the whitelist for accesses via file: scheme.
117 void set_profile_path(const base::FilePath& profile_path) {
118 profile_path_ = profile_path;
121 // If |cookie_settings| is NULL or not set, all cookies are enabled,
122 // otherwise the settings are enforced on all observed network requests.
123 // Not inlined because we assign a scoped_refptr, which requires us to include
124 // the header file. Here we just forward-declare it.
125 void set_cookie_settings(CookieSettings* cookie_settings);
127 // Causes requested URLs to be fed to |predictor| via ConnectInterceptor.
128 void set_predictor(chrome_browser_net::Predictor* predictor);
130 void set_enable_do_not_track(BooleanPrefMember* enable_do_not_track) {
131 enable_do_not_track_ = enable_do_not_track;
134 void set_force_google_safe_search(
135 BooleanPrefMember* force_google_safe_search) {
136 force_google_safe_search_ = force_google_safe_search;
139 void set_data_reduction_proxy_enabled_pref(
140 BooleanPrefMember* data_reduction_proxy_enabled) {
141 data_reduction_proxy_enabled_ = data_reduction_proxy_enabled;
144 void set_domain_reliability_monitor(
145 domain_reliability::DomainReliabilityMonitor* monitor) {
146 domain_reliability_monitor_ = monitor;
149 void set_prerender_tracker(prerender::PrerenderTracker* prerender_tracker) {
150 prerender_tracker_ = prerender_tracker;
153 // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
154 void set_data_reduction_proxy_params(
155 data_reduction_proxy::DataReductionProxyParams* params) {
156 data_reduction_proxy_params_ = params;
159 // |data_reduction_proxy_usage_stats_| must outlive this
160 // ChromeNetworkDelegate.
161 void set_data_reduction_proxy_usage_stats(
162 data_reduction_proxy::DataReductionProxyUsageStats* usage_stats) {
163 data_reduction_proxy_usage_stats_ = usage_stats;
166 // |data_reduction_proxy_auth_request_handler_| must outlive this
167 // ChromeNetworkDelegate.
168 void set_data_reduction_proxy_auth_request_handler(
169 data_reduction_proxy::DataReductionProxyAuthRequestHandler* handler) {
170 data_reduction_proxy_auth_request_handler_ = handler;
173 // |data_reduction_proxy_statistics_prefs_| must outlive this
174 // ChromeNetworkDelegate.
175 void set_data_reduction_proxy_statistics_prefs(
176 data_reduction_proxy::DataReductionProxyStatisticsPrefs*
178 data_reduction_proxy_statistics_prefs_ = statistics_prefs;
181 void set_on_resolve_proxy_handler(OnResolveProxyHandler handler) {
182 on_resolve_proxy_handler_ = handler;
185 void set_proxy_config_getter(const ProxyConfigGetter& getter) {
186 proxy_config_getter_ = getter;
189 // Adds the Client Hints header to HTTP requests.
190 void SetEnableClientHints();
192 // Causes |OnCanThrottleRequest| to always return false, for all
193 // instances of this object.
194 static void NeverThrottleRequests();
196 // Binds the pref members to |pref_service| and moves them to the IO thread.
197 // |enable_referrers| cannot be NULL, the others can.
198 // This method should be called on the UI thread.
199 static void InitializePrefsOnUIThread(
200 BooleanPrefMember* enable_referrers,
201 BooleanPrefMember* enable_do_not_track,
202 BooleanPrefMember* force_google_safe_search,
203 PrefService* pref_service);
205 // When called, all file:// URLs will now be accessible. If this is not
206 // called, then some platforms restrict access to file:// paths.
207 static void AllowAccessToAllFiles();
209 // Creates a Value summary of the persistent state of the network session.
210 // The caller is responsible for deleting the returned value.
211 // Must be called on the UI thread.
212 static base::Value* HistoricNetworkStatsInfoToValue(PrefService* prefs);
214 // Creates a Value summary of the state of the network session. The caller is
215 // responsible for deleting the returned value.
216 base::Value* SessionNetworkStatsInfoToValue() const;
219 friend class ChromeNetworkDelegateTest;
221 // NetworkDelegate implementation.
222 int OnBeforeURLRequest(net::URLRequest* request,
223 const net::CompletionCallback& callback,
224 GURL* new_url) override;
225 void OnResolveProxy(const GURL& url,
227 const net::ProxyService& proxy_service,
228 net::ProxyInfo* result) override;
229 void OnProxyFallback(const net::ProxyServer& bad_proxy,
230 int net_error) override;
231 int OnBeforeSendHeaders(net::URLRequest* request,
232 const net::CompletionCallback& callback,
233 net::HttpRequestHeaders* headers) override;
234 void OnBeforeSendProxyHeaders(net::URLRequest* request,
235 const net::ProxyInfo& proxy_info,
236 net::HttpRequestHeaders* headers) override;
237 void OnSendHeaders(net::URLRequest* request,
238 const net::HttpRequestHeaders& headers) override;
239 int OnHeadersReceived(
240 net::URLRequest* request,
241 const net::CompletionCallback& callback,
242 const net::HttpResponseHeaders* original_response_headers,
243 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
244 GURL* allowed_unsafe_redirect_url) override;
245 void OnBeforeRedirect(net::URLRequest* request,
246 const GURL& new_location) override;
247 void OnResponseStarted(net::URLRequest* request) override;
248 void OnRawBytesRead(const net::URLRequest& request, int bytes_read) override;
249 void OnCompleted(net::URLRequest* request, bool started) override;
250 void OnURLRequestDestroyed(net::URLRequest* request) override;
251 void OnPACScriptError(int line_number, const base::string16& error) override;
252 net::NetworkDelegate::AuthRequiredResponse OnAuthRequired(
253 net::URLRequest* request,
254 const net::AuthChallengeInfo& auth_info,
255 const AuthCallback& callback,
256 net::AuthCredentials* credentials) override;
257 bool OnCanGetCookies(const net::URLRequest& request,
258 const net::CookieList& cookie_list) override;
259 bool OnCanSetCookie(const net::URLRequest& request,
260 const std::string& cookie_line,
261 net::CookieOptions* options) override;
262 bool OnCanAccessFile(const net::URLRequest& request,
263 const base::FilePath& path) const override;
264 bool OnCanThrottleRequest(const net::URLRequest& request) const override;
265 bool OnCanEnablePrivacyMode(
267 const GURL& first_party_for_cookies) const override;
268 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
269 const net::URLRequest& request,
270 const GURL& target_url,
271 const GURL& referrer_url) const override;
273 void AccumulateContentLength(
274 int64 received_payload_byte_count,
275 int64 original_payload_byte_count,
276 data_reduction_proxy::DataReductionProxyRequestType request_type);
278 scoped_ptr<ChromeExtensionsNetworkDelegate> extensions_delegate_;
281 base::FilePath profile_path_;
282 scoped_refptr<CookieSettings> cookie_settings_;
284 scoped_ptr<chrome_browser_net::ConnectInterceptor> connect_interceptor_;
286 // Weak, owned by our owner.
287 BooleanPrefMember* enable_referrers_;
288 BooleanPrefMember* enable_do_not_track_;
289 BooleanPrefMember* force_google_safe_search_;
290 BooleanPrefMember* data_reduction_proxy_enabled_;
292 // Weak, owned by our owner.
293 #if defined(ENABLE_CONFIGURATION_POLICY)
294 const policy::URLBlacklistManager* url_blacklist_manager_;
296 domain_reliability::DomainReliabilityMonitor* domain_reliability_monitor_;
298 // When true, allow access to all file:// URLs.
299 static bool g_allow_file_access_;
301 // True if OnCanThrottleRequest should always return false.
303 // Note: This needs to be static as the instance of
304 // ChromeNetworkDelegate used may change over time, and we need to
305 // set this variable once at start-up time. It is effectively
306 // static anyway since it is based on a command-line flag.
307 static bool g_never_throttle_requests_;
309 // Total size of all content (excluding headers) that has been received
311 int64 received_content_length_;
313 // Total original size of all content before it was transferred.
314 int64 original_content_length_;
316 scoped_ptr<ClientHints> client_hints_;
320 prerender::PrerenderTracker* prerender_tracker_;
322 // |data_reduction_proxy_params_| must outlive this ChromeNetworkDelegate.
323 data_reduction_proxy::DataReductionProxyParams* data_reduction_proxy_params_;
324 // |data_reduction_proxy_usage_stats_| must outlive this
325 // ChromeNetworkDelegate.
326 data_reduction_proxy::DataReductionProxyUsageStats*
327 data_reduction_proxy_usage_stats_;
328 data_reduction_proxy::DataReductionProxyAuthRequestHandler*
329 data_reduction_proxy_auth_request_handler_;
330 data_reduction_proxy::DataReductionProxyStatisticsPrefs*
331 data_reduction_proxy_statistics_prefs_;
333 OnResolveProxyHandler on_resolve_proxy_handler_;
334 ProxyConfigGetter proxy_config_getter_;
336 DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
339 #endif // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_