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
[] = {
28 "auto-select-certificate",
34 "media-stream-camera",
35 "register-protocol-handler",
37 "multiple-automatic-downloads"
40 // TODO(msramek): Assert that |kContentSettingsTypeNames| is synced with
41 // enum |ContentSettingsType|.
42 static_assert(arraysize(kContentSettingsTypeNames
) <=
43 CONTENT_SETTINGS_NUM_TYPES
,
44 "kContentSettingsTypeNames has an unexpected number of elements");
46 const char* const kContentSettingNames
[] = {
52 "detect_important_content"
54 static_assert(arraysize(kContentSettingNames
) <=
55 CONTENT_SETTING_NUM_SETTINGS
,
56 "kContentSettingNames has an unexpected number of elements");
58 // TODO(bauerb): Move this someplace where it can be reused.
59 std::string
GetDefaultPort(const std::string
& scheme
) {
60 if (scheme
== url::kHttpScheme
)
62 if (scheme
== url::kHttpsScheme
)
70 namespace extensions
{
71 namespace content_settings_helpers
{
73 ContentSettingsPattern
ParseExtensionPattern(const std::string
& pattern_str
,
75 const int kAllowedSchemes
=
76 URLPattern::SCHEME_HTTP
| URLPattern::SCHEME_HTTPS
|
77 URLPattern::SCHEME_FILE
;
78 URLPattern
url_pattern(kAllowedSchemes
);
79 URLPattern::ParseResult result
= url_pattern
.Parse(pattern_str
);
80 if (result
!= URLPattern::PARSE_SUCCESS
) {
81 *error
= URLPattern::GetParseResultString(result
);
82 return ContentSettingsPattern();
84 scoped_ptr
<ContentSettingsPattern::BuilderInterface
> builder(
85 ContentSettingsPattern::CreateBuilder(false));
86 builder
->WithHost(url_pattern
.host());
87 if (url_pattern
.match_subdomains())
88 builder
->WithDomainWildcard();
90 std::string scheme
= url_pattern
.scheme();
92 builder
->WithSchemeWildcard();
94 builder
->WithScheme(scheme
);
96 std::string port
= url_pattern
.port();
97 if (port
.empty() && scheme
!= "file") {
101 port
= GetDefaultPort(scheme
);
104 builder
->WithPortWildcard();
106 builder
->WithPort(port
);
108 std::string path
= url_pattern
.path();
109 if (scheme
== "file") {
110 // For file URLs we allow only exact path matches.
111 if (path
.find_first_of("*?") != std::string::npos
) {
112 *error
= kNoPathWildcardsError
;
113 return ContentSettingsPattern();
115 builder
->WithPath(path
);
117 } else if (path
!= "/*") {
118 // For other URLs we allow only paths which match everything.
119 *error
= kNoPathsError
;
120 return ContentSettingsPattern();
123 ContentSettingsPattern pattern
= builder
->Build();
124 if (!pattern
.IsValid())
125 *error
= kInvalidPatternError
;
130 ContentSettingsType
StringToContentSettingsType(
131 const std::string
& content_type
) {
132 for (size_t type
= 0; type
< arraysize(kContentSettingsTypeNames
); ++type
) {
133 if (content_type
== kContentSettingsTypeNames
[type
])
134 return static_cast<ContentSettingsType
>(type
);
136 return CONTENT_SETTINGS_TYPE_DEFAULT
;
139 const char* ContentSettingsTypeToString(ContentSettingsType type
) {
140 size_t index
= static_cast<size_t>(type
);
141 DCHECK_LT(index
, arraysize(kContentSettingsTypeNames
));
142 return kContentSettingsTypeNames
[index
];
145 bool StringToContentSetting(const std::string
& setting_str
,
146 ContentSetting
* setting
) {
147 for (size_t type
= 0; type
< arraysize(kContentSettingNames
); ++type
) {
148 if (setting_str
== kContentSettingNames
[type
]) {
149 *setting
= static_cast<ContentSetting
>(type
);
156 const char* ContentSettingToString(ContentSetting setting
) {
157 size_t index
= static_cast<size_t>(setting
);
158 DCHECK_LT(index
, arraysize(kContentSettingNames
));
159 return kContentSettingNames
[index
];
162 } // namespace content_settings_helpers
163 } // namespace extensions