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. |
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. |
52 // TODO(pkalinnikov): Outline algorithms implemented in this method. | |
53 bool MatchesUrl(const GURL& url) const; | |
44 | 54 |
45 bool match_case = false; | 55 private: |
56 // TODO(pkalinnikov): Store flat:: types instead of proto::, in order to avoid | |
57 // convertions in IndexedRuleset. | |
58 proto::UrlPatternType type_ = proto::URL_PATTERN_TYPE_UNSPECIFIED; | |
59 base::StringPiece url_pattern_; | |
60 | |
61 proto::AnchorType anchor_left_ = proto::ANCHOR_TYPE_NONE; | |
62 proto::AnchorType anchor_right_ = proto::ANCHOR_TYPE_NONE; | |
63 | |
64 bool match_case_ = false; | |
46 | 65 |
47 private: | 66 private: |
Charlie Harrison
2017/04/04 13:58:26
Not needed
pkalinnikov
2017/04/04 16:42:59
Done.
| |
48 DISALLOW_COPY_AND_ASSIGN(UrlPattern); | 67 DISALLOW_COPY_AND_ASSIGN(UrlPattern); |
49 }; | 68 }; |
50 | 69 |
70 // Stream operator so UrlPattern can be used in testing assertions. | |
71 std::ostream& operator<<(std::ostream& out, const UrlPattern& pattern); | |
72 | |
51 } // namespace subresource_filter | 73 } // namespace subresource_filter |
52 | 74 |
53 #endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ | 75 #endif // COMPONENTS_SUBRESOURCE_FILTER_CORE_COMMON_URL_PATTERN_H_ |
OLD | NEW |