Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: chrome/browser/extensions/extension_content_settings_helpers.cc

Issue 10381097: Move content settings extension API to content_settings dir. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fixed conflict Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "chrome/browser/extensions/extension_content_settings_helpers.h"
6
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/common/extensions/url_pattern.h"
11 #include "content/public/common/url_constants.h"
12
13 namespace {
14
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.";
19
20 const char* const kContentSettingsTypeNames[] = {
21 "cookies",
22 "images",
23 "javascript",
24 "plugins",
25 "popups",
26 "location",
27 "notifications",
28 };
29 COMPILE_ASSERT(arraysize(kContentSettingsTypeNames) <=
30 CONTENT_SETTINGS_NUM_TYPES,
31 content_settings_type_names_size_invalid);
32
33 const char* const kContentSettingNames[] = {
34 "default",
35 "allow",
36 "block",
37 "ask",
38 "session_only",
39 };
40 COMPILE_ASSERT(arraysize(kContentSettingNames) <=
41 CONTENT_SETTING_NUM_SETTINGS,
42 content_setting_names_size_invalid);
43
44 // TODO(bauerb): Move this someplace where it can be reused.
45 std::string GetDefaultPort(const std::string& scheme) {
46 if (scheme == chrome::kHttpScheme)
47 return "80";
48 if (scheme == chrome::kHttpsScheme)
49 return "443";
50 NOTREACHED();
51 return "";
52 }
53
54 } // namespace
55
56 namespace extension_content_settings_helpers {
57
58 ContentSettingsPattern ParseExtensionPattern(const std::string& pattern_str,
59 std::string* error) {
60 const int kAllowedSchemes =
61 URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS |
62 URLPattern::SCHEME_FILE;
63 URLPattern url_pattern(kAllowedSchemes);
64 URLPattern::ParseResult result = url_pattern.Parse(pattern_str);
65 if (result != URLPattern::PARSE_SUCCESS) {
66 *error = URLPattern::GetParseResultString(result);
67 return ContentSettingsPattern();
68 } else {
69 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder(
70 ContentSettingsPattern::CreateBuilder(false));
71 builder->WithHost(url_pattern.host());
72 if (url_pattern.match_subdomains())
73 builder->WithDomainWildcard();
74
75 std::string scheme = url_pattern.scheme();
76 if (scheme == "*")
77 builder->WithSchemeWildcard();
78 else
79 builder->WithScheme(scheme);
80
81 std::string port = url_pattern.port();
82 if (port.empty() && scheme != "file") {
83 if (scheme == "*")
84 port = "*";
85 else
86 port = GetDefaultPort(scheme);
87 }
88 if (port == "*")
89 builder->WithPortWildcard();
90 else
91 builder->WithPort(port);
92
93 std::string path = url_pattern.path();
94 if (scheme == "file") {
95 // For file URLs we allow only exact path matches.
96 if (path.find_first_of("*?") != std::string::npos) {
97 *error = kNoPathWildcardsError;
98 return ContentSettingsPattern();
99 } else {
100 builder->WithPath(path);
101 }
102 } else if (path != "/*") {
103 // For other URLs we allow only paths which match everything.
104 *error = kNoPathsError;
105 return ContentSettingsPattern();
106 }
107
108 ContentSettingsPattern pattern = builder->Build();
109 if (!pattern.IsValid())
110 *error = kInvalidPatternError;
111 return pattern;
112 }
113 }
114
115 ContentSettingsType StringToContentSettingsType(
116 const std::string& content_type) {
117 for (size_t type = 0; type < arraysize(kContentSettingsTypeNames); ++type) {
118 if (content_type == kContentSettingsTypeNames[type])
119 return static_cast<ContentSettingsType>(type);
120 }
121 return CONTENT_SETTINGS_TYPE_DEFAULT;
122 }
123
124 const char* ContentSettingsTypeToString(ContentSettingsType type) {
125 size_t index = static_cast<size_t>(type);
126 DCHECK_LT(index, arraysize(kContentSettingsTypeNames));
127 return kContentSettingsTypeNames[index];
128 }
129
130 bool StringToContentSetting(const std::string& setting_str,
131 ContentSetting* setting) {
132 for (size_t type = 0; type < arraysize(kContentSettingNames); ++type) {
133 if (setting_str == kContentSettingNames[type]) {
134 *setting = static_cast<ContentSetting>(type);
135 return true;
136 }
137 }
138 return false;
139 }
140
141 const char* ContentSettingToString(ContentSetting setting) {
142 size_t index = static_cast<size_t>(setting);
143 DCHECK_LT(index, arraysize(kContentSettingNames));
144 return kContentSettingNames[index];
145 }
146
147 } // namespace extension_content_settings_helpers
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698