OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/extension_content_settings_helpers.h" | 5 #include "chrome/browser/extensions/extension_content_settings_helpers.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/scoped_ptr.h" |
| 10 #include "chrome/common/extensions/url_pattern.h" |
| 11 #include "content/common/url_constants.h" |
9 | 12 |
10 namespace { | 13 namespace { |
11 | 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 |
12 const char* const kContentSettingsTypeNames[] = { | 20 const char* const kContentSettingsTypeNames[] = { |
13 "cookies", | 21 "cookies", |
14 "images", | 22 "images", |
15 "javascript", | 23 "javascript", |
16 "plugins", | 24 "plugins", |
17 "popups", | 25 "popups", |
18 "location", | 26 "location", |
19 "notifications", | 27 "notifications", |
20 }; | 28 }; |
21 COMPILE_ASSERT(arraysize(kContentSettingsTypeNames) <= | 29 COMPILE_ASSERT(arraysize(kContentSettingsTypeNames) <= |
22 CONTENT_SETTINGS_NUM_TYPES, | 30 CONTENT_SETTINGS_NUM_TYPES, |
23 content_settings_type_names_size_invalid); | 31 content_settings_type_names_size_invalid); |
24 | 32 |
25 const char* const kContentSettingNames[] = { | 33 const char* const kContentSettingNames[] = { |
26 "default", | 34 "default", |
27 "allow", | 35 "allow", |
28 "block", | 36 "block", |
29 "ask", | 37 "ask", |
30 "session_only", | 38 "session_only", |
31 }; | 39 }; |
32 COMPILE_ASSERT(arraysize(kContentSettingNames) <= | 40 COMPILE_ASSERT(arraysize(kContentSettingNames) <= |
33 CONTENT_SETTING_NUM_SETTINGS, | 41 CONTENT_SETTING_NUM_SETTINGS, |
34 content_setting_names_size_invalid); | 42 content_setting_names_size_invalid); |
35 | 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 |
36 } // namespace | 54 } // namespace |
37 | 55 |
38 namespace extension_content_settings_helpers { | 56 namespace extension_content_settings_helpers { |
39 | 57 |
| 58 ContentSettingsPattern ParseExtensionPattern(const std::string& pattern_str, |
| 59 std::string* error) { |
| 60 URLPattern url_pattern(URLPattern::SCHEME_HTTP | |
| 61 URLPattern::SCHEME_HTTPS | |
| 62 URLPattern::SCHEME_FILE); |
| 63 url_pattern.set_ignore_ports(false); |
| 64 URLPattern::ParseResult result = |
| 65 url_pattern.Parse(pattern_str, URLPattern::PARSE_LENIENT); |
| 66 if (result != URLPattern::PARSE_SUCCESS) { |
| 67 *error = URLPattern::GetParseResultString(result); |
| 68 return ContentSettingsPattern(); |
| 69 } else { |
| 70 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
| 71 ContentSettingsPattern::CreateBuilder(false)); |
| 72 builder->WithHost(url_pattern.host()); |
| 73 if (url_pattern.match_subdomains()) |
| 74 builder->WithDomainWildcard(); |
| 75 |
| 76 std::string scheme = url_pattern.scheme(); |
| 77 if (scheme == "*") |
| 78 builder->WithSchemeWildcard(); |
| 79 else |
| 80 builder->WithScheme(scheme); |
| 81 |
| 82 std::string port = url_pattern.port(); |
| 83 if (port.empty() && scheme != "file") { |
| 84 if (scheme == "*") |
| 85 port = "*"; |
| 86 else |
| 87 port = GetDefaultPort(scheme); |
| 88 } |
| 89 if (port == "*") |
| 90 builder->WithPortWildcard(); |
| 91 else |
| 92 builder->WithPort(port); |
| 93 |
| 94 std::string path = url_pattern.path(); |
| 95 if (scheme == "file") { |
| 96 // For file URLs we allow only exact path matches. |
| 97 if (path.find_first_of("*?") != std::string::npos) { |
| 98 *error = kNoPathWildcardsError; |
| 99 return ContentSettingsPattern(); |
| 100 } else { |
| 101 builder->WithPath(path); |
| 102 } |
| 103 } else if (path != "/*") { |
| 104 // For other URLs we allow only paths which match everything. |
| 105 *error = kNoPathsError; |
| 106 return ContentSettingsPattern(); |
| 107 } |
| 108 |
| 109 ContentSettingsPattern pattern = builder->Build(); |
| 110 if (!pattern.IsValid()) |
| 111 *error = kInvalidPatternError; |
| 112 return pattern; |
| 113 } |
| 114 } |
| 115 |
40 ContentSettingsType StringToContentSettingsType( | 116 ContentSettingsType StringToContentSettingsType( |
41 const std::string& content_type) { | 117 const std::string& content_type) { |
42 for (size_t type = 0; type < arraysize(kContentSettingsTypeNames); ++type) { | 118 for (size_t type = 0; type < arraysize(kContentSettingsTypeNames); ++type) { |
43 if (content_type == kContentSettingsTypeNames[type]) | 119 if (content_type == kContentSettingsTypeNames[type]) |
44 return static_cast<ContentSettingsType>(type); | 120 return static_cast<ContentSettingsType>(type); |
45 } | 121 } |
46 return CONTENT_SETTINGS_TYPE_DEFAULT; | 122 return CONTENT_SETTINGS_TYPE_DEFAULT; |
47 } | 123 } |
48 | 124 |
49 const char* ContentSettingsTypeToString(ContentSettingsType type) { | 125 const char* ContentSettingsTypeToString(ContentSettingsType type) { |
(...skipping 14 matching lines...) Expand all Loading... |
64 } | 140 } |
65 | 141 |
66 const char* ContentSettingToString(ContentSetting setting) { | 142 const char* ContentSettingToString(ContentSetting setting) { |
67 size_t index = static_cast<size_t>(setting); | 143 size_t index = static_cast<size_t>(setting); |
68 DCHECK_LT(index, arraysize(kContentSettingNames)); | 144 DCHECK_LT(index, arraysize(kContentSettingNames)); |
69 return kContentSettingNames[index]; | 145 return kContentSettingNames[index]; |
70 } | 146 } |
71 | 147 |
72 } // namespace extension_content_settings_helpers | 148 } // namespace extension_content_settings_helpers |
73 | 149 |
OLD | NEW |