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"
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"
23 // The names of the ContentSettingsType values, for use with dictionary prefs.
24 const char* kTypeNames
[] = {
32 "auto-select-certificate",
38 "media-stream-camera",
39 "register-protocol-handler",
41 "multiple-automatic-downloads",
46 "metro-switch-to-desktop",
47 #elif defined(OS_ANDROID) || defined(OS_CHROMEOS)
48 "protected-media-identifier",
52 static_assert(arraysize(kTypeNames
) == CONTENT_SETTINGS_NUM_TYPES
,
53 "kTypeNames should have CONTENT_SETTINGS_NUM_TYPES elements");
55 const char kPatternSeparator
[] = ",";
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
);
76 std::string
ContentSettingToString(ContentSetting setting
) {
78 case CONTENT_SETTING_ALLOW
:
80 case CONTENT_SETTING_ASK
:
82 case CONTENT_SETTING_BLOCK
:
84 case CONTENT_SETTING_SESSION_ONLY
:
86 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
:
88 case CONTENT_SETTING_DEFAULT
:
90 case CONTENT_SETTING_NUM_SETTINGS
:
97 ContentSetting
ContentSettingFromString(const std::string
& name
) {
99 return CONTENT_SETTING_ALLOW
;
101 return CONTENT_SETTING_ASK
;
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();
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
;
146 ContentSettingsPattern::FromString(pattern_str_list
[0]);
147 pattern_pair
.second
=
148 ContentSettingsPattern::FromString(pattern_str_list
[1]);
152 ContentSetting
ValueToContentSetting(const base::Value
* value
) {
153 ContentSetting setting
= CONTENT_SETTING_DEFAULT
;
154 bool valid
= ParseContentSettingValue(value
, &setting
);
159 bool ParseContentSettingValue(const base::Value
* value
,
160 ContentSetting
* setting
) {
162 *setting
= CONTENT_SETTING_DEFAULT
;
166 if (!value
->GetAsInteger(&int_value
))
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
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
);
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
)) {
212 *primary_pattern
= rule
.primary_pattern
;
213 if (secondary_pattern
)
214 *secondary_pattern
= rule
.secondary_pattern
;
215 return rule
.value
.get()->DeepCopy();
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