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/browser/data_reduction_proxy_metrics.h"
17 #include "net/base/network_delegate.h"
22 template<class T
> class PrefMember
;
24 typedef PrefMember
<bool> BooleanPrefMember
;
30 namespace chrome_browser_net
{
31 class ConnectInterceptor
;
35 namespace domain_reliability
{
36 class DomainReliabilityMonitor
;
37 } // namespace domain_reliability
39 namespace extensions
{
40 class EventRouterForwarder
;
49 class URLBlacklistManager
;
52 // ChromeNetworkDelegate is the central point from within the chrome code to
53 // add hooks into the network stack.
54 class ChromeNetworkDelegate
: public net::NetworkDelegate
{
56 // |enable_referrers| (and all of the other optional PrefMembers) should be
57 // initialized on the UI thread (see below) beforehand. This object's owner is
58 // responsible for cleaning them up at shutdown.
59 ChromeNetworkDelegate(extensions::EventRouterForwarder
* event_router
,
60 BooleanPrefMember
* enable_referrers
);
61 virtual ~ChromeNetworkDelegate();
63 // Not inlined because we assign a scoped_refptr, which requires us to include
65 void set_extension_info_map(extensions::InfoMap
* extension_info_map
);
67 void set_url_blacklist_manager(
68 const policy::URLBlacklistManager
* url_blacklist_manager
) {
69 url_blacklist_manager_
= url_blacklist_manager
;
72 // If |profile| is NULL or not set, events will be broadcast to all profiles,
73 // otherwise they will only be sent to the specified profile.
74 void set_profile(void* profile
) {
78 // |profile_path| is used to locate the "Downloads" folder on Chrome OS. If it
79 // is set, the location of the Downloads folder for the profile is added to
80 // the whitelist for accesses via file: scheme.
81 void set_profile_path(const base::FilePath
& profile_path
) {
82 profile_path_
= profile_path
;
85 // If |cookie_settings| is NULL or not set, all cookies are enabled,
86 // otherwise the settings are enforced on all observed network requests.
87 // Not inlined because we assign a scoped_refptr, which requires us to include
88 // the header file. Here we just forward-declare it.
89 void set_cookie_settings(CookieSettings
* cookie_settings
);
91 // Causes requested URLs to be fed to |predictor| via ConnectInterceptor.
92 void set_predictor(chrome_browser_net::Predictor
* predictor
);
94 void set_enable_do_not_track(BooleanPrefMember
* enable_do_not_track
) {
95 enable_do_not_track_
= enable_do_not_track
;
98 void set_force_google_safe_search(
99 BooleanPrefMember
* force_google_safe_search
) {
100 force_google_safe_search_
= force_google_safe_search
;
103 void set_domain_reliability_monitor(
104 domain_reliability::DomainReliabilityMonitor
*
105 domain_reliability_monitor
) {
106 domain_reliability_monitor_
= domain_reliability_monitor
;
109 // Adds the Client Hints header to HTTP requests.
110 void SetEnableClientHints();
112 // Causes |OnCanThrottleRequest| to always return false, for all
113 // instances of this object.
114 static void NeverThrottleRequests();
116 // Binds the pref members to |pref_service| and moves them to the IO thread.
117 // |enable_referrers| cannot be NULL, the others can.
118 // This method should be called on the UI thread.
119 static void InitializePrefsOnUIThread(
120 BooleanPrefMember
* enable_referrers
,
121 BooleanPrefMember
* enable_do_not_track
,
122 BooleanPrefMember
* force_google_safe_search
,
123 PrefService
* pref_service
);
125 // When called, all file:// URLs will now be accessible. If this is not
126 // called, then some platforms restrict access to file:// paths.
127 static void AllowAccessToAllFiles();
129 // Creates a Value summary of the persistent state of the network session.
130 // The caller is responsible for deleting the returned value.
131 // Must be called on the UI thread.
132 static base::Value
* HistoricNetworkStatsInfoToValue();
134 // Creates a Value summary of the state of the network session. The caller is
135 // responsible for deleting the returned value.
136 base::Value
* SessionNetworkStatsInfoToValue() const;
139 friend class ChromeNetworkDelegateTest
;
141 // NetworkDelegate implementation.
142 virtual int OnBeforeURLRequest(net::URLRequest
* request
,
143 const net::CompletionCallback
& callback
,
144 GURL
* new_url
) OVERRIDE
;
145 virtual int OnBeforeSendHeaders(net::URLRequest
* request
,
146 const net::CompletionCallback
& callback
,
147 net::HttpRequestHeaders
* headers
) OVERRIDE
;
148 virtual void OnSendHeaders(net::URLRequest
* request
,
149 const net::HttpRequestHeaders
& headers
) OVERRIDE
;
150 virtual int OnHeadersReceived(
151 net::URLRequest
* request
,
152 const net::CompletionCallback
& callback
,
153 const net::HttpResponseHeaders
* original_response_headers
,
154 scoped_refptr
<net::HttpResponseHeaders
>* override_response_headers
,
155 GURL
* allowed_unsafe_redirect_url
) OVERRIDE
;
156 virtual void OnBeforeRedirect(net::URLRequest
* request
,
157 const GURL
& new_location
) OVERRIDE
;
158 virtual void OnResponseStarted(net::URLRequest
* request
) OVERRIDE
;
159 virtual void OnRawBytesRead(const net::URLRequest
& request
,
160 int bytes_read
) OVERRIDE
;
161 virtual void OnCompleted(net::URLRequest
* request
, bool started
) OVERRIDE
;
162 virtual void OnURLRequestDestroyed(net::URLRequest
* request
) OVERRIDE
;
163 virtual void OnPACScriptError(int line_number
,
164 const base::string16
& error
) OVERRIDE
;
165 virtual net::NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
166 net::URLRequest
* request
,
167 const net::AuthChallengeInfo
& auth_info
,
168 const AuthCallback
& callback
,
169 net::AuthCredentials
* credentials
) OVERRIDE
;
170 virtual bool OnCanGetCookies(const net::URLRequest
& request
,
171 const net::CookieList
& cookie_list
) OVERRIDE
;
172 virtual bool OnCanSetCookie(const net::URLRequest
& request
,
173 const std::string
& cookie_line
,
174 net::CookieOptions
* options
) OVERRIDE
;
175 virtual bool OnCanAccessFile(const net::URLRequest
& request
,
176 const base::FilePath
& path
) const OVERRIDE
;
177 virtual bool OnCanThrottleRequest(
178 const net::URLRequest
& request
) const OVERRIDE
;
179 virtual bool OnCanEnablePrivacyMode(
181 const GURL
& first_party_for_cookies
) const OVERRIDE
;
182 virtual int OnBeforeSocketStreamConnect(
183 net::SocketStream
* stream
,
184 const net::CompletionCallback
& callback
) OVERRIDE
;
186 void AccumulateContentLength(
187 int64 received_payload_byte_count
,
188 int64 original_payload_byte_count
,
189 data_reduction_proxy::DataReductionProxyRequestType request_type
);
191 scoped_refptr
<extensions::EventRouterForwarder
> event_router_
;
193 base::FilePath profile_path_
;
194 scoped_refptr
<CookieSettings
> cookie_settings_
;
196 scoped_refptr
<extensions::InfoMap
> extension_info_map_
;
198 scoped_ptr
<chrome_browser_net::ConnectInterceptor
> connect_interceptor_
;
200 // Weak, owned by our owner.
201 BooleanPrefMember
* enable_referrers_
;
202 BooleanPrefMember
* enable_do_not_track_
;
203 BooleanPrefMember
* force_google_safe_search_
;
205 // Weak, owned by our owner.
206 const policy::URLBlacklistManager
* url_blacklist_manager_
;
207 domain_reliability::DomainReliabilityMonitor
* domain_reliability_monitor_
;
209 // When true, allow access to all file:// URLs.
210 static bool g_allow_file_access_
;
212 // True if OnCanThrottleRequest should always return false.
214 // Note: This needs to be static as the instance of
215 // ChromeNetworkDelegate used may change over time, and we need to
216 // set this variable once at start-up time. It is effectively
217 // static anyway since it is based on a command-line flag.
218 static bool g_never_throttle_requests_
;
220 // Total size of all content (excluding headers) that has been received
222 int64 received_content_length_
;
224 // Total original size of all content before it was transferred.
225 int64 original_content_length_
;
227 scoped_ptr
<ClientHints
> client_hints_
;
231 DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate
);
234 #endif // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_