Fix invalid cast in AppCacheGroup::AddUpdateObserver.
[chromium-blink-merge.git] / components / content_settings / core / browser / content_settings_utils.cc
blob6a9a65574b2f7ccd72c1576c7b9e9eeec7a4354c
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/prefs/pref_registry.h"
14 #include "base/strings/string_split.h"
15 #include "base/values.h"
16 #include "components/content_settings/core/browser/content_settings_provider.h"
17 #include "components/content_settings/core/browser/content_settings_rule.h"
18 #include "components/content_settings/core/browser/host_content_settings_map.h"
19 #include "components/content_settings/core/browser/website_settings_info.h"
20 #include "components/content_settings/core/browser/website_settings_registry.h"
21 #include "components/content_settings/core/common/content_settings_pattern.h"
22 #include "components/pref_registry/pref_registry_syncable.h"
23 #include "url/gurl.h"
25 namespace {
27 const char kPatternSeparator[] = ",";
29 } // namespace
31 namespace content_settings {
33 std::string GetTypeName(ContentSettingsType type) {
34 return WebsiteSettingsRegistry::GetInstance()->Get(type)->name();
37 std::string ContentSettingToString(ContentSetting setting) {
38 switch (setting) {
39 case CONTENT_SETTING_ALLOW:
40 return "allow";
41 case CONTENT_SETTING_ASK:
42 return "ask";
43 case CONTENT_SETTING_BLOCK:
44 return "block";
45 case CONTENT_SETTING_SESSION_ONLY:
46 return "session";
47 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT:
48 return "detect";
49 case CONTENT_SETTING_DEFAULT:
50 return "default";
51 case CONTENT_SETTING_NUM_SETTINGS:
52 NOTREACHED();
55 return ResourceIdentifier();
58 ContentSetting ContentSettingFromString(const std::string& name) {
59 if (name == "allow")
60 return CONTENT_SETTING_ALLOW;
61 if (name == "ask")
62 return CONTENT_SETTING_ASK;
63 if (name == "block")
64 return CONTENT_SETTING_BLOCK;
65 if (name == "session")
66 return CONTENT_SETTING_SESSION_ONLY;
67 if (name == "detect")
68 return CONTENT_SETTING_DETECT_IMPORTANT_CONTENT;
70 NOTREACHED() << name << " is not a recognized content setting.";
71 return CONTENT_SETTING_DEFAULT;
74 std::string CreatePatternString(
75 const ContentSettingsPattern& item_pattern,
76 const ContentSettingsPattern& top_level_frame_pattern) {
77 return item_pattern.ToString()
78 + std::string(kPatternSeparator)
79 + top_level_frame_pattern.ToString();
82 PatternPair ParsePatternString(const std::string& pattern_str) {
83 std::vector<std::string> pattern_str_list = base::SplitString(
84 pattern_str, std::string(1, kPatternSeparator[0]),
85 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
87 // If the |pattern_str| is an empty string then the |pattern_string_list|
88 // contains a single empty string. In this case the empty string will be
89 // removed to signal an invalid |pattern_str|. Invalid pattern strings are
90 // handle by the "if"-statment below. So the order of the if statements here
91 // must be preserved.
92 if (pattern_str_list.size() == 1) {
93 if (pattern_str_list[0].empty()) {
94 pattern_str_list.pop_back();
95 } else {
96 pattern_str_list.push_back("*");
100 if (pattern_str_list.size() > 2 ||
101 pattern_str_list.size() == 0) {
102 return PatternPair(ContentSettingsPattern(),
103 ContentSettingsPattern());
106 PatternPair pattern_pair;
107 pattern_pair.first =
108 ContentSettingsPattern::FromString(pattern_str_list[0]);
109 pattern_pair.second =
110 ContentSettingsPattern::FromString(pattern_str_list[1]);
111 return pattern_pair;
114 ContentSetting ValueToContentSetting(const base::Value* value) {
115 ContentSetting setting = CONTENT_SETTING_DEFAULT;
116 bool valid = ParseContentSettingValue(value, &setting);
117 DCHECK(valid);
118 return setting;
121 bool ParseContentSettingValue(const base::Value* value,
122 ContentSetting* setting) {
123 if (!value) {
124 *setting = CONTENT_SETTING_DEFAULT;
125 return true;
127 int int_value = -1;
128 if (!value->GetAsInteger(&int_value))
129 return false;
130 *setting = IntToContentSetting(int_value);
131 return *setting != CONTENT_SETTING_DEFAULT;
134 scoped_ptr<base::Value> ContentSettingToValue(ContentSetting setting) {
135 if (setting <= CONTENT_SETTING_DEFAULT ||
136 setting >= CONTENT_SETTING_NUM_SETTINGS) {
137 return nullptr;
139 return make_scoped_ptr(new base::FundamentalValue(setting));
142 base::Value* GetContentSettingValueAndPatterns(
143 const ProviderInterface* provider,
144 const GURL& primary_url,
145 const GURL& secondary_url,
146 ContentSettingsType content_type,
147 const std::string& resource_identifier,
148 bool include_incognito,
149 ContentSettingsPattern* primary_pattern,
150 ContentSettingsPattern* secondary_pattern) {
151 if (include_incognito) {
152 // Check incognito-only specific settings. It's essential that the
153 // |RuleIterator| gets out of scope before we get a rule iterator for the
154 // normal mode.
155 scoped_ptr<RuleIterator> incognito_rule_iterator(
156 provider->GetRuleIterator(content_type, resource_identifier, true));
157 base::Value* value = GetContentSettingValueAndPatterns(
158 incognito_rule_iterator.get(), primary_url, secondary_url,
159 primary_pattern, secondary_pattern);
160 if (value)
161 return value;
163 // No settings from the incognito; use the normal mode.
164 scoped_ptr<RuleIterator> rule_iterator(
165 provider->GetRuleIterator(content_type, resource_identifier, false));
166 return GetContentSettingValueAndPatterns(
167 rule_iterator.get(), primary_url, secondary_url,
168 primary_pattern, secondary_pattern);
171 base::Value* GetContentSettingValueAndPatterns(
172 RuleIterator* rule_iterator,
173 const GURL& primary_url,
174 const GURL& secondary_url,
175 ContentSettingsPattern* primary_pattern,
176 ContentSettingsPattern* secondary_pattern) {
177 while (rule_iterator->HasNext()) {
178 const Rule& rule = rule_iterator->Next();
179 if (rule.primary_pattern.Matches(primary_url) &&
180 rule.secondary_pattern.Matches(secondary_url)) {
181 if (primary_pattern)
182 *primary_pattern = rule.primary_pattern;
183 if (secondary_pattern)
184 *secondary_pattern = rule.secondary_pattern;
185 return rule.value.get()->DeepCopy();
188 return NULL;
191 void GetRendererContentSettingRules(const HostContentSettingsMap* map,
192 RendererContentSettingRules* rules) {
193 map->GetSettingsForOneType(
194 CONTENT_SETTINGS_TYPE_IMAGES,
195 ResourceIdentifier(),
196 &(rules->image_rules));
197 map->GetSettingsForOneType(
198 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
199 ResourceIdentifier(),
200 &(rules->script_rules));
203 uint32 PrefRegistrationFlagsForType(ContentSettingsType content_type) {
204 uint32 flags = PrefRegistry::NO_REGISTRATION_FLAGS;
206 if (IsContentSettingsTypeSyncable(content_type))
207 flags |= user_prefs::PrefRegistrySyncable::SYNCABLE_PREF;
209 if (IsContentSettingsTypeLossy(content_type))
210 flags |= PrefRegistry::LOSSY_PREF;
212 return flags;
215 } // namespace content_settings