| 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_WEBREQUEST_WEBREQUEST_API_HELPERS_H_ | |
| 8 #define CHROME_BROWSER_EXTENSIONS_API_WEBREQUEST_WEBREQUEST_API_HELPERS_H_ | |
| 9 #pragma once | |
| 10 | |
| 11 #include <list> | |
| 12 #include <set> | |
| 13 #include <string> | |
| 14 | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/linked_ptr.h" | |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/time.h" | |
| 19 #include "googleurl/src/gurl.h" | |
| 20 #include "net/base/auth.h" | |
| 21 #include "net/base/net_log.h" | |
| 22 #include "net/http/http_request_headers.h" | |
| 23 #include "net/http/http_response_headers.h" | |
| 24 | |
| 25 namespace base { | |
| 26 class ListValue; | |
| 27 class Value; | |
| 28 } | |
| 29 | |
| 30 namespace extension_webrequest_api_helpers { | |
| 31 | |
| 32 typedef std::pair<std::string, std::string> ResponseHeader; | |
| 33 typedef std::vector<ResponseHeader> ResponseHeaders; | |
| 34 | |
| 35 // Contains the modification an extension wants to perform on an event. | |
| 36 struct EventResponseDelta { | |
| 37 // ID of the extension that sent this response. | |
| 38 std::string extension_id; | |
| 39 | |
| 40 // The time that the extension was installed. Used for deciding order of | |
| 41 // precedence in case multiple extensions respond with conflicting | |
| 42 // decisions. | |
| 43 base::Time extension_install_time; | |
| 44 | |
| 45 // Response values. These are mutually exclusive. | |
| 46 bool cancel; | |
| 47 GURL new_url; | |
| 48 | |
| 49 // Newly introduced or overridden request headers. | |
| 50 net::HttpRequestHeaders modified_request_headers; | |
| 51 | |
| 52 // Keys of request headers to be deleted. | |
| 53 std::vector<std::string> deleted_request_headers; | |
| 54 | |
| 55 // Headers that were added to the response. A modification of a header | |
| 56 // corresponds to a deletion and subsequent addition of the new header. | |
| 57 ResponseHeaders added_response_headers; | |
| 58 | |
| 59 // Headers that were deleted from the response. | |
| 60 ResponseHeaders deleted_response_headers; | |
| 61 | |
| 62 // Authentication Credentials to use. | |
| 63 scoped_ptr<net::AuthCredentials> auth_credentials; | |
| 64 | |
| 65 EventResponseDelta(const std::string& extension_id, | |
| 66 const base::Time& extension_install_time); | |
| 67 ~EventResponseDelta(); | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(EventResponseDelta); | |
| 70 }; | |
| 71 | |
| 72 typedef std::list<linked_ptr<EventResponseDelta> > EventResponseDeltas; | |
| 73 | |
| 74 // Container for NetLog events that shall be reported. | |
| 75 struct EventLogEntry { | |
| 76 net::NetLog::EventType event_type; | |
| 77 scoped_refptr<net::NetLog::EventParameters> params; | |
| 78 | |
| 79 EventLogEntry(net::NetLog::EventType event_type, | |
| 80 const scoped_refptr<net::NetLog::EventParameters>& params); | |
| 81 ~EventLogEntry(); | |
| 82 | |
| 83 // Allow implicit copy and assignment. | |
| 84 }; | |
| 85 | |
| 86 typedef std::list<EventLogEntry> EventLogEntries; | |
| 87 | |
| 88 // Comparison operator that returns true if the extension that caused | |
| 89 // |a| was installed after the extension that caused |b|. | |
| 90 bool InDecreasingExtensionInstallationTimeOrder( | |
| 91 const linked_ptr<EventResponseDelta>& a, | |
| 92 const linked_ptr<EventResponseDelta>& b); | |
| 93 | |
| 94 // Converts a string to a list of integers, each in 0..255. Ownership | |
| 95 // of the created list is passed to the caller. | |
| 96 base::ListValue* StringToCharList(const std::string& s); | |
| 97 | |
| 98 // Converts a list of integer values between 0 and 255 into a string |*out|. | |
| 99 // Returns true if the conversion was successful. | |
| 100 bool CharListToString(base::ListValue* list, std::string* out); | |
| 101 | |
| 102 // The following functions calculate and return the modifications to requests | |
| 103 // commanded by extension handlers. All functions take the id of the extension | |
| 104 // that commanded a modification, the installation time of this extension (used | |
| 105 // for defining a precedence in conflicting modifications) and whether the | |
| 106 // extension requested to |cancel| the request. Other parameters depend on a | |
| 107 // the signal handler. Ownership of the returned object is passed to the caller. | |
| 108 | |
| 109 EventResponseDelta* CalculateOnBeforeRequestDelta( | |
| 110 const std::string& extension_id, | |
| 111 const base::Time& extension_install_time, | |
| 112 bool cancel, | |
| 113 const GURL& new_url); | |
| 114 EventResponseDelta* CalculateOnBeforeSendHeadersDelta( | |
| 115 const std::string& extension_id, | |
| 116 const base::Time& extension_install_time, | |
| 117 bool cancel, | |
| 118 net::HttpRequestHeaders* old_headers, | |
| 119 net::HttpRequestHeaders* new_headers); | |
| 120 EventResponseDelta* CalculateOnHeadersReceivedDelta( | |
| 121 const std::string& extension_id, | |
| 122 const base::Time& extension_install_time, | |
| 123 bool cancel, | |
| 124 net::HttpResponseHeaders* old_response_headers, | |
| 125 ResponseHeaders* new_response_headers); | |
| 126 // Destructively moves the auth credentials from |auth_credentials| to the | |
| 127 // returned EventResponseDelta. | |
| 128 EventResponseDelta* CalculateOnAuthRequiredDelta( | |
| 129 const std::string& extension_id, | |
| 130 const base::Time& extension_install_time, | |
| 131 bool cancel, | |
| 132 scoped_ptr<net::AuthCredentials>* auth_credentials); | |
| 133 | |
| 134 // These functions merge the responses (the |deltas|) of request handlers. | |
| 135 // The |deltas| need to be sorted in decreasing order of precedence of | |
| 136 // extensions. In case extensions had |deltas| that could not be honored, their | |
| 137 // IDs are reported in |conflicting_extensions|. NetLog events that shall be | |
| 138 // reported will be stored in |event_log_entries|. | |
| 139 | |
| 140 // Stores in |canceled| whether any extension wanted to cancel the request. | |
| 141 void MergeCancelOfResponses( | |
| 142 const EventResponseDeltas& deltas, | |
| 143 bool* canceled, | |
| 144 EventLogEntries* event_log_entries); | |
| 145 // Stores in |*new_url| the redirect request of the extension with highest | |
| 146 // precedence. Extensions that did not command to redirect the request are | |
| 147 // ignored in this logic. | |
| 148 void MergeOnBeforeRequestResponses( | |
| 149 const EventResponseDeltas& deltas, | |
| 150 GURL* new_url, | |
| 151 std::set<std::string>* conflicting_extensions, | |
| 152 EventLogEntries* event_log_entries); | |
| 153 // Modifies the headers in |request_headers| according to |deltas|. Conflicts | |
| 154 // are tried to be resolved. | |
| 155 void MergeOnBeforeSendHeadersResponses( | |
| 156 const EventResponseDeltas& deltas, | |
| 157 net::HttpRequestHeaders* request_headers, | |
| 158 std::set<std::string>* conflicting_extensions, | |
| 159 EventLogEntries* event_log_entries); | |
| 160 // Stores a copy of |original_response_header| into |override_response_headers| | |
| 161 // that is modified according to |deltas|. If |deltas| does not instruct to | |
| 162 // modify the response headers, |override_response_headers| remains empty. | |
| 163 void MergeOnHeadersReceivedResponses( | |
| 164 const EventResponseDeltas& deltas, | |
| 165 const net::HttpResponseHeaders* original_response_headers, | |
| 166 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, | |
| 167 std::set<std::string>* conflicting_extensions, | |
| 168 EventLogEntries* event_log_entries); | |
| 169 // Merge the responses of blocked onAuthRequired handlers. The first | |
| 170 // registered listener that supplies authentication credentials in a response, | |
| 171 // if any, will have its authentication credentials used. |request| must be | |
| 172 // non-NULL, and contain |deltas| that are sorted in decreasing order of | |
| 173 // precedence. | |
| 174 // Returns whether authentication credentials are set. | |
| 175 bool MergeOnAuthRequiredResponses( | |
| 176 const EventResponseDeltas& deltas, | |
| 177 net::AuthCredentials* auth_credentials, | |
| 178 std::set<std::string>* conflicting_extensions, | |
| 179 EventLogEntries* event_log_entries); | |
| 180 | |
| 181 } // namespace extension_webrequest_api_helpers | |
| 182 | |
| 183 #endif // CHROME_BROWSER_EXTENSIONS_API_WEBREQUEST_WEBREQUEST_API_HELPERS_H_ | |
| OLD | NEW |