1 // Copyright (c) 2012 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.
5 #include "chrome/browser/content_settings/content_settings_utils.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/strings/string_split.h"
14 #include "base/values.h"
15 #include "chrome/browser/content_settings/host_content_settings_map.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "components/content_settings/core/browser/content_settings_provider.h"
18 #include "components/content_settings/core/browser/content_settings_rule.h"
19 #include "components/content_settings/core/common/content_settings_pattern.h"
24 // The names of the ContentSettingsType values, for use with dictionary prefs.
25 const char* kTypeNames[] = {
33 "auto-select-certificate",
39 "media-stream-camera",
40 "register-protocol-handler",
42 "multiple-automatic-downloads",
47 "metro-switch-to-desktop",
48 #elif defined(OS_ANDROID) || defined(OS_CHROMEOS)
49 "protected-media-identifier",
51 #if defined(OS_ANDROID)
55 COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
56 type_names_incorrect_size);
58 const char kPatternSeparator[] = ",";
62 namespace content_settings {
64 std::string GetTypeName(ContentSettingsType type) {
65 return std::string(kTypeNames[type]);
68 bool GetTypeFromName(const std::string& name,
69 ContentSettingsType* return_setting) {
70 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) {
71 if (name.compare(kTypeNames[type]) == 0) {
72 *return_setting = static_cast<ContentSettingsType>(type);
79 std::string ContentSettingToString(ContentSetting setting) {
81 case CONTENT_SETTING_ALLOW:
83 case CONTENT_SETTING_ASK:
85 case CONTENT_SETTING_BLOCK:
87 case CONTENT_SETTING_SESSION_ONLY:
89 case CONTENT_SETTING_DEFAULT:
91 case CONTENT_SETTING_NUM_SETTINGS:
98 ContentSetting ContentSettingFromString(const std::string& name) {
100 return CONTENT_SETTING_ALLOW;
102 return CONTENT_SETTING_ASK;
104 return CONTENT_SETTING_BLOCK;
105 if (name == "session")
106 return CONTENT_SETTING_SESSION_ONLY;
108 NOTREACHED() << name << " is not a recognized content setting.";
109 return CONTENT_SETTING_DEFAULT;
112 std::string CreatePatternString(
113 const ContentSettingsPattern& item_pattern,
114 const ContentSettingsPattern& top_level_frame_pattern) {
115 return item_pattern.ToString()
116 + std::string(kPatternSeparator)
117 + top_level_frame_pattern.ToString();
120 PatternPair ParsePatternString(const std::string& pattern_str) {
121 std::vector<std::string> pattern_str_list;
122 base::SplitString(pattern_str, kPatternSeparator[0], &pattern_str_list);
124 // If the |pattern_str| is an empty string then the |pattern_string_list|
125 // contains a single empty string. In this case the empty string will be
126 // removed to signal an invalid |pattern_str|. Invalid pattern strings are
127 // handle by the "if"-statment below. So the order of the if statements here
128 // must be preserved.
129 if (pattern_str_list.size() == 1) {
130 if (pattern_str_list[0].empty()) {
131 pattern_str_list.pop_back();
133 pattern_str_list.push_back("*");
137 if (pattern_str_list.size() > 2 ||
138 pattern_str_list.size() == 0) {
139 return PatternPair(ContentSettingsPattern(),
140 ContentSettingsPattern());
143 PatternPair pattern_pair;
145 ContentSettingsPattern::FromString(pattern_str_list[0]);
146 pattern_pair.second =
147 ContentSettingsPattern::FromString(pattern_str_list[1]);
151 ContentSetting ValueToContentSetting(const base::Value* value) {
152 ContentSetting setting = CONTENT_SETTING_DEFAULT;
153 bool valid = ParseContentSettingValue(value, &setting);
158 bool ParseContentSettingValue(const base::Value* value,
159 ContentSetting* setting) {
161 *setting = CONTENT_SETTING_DEFAULT;
165 if (!value->GetAsInteger(&int_value))
167 *setting = IntToContentSetting(int_value);
168 return *setting != CONTENT_SETTING_DEFAULT;
171 base::Value* GetContentSettingValueAndPatterns(
172 const ProviderInterface* provider,
173 const GURL& primary_url,
174 const GURL& secondary_url,
175 ContentSettingsType content_type,
176 const std::string& resource_identifier,
177 bool include_incognito,
178 ContentSettingsPattern* primary_pattern,
179 ContentSettingsPattern* secondary_pattern) {
180 if (include_incognito) {
181 // Check incognito-only specific settings. It's essential that the
182 // |RuleIterator| gets out of scope before we get a rule iterator for the
184 scoped_ptr<RuleIterator> incognito_rule_iterator(
185 provider->GetRuleIterator(content_type, resource_identifier, true));
186 base::Value* value = GetContentSettingValueAndPatterns(
187 incognito_rule_iterator.get(), primary_url, secondary_url,
188 primary_pattern, secondary_pattern);
192 // No settings from the incognito; use the normal mode.
193 scoped_ptr<RuleIterator> rule_iterator(
194 provider->GetRuleIterator(content_type, resource_identifier, false));
195 return GetContentSettingValueAndPatterns(
196 rule_iterator.get(), primary_url, secondary_url,
197 primary_pattern, secondary_pattern);
200 base::Value* GetContentSettingValueAndPatterns(
201 RuleIterator* rule_iterator,
202 const GURL& primary_url,
203 const GURL& secondary_url,
204 ContentSettingsPattern* primary_pattern,
205 ContentSettingsPattern* secondary_pattern) {
206 while (rule_iterator->HasNext()) {
207 const Rule& rule = rule_iterator->Next();
208 if (rule.primary_pattern.Matches(primary_url) &&
209 rule.secondary_pattern.Matches(secondary_url)) {
211 *primary_pattern = rule.primary_pattern;
212 if (secondary_pattern)
213 *secondary_pattern = rule.secondary_pattern;
214 return rule.value.get()->DeepCopy();
220 base::Value* GetContentSettingValue(const ProviderInterface* provider,
221 const GURL& primary_url,
222 const GURL& secondary_url,
223 ContentSettingsType content_type,
224 const std::string& resource_identifier,
225 bool include_incognito) {
226 return GetContentSettingValueAndPatterns(provider, primary_url, secondary_url,
227 content_type, resource_identifier,
228 include_incognito, NULL, NULL);
231 ContentSetting GetContentSetting(const ProviderInterface* provider,
232 const GURL& primary_url,
233 const GURL& secondary_url,
234 ContentSettingsType content_type,
235 const std::string& resource_identifier,
236 bool include_incognito) {
237 scoped_ptr<base::Value> value(
238 GetContentSettingValue(provider, primary_url, secondary_url,
239 content_type, resource_identifier,
241 return ValueToContentSetting(value.get());
244 void GetRendererContentSettingRules(const HostContentSettingsMap* map,
245 RendererContentSettingRules* rules) {
246 map->GetSettingsForOneType(
247 CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &(rules->image_rules));
248 map->GetSettingsForOneType(
249 CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), &(rules->script_rules));
252 } // namespace content_settings