Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / api / content_settings / content_settings_helpers.cc
blob07e9717a315fcfabfa6845e303393fda25889bdc
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"
13 namespace {
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[] = {
21 "cookies",
22 "images",
23 "javascript",
24 "plugins",
25 "popups",
26 "location",
27 "notifications",
28 "auto-select-certificate",
29 "fullscreen",
30 "mouselock",
31 "mixed-script",
32 "media-stream",
33 "media-stream-mic",
34 "media-stream-camera",
35 "register-protocol-handler",
36 "ppapi-broker",
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[] = {
47 "default",
48 "allow",
49 "block",
50 "ask",
51 "session_only",
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)
61 return "80";
62 if (scheme == url::kHttpsScheme)
63 return "443";
64 NOTREACHED();
65 return std::string();
68 } // namespace
70 namespace extensions {
71 namespace content_settings_helpers {
73 ContentSettingsPattern ParseExtensionPattern(const std::string& pattern_str,
74 std::string* error) {
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();
83 } else {
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();
91 if (scheme == "*")
92 builder->WithSchemeWildcard();
93 else
94 builder->WithScheme(scheme);
96 std::string port = url_pattern.port();
97 if (port.empty() && scheme != "file") {
98 if (scheme == "*")
99 port = "*";
100 else
101 port = GetDefaultPort(scheme);
103 if (port == "*")
104 builder->WithPortWildcard();
105 else
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();
114 } else {
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;
126 return pattern;
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);
150 return true;
153 return false;
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