This source file includes following definitions.
- len
- len
- IsNonEmpty
- ToString
#include "chrome/common/content_settings_pattern_parser.h"
#include "base/strings/string_util.h"
#include "chrome/common/url_constants.h"
#include "extensions/common/constants.h"
#include "net/base/net_util.h"
#include "url/gurl.h"
#include "url/url_canon.h"
namespace {
const char* kUrlPathSeparator = "/";
const char* kUrlPortSeparator = ":";
class Component {
public:
Component() : start(0), len(0) {}
Component(size_t s, size_t l) : start(s), len(l) {}
bool IsNonEmpty() {
return len > 0;
}
size_t start;
size_t len;
};
}
namespace content_settings {
const char* PatternParser::kDomainWildcard = "[*.]";
const size_t PatternParser::kDomainWildcardLength = 4;
const char* PatternParser::kSchemeWildcard = "*";
const char* PatternParser::kHostWildcard = "*";
const char* PatternParser::kPortWildcard = "*";
const char* PatternParser::kPathWildcard = "*";
void PatternParser::Parse(const std::string& pattern_spec,
ContentSettingsPattern::BuilderInterface* builder) {
if (pattern_spec == "*") {
builder->WithSchemeWildcard();
builder->WithDomainWildcard();
builder->WithPortWildcard();
return;
}
Component scheme_component;
Component host_component;
Component port_component;
Component path_component;
size_t start = 0;
size_t current_pos = 0;
if (pattern_spec.empty())
return;
current_pos = pattern_spec.find(
std::string(content::kStandardSchemeSeparator), start);
if (current_pos != std::string::npos) {
scheme_component = Component(start, current_pos);
start = current_pos + strlen(content::kStandardSchemeSeparator);
current_pos = start;
} else {
current_pos = start;
}
if (start >= pattern_spec.size())
return;
if (pattern_spec[current_pos] == '[')
current_pos = pattern_spec.find("]", start);
if (current_pos == std::string::npos)
return;
current_pos = pattern_spec.find(std::string(kUrlPortSeparator), current_pos);
if (current_pos == std::string::npos) {
current_pos = pattern_spec.find(std::string(kUrlPathSeparator), start);
if (current_pos == std::string::npos) {
current_pos = pattern_spec.size();
host_component = Component(start, current_pos - start);
} else {
host_component = Component(start, current_pos - start);
}
start = current_pos;
} else {
host_component = Component(start, current_pos - start);
start = current_pos + 1;
if (start < pattern_spec.size()) {
current_pos = pattern_spec.find(std::string(kUrlPathSeparator), start);
if (current_pos == std::string::npos) {
current_pos = pattern_spec.size();
}
port_component = Component(start, current_pos - start);
start = current_pos;
}
}
current_pos = pattern_spec.size();
if (start < current_pos) {
path_component = Component(start, current_pos - start);
}
std::string scheme;
if (scheme_component.IsNonEmpty()) {
scheme = pattern_spec.substr(scheme_component.start, scheme_component.len);
if (scheme == kSchemeWildcard) {
builder->WithSchemeWildcard();
} else {
builder->WithScheme(scheme);
}
} else {
builder->WithSchemeWildcard();
}
if (host_component.IsNonEmpty()) {
std::string host = pattern_spec.substr(host_component.start,
host_component.len);
if (host == kHostWildcard) {
builder->WithDomainWildcard();
} else if (StartsWithASCII(host, kDomainWildcard, true)) {
host = host.substr(kDomainWildcardLength);
builder->WithDomainWildcard();
builder->WithHost(host);
} else {
if (host.find(kHostWildcard) != std::string::npos) {
builder->Invalid();
return;
}
builder->WithHost(host);
}
}
if (port_component.IsNonEmpty()) {
const std::string port = pattern_spec.substr(port_component.start,
port_component.len);
if (port == kPortWildcard) {
builder->WithPortWildcard();
} else {
for (size_t i = 0; i < port.size(); ++i) {
if (!IsAsciiDigit(port[i])) {
builder->Invalid();
return;
}
}
builder->WithPort(port);
}
} else {
if (scheme != std::string(extensions::kExtensionScheme) &&
scheme != std::string(content::kFileScheme))
builder->WithPortWildcard();
}
if (path_component.IsNonEmpty()) {
const std::string path = pattern_spec.substr(path_component.start,
path_component.len);
if (path.substr(1) == kPathWildcard)
builder->WithPathWildcard();
else
builder->WithPath(path);
}
}
std::string PatternParser::ToString(
const ContentSettingsPattern::PatternParts& parts) {
if (parts.is_scheme_wildcard &&
parts.has_domain_wildcard &&
parts.host.empty() &&
parts.is_port_wildcard)
return "*";
std::string str;
if (!parts.is_scheme_wildcard)
str += parts.scheme + content::kStandardSchemeSeparator;
if (parts.scheme == content::kFileScheme) {
if (parts.is_path_wildcard)
return str + kUrlPathSeparator + kPathWildcard;
else
return str + parts.path;
}
if (parts.has_domain_wildcard) {
if (parts.host.empty())
str += kHostWildcard;
else
str += kDomainWildcard;
}
str += parts.host;
if (parts.scheme == std::string(extensions::kExtensionScheme)) {
str += parts.path.empty() ? std::string(kUrlPathSeparator) : parts.path;
return str;
}
if (!parts.is_port_wildcard) {
str += std::string(kUrlPortSeparator) + parts.port;
}
return str;
}
}