| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/common/content_settings_pattern_parser.h" | 5 #include "chrome/common/content_settings_pattern_parser.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "chrome/common/url_constants.h" | 8 #include "url/url_constants.h" |
| 9 #include "extensions/common/constants.h" | |
| 10 #include "net/base/net_util.h" | |
| 11 #include "url/gurl.h" | |
| 12 #include "url/url_canon.h" | |
| 13 | 9 |
| 14 namespace { | 10 namespace { |
| 15 | 11 |
| 12 const char* kDomainWildcard = "[*.]"; |
| 13 const size_t kDomainWildcardLength = 4; |
| 14 const char* kHostWildcard = "*"; |
| 15 const char* kPathWildcard = "*"; |
| 16 const char* kPortWildcard = "*"; |
| 17 const char* kSchemeWildcard = "*"; |
| 16 const char* kUrlPathSeparator = "/"; | 18 const char* kUrlPathSeparator = "/"; |
| 17 const char* kUrlPortSeparator = ":"; | 19 const char* kUrlPortSeparator = ":"; |
| 18 | 20 |
| 19 class Component { | 21 class Component { |
| 20 public: | 22 public: |
| 21 Component() : start(0), len(0) {} | 23 Component() : start(0), len(0) {} |
| 22 Component(size_t s, size_t l) : start(s), len(l) {} | 24 Component(size_t s, size_t l) : start(s), len(l) {} |
| 23 | 25 |
| 24 bool IsNonEmpty() { | 26 bool IsNonEmpty() { |
| 25 return len > 0; | 27 return len > 0; |
| 26 } | 28 } |
| 27 | 29 |
| 28 size_t start; | 30 size_t start; |
| 29 size_t len; | 31 size_t len; |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 } // namespace | 34 } // namespace |
| 33 | 35 |
| 34 namespace content_settings { | 36 namespace content_settings { |
| 35 | 37 |
| 36 const char* PatternParser::kDomainWildcard = "[*.]"; | 38 void PatternParser::Parse(const char* extension_scheme, |
| 37 | 39 const std::string& pattern_spec, |
| 38 const size_t PatternParser::kDomainWildcardLength = 4; | |
| 39 | |
| 40 const char* PatternParser::kSchemeWildcard = "*"; | |
| 41 | |
| 42 const char* PatternParser::kHostWildcard = "*"; | |
| 43 | |
| 44 const char* PatternParser::kPortWildcard = "*"; | |
| 45 | |
| 46 const char* PatternParser::kPathWildcard = "*"; | |
| 47 | |
| 48 // static | |
| 49 void PatternParser::Parse(const std::string& pattern_spec, | |
| 50 ContentSettingsPattern::BuilderInterface* builder) { | 40 ContentSettingsPattern::BuilderInterface* builder) { |
| 51 if (pattern_spec == "*") { | 41 if (pattern_spec == "*") { |
| 52 builder->WithSchemeWildcard(); | 42 builder->WithSchemeWildcard(); |
| 53 builder->WithDomainWildcard(); | 43 builder->WithDomainWildcard(); |
| 54 builder->WithPortWildcard(); | 44 builder->WithPortWildcard(); |
| 55 return; | 45 return; |
| 56 } | 46 } |
| 57 | 47 |
| 58 // Initialize components for the individual patterns parts to empty | 48 // Initialize components for the individual patterns parts to empty |
| 59 // sub-strings. | 49 // sub-strings. |
| 60 Component scheme_component; | 50 Component scheme_component; |
| 61 Component host_component; | 51 Component host_component; |
| 62 Component port_component; | 52 Component port_component; |
| 63 Component path_component; | 53 Component path_component; |
| 64 | 54 |
| 65 size_t start = 0; | 55 size_t start = 0; |
| 66 size_t current_pos = 0; | 56 size_t current_pos = 0; |
| 67 | 57 |
| 68 if (pattern_spec.empty()) | 58 if (pattern_spec.empty()) |
| 69 return; | 59 return; |
| 70 | 60 |
| 71 // Test if a scheme pattern is in the spec. | 61 // Test if a scheme pattern is in the spec. |
| 72 current_pos = pattern_spec.find( | 62 const std::string standard_scheme_separator(url::kStandardSchemeSeparator); |
| 73 std::string(url::kStandardSchemeSeparator), start); | 63 current_pos = pattern_spec.find(standard_scheme_separator, start); |
| 74 if (current_pos != std::string::npos) { | 64 if (current_pos != std::string::npos) { |
| 75 scheme_component = Component(start, current_pos); | 65 scheme_component = Component(start, current_pos); |
| 76 start = current_pos + strlen(url::kStandardSchemeSeparator); | 66 start = current_pos + standard_scheme_separator.size(); |
| 77 current_pos = start; | 67 current_pos = start; |
| 78 } else { | 68 } else { |
| 79 current_pos = start; | 69 current_pos = start; |
| 80 } | 70 } |
| 81 | 71 |
| 82 if (start >= pattern_spec.size()) | 72 if (start >= pattern_spec.size()) |
| 83 return; // Bad pattern spec. | 73 return; // Bad pattern spec. |
| 84 | 74 |
| 85 // Jump to the end of domain wildcards or an IPv6 addresses. IPv6 addresses | 75 // Jump to the end of domain wildcards or an IPv6 addresses. IPv6 addresses |
| 86 // contain ':'. So first move to the end of an IPv6 address befor searching | 76 // contain ':'. So first move to the end of an IPv6 address befor searching |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 for (size_t i = 0; i < port.size(); ++i) { | 155 for (size_t i = 0; i < port.size(); ++i) { |
| 166 if (!IsAsciiDigit(port[i])) { | 156 if (!IsAsciiDigit(port[i])) { |
| 167 builder->Invalid(); | 157 builder->Invalid(); |
| 168 return; | 158 return; |
| 169 } | 159 } |
| 170 } | 160 } |
| 171 // TODO(markusheintz): Check port range. | 161 // TODO(markusheintz): Check port range. |
| 172 builder->WithPort(port); | 162 builder->WithPort(port); |
| 173 } | 163 } |
| 174 } else { | 164 } else { |
| 175 if (scheme != std::string(extensions::kExtensionScheme) && | 165 if (scheme != extension_scheme && |
| 176 scheme != std::string(url::kFileScheme)) | 166 scheme != url::kFileScheme) |
| 177 builder->WithPortWildcard(); | 167 builder->WithPortWildcard(); |
| 178 } | 168 } |
| 179 | 169 |
| 180 if (path_component.IsNonEmpty()) { | 170 if (path_component.IsNonEmpty()) { |
| 181 const std::string path = pattern_spec.substr(path_component.start, | 171 const std::string path = pattern_spec.substr(path_component.start, |
| 182 path_component.len); | 172 path_component.len); |
| 183 if (path.substr(1) == kPathWildcard) | 173 if (path.substr(1) == kPathWildcard) |
| 184 builder->WithPathWildcard(); | 174 builder->WithPathWildcard(); |
| 185 else | 175 else |
| 186 builder->WithPath(path); | 176 builder->WithPath(path); |
| 187 } | 177 } |
| 188 } | 178 } |
| 189 | 179 |
| 190 // static | |
| 191 std::string PatternParser::ToString( | 180 std::string PatternParser::ToString( |
| 181 const char* extension_scheme, |
| 192 const ContentSettingsPattern::PatternParts& parts) { | 182 const ContentSettingsPattern::PatternParts& parts) { |
| 193 // Return the most compact form to support legacy code and legacy pattern | 183 // Return the most compact form to support legacy code and legacy pattern |
| 194 // strings. | 184 // strings. |
| 195 if (parts.is_scheme_wildcard && | 185 if (parts.is_scheme_wildcard && |
| 196 parts.has_domain_wildcard && | 186 parts.has_domain_wildcard && |
| 197 parts.host.empty() && | 187 parts.host.empty() && |
| 198 parts.is_port_wildcard) | 188 parts.is_port_wildcard) |
| 199 return "*"; | 189 return "*"; |
| 200 | 190 |
| 201 std::string str; | 191 std::string str; |
| 202 if (!parts.is_scheme_wildcard) | 192 if (!parts.is_scheme_wildcard) |
| 203 str += parts.scheme + url::kStandardSchemeSeparator; | 193 str += parts.scheme + url::kStandardSchemeSeparator; |
| 204 | 194 |
| 205 if (parts.scheme == url::kFileScheme) { | 195 if (parts.scheme == url::kFileScheme) { |
| 206 if (parts.is_path_wildcard) | 196 if (parts.is_path_wildcard) |
| 207 return str + kUrlPathSeparator + kPathWildcard; | 197 return str + kUrlPathSeparator + kPathWildcard; |
| 208 else | 198 else |
| 209 return str + parts.path; | 199 return str + parts.path; |
| 210 } | 200 } |
| 211 | 201 |
| 212 if (parts.has_domain_wildcard) { | 202 if (parts.has_domain_wildcard) { |
| 213 if (parts.host.empty()) | 203 if (parts.host.empty()) |
| 214 str += kHostWildcard; | 204 str += kHostWildcard; |
| 215 else | 205 else |
| 216 str += kDomainWildcard; | 206 str += kDomainWildcard; |
| 217 } | 207 } |
| 218 str += parts.host; | 208 str += parts.host; |
| 219 | 209 |
| 220 if (parts.scheme == std::string(extensions::kExtensionScheme)) { | 210 if (parts.scheme == extension_scheme) { |
| 221 str += parts.path.empty() ? std::string(kUrlPathSeparator) : parts.path; | 211 str += parts.path.empty() ? std::string(kUrlPathSeparator) : parts.path; |
| 222 return str; | 212 return str; |
| 223 } | 213 } |
| 224 | 214 |
| 225 if (!parts.is_port_wildcard) { | 215 if (!parts.is_port_wildcard) { |
| 226 str += std::string(kUrlPortSeparator) + parts.port; | 216 str += std::string(kUrlPortSeparator) + parts.port; |
| 227 } | 217 } |
| 228 | 218 |
| 229 return str; | 219 return str; |
| 230 } | 220 } |
| 231 | 221 |
| 232 } // namespace content_settings | 222 } // namespace content_settings |
| OLD | NEW |