| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_URL_MATCHER_URL_MATCHER_H_ | 5 #ifndef COMPONENTS_URL_MATCHER_URL_MATCHER_H_ |
| 6 #define COMPONENTS_URL_MATCHER_URL_MATCHER_H_ | 6 #define COMPONENTS_URL_MATCHER_URL_MATCHER_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 private: | 186 private: |
| 187 // Creates a URLMatcherCondition according to the parameters passed. | 187 // Creates a URLMatcherCondition according to the parameters passed. |
| 188 // The URLMatcherCondition will refer to a StringPattern that is | 188 // The URLMatcherCondition will refer to a StringPattern that is |
| 189 // owned by |pattern_singletons_|. | 189 // owned by |pattern_singletons_|. |
| 190 URLMatcherCondition CreateCondition(URLMatcherCondition::Criterion criterion, | 190 URLMatcherCondition CreateCondition(URLMatcherCondition::Criterion criterion, |
| 191 const std::string& pattern); | 191 const std::string& pattern); |
| 192 | 192 |
| 193 // Prepends a "." to the hostname if it does not start with one. | 193 // Prepends a "." to the hostname if it does not start with one. |
| 194 std::string CanonicalizeHostname(const std::string& hostname) const; | 194 std::string CanonicalizeHostname(const std::string& hostname) const; |
| 195 | 195 |
| 196 // Convert the query string to canonical form suitable for key token search. |
| 197 std::string CanonicalizeQuery(std::string query, |
| 198 bool prepend_beginning_of_query_component, |
| 199 bool append_end_of_query_component) const; |
| 200 |
| 196 // Counter that ensures that all created StringPatterns have unique IDs. | 201 // Counter that ensures that all created StringPatterns have unique IDs. |
| 197 // Note that substring patterns and regex patterns will use different IDs. | 202 // Note that substring patterns and regex patterns will use different IDs. |
| 198 int id_counter_; | 203 int id_counter_; |
| 199 | 204 |
| 200 // This comparison considers only the pattern() value of the | 205 // This comparison considers only the pattern() value of the |
| 201 // StringPatterns. | 206 // StringPatterns. |
| 202 struct StringPatternPointerCompare { | 207 struct StringPatternPointerCompare { |
| 203 bool operator()(StringPattern* lhs, StringPattern* rhs) const; | 208 bool operator()(StringPattern* lhs, StringPattern* rhs) const; |
| 204 }; | 209 }; |
| 205 // Set to ensure that we generate only one StringPattern for each content | 210 // Set to ensure that we generate only one StringPattern for each content |
| 206 // of StringPattern::pattern(). | 211 // of StringPattern::pattern(). |
| 207 typedef std::set<StringPattern*, StringPatternPointerCompare> | 212 typedef std::set<StringPattern*, StringPatternPointerCompare> |
| 208 PatternSingletons; | 213 PatternSingletons; |
| 209 PatternSingletons substring_pattern_singletons_; | 214 PatternSingletons substring_pattern_singletons_; |
| 210 PatternSingletons regex_pattern_singletons_; | 215 PatternSingletons regex_pattern_singletons_; |
| 211 PatternSingletons origin_and_path_regex_pattern_singletons_; | 216 PatternSingletons origin_and_path_regex_pattern_singletons_; |
| 212 | 217 |
| 213 DISALLOW_COPY_AND_ASSIGN(URLMatcherConditionFactory); | 218 DISALLOW_COPY_AND_ASSIGN(URLMatcherConditionFactory); |
| 214 }; | 219 }; |
| 215 | 220 |
| 221 // This class represents a single URL query matching condition. The query |
| 222 // matching is done as a search for a key and optionally a value. |
| 223 // The matching makes use of CanonicalizeURLForComponentSearches to ensure that |
| 224 // the key starts and ends (optionally) with the right marker. |
| 225 class URL_MATCHER_EXPORT URLQueryElementMatcherCondition { |
| 226 public: |
| 227 // Multiple occurrences of the same key can happen in a URL query. The type |
| 228 // ensures that every (MATCH_ALL), any (MATCH_ANY), first (MATCH_FIRST) or |
| 229 // last (MATCH_LAST) instance of the key occurrence matches the value. |
| 230 enum Type { MATCH_ANY, MATCH_FIRST, MATCH_LAST, MATCH_ALL }; |
| 231 |
| 232 // Allows the match to be exact (QUERY_VALUE_MATCH_EXACT, starts and ends with |
| 233 // a delimiter or a border) or simply a prefix (QUERY_VALUE_MATCH_PREFIX, |
| 234 // starts with a delimiter or a border). |
| 235 enum QueryValueMatchType { |
| 236 QUERY_VALUE_MATCH_EXACT, |
| 237 QUERY_VALUE_MATCH_PREFIX |
| 238 }; |
| 239 |
| 240 // Used to indicate if the query parameter is of type &key=value& |
| 241 // (ELEMENT_TYPE_KEY_VALUE) or simply &key& (ELEMENT_TYPE_KEY). |
| 242 enum QueryElementType { ELEMENT_TYPE_KEY_VALUE, ELEMENT_TYPE_KEY }; |
| 243 |
| 244 URLQueryElementMatcherCondition(const std::string& key, |
| 245 const std::string& value, |
| 246 QueryValueMatchType query_value_match_type, |
| 247 QueryElementType query_element_type, |
| 248 Type match_type, |
| 249 URLMatcherConditionFactory* factory); |
| 250 ~URLQueryElementMatcherCondition(); |
| 251 |
| 252 bool operator<(const URLQueryElementMatcherCondition& rhs) const; |
| 253 |
| 254 // Returns whether the URL query satisfies the key value constraint. |
| 255 bool IsMatch(const std::string& canonical_url_query) const; |
| 256 |
| 257 const StringPattern* string_pattern() const { return string_pattern_; } |
| 258 |
| 259 private: |
| 260 Type match_type_; |
| 261 std::string key_; |
| 262 std::string value_; |
| 263 size_t key_length_; |
| 264 size_t value_length_; |
| 265 const StringPattern* string_pattern_; |
| 266 }; |
| 267 |
| 216 // This class represents a filter for the URL scheme to be hooked up into a | 268 // This class represents a filter for the URL scheme to be hooked up into a |
| 217 // URLMatcherConditionSet. | 269 // URLMatcherConditionSet. |
| 218 class URL_MATCHER_EXPORT URLMatcherSchemeFilter { | 270 class URL_MATCHER_EXPORT URLMatcherSchemeFilter { |
| 219 public: | 271 public: |
| 220 explicit URLMatcherSchemeFilter(const std::string& filter); | 272 explicit URLMatcherSchemeFilter(const std::string& filter); |
| 221 explicit URLMatcherSchemeFilter(const std::vector<std::string>& filters); | 273 explicit URLMatcherSchemeFilter(const std::vector<std::string>& filters); |
| 222 ~URLMatcherSchemeFilter(); | 274 ~URLMatcherSchemeFilter(); |
| 223 bool IsMatch(const GURL& url) const; | 275 bool IsMatch(const GURL& url) const; |
| 224 | 276 |
| 225 private: | 277 private: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 249 DISALLOW_COPY_AND_ASSIGN(URLMatcherPortFilter); | 301 DISALLOW_COPY_AND_ASSIGN(URLMatcherPortFilter); |
| 250 }; | 302 }; |
| 251 | 303 |
| 252 // This class represents a set of conditions that all need to match on a | 304 // This class represents a set of conditions that all need to match on a |
| 253 // given URL in order to be considered a match. | 305 // given URL in order to be considered a match. |
| 254 class URL_MATCHER_EXPORT URLMatcherConditionSet | 306 class URL_MATCHER_EXPORT URLMatcherConditionSet |
| 255 : public base::RefCounted<URLMatcherConditionSet> { | 307 : public base::RefCounted<URLMatcherConditionSet> { |
| 256 public: | 308 public: |
| 257 typedef int ID; | 309 typedef int ID; |
| 258 typedef std::set<URLMatcherCondition> Conditions; | 310 typedef std::set<URLMatcherCondition> Conditions; |
| 311 typedef std::set<URLQueryElementMatcherCondition> QueryConditions; |
| 259 typedef std::vector<scoped_refptr<URLMatcherConditionSet> > Vector; | 312 typedef std::vector<scoped_refptr<URLMatcherConditionSet> > Vector; |
| 260 | 313 |
| 261 // Matches if all conditions in |conditions| are fulfilled. | 314 // Matches if all conditions in |conditions| are fulfilled. |
| 262 URLMatcherConditionSet(ID id, const Conditions& conditions); | 315 URLMatcherConditionSet(ID id, const Conditions& conditions); |
| 263 | 316 |
| 264 // Matches if all conditions in |conditions|, |scheme_filter| and | 317 // Matches if all conditions in |conditions|, |scheme_filter| and |
| 265 // |port_filter| are fulfilled. |scheme_filter| and |port_filter| may be NULL, | 318 // |port_filter| are fulfilled. |scheme_filter| and |port_filter| may be NULL, |
| 266 // in which case, no restrictions are imposed on the scheme/port of a URL. | 319 // in which case, no restrictions are imposed on the scheme/port of a URL. |
| 267 URLMatcherConditionSet(ID id, const Conditions& conditions, | 320 URLMatcherConditionSet(ID id, const Conditions& conditions, |
| 268 scoped_ptr<URLMatcherSchemeFilter> scheme_filter, | 321 scoped_ptr<URLMatcherSchemeFilter> scheme_filter, |
| 269 scoped_ptr<URLMatcherPortFilter> port_filter); | 322 scoped_ptr<URLMatcherPortFilter> port_filter); |
| 270 | 323 |
| 324 // Matches if all conditions in |conditions|, |query_conditions|, |
| 325 // |scheme_filter| and |port_filter| are fulfilled. |scheme_filter| and |
| 326 // |port_filter| may be NULL, in which case, no restrictions are imposed on |
| 327 // the scheme/port of a URL. |
| 328 URLMatcherConditionSet(ID id, |
| 329 const Conditions& conditions, |
| 330 const QueryConditions& query_conditions, |
| 331 scoped_ptr<URLMatcherSchemeFilter> scheme_filter, |
| 332 scoped_ptr<URLMatcherPortFilter> port_filter); |
| 333 |
| 271 ID id() const { return id_; } | 334 ID id() const { return id_; } |
| 272 const Conditions& conditions() const { return conditions_; } | 335 const Conditions& conditions() const { return conditions_; } |
| 336 const QueryConditions& query_conditions() const { return query_conditions_; } |
| 273 | 337 |
| 274 bool IsMatch(const std::set<StringPattern::ID>& matching_patterns, | 338 bool IsMatch(const std::set<StringPattern::ID>& matching_patterns, |
| 275 const GURL& url) const; | 339 const GURL& url) const; |
| 276 | 340 |
| 341 bool IsMatch(const std::set<StringPattern::ID>& matching_patterns, |
| 342 const GURL& url, |
| 343 const std::string& url_for_component_searches) const; |
| 344 |
| 277 private: | 345 private: |
| 278 friend class base::RefCounted<URLMatcherConditionSet>; | 346 friend class base::RefCounted<URLMatcherConditionSet>; |
| 279 ~URLMatcherConditionSet(); | 347 ~URLMatcherConditionSet(); |
| 280 ID id_; | 348 ID id_; |
| 281 Conditions conditions_; | 349 Conditions conditions_; |
| 350 QueryConditions query_conditions_; |
| 282 scoped_ptr<URLMatcherSchemeFilter> scheme_filter_; | 351 scoped_ptr<URLMatcherSchemeFilter> scheme_filter_; |
| 283 scoped_ptr<URLMatcherPortFilter> port_filter_; | 352 scoped_ptr<URLMatcherPortFilter> port_filter_; |
| 284 | 353 |
| 285 DISALLOW_COPY_AND_ASSIGN(URLMatcherConditionSet); | 354 DISALLOW_COPY_AND_ASSIGN(URLMatcherConditionSet); |
| 286 }; | 355 }; |
| 287 | 356 |
| 288 // This class allows matching one URL against a large set of | 357 // This class allows matching one URL against a large set of |
| 289 // URLMatcherConditionSets at the same time. | 358 // URLMatcherConditionSets at the same time. |
| 290 class URL_MATCHER_EXPORT URLMatcher { | 359 class URL_MATCHER_EXPORT URLMatcher { |
| 291 public: | 360 public: |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 RegexSetMatcher origin_and_path_regex_set_matcher_; | 417 RegexSetMatcher origin_and_path_regex_set_matcher_; |
| 349 std::set<const StringPattern*> registered_full_url_patterns_; | 418 std::set<const StringPattern*> registered_full_url_patterns_; |
| 350 std::set<const StringPattern*> registered_url_component_patterns_; | 419 std::set<const StringPattern*> registered_url_component_patterns_; |
| 351 | 420 |
| 352 DISALLOW_COPY_AND_ASSIGN(URLMatcher); | 421 DISALLOW_COPY_AND_ASSIGN(URLMatcher); |
| 353 }; | 422 }; |
| 354 | 423 |
| 355 } // namespace url_matcher | 424 } // namespace url_matcher |
| 356 | 425 |
| 357 #endif // COMPONENTS_URL_MATCHER_URL_MATCHER_H_ | 426 #endif // COMPONENTS_URL_MATCHER_URL_MATCHER_H_ |
| OLD | NEW |