| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ | 5 #ifndef COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ |
| 6 #define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ | 6 #define COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ |
| 7 | 7 |
| 8 #include <iosfwd> |
| 9 |
| 8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 9 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 10 #include "components/subresource_filter/core/common/proto/rules.pb.h" | 12 #include "components/subresource_filter/core/common/proto/rules.pb.h" |
| 11 | 13 |
| 14 class GURL; |
| 15 |
| 12 namespace subresource_filter { | 16 namespace subresource_filter { |
| 13 | 17 |
| 14 namespace flat { | 18 namespace flat { |
| 15 struct UrlRule; // The FlatBuffers version of UrlRule. | 19 struct UrlRule; // The FlatBuffers version of UrlRule. |
| 16 } | 20 } |
| 17 | 21 |
| 18 // The structure used to mirror a URL pattern regardless of the representation | 22 // The structure used to mirror a URL pattern regardless of the representation |
| 19 // of the UrlRule that owns it. | 23 // of the UrlRule that owns it, and to match it against URLs. |
| 20 struct UrlPattern { | 24 class UrlPattern { |
| 25 public: |
| 21 UrlPattern(); | 26 UrlPattern(); |
| 22 | 27 |
| 23 // Creates a |url_pattern| of a certain |type|. | 28 // Creates a |url_pattern| of a certain |type|. |
| 24 UrlPattern(base::StringPiece url_pattern, | 29 UrlPattern(base::StringPiece url_pattern, |
| 25 proto::UrlPatternType type = proto::URL_PATTERN_TYPE_WILDCARDED); | 30 proto::UrlPatternType type = proto::URL_PATTERN_TYPE_WILDCARDED); |
| 26 | 31 |
| 27 // Creates a WILDCARDED |url_pattern| with the specified anchors. | 32 // Creates a WILDCARDED |url_pattern| with the specified anchors. |
| 28 UrlPattern(base::StringPiece url_pattern, | 33 UrlPattern(base::StringPiece url_pattern, |
| 29 proto::AnchorType anchor_left, | 34 proto::AnchorType anchor_left, |
| 30 proto::AnchorType anchor_right); | 35 proto::AnchorType anchor_right); |
| 31 | 36 |
| 32 // The following constructors create UrlPattern from one of the UrlRule | 37 // The following constructors create UrlPattern from one of the UrlRule |
| 33 // representations. The passed in |rule| must outlive the created instance. | 38 // representations. The passed in |rule| must outlive the created instance. |
| 34 explicit UrlPattern(const flat::UrlRule& rule); | 39 explicit UrlPattern(const flat::UrlRule& rule); |
| 35 explicit UrlPattern(const proto::UrlRule& rule); | 40 explicit UrlPattern(const proto::UrlRule& rule); |
| 36 | 41 |
| 37 ~UrlPattern(); | 42 ~UrlPattern(); |
| 38 | 43 |
| 39 proto::UrlPatternType type = proto::URL_PATTERN_TYPE_UNSPECIFIED; | 44 proto::UrlPatternType type() const { return type_; } |
| 40 base::StringPiece url_pattern; | 45 base::StringPiece url_pattern() const { return url_pattern_; } |
| 46 proto::AnchorType anchor_left() const { return anchor_left_; } |
| 47 proto::AnchorType anchor_right() const { return anchor_right_; } |
| 48 bool match_case() const { return match_case_; } |
| 41 | 49 |
| 42 proto::AnchorType anchor_left = proto::ANCHOR_TYPE_NONE; | 50 // Returns whether the |url| matches the URL |pattern|. Requires the type of |
| 43 proto::AnchorType anchor_right = proto::ANCHOR_TYPE_NONE; | 51 // |this| pattern to be either SUBSTRING or WILDCARDED. |
| 44 | 52 // |
| 45 bool match_case = false; | 53 // Splits the pattern into subpatterns separated by '*' wildcards, and |
| 54 // greedily finds each of them in the spec of the |url|. Respects anchors at |
| 55 // either end of the pattern, and '^' separator placeholders when comparing a |
| 56 // subpattern to a subtring of the spec. |
| 57 bool MatchesUrl(const GURL& url) const; |
| 46 | 58 |
| 47 private: | 59 private: |
| 60 // TODO(pkalinnikov): Store flat:: types instead of proto::, in order to avoid |
| 61 // conversions in IndexedRuleset. |
| 62 proto::UrlPatternType type_ = proto::URL_PATTERN_TYPE_UNSPECIFIED; |
| 63 base::StringPiece url_pattern_; |
| 64 |
| 65 proto::AnchorType anchor_left_ = proto::ANCHOR_TYPE_NONE; |
| 66 proto::AnchorType anchor_right_ = proto::ANCHOR_TYPE_NONE; |
| 67 |
| 68 // TODO(pkalinnikov): Implement case-insensitive matching. |
| 69 bool match_case_ = false; |
| 70 |
| 48 DISALLOW_COPY_AND_ASSIGN(UrlPattern); | 71 DISALLOW_COPY_AND_ASSIGN(UrlPattern); |
| 49 }; | 72 }; |
| 50 | 73 |
| 74 // Allow pretty-printing URLPatterns when they are used in GTest assertions. |
| 75 std::ostream& operator<<(std::ostream& out, const UrlPattern& pattern); |
| 76 |
| 51 } // namespace subresource_filter | 77 } // namespace subresource_filter |
| 52 | 78 |
| 53 #endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ | 79 #endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ |
| OLD | NEW |