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

Side by Side Diff: chrome/browser/content_settings/content_settings_pattern_parser.cc

Issue 7885006: Moving ContentSettingsPattern from chrome/browser/content_settings to chrome/common. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Keeping up to date with trunk. Created 9 years, 3 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 | Annotate | Revision Log
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/content_settings/content_settings_pattern_parser.h"
6
7 #include "base/string_util.h"
8 #include "chrome/common/url_constants.h"
9 #include "net/base/net_util.h"
10 #include "googleurl/src/gurl.h"
11 #include "googleurl/src/url_canon.h"
12
13 namespace {
14
15 const char* kUrlPathSeparator = "/";
16 const char* kUrlPortSeparator = ":";
17
18 class Component {
19 public:
20 Component() : start(0), len(0) {}
21 Component(size_t s, size_t l) : start(s), len(l) {}
22
23 bool IsNonEmpty() {
24 return len > 0;
25 }
26
27 size_t start;
28 size_t len;
29 };
30
31 } // namespace
32
33 namespace content_settings {
34
35 const char* PatternParser::kDomainWildcard = "[*.]";
36
37 const size_t PatternParser::kDomainWildcardLength = 4;
38
39 const char* PatternParser::kSchemeWildcard = "*";
40
41 const char* PatternParser::kHostWildcard = "*";
42
43 const char* PatternParser::kPortWildcard = "*";
44
45 // static
46 void PatternParser::Parse(const std::string& pattern_spec,
47 ContentSettingsPattern::BuilderInterface* builder) {
48 if (pattern_spec == "*") {
49 builder->WithSchemeWildcard();
50 builder->WithDomainWildcard();
51 builder->WithPortWildcard();
52 return;
53 }
54
55 // Initialize components for the individual patterns parts to empty
56 // sub-strings.
57 Component scheme_component;
58 Component host_component;
59 Component port_component;
60 Component path_component;
61
62 size_t start = 0;
63 size_t current_pos = 0;
64
65 if (pattern_spec.empty())
66 return;
67
68 // Test if a scheme pattern is in the spec.
69 current_pos = pattern_spec.find(
70 std::string(chrome::kStandardSchemeSeparator), start);
71 if (current_pos != std::string::npos) {
72 scheme_component = Component(start, current_pos);
73 start = current_pos + strlen(chrome::kStandardSchemeSeparator);
74 current_pos = start;
75 } else {
76 current_pos = start;
77 }
78
79 if (start >= pattern_spec.size())
80 return; // Bad pattern spec.
81
82 // Jump to the end of domain wildcards or an IPv6 addresses. IPv6 addresses
83 // contain ':'. So first move to the end of an IPv6 address befor searching
84 // for the ':' that separates the port form the host.
85 if (pattern_spec[current_pos] == '[')
86 current_pos = pattern_spec.find("]", start);
87
88 if (current_pos == std::string::npos)
89 return; // Bad pattern spec.
90
91 current_pos = pattern_spec.find(std::string(kUrlPortSeparator), current_pos);
92 if (current_pos == std::string::npos) {
93 // No port spec found
94 current_pos = pattern_spec.find(std::string(kUrlPathSeparator), start);
95 if (current_pos == std::string::npos) {
96 current_pos = pattern_spec.size();
97 host_component = Component(start, current_pos - start);
98 } else {
99 // Pattern has a path spec.
100 host_component = Component(start, current_pos - start);
101 }
102 start = current_pos;
103 } else {
104 // Port spec found.
105 host_component = Component(start, current_pos - start);
106 start = current_pos + 1;
107 if (start < pattern_spec.size()) {
108 current_pos = pattern_spec.find(std::string(kUrlPathSeparator), start);
109 if (current_pos == std::string::npos) {
110 current_pos = pattern_spec.size();
111 }
112 port_component = Component(start, current_pos - start);
113 start = current_pos;
114 }
115 }
116
117 current_pos = pattern_spec.size();
118 if (start < current_pos) {
119 // Pattern has a path spec.
120 path_component = Component(start, current_pos - start);
121 }
122
123 // Set pattern parts.
124 std::string scheme;
125 if (scheme_component.IsNonEmpty()) {
126 scheme = pattern_spec.substr(scheme_component.start, scheme_component.len);
127 if (scheme == kSchemeWildcard) {
128 builder->WithSchemeWildcard();
129 } else {
130 builder->WithScheme(scheme);
131 }
132 } else {
133 builder->WithSchemeWildcard();
134 }
135
136 if (host_component.IsNonEmpty()) {
137 std::string host = pattern_spec.substr(host_component.start,
138 host_component.len);
139 if (host == kHostWildcard) {
140 builder->WithDomainWildcard();
141 } else if (StartsWithASCII(host, kDomainWildcard, true)) {
142 host = host.substr(kDomainWildcardLength);
143 // If the host still contains a wildcard symbol then it is invalid.
144 if (host.find(kHostWildcard) != std::string::npos) {
145 builder->Invalid();
146 return;
147 } else {
148 builder->WithDomainWildcard();
149 builder->WithHost(host);
150 }
151 } else {
152 // If the host contains a wildcard symbol then it is invalid.
153 if (host.find(kHostWildcard) != std::string::npos) {
154 builder->Invalid();
155 return;
156 }
157 builder->WithHost(host);
158 }
159 }
160
161 if (port_component.IsNonEmpty()) {
162 const std::string port = pattern_spec.substr(port_component.start,
163 port_component.len);
164 if (port == kPortWildcard) {
165 builder->WithPortWildcard();
166 } else {
167 // Check if the port string represents a valid port.
168 for (size_t i = 0; i < port.size(); ++i) {
169 if (!IsAsciiDigit(port[i])) {
170 builder->Invalid();
171 return;
172 }
173 }
174 // TODO(markusheintz): Check port range.
175 builder->WithPort(port);
176 }
177 } else {
178 if (scheme != std::string(chrome::kExtensionScheme))
179 builder->WithPortWildcard();
180 }
181
182 if (path_component.IsNonEmpty()) {
183 builder->WithPath(pattern_spec.substr(path_component.start,
184 path_component.len));
185 }
186 }
187
188 // static
189 std::string PatternParser::ToString(
190 const ContentSettingsPattern::PatternParts& parts) {
191 // Return the most compact form to support legacy code and legacy pattern
192 // strings.
193 if (parts.is_scheme_wildcard &&
194 parts.has_domain_wildcard &&
195 parts.host.empty() &&
196 parts.is_port_wildcard)
197 return "*";
198
199 std::string str = "";
200 if (!parts.is_scheme_wildcard)
201 str += parts.scheme + chrome::kStandardSchemeSeparator;
202
203 if (parts.scheme == std::string(chrome::kFileScheme))
204 return str + parts.path;
205
206 if (parts.has_domain_wildcard) {
207 if (parts.host.empty())
208 str += kHostWildcard;
209 else
210 str += kDomainWildcard;
211 }
212 str += parts.host;
213
214 if (parts.scheme == std::string(chrome::kExtensionScheme)) {
215 str += parts.path.empty() ? std::string(kUrlPathSeparator) : parts.path;
216 return str;
217 }
218
219 if (!parts.is_port_wildcard) {
220 str += std::string(kUrlPortSeparator) + parts.port;
221 }
222
223 return str;
224 }
225
226 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698