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/memory/ref_counted.h"
13 #include "base/values.h"
14 #include "net/base/network_delegate.h"
17 class ExtensionInfoMap
;
19 template<class T
> class PrefMember
;
21 typedef PrefMember
<bool> BooleanPrefMember
;
27 namespace chrome_browser_net
{
28 class ConnectInterceptor
;
33 namespace extensions
{
34 class EventRouterForwarder
;
42 class URLBlacklistManager
;
45 // ChromeNetworkDelegate is the central point from within the chrome code to
46 // add hooks into the network stack.
47 class ChromeNetworkDelegate
: public net::NetworkDelegate
{
49 // |enable_referrers| (and all of the other optional PrefMembers) should be
50 // initialized on the UI thread (see below) beforehand. This object's owner is
51 // responsible for cleaning them up at shutdown.
52 ChromeNetworkDelegate(extensions::EventRouterForwarder
* event_router
,
53 BooleanPrefMember
* enable_referrers
);
54 virtual ~ChromeNetworkDelegate();
56 // Not inlined because we assign a scoped_refptr, which requires us to include
58 void set_extension_info_map(ExtensionInfoMap
* extension_info_map
);
60 void set_url_blacklist_manager(
61 const policy::URLBlacklistManager
* url_blacklist_manager
) {
62 url_blacklist_manager_
= url_blacklist_manager
;
65 // If |profile| is NULL or not set, events will be broadcast to all profiles,
66 // otherwise they will only be sent to the specified profile.
67 void set_profile(void* profile
) {
71 // If |cookie_settings| is NULL or not set, all cookies are enabled,
72 // otherwise the settings are enforced on all observed network requests.
73 // Not inlined because we assign a scoped_refptr, which requires us to include
74 // the header file. Here we just forward-declare it.
75 void set_cookie_settings(CookieSettings
* cookie_settings
);
77 // Causes requested URLs to be fed to |predictor| via ConnectInterceptor.
78 void set_predictor(chrome_browser_net::Predictor
* predictor
);
80 void set_load_time_stats(chrome_browser_net::LoadTimeStats
* load_time_stats
) {
81 load_time_stats_
= load_time_stats
;
84 void set_enable_do_not_track(BooleanPrefMember
* enable_do_not_track
) {
85 enable_do_not_track_
= enable_do_not_track
;
88 void set_force_google_safe_search(
89 BooleanPrefMember
* force_google_safe_search
) {
90 force_google_safe_search_
= force_google_safe_search
;
93 // Causes |OnCanThrottleRequest| to always return false, for all
94 // instances of this object.
95 static void NeverThrottleRequests();
97 // Binds the pref members to |pref_service| and moves them to the IO thread.
98 // |enable_referrers| cannot be NULL, the others can.
99 // This method should be called on the UI thread.
100 static void InitializePrefsOnUIThread(
101 BooleanPrefMember
* enable_referrers
,
102 BooleanPrefMember
* enable_do_not_track
,
103 BooleanPrefMember
* force_google_safe_search
,
104 PrefService
* pref_service
);
106 // When called, all file:// URLs will now be accessible. If this is not
107 // called, then some platforms restrict access to file:// paths.
108 static void AllowAccessToAllFiles();
110 // Creates a Value summary of the persistent state of the network session.
111 // The caller is responsible for deleting the returned value.
112 // Must be called on the UI thread.
113 static Value
* HistoricNetworkStatsInfoToValue();
115 // Creates a Value summary of the state of the network session. The caller is
116 // responsible for deleting the returned value.
117 Value
* SessionNetworkStatsInfoToValue() const;
120 friend class ChromeNetworkDelegateTest
;
122 // NetworkDelegate implementation.
123 virtual int OnBeforeURLRequest(net::URLRequest
* request
,
124 const net::CompletionCallback
& callback
,
125 GURL
* new_url
) OVERRIDE
;
126 virtual int OnBeforeSendHeaders(net::URLRequest
* request
,
127 const net::CompletionCallback
& callback
,
128 net::HttpRequestHeaders
* headers
) OVERRIDE
;
129 virtual void OnSendHeaders(net::URLRequest
* request
,
130 const net::HttpRequestHeaders
& headers
) OVERRIDE
;
131 virtual int OnHeadersReceived(
132 net::URLRequest
* request
,
133 const net::CompletionCallback
& callback
,
134 const net::HttpResponseHeaders
* original_response_headers
,
135 scoped_refptr
<net::HttpResponseHeaders
>* override_response_headers
)
137 virtual void OnBeforeRedirect(net::URLRequest
* request
,
138 const GURL
& new_location
) OVERRIDE
;
139 virtual void OnResponseStarted(net::URLRequest
* request
) OVERRIDE
;
140 virtual void OnRawBytesRead(const net::URLRequest
& request
,
141 int bytes_read
) OVERRIDE
;
142 virtual void OnCompleted(net::URLRequest
* request
, bool started
) OVERRIDE
;
143 virtual void OnURLRequestDestroyed(net::URLRequest
* request
) OVERRIDE
;
144 virtual void OnPACScriptError(int line_number
,
145 const string16
& error
) OVERRIDE
;
146 virtual net::NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
147 net::URLRequest
* request
,
148 const net::AuthChallengeInfo
& auth_info
,
149 const AuthCallback
& callback
,
150 net::AuthCredentials
* credentials
) OVERRIDE
;
151 virtual bool OnCanGetCookies(const net::URLRequest
& request
,
152 const net::CookieList
& cookie_list
) OVERRIDE
;
153 virtual bool OnCanSetCookie(const net::URLRequest
& request
,
154 const std::string
& cookie_line
,
155 net::CookieOptions
* options
) OVERRIDE
;
156 virtual bool OnCanAccessFile(const net::URLRequest
& request
,
157 const base::FilePath
& path
) const OVERRIDE
;
158 virtual bool OnCanThrottleRequest(
159 const net::URLRequest
& request
) const OVERRIDE
;
160 virtual bool OnCanEnablePrivacyMode(
162 const GURL
& first_party_for_cookies
) const OVERRIDE
;
163 virtual int OnBeforeSocketStreamConnect(
164 net::SocketStream
* stream
,
165 const net::CompletionCallback
& callback
) OVERRIDE
;
166 virtual void OnRequestWaitStateChange(const net::URLRequest
& request
,
167 RequestWaitState state
) OVERRIDE
;
169 void AccumulateContentLength(
170 int64 received_payload_byte_count
, int64 original_payload_byte_count
,
171 bool data_reduction_proxy_was_used
);
173 scoped_refptr
<extensions::EventRouterForwarder
> event_router_
;
175 scoped_refptr
<CookieSettings
> cookie_settings_
;
177 scoped_refptr
<ExtensionInfoMap
> extension_info_map_
;
179 scoped_ptr
<chrome_browser_net::ConnectInterceptor
> connect_interceptor_
;
181 // Weak, owned by our owner.
182 BooleanPrefMember
* enable_referrers_
;
183 BooleanPrefMember
* enable_do_not_track_
;
184 BooleanPrefMember
* force_google_safe_search_
;
186 // Weak, owned by our owner.
187 const policy::URLBlacklistManager
* url_blacklist_manager_
;
189 // When true, allow access to all file:// URLs.
190 static bool g_allow_file_access_
;
192 // True if OnCanThrottleRequest should always return false.
194 // Note: This needs to be static as the instance of
195 // ChromeNetworkDelegate used may change over time, and we need to
196 // set this variable once at start-up time. It is effectively
197 // static anyway since it is based on a command-line flag.
198 static bool g_never_throttle_requests_
;
200 // Pointer to IOThread global, should outlive ChromeNetworkDelegate.
201 chrome_browser_net::LoadTimeStats
* load_time_stats_
;
203 // Total size of all content (excluding headers) that has been received
205 int64 received_content_length_
;
207 // Total original size of all content before it was transferred.
208 int64 original_content_length_
;
210 DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate
);
213 #endif // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_