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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/content_settings/content_settings_helpers.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "content/public/common/url_constants.h"
11 #include "extensions/common/url_pattern.h"
15 const char kNoPathWildcardsError[] =
16 "Path wildcards in file URL patterns are not allowed.";
17 const char kNoPathsError[] = "Specific paths are not allowed.";
18 const char kInvalidPatternError[] = "The pattern \"*\" is invalid.";
20 const char* const kContentSettingsTypeNames[] = {
29 COMPILE_ASSERT(arraysize(kContentSettingsTypeNames) <=
30 CONTENT_SETTINGS_NUM_TYPES,
31 content_settings_type_names_size_invalid);
33 const char* const kContentSettingNames[] = {
40 COMPILE_ASSERT(arraysize(kContentSettingNames) <=
41 CONTENT_SETTING_NUM_SETTINGS,
42 content_setting_names_size_invalid);
44 // TODO(bauerb): Move this someplace where it can be reused.
45 std::string GetDefaultPort(const std::string& scheme) {
46 if (scheme == url::kHttpScheme)
48 if (scheme == url::kHttpsScheme)
56 namespace extensions {
57 namespace content_settings_helpers {
59 ContentSettingsPattern ParseExtensionPattern(const std::string& pattern_str,
61 const int kAllowedSchemes =
62 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS |
63 URLPattern::SCHEME_FILE;
64 URLPattern url_pattern(kAllowedSchemes);
65 URLPattern::ParseResult result = url_pattern.Parse(pattern_str);
66 if (result != URLPattern::PARSE_SUCCESS) {
67 *error = URLPattern::GetParseResultString(result);
68 return ContentSettingsPattern();
70 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder(
71 ContentSettingsPattern::CreateBuilder(false));
72 builder->WithHost(url_pattern.host());
73 if (url_pattern.match_subdomains())
74 builder->WithDomainWildcard();
76 std::string scheme = url_pattern.scheme();
78 builder->WithSchemeWildcard();
80 builder->WithScheme(scheme);
82 std::string port = url_pattern.port();
83 if (port.empty() && scheme != "file") {
87 port = GetDefaultPort(scheme);
90 builder->WithPortWildcard();
92 builder->WithPort(port);
94 std::string path = url_pattern.path();
95 if (scheme == "file") {
96 // For file URLs we allow only exact path matches.
97 if (path.find_first_of("*?") != std::string::npos) {
98 *error = kNoPathWildcardsError;
99 return ContentSettingsPattern();
101 builder->WithPath(path);
103 } else if (path != "/*") {
104 // For other URLs we allow only paths which match everything.
105 *error = kNoPathsError;
106 return ContentSettingsPattern();
109 ContentSettingsPattern pattern = builder->Build();
110 if (!pattern.IsValid())
111 *error = kInvalidPatternError;
116 ContentSettingsType StringToContentSettingsType(
117 const std::string& content_type) {
118 for (size_t type = 0; type < arraysize(kContentSettingsTypeNames); ++type) {
119 if (content_type == kContentSettingsTypeNames[type])
120 return static_cast<ContentSettingsType>(type);
122 return CONTENT_SETTINGS_TYPE_DEFAULT;
125 const char* ContentSettingsTypeToString(ContentSettingsType type) {
126 size_t index = static_cast<size_t>(type);
127 DCHECK_LT(index, arraysize(kContentSettingsTypeNames));
128 return kContentSettingsTypeNames[index];
131 bool StringToContentSetting(const std::string& setting_str,
132 ContentSetting* setting) {
133 for (size_t type = 0; type < arraysize(kContentSettingNames); ++type) {
134 if (setting_str == kContentSettingNames[type]) {
135 *setting = static_cast<ContentSetting>(type);
142 const char* ContentSettingToString(ContentSetting setting) {
143 size_t index = static_cast<size_t>(setting);
144 DCHECK_LT(index, arraysize(kContentSettingNames));
145 return kContentSettingNames[index];
148 } // namespace content_settings_helpers
149 } // namespace extensions