| 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 #include "chrome/browser/extensions/api/declarative/url_matcher.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "googleurl/src/gurl.h" |
| 11 |
| 12 namespace extensions { |
| 13 |
| 14 // This set of classes implement a mapping of URL Component Patterns, such as |
| 15 // host_prefix, host_suffix, host_equals, ..., etc., to SubstringPatterns. |
| 16 // |
| 17 // The idea of this mapping is to reduce the problem of comparing many |
| 18 // URL Component Patterns against one URL to the problem of searching many |
| 19 // substrings in one string: |
| 20 // |
| 21 // ---------------------- -------------------- |
| 22 // | URL Query operator | ----translate----> | SubstringPattern | |
| 23 // ---------------------- -------------------- |
| 24 // ^ |
| 25 // | |
| 26 // compare |
| 27 // | |
| 28 // v |
| 29 // ---------------------- -------------------- |
| 30 // | URL to compare | | | |
| 31 // | to all URL Query | ----translate----> | String | |
| 32 // | operators | | | |
| 33 // ---------------------- -------------------- |
| 34 // |
| 35 // The reason for this problem reduction is that there are efficient algorithms |
| 36 // for searching many substrings in one string (see Aho-Corasick algorithm). |
| 37 // |
| 38 // Case 1: {host,path,query}_{prefix,suffix,equals} searches. |
| 39 // ========================================================== |
| 40 // |
| 41 // For searches in this class, we normalize URLs as follows: |
| 42 // |
| 43 // Step 1: |
| 44 // Remove scheme, port and segment from URL: |
| 45 // -> http://www.example.com:8080/index.html?search=foo#first_match becomes |
| 46 // www.example.com/index.html?search=foo |
| 47 // |
| 48 // We remove the scheme and port number because they can be checked later |
| 49 // in a secondary filter step. We remove the segment (the #... part) because |
| 50 // this is not guaranteed to be ASCII-7 encoded. |
| 51 // |
| 52 // Step 2: |
| 53 // Translate URL to String and add the following position markers: |
| 54 // - BU = Beginning of URL |
| 55 // - ED = End of Domain |
| 56 // - EP = End of Path |
| 57 // - EU = End of URL |
| 58 // Furthermore, the hostname is canonicalized to start with a ".". |
| 59 // |
| 60 // Position markers are represented as characters >127, which are therefore |
| 61 // guaranteed not to be part of the ASCII-7 encoded URL character set. |
| 62 // |
| 63 // -> www.example.com/index.html?search=foo becomes |
| 64 // BU .www.example.com ED /index.html EP ?search=foo EU |
| 65 // |
| 66 // -> www.example.com/index.html becomes |
| 67 // BU .www.example.com ED /index.html EP EU |
| 68 // |
| 69 // Step 3: |
| 70 // Translate URL Component Patterns as follows: |
| 71 // |
| 72 // host_prefix(prefix) = BU add_missing_dot_prefix(prefix) |
| 73 // -> host_prefix("www.example") = BU .www.example |
| 74 // |
| 75 // host_suffix(suffix) = suffix ED |
| 76 // -> host_suffix("example.com") = example.com ED |
| 77 // -> host_suffix(".example.com") = .example.com ED |
| 78 // |
| 79 // host_equals(domain) = BU add_missing_dot_prefix(domain) ED |
| 80 // -> host_equals("www.example.com") = BU .www.example.com ED |
| 81 // |
| 82 // Similarly for path query parameters ({path, query}_{prefix, suffix, equals}). |
| 83 // |
| 84 // With this, we can search the SubstringPatterns in the normalized URL. |
| 85 // |
| 86 // |
| 87 // Case 2: url_{prefix,suffix,equals,contains} searches. |
| 88 // ===================================================== |
| 89 // |
| 90 // Step 1: as above |
| 91 // |
| 92 // Step 2: |
| 93 // Translate URL to String and add the following position markers: |
| 94 // - BU = Beginning of URL |
| 95 // - EU = End of URL |
| 96 // Furthermore, the hostname is canonicalized to start with a ".". |
| 97 // |
| 98 // -> www.example.com/index.html?search=foo becomes |
| 99 // BU .www.example.com/index.html?search=foo EU |
| 100 // |
| 101 // url_prefix(prefix) = BU add_missing_dot_prefix(prefix) |
| 102 // -> url_prefix("www.example") = BU .www.example |
| 103 // |
| 104 // url_contains(substring) = substring |
| 105 // -> url_contains("index") = index |
| 106 // |
| 107 // |
| 108 // Case 3: {host,path,query}_contains searches. |
| 109 // ============================================ |
| 110 // |
| 111 // These kinds of searches are not supported directly but can be derived |
| 112 // by a combination of a url_contains() query followed by an explicit test: |
| 113 // |
| 114 // host_contains(str) = url_contains(str) followed by test whether str occurs |
| 115 // in host comonent of original URL. |
| 116 // -> host_contains("example.co") = example.co |
| 117 // followed by gurl.host().find("example.co"); |
| 118 // |
| 119 // [similarly for path_contains and query_contains]. |
| 120 |
| 121 |
| 122 // |
| 123 // URLMatcherCondition |
| 124 // |
| 125 |
| 126 URLMatcherCondition::URLMatcherCondition() |
| 127 : criterion_(HOST_PREFIX), |
| 128 substring_pattern_(NULL) {} |
| 129 |
| 130 URLMatcherCondition::~URLMatcherCondition() {} |
| 131 |
| 132 URLMatcherCondition::URLMatcherCondition( |
| 133 Criterion criterion, |
| 134 const SubstringPattern* substring_pattern) |
| 135 : criterion_(criterion), |
| 136 substring_pattern_(substring_pattern) {} |
| 137 |
| 138 URLMatcherCondition::URLMatcherCondition(const URLMatcherCondition& rhs) |
| 139 : criterion_(rhs.criterion_), |
| 140 substring_pattern_(rhs.substring_pattern_) {} |
| 141 |
| 142 URLMatcherCondition& URLMatcherCondition::operator=( |
| 143 const URLMatcherCondition& rhs) { |
| 144 criterion_ = rhs.criterion_; |
| 145 substring_pattern_ = rhs.substring_pattern_; |
| 146 return *this; |
| 147 } |
| 148 |
| 149 bool URLMatcherCondition::operator<(const URLMatcherCondition& rhs) const { |
| 150 if (criterion_ < rhs.criterion_) return true; |
| 151 if (criterion_ > rhs.criterion_) return false; |
| 152 if (substring_pattern_ != NULL && rhs.substring_pattern_ != NULL) |
| 153 return *substring_pattern_ < *rhs.substring_pattern_; |
| 154 if (substring_pattern_ == NULL && rhs.substring_pattern_ != NULL) return true; |
| 155 // Either substring_pattern_ != NULL && rhs.substring_pattern_ == NULL, |
| 156 // or both are NULL. |
| 157 return false; |
| 158 } |
| 159 |
| 160 bool URLMatcherCondition::IsFullURLCondition() const { |
| 161 // For these criteria the SubstringMatcher needs to be executed on the |
| 162 // GURL that is canonlizaliced with |
| 163 // URLMatcherConditionFactory::CanonicalizeURLForFullSearches. |
| 164 switch (criterion_) { |
| 165 case HOST_CONTAINS: |
| 166 case PATH_CONTAINS: |
| 167 case QUERY_CONTAINS: |
| 168 case URL_PREFIX: |
| 169 case URL_SUFFIX: |
| 170 case URL_CONTAINS: |
| 171 case URL_EQUALS: |
| 172 return true; |
| 173 default: |
| 174 break; |
| 175 } |
| 176 return false; |
| 177 } |
| 178 |
| 179 bool URLMatcherCondition::IsMatch( |
| 180 const std::set<SubstringPattern::ID>& matching_substring_patterns, |
| 181 const GURL& url) const { |
| 182 DCHECK(substring_pattern_); |
| 183 if (matching_substring_patterns.find(substring_pattern_->id()) == |
| 184 matching_substring_patterns.end()) |
| 185 return false; |
| 186 // The criteria HOST_CONTAINS, PATH_CONTAINS, QUERY_CONTAINS are based on |
| 187 // a substring match on the raw URL. In case of a match, we need to verify |
| 188 // that the match was found in the correct component of the URL. |
| 189 switch (criterion_) { |
| 190 case HOST_CONTAINS: |
| 191 return url.host().find(substring_pattern_->pattern()) != |
| 192 std::string::npos; |
| 193 case PATH_CONTAINS: |
| 194 return url.path().find(substring_pattern_->pattern()) != |
| 195 std::string::npos; |
| 196 case QUERY_CONTAINS: |
| 197 return url.query().find(substring_pattern_->pattern()) != |
| 198 std::string::npos; |
| 199 default: |
| 200 break; |
| 201 } |
| 202 return true; |
| 203 } |
| 204 |
| 205 // |
| 206 // URLMatcherConditionFactory |
| 207 // |
| 208 |
| 209 namespace { |
| 210 // These are symbols that are not contained in 7-bit ASCII used in GURLs. |
| 211 const char kBeginningOfURL[] = {-1, 0}; |
| 212 const char kEndOfDomain[] = {-2, 0}; |
| 213 const char kEndOfPath[] = {-3, 0}; |
| 214 const char kEndOfURL[] = {-4, 0}; |
| 215 } // namespace |
| 216 |
| 217 URLMatcherConditionFactory::URLMatcherConditionFactory() : id_counter_(0) {} |
| 218 |
| 219 URLMatcherConditionFactory::~URLMatcherConditionFactory() { |
| 220 STLDeleteElements(&pattern_singletons_); |
| 221 } |
| 222 |
| 223 std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches( |
| 224 const GURL& url) { |
| 225 return kBeginningOfURL + CanonicalizeHostname(url.host()) + kEndOfDomain + |
| 226 url.path() + kEndOfPath + (url.has_query() ? "?" + url.query() : "") + |
| 227 kEndOfURL; |
| 228 } |
| 229 |
| 230 URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition( |
| 231 const std::string& prefix) { |
| 232 return CreateCondition(URLMatcherCondition::HOST_PREFIX, |
| 233 kBeginningOfURL + CanonicalizeHostname(prefix)); |
| 234 } |
| 235 |
| 236 URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition( |
| 237 const std::string& suffix) { |
| 238 return CreateCondition(URLMatcherCondition::HOST_SUFFIX, |
| 239 suffix + kEndOfDomain); |
| 240 } |
| 241 |
| 242 URLMatcherCondition URLMatcherConditionFactory::CreateHostContainsCondition( |
| 243 const std::string& str) { |
| 244 return CreateCondition(URLMatcherCondition::HOST_CONTAINS, str); |
| 245 } |
| 246 |
| 247 URLMatcherCondition URLMatcherConditionFactory::CreateHostEqualsCondition( |
| 248 const std::string& str) { |
| 249 return CreateCondition(URLMatcherCondition::HOST_EQUALS, |
| 250 kBeginningOfURL + CanonicalizeHostname(str) + kEndOfDomain); |
| 251 } |
| 252 |
| 253 URLMatcherCondition URLMatcherConditionFactory::CreatePathPrefixCondition( |
| 254 const std::string& prefix) { |
| 255 return CreateCondition(URLMatcherCondition::PATH_PREFIX, |
| 256 kEndOfDomain + prefix); |
| 257 } |
| 258 |
| 259 URLMatcherCondition URLMatcherConditionFactory::CreatePathSuffixCondition( |
| 260 const std::string& suffix) { |
| 261 return CreateCondition(URLMatcherCondition::HOST_SUFFIX, suffix + kEndOfPath); |
| 262 } |
| 263 |
| 264 URLMatcherCondition URLMatcherConditionFactory::CreatePathContainsCondition( |
| 265 const std::string& str) { |
| 266 return CreateCondition(URLMatcherCondition::PATH_CONTAINS, str); |
| 267 } |
| 268 |
| 269 URLMatcherCondition URLMatcherConditionFactory::CreatePathEqualsCondition( |
| 270 const std::string& str) { |
| 271 return CreateCondition(URLMatcherCondition::PATH_EQUALS, |
| 272 kEndOfDomain + str + kEndOfPath); |
| 273 } |
| 274 |
| 275 URLMatcherCondition URLMatcherConditionFactory::CreateQueryPrefixCondition( |
| 276 const std::string& prefix) { |
| 277 return CreateCondition(URLMatcherCondition::QUERY_PREFIX, |
| 278 kEndOfPath + prefix); |
| 279 } |
| 280 |
| 281 URLMatcherCondition URLMatcherConditionFactory::CreateQuerySuffixCondition( |
| 282 const std::string& suffix) { |
| 283 return CreateCondition(URLMatcherCondition::QUERY_SUFFIX, suffix + kEndOfURL); |
| 284 } |
| 285 |
| 286 URLMatcherCondition URLMatcherConditionFactory::CreateQueryContainsCondition( |
| 287 const std::string& str) { |
| 288 return CreateCondition(URLMatcherCondition::QUERY_CONTAINS, str); |
| 289 } |
| 290 |
| 291 URLMatcherCondition URLMatcherConditionFactory::CreateQueryEqualsCondition( |
| 292 const std::string& str) { |
| 293 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, |
| 294 kEndOfPath + str + kEndOfURL); |
| 295 } |
| 296 |
| 297 URLMatcherCondition |
| 298 URLMatcherConditionFactory::CreateHostSuffixPathPrefixCondition( |
| 299 const std::string& host_suffix, |
| 300 const std::string& path_prefix) { |
| 301 return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX, |
| 302 host_suffix + kEndOfDomain + path_prefix); |
| 303 } |
| 304 |
| 305 std::string URLMatcherConditionFactory::CanonicalizeURLForFullSearches( |
| 306 const GURL& url) { |
| 307 return kBeginningOfURL + CanonicalizeHostname(url.host()) + url.path() + |
| 308 (url.has_query() ? "?" + url.query() : "") + kEndOfURL; |
| 309 } |
| 310 |
| 311 URLMatcherCondition URLMatcherConditionFactory::CreateURLPrefixCondition( |
| 312 const std::string& prefix) { |
| 313 return CreateCondition(URLMatcherCondition::URL_PREFIX, |
| 314 kBeginningOfURL + CanonicalizeHostname(prefix)); |
| 315 } |
| 316 |
| 317 URLMatcherCondition URLMatcherConditionFactory::CreateURLSuffixCondition( |
| 318 const std::string& suffix) { |
| 319 return CreateCondition(URLMatcherCondition::URL_SUFFIX, suffix + kEndOfURL); |
| 320 } |
| 321 |
| 322 URLMatcherCondition URLMatcherConditionFactory::CreateURLContainsCondition( |
| 323 const std::string& str) { |
| 324 return CreateCondition(URLMatcherCondition::URL_CONTAINS, str); |
| 325 } |
| 326 |
| 327 URLMatcherCondition URLMatcherConditionFactory::CreateURLEqualsCondition( |
| 328 const std::string& str) { |
| 329 return CreateCondition(URLMatcherCondition::QUERY_EQUALS, |
| 330 kBeginningOfURL + CanonicalizeHostname(str) + kEndOfURL); |
| 331 } |
| 332 |
| 333 void URLMatcherConditionFactory::ForgetUnusedPatterns( |
| 334 const std::set<SubstringPattern::ID>& used_patterns) { |
| 335 PatternSingletons::iterator i = pattern_singletons_.begin(); |
| 336 while (i != pattern_singletons_.end()) { |
| 337 if (used_patterns.find((*i)->id()) != used_patterns.end()) |
| 338 ++i; |
| 339 else |
| 340 pattern_singletons_.erase(i++); |
| 341 } |
| 342 } |
| 343 |
| 344 URLMatcherCondition URLMatcherConditionFactory::CreateCondition( |
| 345 URLMatcherCondition::Criterion criterion, |
| 346 const std::string& pattern) { |
| 347 SubstringPattern search_pattern(pattern, 0); |
| 348 PatternSingletons::const_iterator iter = |
| 349 pattern_singletons_.find(&search_pattern); |
| 350 if (iter != pattern_singletons_.end()) { |
| 351 return URLMatcherCondition(criterion, *iter); |
| 352 } else { |
| 353 SubstringPattern* new_pattern = |
| 354 new SubstringPattern(pattern, id_counter_++); |
| 355 pattern_singletons_.insert(new_pattern); |
| 356 return URLMatcherCondition(criterion, new_pattern); |
| 357 } |
| 358 } |
| 359 |
| 360 std::string URLMatcherConditionFactory::CanonicalizeHostname( |
| 361 const std::string& hostname) const { |
| 362 if (!hostname.empty() && hostname[0] == '.') |
| 363 return hostname; |
| 364 else |
| 365 return "." + hostname; |
| 366 } |
| 367 |
| 368 bool URLMatcherConditionFactory::SubstringPatternPointerCompare::operator()( |
| 369 SubstringPattern* lhs, |
| 370 SubstringPattern* rhs) const { |
| 371 if (lhs == NULL && rhs != NULL) return true; |
| 372 if (lhs != NULL && rhs != NULL) |
| 373 return lhs->pattern() < rhs->pattern(); |
| 374 // Either both are NULL or only rhs is NULL. |
| 375 return false; |
| 376 } |
| 377 |
| 378 // |
| 379 // URLMatcherConditionSet |
| 380 // |
| 381 |
| 382 URLMatcherConditionSet::URLMatcherConditionSet() : id_(-1) {} |
| 383 |
| 384 URLMatcherConditionSet::~URLMatcherConditionSet() {} |
| 385 |
| 386 URLMatcherConditionSet::URLMatcherConditionSet( |
| 387 ID id, |
| 388 const Conditions& conditions) |
| 389 : id_(id), |
| 390 conditions_(conditions) {} |
| 391 |
| 392 URLMatcherConditionSet::URLMatcherConditionSet( |
| 393 const URLMatcherConditionSet& rhs) |
| 394 : id_(rhs.id_), conditions_(rhs.conditions_) {} |
| 395 |
| 396 URLMatcherConditionSet& URLMatcherConditionSet::operator=( |
| 397 const URLMatcherConditionSet& rhs) { |
| 398 id_ = rhs.id_; |
| 399 conditions_ = rhs.conditions_; |
| 400 return *this; |
| 401 } |
| 402 |
| 403 bool URLMatcherConditionSet::IsMatch( |
| 404 const std::set<SubstringPattern::ID>& matching_substring_patterns, |
| 405 const GURL& url) const { |
| 406 for (Conditions::const_iterator i = conditions_.begin(); |
| 407 i != conditions_.end(); ++i) { |
| 408 if (!i->IsMatch(matching_substring_patterns, url)) |
| 409 return false; |
| 410 } |
| 411 return true; |
| 412 } |
| 413 |
| 414 |
| 415 // |
| 416 // URLMatcher |
| 417 // |
| 418 |
| 419 URLMatcher::URLMatcher() {} |
| 420 |
| 421 URLMatcher::~URLMatcher() {} |
| 422 |
| 423 void URLMatcher::AddConditionSets( |
| 424 const std::vector<URLMatcherConditionSet>& condition_sets) { |
| 425 for (std::vector<URLMatcherConditionSet>::const_iterator i = |
| 426 condition_sets.begin(); i != condition_sets.end(); ++i) { |
| 427 DCHECK(url_matcher_condition_sets_.find(i->id()) == |
| 428 url_matcher_condition_sets_.end()); |
| 429 url_matcher_condition_sets_[i->id()] = *i; |
| 430 } |
| 431 UpdateInternalDatastructures(); |
| 432 } |
| 433 |
| 434 void URLMatcher::RemoveConditionSets( |
| 435 const std::vector<URLMatcherConditionSet::ID>& condition_set_ids) { |
| 436 for (std::vector<URLMatcherConditionSet::ID>::const_iterator i = |
| 437 condition_set_ids.begin(); i != condition_set_ids.end(); ++i) { |
| 438 DCHECK(url_matcher_condition_sets_.find(*i) != |
| 439 url_matcher_condition_sets_.end()); |
| 440 url_matcher_condition_sets_.erase(*i); |
| 441 } |
| 442 UpdateInternalDatastructures(); |
| 443 } |
| 444 |
| 445 std::set<URLMatcherConditionSet::ID> URLMatcher::MatchURL(const GURL& url) { |
| 446 // Find all IDs of SubstringPatterns that match |url|. |
| 447 // See URLMatcherConditionFactory for the canonicalization of URLs and the |
| 448 // distinction between full url searches and url component searches. |
| 449 std::set<SubstringPattern::ID> matches; |
| 450 full_url_matcher_.Match( |
| 451 condition_factory_.CanonicalizeURLForFullSearches(url), &matches); |
| 452 url_component_matcher_.Match( |
| 453 condition_factory_.CanonicalizeURLForComponentSearches(url), &matches); |
| 454 |
| 455 // Calculate all URLMatcherConditionSets for which all URLMatcherConditions |
| 456 // were fulfilled. |
| 457 std::set<URLMatcherConditionSet::ID> result; |
| 458 for (std::set<SubstringPattern::ID>::const_iterator i = matches.begin(); |
| 459 i != matches.end(); ++i) { |
| 460 // For each URLMatcherConditionSet there is exactly one condition |
| 461 // registered in substring_match_triggers_. This means that the following |
| 462 // logic tests each URLMatcherConditionSet exactly once if it can be |
| 463 // completely fulfilled. |
| 464 std::set<URLMatcherConditionSet::ID>& condition_sets = |
| 465 substring_match_triggers_[*i]; |
| 466 for (std::set<URLMatcherConditionSet::ID>::const_iterator j = |
| 467 condition_sets.begin(); j != condition_sets.end(); ++j) { |
| 468 if (url_matcher_condition_sets_[*j].IsMatch(matches, url)) |
| 469 result.insert(*j); |
| 470 } |
| 471 } |
| 472 |
| 473 return result; |
| 474 } |
| 475 |
| 476 void URLMatcher::UpdateSubstringSetMatcher(bool full_url_conditions) { |
| 477 // The purpose of |full_url_conditions| is just that we need to execute |
| 478 // the same logic once for Full URL searches and once for URL Component |
| 479 // searches (see URLMatcherConditionFactory). |
| 480 |
| 481 // Determine which patterns need to be registered when this function |
| 482 // terminates. |
| 483 std::set<const SubstringPattern*> new_patterns; |
| 484 for (URLMatcherConditionSets::const_iterator condition_set_iter = |
| 485 url_matcher_condition_sets_.begin(); |
| 486 condition_set_iter != url_matcher_condition_sets_.end(); |
| 487 ++condition_set_iter) { |
| 488 const URLMatcherConditionSet::Conditions& conditions = |
| 489 condition_set_iter->second.conditions(); |
| 490 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter = |
| 491 conditions.begin(); condition_iter != conditions.end(); |
| 492 ++condition_iter) { |
| 493 // If we are called to process Full URL searches, ignore all others, |
| 494 // and vice versa. |
| 495 if (full_url_conditions == condition_iter->IsFullURLCondition()) |
| 496 new_patterns.insert(condition_iter->substring_pattern()); |
| 497 } |
| 498 } |
| 499 |
| 500 // This is the set of patterns that were registered before this function |
| 501 // is called. |
| 502 std::set<const SubstringPattern*>& registered_patterns = |
| 503 full_url_conditions ? registered_full_url_patterns_ |
| 504 : registered_url_component_patterns_; |
| 505 |
| 506 // Add all patterns that are in new_patterns but not in registered_patterns. |
| 507 std::vector<const SubstringPattern*> patterns_to_register; |
| 508 std::set_difference( |
| 509 new_patterns.begin(), new_patterns.end(), |
| 510 registered_patterns.begin(), registered_patterns.end(), |
| 511 std::back_inserter(patterns_to_register)); |
| 512 |
| 513 // Remove all patterns that are in registered_patterns but not in |
| 514 // new_patterns. |
| 515 std::vector<const SubstringPattern*> patterns_to_unregister; |
| 516 std::set_difference( |
| 517 registered_patterns.begin(), registered_patterns.end(), |
| 518 new_patterns.begin(), new_patterns.end(), |
| 519 std::back_inserter(patterns_to_unregister)); |
| 520 |
| 521 // Update the SubstringSetMatcher. |
| 522 SubstringSetMatcher& url_matcher = |
| 523 full_url_conditions ? full_url_matcher_ : url_component_matcher_; |
| 524 url_matcher.RegisterAndUnregisterPatterns(patterns_to_register, |
| 525 patterns_to_unregister); |
| 526 |
| 527 // Update the set of registered_patterns for the next time this function |
| 528 // is being called. |
| 529 registered_patterns.swap(new_patterns); |
| 530 } |
| 531 |
| 532 void URLMatcher::UpdateTriggers() { |
| 533 // Count substring pattern frequencies. |
| 534 std::map<SubstringPattern::ID, size_t> substring_pattern_frequencies; |
| 535 for (URLMatcherConditionSets::const_iterator condition_set_iter = |
| 536 url_matcher_condition_sets_.begin(); |
| 537 condition_set_iter != url_matcher_condition_sets_.end(); |
| 538 ++condition_set_iter) { |
| 539 const URLMatcherConditionSet::Conditions& conditions = |
| 540 condition_set_iter->second.conditions(); |
| 541 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter = |
| 542 conditions.begin(); condition_iter != conditions.end(); |
| 543 ++condition_iter) { |
| 544 const SubstringPattern* pattern = condition_iter->substring_pattern(); |
| 545 substring_pattern_frequencies[pattern->id()]++; |
| 546 } |
| 547 } |
| 548 |
| 549 // Update trigger conditions: Determine for each URLMatcherConditionSet which |
| 550 // URLMatcherCondition contains a SubstringPattern that occurs least |
| 551 // frequently in this URLMatcher. We assume that this condition is very |
| 552 // specific and occurs rarely in URLs. If a match occurs for this |
| 553 // URLMatcherCondition, we want to test all other URLMatcherCondition in the |
| 554 // respective URLMatcherConditionSet as well to see whether the entire |
| 555 // URLMatcherConditionSet is considered matching. |
| 556 substring_match_triggers_.clear(); |
| 557 for (URLMatcherConditionSets::const_iterator condition_set_iter = |
| 558 url_matcher_condition_sets_.begin(); |
| 559 condition_set_iter != url_matcher_condition_sets_.end(); |
| 560 ++condition_set_iter) { |
| 561 const URLMatcherConditionSet::Conditions& conditions = |
| 562 condition_set_iter->second.conditions(); |
| 563 if (conditions.empty()) |
| 564 continue; |
| 565 URLMatcherConditionSet::Conditions::const_iterator condition_iter = |
| 566 conditions.begin(); |
| 567 SubstringPattern::ID trigger = condition_iter->substring_pattern()->id(); |
| 568 // We skip the first element in the following loop. |
| 569 ++condition_iter; |
| 570 for (; condition_iter != conditions.end(); ++condition_iter) { |
| 571 SubstringPattern::ID current_id = |
| 572 condition_iter->substring_pattern()->id(); |
| 573 if (substring_pattern_frequencies[trigger] > |
| 574 substring_pattern_frequencies[current_id]) { |
| 575 trigger = current_id; |
| 576 } |
| 577 } |
| 578 substring_match_triggers_[trigger].insert(condition_set_iter->second.id()); |
| 579 } |
| 580 } |
| 581 |
| 582 void URLMatcher::UpdateConditionFactory() { |
| 583 std::set<SubstringPattern::ID> used_patterns; |
| 584 for (URLMatcherConditionSets::const_iterator condition_set_iter = |
| 585 url_matcher_condition_sets_.begin(); |
| 586 condition_set_iter != url_matcher_condition_sets_.end(); |
| 587 ++condition_set_iter) { |
| 588 const URLMatcherConditionSet::Conditions& conditions = |
| 589 condition_set_iter->second.conditions(); |
| 590 for (URLMatcherConditionSet::Conditions::const_iterator condition_iter = |
| 591 conditions.begin(); condition_iter != conditions.end(); |
| 592 ++condition_iter) { |
| 593 used_patterns.insert(condition_iter->substring_pattern()->id()); |
| 594 } |
| 595 } |
| 596 condition_factory_.ForgetUnusedPatterns(used_patterns); |
| 597 } |
| 598 |
| 599 void URLMatcher::UpdateInternalDatastructures() { |
| 600 UpdateSubstringSetMatcher(false); |
| 601 UpdateSubstringSetMatcher(true); |
| 602 UpdateTriggers(); |
| 603 UpdateConditionFactory(); |
| 604 } |
| 605 |
| 606 } // namespace extensions |
| OLD | NEW |