[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 5 | #include "components/url_matcher/url_matcher_factory.h" |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 6 | |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 7 | #include <algorithm> |
8 | #include <cctype> | ||||
Gyuyoung Kim | 8b084c0 | 2018-01-23 13:34:37 | [diff] [blame] | 9 | #include <memory> |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 10 | #include <utility> |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 11 | |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 12 | #include "base/check.h" |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
[email protected] | f439096 | 2013-06-11 07:29:22 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 16 | #include "components/url_matcher/url_matcher_constants.h" |
Yann Dago | e65b7ee | 2022-01-04 19:01:35 | [diff] [blame] | 17 | #include "components/url_matcher/url_util.h" |
battre | 0739c64 | 2015-12-21 17:45:27 | [diff] [blame] | 18 | #include "third_party/re2/src/re2/re2.h" |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 19 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 20 | namespace url_matcher { |
21 | |||||
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 22 | namespace keys = url_matcher_constants; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 23 | |
24 | namespace { | ||||
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 25 | |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 26 | // Error messages: |
27 | const char kInvalidPortRanges[] = "Invalid port ranges in UrlFilter."; | ||||
28 | const char kVectorOfStringsExpected[] = | ||||
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 29 | "UrlFilter attribute '%s' expected a vector of strings as parameter."; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 30 | const char kUnknownURLFilterAttribute[] = |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 31 | "Unknown attribute '%s' in UrlFilter."; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 32 | const char kAttributeExpectedString[] = |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 33 | "UrlFilter attribute '%s' expected a string value."; |
[email protected] | 7a80ce8 | 2012-10-15 20:37:21 | [diff] [blame] | 34 | const char kUnparseableRegexString[] = |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 35 | "Could not parse regular expression '%s': %s"; |
36 | const char kLowerCaseExpected[] = "%s values need to be in lower case."; | ||||
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 37 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 38 | // Registry for all factory methods of URLMatcherConditionFactory |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 39 | // that allows translating string literals from the extension API into |
40 | // the corresponding factory method to be called. | ||||
41 | class URLMatcherConditionFactoryMethods { | ||||
42 | public: | ||||
43 | URLMatcherConditionFactoryMethods() { | ||||
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 44 | typedef URLMatcherConditionFactory F; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 45 | factory_methods_[keys::kHostContainsKey] = &F::CreateHostContainsCondition; |
46 | factory_methods_[keys::kHostEqualsKey] = &F::CreateHostEqualsCondition; | ||||
47 | factory_methods_[keys::kHostPrefixKey] = &F::CreateHostPrefixCondition; | ||||
48 | factory_methods_[keys::kHostSuffixKey] = &F::CreateHostSuffixCondition; | ||||
[email protected] | 2280dc8 | 2013-04-11 20:04:01 | [diff] [blame] | 49 | factory_methods_[keys::kOriginAndPathMatchesKey] = |
50 | &F::CreateOriginAndPathMatchesCondition; | ||||
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 51 | factory_methods_[keys::kPathContainsKey] = &F::CreatePathContainsCondition; |
52 | factory_methods_[keys::kPathEqualsKey] = &F::CreatePathEqualsCondition; | ||||
53 | factory_methods_[keys::kPathPrefixKey] = &F::CreatePathPrefixCondition; | ||||
54 | factory_methods_[keys::kPathSuffixKey] = &F::CreatePathSuffixCondition; | ||||
55 | factory_methods_[keys::kQueryContainsKey] = | ||||
56 | &F::CreateQueryContainsCondition; | ||||
57 | factory_methods_[keys::kQueryEqualsKey] = &F::CreateQueryEqualsCondition; | ||||
58 | factory_methods_[keys::kQueryPrefixKey] = &F::CreateQueryPrefixCondition; | ||||
59 | factory_methods_[keys::kQuerySuffixKey] = &F::CreateQuerySuffixCondition; | ||||
60 | factory_methods_[keys::kURLContainsKey] = &F::CreateURLContainsCondition; | ||||
61 | factory_methods_[keys::kURLEqualsKey] = &F::CreateURLEqualsCondition; | ||||
62 | factory_methods_[keys::kURLPrefixKey] = &F::CreateURLPrefixCondition; | ||||
63 | factory_methods_[keys::kURLSuffixKey] = &F::CreateURLSuffixCondition; | ||||
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 64 | factory_methods_[keys::kURLMatchesKey] = &F::CreateURLMatchesCondition; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 65 | } |
66 | |||||
Peter Boström | 9f667c38 | 2021-10-01 20:09:31 | [diff] [blame] | 67 | URLMatcherConditionFactoryMethods(const URLMatcherConditionFactoryMethods&) = |
68 | delete; | ||||
69 | URLMatcherConditionFactoryMethods& operator=( | ||||
70 | const URLMatcherConditionFactoryMethods&) = delete; | ||||
71 | |||||
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 72 | // Returns whether a factory method for the specified |pattern_type| (e.g. |
73 | // "host_suffix") is known. | ||||
74 | bool Contains(const std::string& pattern_type) const { | ||||
75 | return factory_methods_.find(pattern_type) != factory_methods_.end(); | ||||
76 | } | ||||
77 | |||||
78 | // Creates a URLMatcherCondition instance from |url_matcher_condition_factory| | ||||
79 | // of the given |pattern_type| (e.g. "host_suffix") for the given | ||||
80 | // |pattern_value| (e.g. "example.com"). | ||||
81 | // The |pattern_type| needs to be known to this class (see Contains()) or | ||||
82 | // a CHECK is triggered. | ||||
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 83 | URLMatcherCondition Call( |
84 | URLMatcherConditionFactory* url_matcher_condition_factory, | ||||
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 85 | const std::string& pattern_type, |
86 | const std::string& pattern_value) const { | ||||
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 87 | auto i = factory_methods_.find(pattern_type); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 88 | CHECK(i != factory_methods_.end()); |
89 | const FactoryMethod& method = i->second; | ||||
90 | return (url_matcher_condition_factory->*method)(pattern_value); | ||||
91 | } | ||||
92 | |||||
93 | private: | ||||
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 94 | typedef URLMatcherCondition |
95 | (URLMatcherConditionFactory::* FactoryMethod) | ||||
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 96 | (const std::string& prefix); |