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