ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / content_settings / core / browser / content_settings_utils.cc
blobc8dfe50bdfb3288b53062a38c63371ad8916b7ee
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 "components/content_settings/core/browser/content_settings_utils.h"
7 #include <vector>
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 "components/content_settings/core/browser/content_settings_provider.h"
16 #include "components/content_settings/core/browser/content_settings_rule.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "components/content_settings/core/common/content_settings_pattern.h"
19 #include "url/gurl.h"
21 namespace {
23 // The names of the ContentSettingsType values, for use with dictionary prefs.
24 const char* kTypeNames[] = {
25 "cookies",
26 "images",
27 "javascript",
28 "plugins",
29 "popups",
30 "geolocation",
31 "notifications",
32 "auto-select-certificate",
33 "fullscreen",
34 "mouselock",
35 "mixed-script",
36 "media-stream",
37 "media-stream-mic",
38 "media-stream-camera",
39 "register-protocol-handler",
40 "ppapi-broker",
41 "multiple-automatic-downloads",
42 "midi-sysex",
43 "push-messaging",
44 "ssl-cert-decisions",
45 #if defined(OS_WIN)
46 "metro-switch-to-desktop",
47 #elif defined(OS_ANDROID) || defined(OS_CHROMEOS)
48 "protected-media-identifier",
49 #endif
50 "app-banner",
52 static_assert(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
53 "kTypeNames should have CONTENT_SETTINGS_NUM_TYPES elements");
55 const char kPatternSeparator[] = ",";
57 } // namespace
59 namespace content_settings {
61 std::string GetTypeName(ContentSettingsType type) {
62 return std::string(kTypeNames[type]);
65 bool GetTypeFromName(const std::string& name,
66 ContentSettingsType* return_setting) {
67 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) {
68 if (name.compare(kTypeNames[type]) == 0) {
69 *return_setting = static_cast<ContentSettingsType>(type);
70 return true;
73 return false;
76 std::string ContentSettingToString(ContentSetting setting) {
77 switch (setting) {
78 case CONTENT_SETTING_ALLOW:
79 return "allow";
80 case CONTENT_SETTING_ASK:
81 return "ask";
82 case CONTENT_SETTING_BLOCK:
83 return "block";
84 case CONTENT_SETTING_SESSION_ONLY:
85 return "session";
86 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT:
87 return "detect";
88 case CONTENT_SETTING_DEFAULT:
89 return "default";
90 case CONTENT_SETTING_NUM_SETTINGS:
91 NOTREACHED();
94 return std::string();
97 ContentSetting ContentSettingFromString(const std::string& name) {
98 if (name == "allow")
99 return CONTENT_SETTING_ALLOW;
100 if (name == "ask")
101 return CONTENT_SETTING_ASK;
102 if (name == "block")
103 return CONTENT_SETTING_BLOCK;
104 if (name == "session")
105 return CONTENT_SETTING_SESSION_ONLY;
106 if (name == "detect")
107 return CONTENT_SETTING_DETECT_IMPORTANT_CONTENT;
109 NOTREACHED() << name << " is not a recognized content setting.";
110 return CONTENT_SETTING_DEFAULT;
113 std::string CreatePatternString(
114 const ContentSettingsPattern& item_pattern,
115 const ContentSettingsPattern& top_level_frame_pattern) {
116 return item_pattern.ToString()
117 + std::string(kPatternSeparator)
118 + top_level_frame_pattern.ToString();
121 PatternPair ParsePatternString(const std::string& pattern_str) {
122 std::vector<std::string> pattern_str_list;
123 base::SplitString(pattern_str, kPatternSeparator[0], &pattern_str_list);
125 // If the |pattern_str| is an empty string then the |pattern_string_list|
126 // contains a single empty string. In this case the empty string will be
127 // removed to signal an invalid |pattern_str|. Invalid pattern strings are
128 // handle by the "if"-statment below. So the order of the if statements here
129 // must be preserved.
130 if (pattern_str_list.size() == 1) {
131 if (pattern_str_list[0].empty()) {
132 pattern_str_list.pop_back();
133 } else {
134 pattern_str_list.push_back("*");
138 if (pattern_str_list.size() > 2 ||
139 pattern_str_list.size() == 0) {
140 return PatternPair(ContentSettingsPattern(),
141 ContentSettingsPattern());
144 PatternPair pattern_pair;
145 pattern_pair.first =
146 ContentSettingsPattern::FromString(pattern_str_list[0]);
147 pattern_pair.second =
148 ContentSettingsPattern::FromString(pattern_str_list[1]);
149 return pattern_pair;
152 ContentSetting ValueToContentSetting(const base::Value* value) {
153 ContentSetting setting = CONTENT_SETTING_DEFAULT;
154 bool valid = ParseContentSettingValue(value, &setting);
155 DCHECK(valid);
156 return setting;
159 bool ParseContentSettingValue(const base::Value* value,
160 ContentSetting* setting) {
161 if (!value) {
162 *setting = CONTENT_SETTING_DEFAULT;
163 return true;
165 int int_value = -1;
166 if (!value->GetAsInteger(&int_value))
167 return false;
168 *setting = IntToContentSetting(int_value);
169 return *setting != CONTENT_SETTING_DEFAULT;
172 base::Value* GetContentSettingValueAndPatterns(
173 const ProviderInterface* provider,
174 const GURL& primary_url,
175 const GURL& secondary_url,
176 ContentSettingsType content_type,
177 const std::string& resource_identifier,
178 bool include_incognito,
179 ContentSettingsPattern* primary_pattern,
180 ContentSettingsPattern* secondary_pattern) {
181 if (include_incognito) {
182 // Check incognito-only specific settings. It's essential that the
183 // |RuleIterator| gets out of scope before we get a rule iterator for the
184 // normal mode.
185 scoped_ptr<RuleIterator> incognito_rule_iterator(
186 provider->GetRuleIterator(content_type, resource_identifier, true));
187 base::Value* value = GetContentSettingValueAndPatterns(
188 incognito_rule_iterator.get(), primary_url, secondary_url,
189 primary_pattern, secondary_pattern);
190 if (value)
191 return value;
193 // No settings from the incognito; use the normal mode.
194 scoped_ptr<RuleIterator> rule_iterator(
195 provider->GetRuleIterator(content_type, resource_identifier, false));
196 return GetContentSettingValueAndPatterns(
197 rule_iterator.get(), primary_url, secondary_url,
198 primary_pattern, secondary_pattern);
201 base::Value* GetContentSettingValueAndPatterns(
202 RuleIterator* rule_iterator,
203 const GURL& primary_url,
204 const GURL& secondary_url,
205 ContentSettingsPattern* primary_pattern,
206 ContentSettingsPattern* secondary_pattern) {
207 while (rule_iterator->HasNext()) {
208 const Rule& rule = rule_iterator->Next();
209 if (rule.primary_pattern.Matches(primary_url) &&
210 rule.secondary_pattern.Matches(secondary_url)) {
211 if (primary_pattern)
212 *primary_pattern = rule.primary_pattern;
213 if (secondary_pattern)
214 *secondary_pattern = rule.secondary_pattern;
215 return rule.value.get()->DeepCopy();
218 return NULL;
221 void GetRendererContentSettingRules(const HostContentSettingsMap* map,
222 RendererContentSettingRules* rules) {
223 map->GetSettingsForOneType(
224 CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &(rules->image_rules));
225 map->GetSettingsForOneType(
226 CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), &(rules->script_rules));
229 } // namespace content_settings