OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "url/origin_filter.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 namespace url { | |
11 | |
12 // static | |
13 scoped_ptr<OriginFilter> OriginFilter::AsWhitelist( | |
14 const std::vector<Origin>& origins) { | |
15 return make_scoped_ptr(new OriginFilter(origins, true /* whitelist_mode */)); | |
16 } | |
17 | |
18 // static | |
19 scoped_ptr<OriginFilter> OriginFilter::AsBlacklist( | |
20 const std::vector<Origin>& origins) { | |
21 return make_scoped_ptr(new OriginFilter(origins, false /* whitelist_mode */)); | |
22 } | |
23 | |
24 // static | |
25 scoped_ptr<OriginFilter> OriginFilter::Empty() { | |
26 // Empty filter matches everything, which means it's an empty blacklist. | |
27 std::vector<Origin> empty; | |
28 return make_scoped_ptr(new OriginFilter(empty, false /* whitelist_mode */)); | |
29 } | |
30 | |
31 OriginFilter::~OriginFilter() { | |
32 } | |
33 | |
34 bool OriginFilter::MatchesURL(const GURL& url) const { | |
35 return ((origin_list_.find(Origin(url)) != origin_list_.end()) == | |
36 whitelist_mode_); | |
37 } | |
38 | |
39 bool OriginFilter::MatchesURLWithSubdomains(const GURL& url) const { | |
brettw
2016/02/01 22:48:25
I don't understand this function. Why would the ca
msramek
2016/02/05 17:16:35
If the decision between exact origin / subdomains
| |
40 // If there is no concept of subdomains, simply delegate to MatchesURL(). | |
41 if (url.HostIsIPAddress()) | |
42 return MatchesURL(url); | |
43 | |
44 // TODO(msramek): We do not expect filters to be particularly large. | |
45 // If they are, replace std::set with a trie for faster searching. | |
46 std::string host = url.host(); | |
47 for (unsigned int i = 0; i < host.length(); ++i) { | |
48 if (i != 0 && host[i - 1] != '.') | |
49 continue; | |
50 | |
51 Origin origin_with_removed_subdomain = | |
52 Origin::UnsafelyCreateOriginWithoutNormalization( | |
53 url.scheme(), | |
54 host.substr(i), | |
55 url.EffectiveIntPort()); | |
56 | |
57 // If we recognize the origin, return true for whitelist and false | |
58 // for blacklist. | |
59 if (origin_list_.find(Origin(origin_with_removed_subdomain)) | |
60 != origin_list_.end()) { | |
61 return whitelist_mode_; | |
62 } | |
63 } | |
64 | |
65 // We do not recognize the URL. Return false for whitelist mode and true | |
66 // for blacklist mode. | |
67 return !whitelist_mode_; | |
68 } | |
69 | |
70 bool OriginFilter::operator==(const OriginFilter& other) const { | |
71 // Two filters can only be equal if they are both whitelists or both | |
72 // blacklists, otherwise one of them is finite and the other infinite. | |
73 if (whitelist_mode_ != other.whitelist_mode_) | |
74 return false; | |
75 | |
76 // Since unique origins are excluded, the filters are equivalent if they | |
77 // contain the same set of origins. | |
78 if (origin_list_.size() != other.origin_list_.size()) | |
79 return false; | |
80 | |
81 std::set<Origin>::const_iterator their_origin = | |
82 other.origin_list_.begin(); | |
83 for (std::set<Origin>::const_iterator our_origin = origin_list_.begin(); | |
brettw
2016/02/01 22:48:25
for (const Origin& : origin_list) ...
msramek
2016/02/05 17:16:35
I removed operator==, as it's not needed anymore.
| |
84 our_origin != origin_list_.end(); ++our_origin, ++their_origin) { | |
85 if (!our_origin->IsSameOriginWith(*their_origin)) | |
86 return false; | |
87 } | |
88 DCHECK(their_origin == other.origin_list_.end()); | |
89 | |
90 return true; | |
91 } | |
92 | |
93 bool OriginFilter::operator!=(const OriginFilter& other) const { | |
94 return !(this->operator==(other)); | |
95 } | |
96 | |
97 OriginFilter::OriginFilter(const std::vector<Origin>& origins, | |
98 bool whitelist_mode) | |
99 : whitelist_mode_(whitelist_mode) { | |
100 for (const Origin& origin : origins) { | |
101 // By limiting the filter to non-unique origins, we can guarantee that | |
102 // origin1 < origin2 && origin1 > origin2 <=> origin1.isSameOrigin(origin2). | |
103 // This means that std::set::find() will use the same semantics for | |
104 // origin comparison as Origin::IsSameOriginWith(). Furthermore, this | |
105 // means that two filters are equal iff they are equal element-wise. | |
106 if (origin.unique()) { | |
107 LOG(WARNING) << "Invalid origin passed into OriginFilter."; | |
108 continue; | |
109 } | |
110 | |
111 // TODO(msramek): All urls with file scheme currently map to the same | |
112 // origin. This is currently not a problem, but if it becomes one, | |
113 // consider recognizing the URL path. | |
114 | |
115 origin_list_.insert(origin); | |
116 } | |
117 } | |
118 | |
119 std::ostream& operator<<(std::ostream& out, const OriginFilter& filter) { | |
120 out << "OriginFilter(" | |
121 << (filter.whitelist_mode_ ? "whitelist" : "blacklist") | |
122 << " = { "; | |
123 for (const Origin& origin : filter.origin_list_) { | |
124 out << origin << ", "; | |
125 } | |
126 out << "})"; | |
127 return out; | |
128 } | |
129 | |
130 } // namespace url | |
OLD | NEW |