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 // Helper classes and functions used for the WebRequest API.
7 #ifndef EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_HELPERS_H_
8 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_HELPERS_H_
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/time/time.h"
18 #include "content/public/common/resource_type.h"
19 #include "extensions/browser/warning_set.h"
20 #include "net/base/auth.h"
21 #include "net/http/http_request_headers.h"
22 #include "net/http/http_response_headers.h"
31 class RenderProcessHost
;
34 namespace extensions
{
43 namespace extension_web_request_api_helpers
{
45 typedef std::pair
<std::string
, std::string
> ResponseHeader
;
46 typedef std::vector
<ResponseHeader
> ResponseHeaders
;
48 // Data container for RequestCookies as defined in the declarative WebRequest
50 struct RequestCookie
{
53 scoped_ptr
<std::string
> name
;
54 scoped_ptr
<std::string
> value
;
56 DISALLOW_COPY_AND_ASSIGN(RequestCookie
);
59 bool NullableEquals(const RequestCookie
* a
, const RequestCookie
* b
);
61 // Data container for ResponseCookies as defined in the declarative WebRequest
63 struct ResponseCookie
{
66 scoped_ptr
<std::string
> name
;
67 scoped_ptr
<std::string
> value
;
68 scoped_ptr
<std::string
> expires
;
69 scoped_ptr
<int> max_age
;
70 scoped_ptr
<std::string
> domain
;
71 scoped_ptr
<std::string
> path
;
72 scoped_ptr
<bool> secure
;
73 scoped_ptr
<bool> http_only
;
75 DISALLOW_COPY_AND_ASSIGN(ResponseCookie
);
78 bool NullableEquals(const ResponseCookie
* a
, const ResponseCookie
* b
);
80 // Data container for FilterResponseCookies as defined in the declarative
81 // WebRequest API definition.
82 struct FilterResponseCookie
: ResponseCookie
{
83 FilterResponseCookie();
84 ~FilterResponseCookie();
85 scoped_ptr
<int> age_lower_bound
;
86 scoped_ptr
<int> age_upper_bound
;
87 scoped_ptr
<bool> session_cookie
;
89 DISALLOW_COPY_AND_ASSIGN(FilterResponseCookie
);
92 bool NullableEquals(const FilterResponseCookie
* a
,
93 const FilterResponseCookie
* b
);
95 enum CookieModificationType
{
101 struct RequestCookieModification
{
102 RequestCookieModification();
103 ~RequestCookieModification();
104 CookieModificationType type
;
105 // Used for EDIT and REMOVE. NULL for ADD.
106 scoped_ptr
<RequestCookie
> filter
;
107 // Used for ADD and EDIT. NULL for REMOVE.
108 scoped_ptr
<RequestCookie
> modification
;
110 DISALLOW_COPY_AND_ASSIGN(RequestCookieModification
);
113 bool NullableEquals(const RequestCookieModification
* a
,
114 const RequestCookieModification
* b
);
116 struct ResponseCookieModification
{
117 ResponseCookieModification();
118 ~ResponseCookieModification();
119 CookieModificationType type
;
120 // Used for EDIT and REMOVE.
121 scoped_ptr
<FilterResponseCookie
> filter
;
122 // Used for ADD and EDIT.
123 scoped_ptr
<ResponseCookie
> modification
;
125 DISALLOW_COPY_AND_ASSIGN(ResponseCookieModification
);
128 bool NullableEquals(const ResponseCookieModification
* a
,
129 const ResponseCookieModification
* b
);
131 typedef std::vector
<linked_ptr
<RequestCookieModification
> >
132 RequestCookieModifications
;
133 typedef std::vector
<linked_ptr
<ResponseCookieModification
> >
134 ResponseCookieModifications
;
136 // Contains the modification an extension wants to perform on an event.
137 struct EventResponseDelta
{
138 // ID of the extension that sent this response.
139 std::string extension_id
;
141 // The time that the extension was installed. Used for deciding order of
142 // precedence in case multiple extensions respond with conflicting
144 base::Time extension_install_time
;
146 // Response values. These are mutually exclusive.
150 // Newly introduced or overridden request headers.
151 net::HttpRequestHeaders modified_request_headers
;
153 // Keys of request headers to be deleted.
154 std::vector
<std::string
> deleted_request_headers
;
156 // Headers that were added to the response. A modification of a header
157 // corresponds to a deletion and subsequent addition of the new header.
158 ResponseHeaders added_response_headers
;
160 // Headers that were deleted from the response.
161 ResponseHeaders deleted_response_headers
;
163 // Authentication Credentials to use.
164 scoped_ptr
<net::AuthCredentials
> auth_credentials
;
166 // Modifications to cookies in request headers.
167 RequestCookieModifications request_cookie_modifications
;
169 // Modifications to cookies in response headers.
170 ResponseCookieModifications response_cookie_modifications
;
172 // Messages that shall be sent to the background/event/... pages of the
174 std::set
<std::string
> messages_to_extension
;
176 EventResponseDelta(const std::string
& extension_id
,
177 const base::Time
& extension_install_time
);
178 ~EventResponseDelta();
180 DISALLOW_COPY_AND_ASSIGN(EventResponseDelta
);
183 typedef std::list
<linked_ptr
<EventResponseDelta
> > EventResponseDeltas
;
185 // Comparison operator that returns true if the extension that caused
186 // |a| was installed after the extension that caused |b|.
187 bool InDecreasingExtensionInstallationTimeOrder(
188 const linked_ptr
<EventResponseDelta
>& a
,
189 const linked_ptr
<EventResponseDelta
>& b
);
191 // Converts a string to a list of integers, each in 0..255. Ownership
192 // of the created list is passed to the caller.
193 base::ListValue
* StringToCharList(const std::string
& s
);
195 // Converts a list of integer values between 0 and 255 into a string |*out|.
196 // Returns true if the conversion was successful.
197 bool CharListToString(const base::ListValue
* list
, std::string
* out
);
199 // The following functions calculate and return the modifications to requests
200 // commanded by extension handlers. All functions take the id of the extension
201 // that commanded a modification, the installation time of this extension (used
202 // for defining a precedence in conflicting modifications) and whether the
203 // extension requested to |cancel| the request. Other parameters depend on a
204 // the signal handler. Ownership of the returned object is passed to the caller.
206 EventResponseDelta
* CalculateOnBeforeRequestDelta(
207 const std::string
& extension_id
,
208 const base::Time
& extension_install_time
,
210 const GURL
& new_url
);
211 EventResponseDelta
* CalculateOnBeforeSendHeadersDelta(
212 const std::string
& extension_id
,
213 const base::Time
& extension_install_time
,
215 net::HttpRequestHeaders
* old_headers
,
216 net::HttpRequestHeaders
* new_headers
);
217 EventResponseDelta
* CalculateOnHeadersReceivedDelta(
218 const std::string
& extension_id
,
219 const base::Time
& extension_install_time
,
222 const net::HttpResponseHeaders
* old_response_headers
,
223 ResponseHeaders
* new_response_headers
);
224 // Destructively moves the auth credentials from |auth_credentials| to the
225 // returned EventResponseDelta.
226 EventResponseDelta
* CalculateOnAuthRequiredDelta(
227 const std::string
& extension_id
,
228 const base::Time
& extension_install_time
,
230 scoped_ptr
<net::AuthCredentials
>* auth_credentials
);
232 // These functions merge the responses (the |deltas|) of request handlers.
233 // The |deltas| need to be sorted in decreasing order of precedence of
234 // extensions. In case extensions had |deltas| that could not be honored, their
235 // IDs are reported in |conflicting_extensions|. NetLog events that shall be
236 // reported will be stored in |event_log_entries|.
238 // Stores in |canceled| whether any extension wanted to cancel the request.
239 void MergeCancelOfResponses(
240 const EventResponseDeltas
& deltas
,
242 const net::BoundNetLog
* net_log
);
243 // Stores in |*new_url| the redirect request of the extension with highest
244 // precedence. Extensions that did not command to redirect the request are
245 // ignored in this logic.
246 void MergeRedirectUrlOfResponses(
247 const EventResponseDeltas
& deltas
,
249 extensions::WarningSet
* conflicting_extensions
,
250 const net::BoundNetLog
* net_log
);
251 // Stores in |*new_url| the redirect request of the extension with highest
252 // precedence. Extensions that did not command to redirect the request are
253 // ignored in this logic.
254 void MergeOnBeforeRequestResponses(
255 const EventResponseDeltas
& deltas
,
257 extensions::WarningSet
* conflicting_extensions
,
258 const net::BoundNetLog
* net_log
);
259 // Modifies the "Cookie" header in |request_headers| according to
260 // |deltas.request_cookie_modifications|. Conflicts are currently ignored
262 void MergeCookiesInOnBeforeSendHeadersResponses(
263 const EventResponseDeltas
& deltas
,
264 net::HttpRequestHeaders
* request_headers
,
265 extensions::WarningSet
* conflicting_extensions
,
266 const net::BoundNetLog
* net_log
);
267 // Modifies the headers in |request_headers| according to |deltas|. Conflicts
268 // are tried to be resolved.
269 void MergeOnBeforeSendHeadersResponses(
270 const EventResponseDeltas
& deltas
,
271 net::HttpRequestHeaders
* request_headers
,
272 extensions::WarningSet
* conflicting_extensions
,
273 const net::BoundNetLog
* net_log
);
274 // Modifies the "Set-Cookie" headers in |override_response_headers| according to
275 // |deltas.response_cookie_modifications|. If |override_response_headers| is
276 // NULL, a copy of |original_response_headers| is created. Conflicts are
277 // currently ignored silently.
278 void MergeCookiesInOnHeadersReceivedResponses(
279 const EventResponseDeltas
& deltas
,
280 const net::HttpResponseHeaders
* original_response_headers
,
281 scoped_refptr
<net::HttpResponseHeaders
>* override_response_headers
,
282 extensions::WarningSet
* conflicting_extensions
,
283 const net::BoundNetLog
* net_log
);
284 // Stores a copy of |original_response_header| into |override_response_headers|
285 // that is modified according to |deltas|. If |deltas| does not instruct to
286 // modify the response headers, |override_response_headers| remains empty.
287 // Extension-initiated redirects are written to |override_response_headers|
288 // (to request redirection) and |*allowed_unsafe_redirect_url| (to make sure
289 // that the request is not cancelled with net::ERR_UNSAFE_REDIRECT).
290 void MergeOnHeadersReceivedResponses(
291 const EventResponseDeltas
& deltas
,
292 const net::HttpResponseHeaders
* original_response_headers
,
293 scoped_refptr
<net::HttpResponseHeaders
>* override_response_headers
,
294 GURL
* allowed_unsafe_redirect_url
,
295 extensions::WarningSet
* conflicting_extensions
,
296 const net::BoundNetLog
* net_log
);
297 // Merge the responses of blocked onAuthRequired handlers. The first
298 // registered listener that supplies authentication credentials in a response,
299 // if any, will have its authentication credentials used. |request| must be
300 // non-NULL, and contain |deltas| that are sorted in decreasing order of
302 // Returns whether authentication credentials are set.
303 bool MergeOnAuthRequiredResponses(
304 const EventResponseDeltas
& deltas
,
305 net::AuthCredentials
* auth_credentials
,
306 extensions::WarningSet
* conflicting_extensions
,
307 const net::BoundNetLog
* net_log
);
309 // Triggers clearing each renderer's in-memory cache the next time it navigates.
310 void ClearCacheOnNavigation();
312 // Tells renderer processes that the web request or declarative web request
313 // API has been used by the extension with the given |extension_id| in the
314 // given |browser_context_id| to collect UMA statistics on Page Load Times.
315 // Needs to be called on the UI thread.
316 void NotifyWebRequestAPIUsed(void* browser_context_id
,
317 const std::string
& extension_id
);
319 // Send updates to |host| with information about what webRequest-related
320 // extensions are installed.
321 void SendExtensionWebRequestStatusToHost(content::RenderProcessHost
* host
);
323 // Converts the |name|, |value| pair of a http header to a HttpHeaders
324 // dictionary. Ownership is passed to the caller.
325 base::DictionaryValue
* CreateHeaderDictionary(
326 const std::string
& name
, const std::string
& value
);
328 // Returns whether |type| is a ResourceType that is handled by the web request
330 bool IsRelevantResourceType(content::ResourceType type
);
332 // Returns a string representation of |type| or |other| if |type| is not handled
333 // by the web request API.
334 const char* ResourceTypeToString(content::ResourceType type
);
336 // Stores a |content::ResourceType| representation in |type| if |type_str| is
337 // a resource type handled by the web request API. Returns true in case of
339 bool ParseResourceType(const std::string
& type_str
,
340 content::ResourceType
* type
);
342 } // namespace extension_web_request_api_helpers
344 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_HELPERS_H_