Update To 11.40.268.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/core/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. 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(
80       const GURL& url,
81       int load_flags,
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;
87
88   // Provides an additional proxy configuration that can be consulted after
89   // proxy resolution.
90   typedef base::Callback<const net::ProxyConfig&()> ProxyConfigGetter;
91
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;
98
99   // Pass through to ChromeExtensionsNetworkDelegate::set_extension_info_map().
100   void set_extension_info_map(extensions::InfoMap* extension_info_map);
101
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;
106   }
107 #endif
108
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);
113
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;
119   }
120
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);
126
127   // Causes requested URLs to be fed to |predictor| via ConnectInterceptor.
128   void set_predictor(chrome_browser_net::Predictor* predictor);
129
130   void set_enable_do_not_track(BooleanPrefMember* enable_do_not_track) {
131     enable_do_not_track_ = enable_do_not_track;
132   }
133
134   void set_force_google_safe_search(
135       BooleanPrefMember* force_google_safe_search) {
136     force_google_safe_search_ = force_google_safe_search;
137   }
138
139   void set_data_reduction_proxy_enabled_pref(
140       BooleanPrefMember* data_reduction_proxy_enabled) {
141     data_reduction_proxy_enabled_ = data_reduction_proxy_enabled;
142   }
143
144   void set_domain_reliability_monitor(
145       domain_reliability::DomainReliabilityMonitor* monitor) {
146     domain_reliability_monitor_ = monitor;
147   }
148
149   void set_prerender_tracker(prerender::PrerenderTracker* prerender_tracker) {
150     prerender_tracker_ = prerender_tracker;
151   }
152
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;
157   }
158
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;
164   }
165
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;
171   }
172
173   // |data_reduction_proxy_statistics_prefs_| must outlive this
174   // ChromeNetworkDelegate.
175   void set_data_reduction_proxy_statistics_prefs(
176       data_reduction_proxy::DataReductionProxyStatisticsPrefs*
177           statistics_prefs) {
178     data_reduction_proxy_statistics_prefs_ = statistics_prefs;
179   }
180
181   void set_on_resolve_proxy_handler(OnResolveProxyHandler handler) {
182     on_resolve_proxy_handler_ = handler;
183   }
184
185   void set_proxy_config_getter(const ProxyConfigGetter& getter) {
186     proxy_config_getter_ = getter;
187   }
188
189   // Adds the Client Hints header to HTTP requests.
190   void SetEnableClientHints();
191
192   // Causes |OnCanThrottleRequest| to always return false, for all
193   // instances of this object.
194   static void NeverThrottleRequests();
195
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);
204
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();
208
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);
213
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;
217
218  private:
219   friend class ChromeNetworkDelegateTest;
220
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,
226                       int load_flags,
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(
266       const GURL& url,
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;
272
273   void AccumulateContentLength(
274       int64 received_payload_byte_count,
275       int64 original_payload_byte_count,
276       data_reduction_proxy::DataReductionProxyRequestType request_type);
277
278   scoped_ptr<ChromeExtensionsNetworkDelegate> extensions_delegate_;
279
280   void* profile_;
281   base::FilePath profile_path_;
282   scoped_refptr<CookieSettings> cookie_settings_;
283
284   scoped_ptr<chrome_browser_net::ConnectInterceptor> connect_interceptor_;
285
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_;
291
292   // Weak, owned by our owner.
293 #if defined(ENABLE_CONFIGURATION_POLICY)
294   const policy::URLBlacklistManager* url_blacklist_manager_;
295 #endif
296   domain_reliability::DomainReliabilityMonitor* domain_reliability_monitor_;
297
298   // When true, allow access to all file:// URLs.
299   static bool g_allow_file_access_;
300
301   // True if OnCanThrottleRequest should always return false.
302   //
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_;
308
309   // Total size of all content (excluding headers) that has been received
310   // over the network.
311   int64 received_content_length_;
312
313   // Total original size of all content before it was transferred.
314   int64 original_content_length_;
315
316   scoped_ptr<ClientHints> client_hints_;
317
318   bool first_request_;
319
320   prerender::PrerenderTracker* prerender_tracker_;
321
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_;
332
333   OnResolveProxyHandler on_resolve_proxy_handler_;
334   ProxyConfigGetter proxy_config_getter_;
335
336   DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
337 };
338
339 #endif  // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_