OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/permissions/socket_permission_entry.h" | 5 #include "extensions/common/permissions/socket_permission_entry.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 #include <sstream> | 8 #include <sstream> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "extensions/common/permissions/api_permission.h" | 16 #include "extensions/common/permissions/api_permission.h" |
17 #include "extensions/common/permissions/socket_permission.h" | 17 #include "extensions/common/permissions/socket_permission.h" |
18 #include "url/url_canon.h" | 18 #include "url/url_canon.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 using content::SocketPermissionRequest; | 22 using content::SocketPermissionRequest; |
23 | 23 |
24 const char kColon = ':'; | 24 const char kColon = ':'; |
25 const char kDot = '.'; | 25 const char kDot = '.'; |
26 const char kWildcard[] = "*"; | 26 const char kWildcard[] = "*"; |
27 const int kWildcardPortNumber = 0; | 27 const uint16 kWildcardPortNumber = 0; |
28 const int kInvalidPort = -1; | 28 const uint16 kInvalidPort = 65535; |
29 | 29 |
30 bool StartsOrEndsWithWhitespace(const std::string& str) { | 30 bool StartsOrEndsWithWhitespace(const std::string& str) { |
31 return !str.empty() && | 31 return !str.empty() && |
32 (IsWhitespace(str[0]) || IsWhitespace(str[str.length() - 1])); | 32 (IsWhitespace(str[0]) || IsWhitespace(str[str.length() - 1])); |
33 } | 33 } |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 namespace extensions { | 37 namespace extensions { |
38 | 38 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 183 |
184 if (pattern_tokens.size() == 1 || pattern_tokens[1].empty() || | 184 if (pattern_tokens.size() == 1 || pattern_tokens[1].empty() || |
185 pattern_tokens[1] == kWildcard) { | 185 pattern_tokens[1] == kWildcard) { |
186 *entry = result; | 186 *entry = result; |
187 return true; | 187 return true; |
188 } | 188 } |
189 | 189 |
190 if (StartsOrEndsWithWhitespace(pattern_tokens[1])) | 190 if (StartsOrEndsWithWhitespace(pattern_tokens[1])) |
191 return false; | 191 return false; |
192 | 192 |
193 if (!base::StringToInt(pattern_tokens[1], &result.pattern_.port) || | 193 int port; |
194 result.pattern_.port < 1 || result.pattern_.port > 65535) | 194 if (!base::StringToInt(pattern_tokens[1], &port) || port < 1 || port > 65535) |
195 return false; | 195 return false; |
| 196 result.pattern_.port = static_cast<uint16>(port); |
196 | 197 |
197 *entry = result; | 198 *entry = result; |
198 return true; | 199 return true; |
199 } | 200 } |
200 | 201 |
201 std::string SocketPermissionEntry::GetHostPatternAsString() const { | 202 std::string SocketPermissionEntry::GetHostPatternAsString() const { |
202 std::string result; | 203 std::string result; |
203 | 204 |
204 if (!IsAddressBoundType()) | 205 if (!IsAddressBoundType()) |
205 return result; | 206 return result; |
206 | 207 |
207 if (match_subdomains()) { | 208 if (match_subdomains()) { |
208 result.append(kWildcard); | 209 result.append(kWildcard); |
209 if (!pattern_.host.empty()) | 210 if (!pattern_.host.empty()) |
210 result.append(1, kDot).append(pattern_.host); | 211 result.append(1, kDot).append(pattern_.host); |
211 } else { | 212 } else { |
212 result.append(pattern_.host); | 213 result.append(pattern_.host); |
213 } | 214 } |
214 | 215 |
215 if (pattern_.port == kWildcardPortNumber) | 216 if (pattern_.port == kWildcardPortNumber) |
216 result.append(1, kColon).append(kWildcard); | 217 result.append(1, kColon).append(kWildcard); |
217 else | 218 else |
218 result.append(1, kColon).append(base::IntToString(pattern_.port)); | 219 result.append(1, kColon).append(base::IntToString(pattern_.port)); |
219 | 220 |
220 return result; | 221 return result; |
221 } | 222 } |
222 | 223 |
223 } // namespace extensions | 224 } // namespace extensions |
OLD | NEW |