OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "extensions/common/api/declarative_net_request/indexed_rule.h" |
| 6 |
| 7 #include <set> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "extensions/common/api/declarative_net_request/constants.h" |
| 13 |
| 14 namespace extensions { |
| 15 namespace declarative_net_request { |
| 16 |
| 17 namespace { |
| 18 |
| 19 class UrlFilterParser { |
| 20 public: |
| 21 UrlFilterParser(std::string url_filter, IndexedRule* indexed_rule) |
| 22 : url_filter_(std::move(url_filter)), |
| 23 url_filter_len_(url_filter_.length()), |
| 24 index_(0), |
| 25 result_(ParseResult::SUCCESS), |
| 26 indexed_rule_(indexed_rule) { |
| 27 DCHECK_NE(0u, url_filter_len_); |
| 28 } |
| 29 |
| 30 ParseResult Parse() { |
| 31 DCHECK_EQ(0u, index_); |
| 32 DCHECK_EQ(ParseResult::SUCCESS, result_); |
| 33 |
| 34 ParseLeftAnchor(); |
| 35 DCHECK(result_ != ParseResult::SUCCESS || index_ <= 2u); |
| 36 |
| 37 ParseFilterString(); |
| 38 DCHECK(result_ != ParseResult::SUCCESS || index_ == url_filter_len_ || |
| 39 index_ + 1 == url_filter_len_); |
| 40 |
| 41 ParseRightAnchor(); |
| 42 DCHECK(result_ != ParseResult::SUCCESS || index_ == url_filter_len_); |
| 43 |
| 44 return result_; |
| 45 } |
| 46 |
| 47 void ParseLeftAnchor() { |
| 48 DCHECK_EQ(0u, index_); |
| 49 DCHECK_EQ(ParseResult::SUCCESS, result_); |
| 50 |
| 51 indexed_rule_->anchor_left = AnchorType::AnchorType_NONE; |
| 52 |
| 53 if (IsAtAnchor()) { |
| 54 index_++; |
| 55 indexed_rule_->anchor_left = AnchorType::AnchorType_BOUNDARY; |
| 56 if (IsAtAnchor()) { |
| 57 index_++; |
| 58 indexed_rule_->anchor_left = AnchorType::AnchorType_SUBDOMAIN; |
| 59 } |
| 60 } |
| 61 } |
| 62 |
| 63 void ParseFilterString() { |
| 64 if (result_ != ParseResult::SUCCESS) |
| 65 return; |
| 66 |
| 67 indexed_rule_->url_pattern_type = UrlPatternType::UrlPatternType_SUBSTRING; |
| 68 int left_index = index_; |
| 69 while (index_ < url_filter_len_) { |
| 70 if (IsAtRightAnchor()) |
| 71 break; |
| 72 if (IsAtSeparatorOrWildcard()) |
| 73 indexed_rule_->url_pattern_type = |
| 74 UrlPatternType::UrlPatternType_WILDCARDED; |
| 75 index_++; |
| 76 } |
| 77 // Note empty url patterns need to be supported. |
| 78 indexed_rule_->url_pattern = |
| 79 url_filter_.substr(left_index, index_ - left_index); |
| 80 } |
| 81 |
| 82 void ParseRightAnchor() { |
| 83 if (result_ != ParseResult::SUCCESS) |
| 84 return; |
| 85 indexed_rule_->anchor_right = AnchorType::AnchorType_NONE; |
| 86 if (IsAtRightAnchor()) { |
| 87 index_++; |
| 88 indexed_rule_->anchor_right = AnchorType::AnchorType_BOUNDARY; |
| 89 } |
| 90 } |
| 91 |
| 92 private: |
| 93 bool IsAtSeparatorOrWildcard() const { |
| 94 return IsAtValidIndex() && (url_filter_[index_] == kSeparatorCharacter || |
| 95 url_filter_[index_] == kWildcardCharacter); |
| 96 } |
| 97 |
| 98 bool IsAtRightAnchor() const { |
| 99 return IsAtAnchor() && index_ > 0 && index_ + 1 == url_filter_len_; |
| 100 } |
| 101 |
| 102 bool IsAtValidIndex() const { return index_ < url_filter_len_; } |
| 103 |
| 104 bool IsAtAnchor() const { |
| 105 return IsAtValidIndex() && url_filter_[index_] == kAnchorCharacter; |
| 106 } |
| 107 |
| 108 static constexpr char kAnchorCharacter = '|'; |
| 109 static constexpr char kSeparatorCharacter = '^'; |
| 110 static constexpr char kWildcardCharacter = '*'; |
| 111 |
| 112 const std::string url_filter_; |
| 113 const size_t url_filter_len_; |
| 114 size_t index_; |
| 115 ParseResult result_; |
| 116 IndexedRule* indexed_rule_; // Weak. |
| 117 }; |
| 118 |
| 119 uint8_t GetOptionsMask(const Rule& parsed_rule) { |
| 120 uint8_t mask = OptionFlag::OptionFlag_NONE; |
| 121 if (parsed_rule.action.type == |
| 122 api::declarative_net_request::RULE_ACTION_TYPE_WHITELIST) |
| 123 mask |= OptionFlag::OptionFlag_IS_WHITELIST; |
| 124 if (parsed_rule.condition.url_filter_is_case_sensitive && |
| 125 *parsed_rule.condition.url_filter_is_case_sensitive) |
| 126 mask |= OptionFlag::OptionFlag_IS_MATCH_CASE; |
| 127 |
| 128 switch (parsed_rule.condition.domain_type) { |
| 129 case api::declarative_net_request::DOMAIN_TYPE_FIRST_PARTY: |
| 130 mask |= OptionFlag::OptionFlag_APPLIES_TO_FIRST_PARTY; |
| 131 break; |
| 132 case api::declarative_net_request::DOMAIN_TYPE_THIRD_PARTY: |
| 133 mask |= OptionFlag::OptionFlag_APPLIES_TO_THIRD_PARTY; |
| 134 break; |
| 135 case api::declarative_net_request::DOMAIN_TYPE_ANY: |
| 136 case api::declarative_net_request::DOMAIN_TYPE_NONE: |
| 137 mask |= (OptionFlag::OptionFlag_APPLIES_TO_FIRST_PARTY | |
| 138 OptionFlag::OptionFlag_APPLIES_TO_THIRD_PARTY); |
| 139 break; |
| 140 } |
| 141 return mask; |
| 142 } |
| 143 |
| 144 uint8_t GetActivationTypes(const Rule& parsed_rule) { |
| 145 // Extensions don't use any activation types. |
| 146 return ActivationType::ActivationType_NONE; |
| 147 } |
| 148 |
| 149 ElementType GetElementType( |
| 150 api::declarative_net_request::ResourceType resource_type) { |
| 151 switch (resource_type) { |
| 152 case api::declarative_net_request::RESOURCE_TYPE_NONE: |
| 153 return ElementType::ElementType_NONE; |
| 154 case api::declarative_net_request::RESOURCE_TYPE_SUB_FRAME: |
| 155 return ElementType::ElementType_SUBDOCUMENT; |
| 156 case api::declarative_net_request::RESOURCE_TYPE_STYLESHEET: |
| 157 return ElementType::ElementType_STYLESHEET; |
| 158 case api::declarative_net_request::RESOURCE_TYPE_SCRIPT: |
| 159 return ElementType::ElementType_SCRIPT; |
| 160 case api::declarative_net_request::RESOURCE_TYPE_IMAGE: |
| 161 return ElementType::ElementType_IMAGE; |
| 162 case api::declarative_net_request::RESOURCE_TYPE_FONT: |
| 163 return ElementType::ElementType_FONT; |
| 164 case api::declarative_net_request::RESOURCE_TYPE_OBJECT: |
| 165 return ElementType::ElementType_OBJECT; |
| 166 case api::declarative_net_request::RESOURCE_TYPE_XMLHTTPREQUEST: |
| 167 return ElementType::ElementType_XMLHTTPREQUEST; |
| 168 case api::declarative_net_request::RESOURCE_TYPE_PING: |
| 169 return ElementType::ElementType_PING; |
| 170 case api::declarative_net_request::RESOURCE_TYPE_MEDIA: |
| 171 return ElementType::ElementType_MEDIA; |
| 172 case api::declarative_net_request::RESOURCE_TYPE_WEBSOCKET: |
| 173 return ElementType::ElementType_WEBSOCKET; |
| 174 case api::declarative_net_request::RESOURCE_TYPE_OTHER: |
| 175 return ElementType::ElementType_OTHER; |
| 176 } |
| 177 NOTREACHED(); |
| 178 return ElementType::ElementType_NONE; |
| 179 } |
| 180 |
| 181 uint16_t GetResourceTypesMask( |
| 182 const std::vector<api::declarative_net_request::ResourceType>* |
| 183 resource_types) { |
| 184 if (!resource_types) |
| 185 return ElementType::ElementType_NONE; |
| 186 |
| 187 uint16_t mask = ElementType::ElementType_NONE; |
| 188 for (const auto resource_type : *resource_types) |
| 189 mask |= GetElementType(resource_type); |
| 190 DCHECK(!(mask & (ElementType::ElementType_OBJECT_SUBREQUEST | |
| 191 ElementType::ElementType_POPUP))); |
| 192 return mask; |
| 193 } |
| 194 |
| 195 ParseResult ComputeElementTypes(const Rule& rule, uint16_t* element_types) { |
| 196 uint16_t include_element_type_mask = |
| 197 GetResourceTypesMask(rule.condition.resource_types.get()); |
| 198 uint16_t exclude_element_type_mask = |
| 199 GetResourceTypesMask(rule.condition.exclude_resource_types.get()); |
| 200 exclude_element_type_mask |= (ElementType::ElementType_POPUP | |
| 201 ElementType::ElementType_OBJECT_SUBREQUEST); |
| 202 if (include_element_type_mask & exclude_element_type_mask) |
| 203 return ParseResult::ERROR_RESOURCE_TYPE_DUPLICATED; |
| 204 *element_types = (include_element_type_mask ? include_element_type_mask |
| 205 : ElementType::ElementType_ANY) & |
| 206 (~exclude_element_type_mask); |
| 207 return ParseResult::SUCCESS; |
| 208 } |
| 209 |
| 210 std::vector<std::string> GetSortedAndUniqueDomains( |
| 211 std::unique_ptr<std::vector<std::string>> domains) { |
| 212 std::vector<std::string> result; |
| 213 if (!domains) |
| 214 return result; |
| 215 // TODO is std::move necessary already rvalue. |
| 216 result = std::move(*domains.release()); |
| 217 std::sort(result.begin(), result.end()); |
| 218 result.erase(std::unique(result.begin(), result.end()), result.end()); |
| 219 return result; |
| 220 } |
| 221 |
| 222 } // namespace |
| 223 |
| 224 IndexedRule::IndexedRule() = default; |
| 225 IndexedRule::~IndexedRule() = default; |
| 226 |
| 227 ParseResult CreateIndexedRule(std::unique_ptr<Rule> parsed_rule, |
| 228 IndexedRule* indexed_rule) { |
| 229 DCHECK(indexed_rule); |
| 230 |
| 231 if (parsed_rule->id < kMinValidID) |
| 232 return ParseResult::ERROR_INVALID_RULE_ID; |
| 233 |
| 234 const bool is_redirect_rule = |
| 235 parsed_rule->action.type == |
| 236 api::declarative_net_request::RULE_ACTION_TYPE_REDIRECT; |
| 237 if (is_redirect_rule && (!parsed_rule->action.redirect_url || |
| 238 parsed_rule->action.redirect_url->empty())) |
| 239 return ParseResult::ERROR_EMPTY_REDIRECT_URL; |
| 240 if (is_redirect_rule && !parsed_rule->priority) |
| 241 return ParseResult::ERROR_EMPTY_REDIRECT_RULE_PRIORITY; |
| 242 if (is_redirect_rule && *parsed_rule->priority < kMinValidPriority) |
| 243 return ParseResult::ERROR_INVALID_REDIRECT_RULE_PRIORITY; |
| 244 |
| 245 indexed_rule->id = base::checked_cast<uint32_t>(parsed_rule->id); |
| 246 indexed_rule->priority = base::checked_cast<uint32_t>( |
| 247 parsed_rule->priority ? *parsed_rule->priority : kDefaultPriority); |
| 248 indexed_rule->options = GetOptionsMask(*parsed_rule); |
| 249 indexed_rule->activation_types = GetActivationTypes(*parsed_rule); |
| 250 |
| 251 // TODO is std::move needed here, already an rvalue. |
| 252 |
| 253 indexed_rule->domains_included = |
| 254 GetSortedAndUniqueDomains(std::move(parsed_rule->condition.domains)); |
| 255 indexed_rule->domains_excluded = GetSortedAndUniqueDomains( |
| 256 std::move(parsed_rule->condition.excluded_domains)); |
| 257 |
| 258 ParseResult result = |
| 259 ComputeElementTypes(*parsed_rule, &indexed_rule->element_types); |
| 260 if (result != ParseResult::SUCCESS) |
| 261 return result; |
| 262 |
| 263 if (parsed_rule->condition.url_filter && |
| 264 !parsed_rule->condition.url_filter->empty()) { |
| 265 UrlFilterParser parser( |
| 266 std::move(*parsed_rule->condition.url_filter.release()), indexed_rule); |
| 267 result = parser.Parse(); |
| 268 if (result != ParseResult::SUCCESS) |
| 269 return result; |
| 270 } |
| 271 |
| 272 return ParseResult::SUCCESS; |
| 273 } |
| 274 |
| 275 } // namespace extensions |
| 276 } // namespace declarative_net_request |
OLD | NEW |