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 "components/content_settings/core/browser/website_settings_info.h"
11 #include "components/content_settings/core/browser/website_settings_registry.h"
12 #include "content/public/common/url_constants.h"
13 #include "extensions/common/url_pattern.h"
17 const char kNoPathWildcardsError
[] =
18 "Path wildcards in file URL patterns are not allowed.";
19 const char kNoPathsError
[] = "Specific paths are not allowed.";
20 const char kInvalidPatternError
[] = "The pattern \"*\" is invalid.";
22 const char* const kContentSettingNames
[] = {
28 "detect_important_content"
30 static_assert(arraysize(kContentSettingNames
) <=
31 CONTENT_SETTING_NUM_SETTINGS
,
32 "kContentSettingNames has an unexpected number of elements");
34 // TODO(bauerb): Move this someplace where it can be reused.
35 std::string
GetDefaultPort(const std::string
& scheme
) {
36 if (scheme
== url::kHttpScheme
)
38 if (scheme
== url::kHttpsScheme
)
46 namespace extensions
{
47 namespace content_settings_helpers
{
49 ContentSettingsPattern
ParseExtensionPattern(const std::string
& pattern_str
,
51 const int kAllowedSchemes
=
52 URLPattern::SCHEME_HTTP
| URLPattern::SCHEME_HTTPS
|
53 URLPattern::SCHEME_FILE
;
54 URLPattern
url_pattern(kAllowedSchemes
);
55 URLPattern::ParseResult result
= url_pattern
.Parse(pattern_str
);
56 if (result
!= URLPattern::PARSE_SUCCESS
) {
57 *error
= URLPattern::GetParseResultString(result
);
58 return ContentSettingsPattern();
60 scoped_ptr
<ContentSettingsPattern::BuilderInterface
> builder(
61 ContentSettingsPattern::CreateBuilder(false));
62 builder
->WithHost(url_pattern
.host());
63 if (url_pattern
.match_subdomains())
64 builder
->WithDomainWildcard();
66 std::string scheme
= url_pattern
.scheme();
68 builder
->WithSchemeWildcard();
70 builder
->WithScheme(scheme
);
72 std::string port
= url_pattern
.port();
73 if (port
.empty() && scheme
!= "file") {
77 port
= GetDefaultPort(scheme
);
80 builder
->WithPortWildcard();
82 builder
->WithPort(port
);
84 std::string path
= url_pattern
.path();
85 if (scheme
== "file") {
86 // For file URLs we allow only exact path matches.
87 if (path
.find_first_of("*?") != std::string::npos
) {
88 *error
= kNoPathWildcardsError
;
89 return ContentSettingsPattern();
91 builder
->WithPath(path
);
93 } else if (path
!= "/*") {
94 // For other URLs we allow only paths which match everything.
95 *error
= kNoPathsError
;
96 return ContentSettingsPattern();
99 ContentSettingsPattern pattern
= builder
->Build();
100 if (!pattern
.IsValid())
101 *error
= kInvalidPatternError
;
106 ContentSettingsType
StringToContentSettingsType(
107 const std::string
& content_type
) {
108 const content_settings::WebsiteSettingsInfo
* info
=
109 content_settings::WebsiteSettingsRegistry::GetInstance()->GetByName(
114 return CONTENT_SETTINGS_TYPE_DEFAULT
;
117 std::string
ContentSettingsTypeToString(ContentSettingsType type
) {
118 return content_settings::WebsiteSettingsRegistry::GetInstance()
123 bool StringToContentSetting(const std::string
& setting_str
,
124 ContentSetting
* setting
) {
125 for (size_t type
= 0; type
< arraysize(kContentSettingNames
); ++type
) {
126 if (setting_str
== kContentSettingNames
[type
]) {
127 *setting
= static_cast<ContentSetting
>(type
);
134 const char* ContentSettingToString(ContentSetting setting
) {
135 size_t index
= static_cast<size_t>(setting
);
136 DCHECK_LT(index
, arraysize(kContentSettingNames
));
137 return kContentSettingNames
[index
];
140 } // namespace content_settings_helpers
141 } // namespace extensions