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

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

Issue 7229012: Use extension match pattern syntax in content settings extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 9 years, 5 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
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 URLPattern::ParseResult result =
64 url_pattern.Parse(pattern_str, URLPattern::USE_PORTS);
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
40 ContentSettingsType StringToContentSettingsType( 115 ContentSettingsType StringToContentSettingsType(
41 const std::string& content_type) { 116 const std::string& content_type) {
42 for (size_t type = 0; type < arraysize(kContentSettingsTypeNames); ++type) { 117 for (size_t type = 0; type < arraysize(kContentSettingsTypeNames); ++type) {
43 if (content_type == kContentSettingsTypeNames[type]) 118 if (content_type == kContentSettingsTypeNames[type])
44 return static_cast<ContentSettingsType>(type); 119 return static_cast<ContentSettingsType>(type);
45 } 120 }
46 return CONTENT_SETTINGS_TYPE_DEFAULT; 121 return CONTENT_SETTINGS_TYPE_DEFAULT;
47 } 122 }
48 123
49 const char* ContentSettingsTypeToString(ContentSettingsType type) { 124 const char* ContentSettingsTypeToString(ContentSettingsType type) {
(...skipping 14 matching lines...) Expand all
64 } 139 }
65 140
66 const char* ContentSettingToString(ContentSetting setting) { 141 const char* ContentSettingToString(ContentSetting setting) {
67 size_t index = static_cast<size_t>(setting); 142 size_t index = static_cast<size_t>(setting);
68 DCHECK_LT(index, arraysize(kContentSettingNames)); 143 DCHECK_LT(index, arraysize(kContentSettingNames));
69 return kContentSettingNames[index]; 144 return kContentSettingNames[index];
70 } 145 }
71 146
72 } // namespace extension_content_settings_helpers 147 } // namespace extension_content_settings_helpers
73 148
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698