| OLD | NEW |
| (Empty) |
| 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 // Helper classes and functions used for the WebRequest API. | |
| 6 | |
| 7 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_WEB_REQUEST_API_HELPERS_H_ | |
| 8 #define CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_WEB_REQUEST_API_HELPERS_H_ | |
| 9 | |
| 10 #include <list> | |
| 11 #include <set> | |
| 12 #include <string> | |
| 13 | |
| 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 "extensions/browser/warning_set.h" | |
| 19 #include "net/base/auth.h" | |
| 20 #include "net/http/http_request_headers.h" | |
| 21 #include "net/http/http_response_headers.h" | |
| 22 #include "url/gurl.h" | |
| 23 | |
| 24 namespace base { | |
| 25 class ListValue; | |
| 26 class Value; | |
| 27 } | |
| 28 | |
| 29 namespace extensions { | |
| 30 class Extension; | |
| 31 } | |
| 32 | |
| 33 namespace net { | |
| 34 class BoundNetLog; | |
| 35 class URLRequest; | |
| 36 } | |
| 37 | |
| 38 namespace extension_web_request_api_helpers { | |
| 39 | |
| 40 typedef std::pair<std::string, std::string> ResponseHeader; | |
| 41 typedef std::vector<ResponseHeader> ResponseHeaders; | |
| 42 | |
| 43 // Data container for RequestCookies as defined in the declarative WebRequest | |
| 44 // API definition. | |
| 45 struct RequestCookie { | |
| 46 RequestCookie(); | |
| 47 ~RequestCookie(); | |
| 48 scoped_ptr<std::string> name; | |
| 49 scoped_ptr<std::string> value; | |
| 50 private: | |
| 51 DISALLOW_COPY_AND_ASSIGN(RequestCookie); | |
| 52 }; | |
| 53 | |
| 54 bool NullableEquals(const RequestCookie* a, const RequestCookie* b); | |
| 55 | |
| 56 // Data container for ResponseCookies as defined in the declarative WebRequest | |
| 57 // API definition. | |
| 58 struct ResponseCookie { | |
| 59 ResponseCookie(); | |
| 60 ~ResponseCookie(); | |
| 61 scoped_ptr<std::string> name; | |
| 62 scoped_ptr<std::string> value; | |
| 63 scoped_ptr<std::string> expires; | |
| 64 scoped_ptr<int> max_age; | |
| 65 scoped_ptr<std::string> domain; | |
| 66 scoped_ptr<std::string> path; | |
| 67 scoped_ptr<bool> secure; | |
| 68 scoped_ptr<bool> http_only; | |
| 69 private: | |
| 70 DISALLOW_COPY_AND_ASSIGN(ResponseCookie); | |
| 71 }; | |
| 72 | |
| 73 bool NullableEquals(const ResponseCookie* a, const ResponseCookie* b); | |
| 74 | |
| 75 // Data container for FilterResponseCookies as defined in the declarative | |
| 76 // WebRequest API definition. | |
| 77 struct FilterResponseCookie : ResponseCookie { | |
| 78 FilterResponseCookie(); | |
| 79 ~FilterResponseCookie(); | |
| 80 scoped_ptr<int> age_lower_bound; | |
| 81 scoped_ptr<int> age_upper_bound; | |
| 82 scoped_ptr<bool> session_cookie; | |
| 83 private: | |
| 84 DISALLOW_COPY_AND_ASSIGN(FilterResponseCookie); | |
| 85 }; | |
| 86 | |
| 87 bool NullableEquals(const FilterResponseCookie* a, | |
| 88 const FilterResponseCookie* b); | |
| 89 | |
| 90 enum CookieModificationType { | |
| 91 ADD, | |
| 92 EDIT, | |
| 93 REMOVE, | |
| 94 }; | |
| 95 | |
| 96 struct RequestCookieModification { | |
| 97 RequestCookieModification(); | |
| 98 ~RequestCookieModification(); | |
| 99 CookieModificationType type; | |
| 100 // Used for EDIT and REMOVE. NULL for ADD. | |
| 101 scoped_ptr<RequestCookie> filter; | |
| 102 // Used for ADD and EDIT. NULL for REMOVE. | |
| 103 scoped_ptr<RequestCookie> modification; | |
| 104 private: | |
| 105 DISALLOW_COPY_AND_ASSIGN(RequestCookieModification); | |
| 106 }; | |
| 107 | |
| 108 bool NullableEquals(const RequestCookieModification* a, | |
| 109 const RequestCookieModification* b); | |
| 110 | |
| 111 struct ResponseCookieModification { | |
| 112 ResponseCookieModification(); | |
| 113 ~ResponseCookieModification(); | |
| 114 CookieModificationType type; | |
| 115 // Used for EDIT and REMOVE. | |
| 116 scoped_ptr<FilterResponseCookie> filter; | |
| 117 // Used for ADD and EDIT. | |
| 118 scoped_ptr<ResponseCookie> modification; | |
| 119 private: | |
| 120 DISALLOW_COPY_AND_ASSIGN(ResponseCookieModification); | |
| 121 }; | |
| 122 | |
| 123 bool NullableEquals(const ResponseCookieModification* a, | |
| 124 const ResponseCookieModification* b); | |
| 125 | |
| 126 typedef std::vector<linked_ptr<RequestCookieModification> > | |
| 127 RequestCookieModifications; | |
| 128 typedef std::vector<linked_ptr<ResponseCookieModification> > | |
| 129 ResponseCookieModifications; | |
| 130 | |
| 131 // Contains the modification an extension wants to perform on an event. | |
| 132 struct EventResponseDelta { | |
| 133 // ID of the extension that sent this response. | |
| 134 std::string extension_id; | |
| 135 | |
| 136 // The time that the extension was installed. Used for deciding order of | |
| 137 // precedence in case multiple extensions respond with conflicting | |
| 138 // decisions. | |
| 139 base::Time extension_install_time; | |
| 140 | |
| 141 // Response values. These are mutually exclusive. | |
| 142 bool cancel; | |
| 143 GURL new_url; | |
| 144 | |
| 145 // Newly introduced or overridden request headers. | |
| 146 net::HttpRequestHeaders modified_request_headers; | |
| 147 | |
| 148 // Keys of request headers to be deleted. | |
| 149 std::vector<std::string> deleted_request_headers; | |
| 150 | |
| 151 // Headers that were added to the response. A modification of a header | |
| 152 // corresponds to a deletion and subsequent addition of the new header. | |
| 153 ResponseHeaders added_response_headers; | |
| 154 | |
| 155 // Headers that were deleted from the response. | |
| 156 ResponseHeaders deleted_response_headers; | |
| 157 | |
| 158 // Authentication Credentials to use. | |
| 159 scoped_ptr<net::AuthCredentials> auth_credentials; | |
| 160 | |
| 161 // Modifications to cookies in request headers. | |
| 162 RequestCookieModifications request_cookie_modifications; | |
| 163 | |
| 164 // Modifications to cookies in response headers. | |
| 165 ResponseCookieModifications response_cookie_modifications; | |
| 166 | |
| 167 // Messages that shall be sent to the background/event/... pages of the | |
| 168 // extension. | |
| 169 std::set<std::string> messages_to_extension; | |
| 170 | |
| 171 EventResponseDelta(const std::string& extension_id, | |
| 172 const base::Time& extension_install_time); | |
| 173 ~EventResponseDelta(); | |
| 174 | |
| 175 DISALLOW_COPY_AND_ASSIGN(EventResponseDelta); | |
| 176 }; | |
| 177 | |
| 178 typedef std::list<linked_ptr<EventResponseDelta> > EventResponseDeltas; | |
| 179 | |
| 180 // Comparison operator that returns true if the extension that caused | |
| 181 // |a| was installed after the extension that caused |b|. | |
| 182 bool InDecreasingExtensionInstallationTimeOrder( | |
| 183 const linked_ptr<EventResponseDelta>& a, | |
| 184 const linked_ptr<EventResponseDelta>& b); | |
| 185 | |
| 186 // Converts a string to a list of integers, each in 0..255. Ownership | |
| 187 // of the created list is passed to the caller. | |
| 188 base::ListValue* StringToCharList(const std::string& s); | |
| 189 | |
| 190 // Converts a list of integer values between 0 and 255 into a string |*out|. | |
| 191 // Returns true if the conversion was successful. | |
| 192 bool CharListToString(const base::ListValue* list, std::string* out); | |
| 193 | |
| 194 // The following functions calculate and return the modifications to requests | |
| 195 // commanded by extension handlers. All functions take the id of the extension | |
| 196 // that commanded a modification, the installation time of this extension (used | |
| 197 // for defining a precedence in conflicting modifications) and whether the | |
| 198 // extension requested to |cancel| the request. Other parameters depend on a | |
| 199 // the signal handler. Ownership of the returned object is passed to the caller. | |
| 200 | |
| 201 EventResponseDelta* CalculateOnBeforeRequestDelta( | |
| 202 const std::string& extension_id, | |
| 203 const base::Time& extension_install_time, | |
| 204 bool cancel, | |
| 205 const GURL& new_url); | |
| 206 EventResponseDelta* CalculateOnBeforeSendHeadersDelta( | |
| 207 const std::string& extension_id, | |
| 208 const base::Time& extension_install_time, | |
| 209 bool cancel, | |
| 210 net::HttpRequestHeaders* old_headers, | |
| 211 net::HttpRequestHeaders* new_headers); | |
| 212 EventResponseDelta* CalculateOnHeadersReceivedDelta( | |
| 213 const std::string& extension_id, | |
| 214 const base::Time& extension_install_time, | |
| 215 bool cancel, | |
| 216 const GURL& new_url, | |
| 217 const net::HttpResponseHeaders* old_response_headers, | |
| 218 ResponseHeaders* new_response_headers); | |
| 219 // Destructively moves the auth credentials from |auth_credentials| to the | |
| 220 // returned EventResponseDelta. | |
| 221 EventResponseDelta* CalculateOnAuthRequiredDelta( | |
| 222 const std::string& extension_id, | |
| 223 const base::Time& extension_install_time, | |
| 224 bool cancel, | |
| 225 scoped_ptr<net::AuthCredentials>* auth_credentials); | |
| 226 | |
| 227 // These functions merge the responses (the |deltas|) of request handlers. | |
| 228 // The |deltas| need to be sorted in decreasing order of precedence of | |
| 229 // extensions. In case extensions had |deltas| that could not be honored, their | |
| 230 // IDs are reported in |conflicting_extensions|. NetLog events that shall be | |
| 231 // reported will be stored in |event_log_entries|. | |
| 232 | |
| 233 // Stores in |canceled| whether any extension wanted to cancel the request. | |
| 234 void MergeCancelOfResponses( | |
| 235 const EventResponseDeltas& deltas, | |
| 236 bool* canceled, | |
| 237 const net::BoundNetLog* net_log); | |
| 238 // Stores in |*new_url| the redirect request of the extension with highest | |
| 239 // precedence. Extensions that did not command to redirect the request are | |
| 240 // ignored in this logic. | |
| 241 void MergeRedirectUrlOfResponses( | |
| 242 const EventResponseDeltas& deltas, | |
| 243 GURL* new_url, | |
| 244 extensions::WarningSet* conflicting_extensions, | |
| 245 const net::BoundNetLog* net_log); | |
| 246 // Stores in |*new_url| the redirect request of the extension with highest | |
| 247 // precedence. Extensions that did not command to redirect the request are | |
| 248 // ignored in this logic. | |
| 249 void MergeOnBeforeRequestResponses( | |
| 250 const EventResponseDeltas& deltas, | |
| 251 GURL* new_url, | |
| 252 extensions::WarningSet* conflicting_extensions, | |
| 253 const net::BoundNetLog* net_log); | |
| 254 // Modifies the "Cookie" header in |request_headers| according to | |
| 255 // |deltas.request_cookie_modifications|. Conflicts are currently ignored | |
| 256 // silently. | |
| 257 void MergeCookiesInOnBeforeSendHeadersResponses( | |
| 258 const EventResponseDeltas& deltas, | |
| 259 net::HttpRequestHeaders* request_headers, | |
| 260 extensions::WarningSet* conflicting_extensions, | |
| 261 const net::BoundNetLog* net_log); | |
| 262 // Modifies the headers in |request_headers| according to |deltas|. Conflicts | |
| 263 // are tried to be resolved. | |
| 264 void MergeOnBeforeSendHeadersResponses( | |
| 265 const EventResponseDeltas& deltas, | |
| 266 net::HttpRequestHeaders* request_headers, | |
| 267 extensions::WarningSet* conflicting_extensions, | |
| 268 const net::BoundNetLog* net_log); | |
| 269 // Modifies the "Set-Cookie" headers in |override_response_headers| according to | |
| 270 // |deltas.response_cookie_modifications|. If |override_response_headers| is | |
| 271 // NULL, a copy of |original_response_headers| is created. Conflicts are | |
| 272 // currently ignored silently. | |
| 273 void MergeCookiesInOnHeadersReceivedResponses( | |
| 274 const EventResponseDeltas& deltas, | |
| 275 const net::HttpResponseHeaders* original_response_headers, | |
| 276 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, | |
| 277 extensions::WarningSet* conflicting_extensions, | |
| 278 const net::BoundNetLog* net_log); | |
| 279 // Stores a copy of |original_response_header| into |override_response_headers| | |
| 280 // that is modified according to |deltas|. If |deltas| does not instruct to | |
| 281 // modify the response headers, |override_response_headers| remains empty. | |
| 282 // Extension-initiated redirects are written to |override_response_headers| | |
| 283 // (to request redirection) and |*allowed_unsafe_redirect_url| (to make sure | |
| 284 // that the request is not cancelled with net::ERR_UNSAFE_REDIRECT). | |
| 285 void MergeOnHeadersReceivedResponses( | |
| 286 const EventResponseDeltas& deltas, | |
| 287 const net::HttpResponseHeaders* original_response_headers, | |
| 288 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, | |
| 289 GURL* allowed_unsafe_redirect_url, | |
| 290 extensions::WarningSet* conflicting_extensions, | |
| 291 const net::BoundNetLog* net_log); | |
| 292 // Merge the responses of blocked onAuthRequired handlers. The first | |
| 293 // registered listener that supplies authentication credentials in a response, | |
| 294 // if any, will have its authentication credentials used. |request| must be | |
| 295 // non-NULL, and contain |deltas| that are sorted in decreasing order of | |
| 296 // precedence. | |
| 297 // Returns whether authentication credentials are set. | |
| 298 bool MergeOnAuthRequiredResponses( | |
| 299 const EventResponseDeltas& deltas, | |
| 300 net::AuthCredentials* auth_credentials, | |
| 301 extensions::WarningSet* conflicting_extensions, | |
| 302 const net::BoundNetLog* net_log); | |
| 303 | |
| 304 // Triggers clearing each renderer's in-memory cache the next time it navigates. | |
| 305 void ClearCacheOnNavigation(); | |
| 306 | |
| 307 // Tells renderer processes that the web request or declarative web request | |
| 308 // API has been used by |extension| in browser_context |browser_context_id| to | |
| 309 // collect UMA statistics on Page Load Times. Needs to be called on the UI | |
| 310 // thread. | |
| 311 void NotifyWebRequestAPIUsed( | |
| 312 void* browser_context_id, | |
| 313 scoped_refptr<const extensions::Extension> extension); | |
| 314 | |
| 315 } // namespace extension_web_request_api_helpers | |
| 316 | |
| 317 #endif // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_WEB_REQUEST_API_HELPERS_H_ | |
| OLD | NEW |