Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / net / chrome_network_delegate.h
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 "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h"
17 #include "net/base/network_delegate.h"
18 #include "net/proxy/proxy_retry_info.h"
19
20 class ChromeExtensionsNetworkDelegate;
21 class ClientHints;
22 class CookieSettings;
23 class PrefService;
24
25 template<class T> class PrefMember;
26
27 typedef PrefMember<bool> BooleanPrefMember;
28
29 namespace base {
30 class Value;
31 }
32
33 namespace chrome_browser_net {
34 class ConnectInterceptor;
35 class Predictor;
36 }
37
38 namespace data_reduction_proxy {
39 class DataReductionProxyAuthRequestHandler;
40 class DataReductionProxyParams;
41 class DataReductionProxyStatisticsPrefs;
42 class DataReductionProxyUsageStats;
43 }
44
45 namespace domain_reliability {
46 class DomainReliabilityMonitor;
47 }
48
49 namespace extensions {
50 class EventRouterForwarder;
51 class InfoMap;
52 }
53
54 namespace net {
55 class ProxyConfig;
56 class ProxyInfo;
57 class ProxyServer;
58 class ProxyService;
59 class URLRequest;
60 }
61
62 namespace policy {
63 class URLBlacklistManager;
64 }
65
66 namespace prerender {
67 class PrerenderTracker;
68 }
69
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 {
73  public:
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(
78       const GURL& url,
79       int load_flags,
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;
84
85   // Provides an additional proxy configuration that can be consulted after
86   // proxy resolution.
87   typedef base::Callback<const net::ProxyConfig&()> ProxyConfigGetter;
88
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();
95
96   // Pass through to ChromeExtensionsNetworkDelegate::set_extension_info_map().
97   void set_extension_info_map(extensions::InfoMap* extension_info_map);
98
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;
103   }
104 #endif
105
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);
110
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;
116   }
117
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);
123
124   // Causes requested URLs to be fed to |predictor| via ConnectInterceptor.
125   void set_predictor(chrome_browser_net::Predictor* predictor);
126
127   void set_enable_do_not_track(BooleanPrefMember* enable_do_not_track) {
128     enable_do_not_track_ = enable_do_not_track;
129   }
130
131   void set_force_google_safe_search(
132       BooleanPrefMember* force_google_safe_search) {
133     force_google_safe_search_ = force_google_safe_search;
134   }
135
136   void set_data_reduction_proxy_enabled_pref(
137       BooleanPrefMember* data_reduction_proxy_enabled) {
138     data_reduction_proxy_enabled_ = data_reduction_proxy_enabled;
139   }
140
141   void set_domain_reliability_monitor(
142       domain_reliability::DomainReliabilityMonitor* monitor) {
143     domain_reliability_monitor_ = monitor;
144   }
145
146   void set_prerender_tracker(prerender::PrerenderTracker* prerender_tracker) {
147     prerender_tracker_ = prerender_tracker;
148   }
149
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;
154   }
155
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;
161   }
162
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;
168   }
169
170   // |data_reduction_proxy_statistics_prefs_| must outlive this
171   // ChromeNetworkDelegate.
172   void set_data_reduction_proxy_statistics_prefs(
173       data_reduction_proxy::DataReductionProxyStatisticsPrefs*
174           statistics_prefs) {
175     data_reduction_proxy_statistics_prefs_ = statistics_prefs;
176   }
177
178   void set_on_resolve_proxy_handler(OnResolveProxyHandler handler) {
179     on_resolve_proxy_handler_ = handler;
180   }
181
182   void set_proxy_config_getter(const ProxyConfigGetter& getter) {
183     proxy_config_getter_ = getter;
184   }
185
186   // Adds the Client Hints header to HTTP requests.
187   void SetEnableClientHints();
188
189   // Causes |OnCanThrottleRequest| to always return false, for all
190   // instances of this object.
191   static void NeverThrottleRequests();
192
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);
201
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();
205
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();
210
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;
214
215  private:
216   friend class ChromeNetworkDelegateTest;
217
218   // NetworkDelegate implementation.
219   virtual int OnBeforeURLRequest(net::URLRequest* request,
220                                  const net::CompletionCallback& callback,
221                                  GURL* new_url) OVERRIDE;
222   virtual void OnResolveProxy(
223       const GURL& url,
224       int load_flags,
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(
268       const GURL& url,
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;
277
278   void AccumulateContentLength(
279       int64 received_payload_byte_count,
280       int64 original_payload_byte_count,
281       data_reduction_proxy::DataReductionProxyRequestType request_type);
282
283   scoped_ptr<ChromeExtensionsNetworkDelegate> extensions_delegate_;
284
285   void* profile_;
286   base::FilePath profile_path_;
287   scoped_refptr<CookieSettings> cookie_settings_;
288
289   scoped_ptr<chrome_browser_net::ConnectInterceptor> connect_interceptor_;
290
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_;
296
297   // Weak, owned by our owner.
298 #if defined(ENABLE_CONFIGURATION_POLICY)
299   const policy::URLBlacklistManager* url_blacklist_manager_;
300 #endif
301   domain_reliability::DomainReliabilityMonitor* domain_reliability_monitor_;
302
303   // When true, allow access to all file:// URLs.
304   static bool g_allow_file_access_;
305
306   // True if OnCanThrottleRequest should always return false.
307   //
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_;
313
314   // Total size of all content (excluding headers) that has been received
315   // over the network.
316   int64 received_content_length_;
317
318   // Total original size of all content before it was transferred.
319   int64 original_content_length_;
320
321   scoped_ptr<ClientHints> client_hints_;
322
323   bool first_request_;
324
325   prerender::PrerenderTracker* prerender_tracker_;
326
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_;
337
338   OnResolveProxyHandler on_resolve_proxy_handler_;
339   ProxyConfigGetter proxy_config_getter_;
340
341   DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
342 };
343
344 #endif  // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_