OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/content_settings/core/browser/content_settings_pref.h" | 5 #include "components/content_settings/core/browser/content_settings_pref.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/prefs/scoped_user_pref_update.h" | 11 #include "base/prefs/scoped_user_pref_update.h" |
12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
13 #include "base/time/clock.h" | 13 #include "base/time/clock.h" |
14 #include "components/content_settings/core/browser/content_settings_rule.h" | 14 #include "components/content_settings/core/browser/content_settings_rule.h" |
15 #include "components/content_settings/core/browser/content_settings_utils.h" | 15 #include "components/content_settings/core/browser/content_settings_utils.h" |
16 #include "components/content_settings/core/browser/host_content_settings_map.h" | 16 #include "components/content_settings/core/browser/host_content_settings_map.h" |
17 #include "components/content_settings/core/common/content_settings.h" | 17 #include "components/content_settings/core/common/content_settings.h" |
18 #include "components/content_settings/core/common/content_settings_pattern.h" | 18 #include "components/content_settings/core/common/content_settings_pattern.h" |
19 #include "components/content_settings/core/common/pref_names.h" | 19 #include "components/content_settings/core/common/pref_names.h" |
20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 typedef std::pair<std::string, std::string> StringPair; | 24 typedef std::pair<std::string, std::string> StringPair; |
25 | 25 |
26 const char kPerPluginPrefName[] = "per_plugin"; | 26 const char kSettingPath[] = "setting"; |
27 const char kPerResourceIdentifierPrefName[] = "per_resource"; | |
27 const char kLastUsed[] = "last_used"; | 28 const char kLastUsed[] = "last_used"; |
28 | 29 |
29 ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type, | 30 ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type, |
30 ContentSetting setting) { | 31 ContentSetting setting) { |
31 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES && | 32 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES && |
32 setting == CONTENT_SETTING_ASK) { | 33 setting == CONTENT_SETTING_ASK) { |
33 return CONTENT_SETTING_BLOCK; | 34 return CONTENT_SETTING_BLOCK; |
34 } | 35 } |
35 return setting; | 36 return setting; |
36 } | 37 } |
37 | 38 |
38 // If the given content type supports resource identifiers in user preferences, | 39 // If the given content type supports resource identifiers in user preferences, |
39 // returns true and sets |pref_key| to the key in the content settings | 40 // returns true and sets |pref_key| to the key in the content settings |
40 // dictionary under which per-resource content settings are stored. | 41 // dictionary under which per-resource content settings are stored. |
41 // Otherwise, returns false. | 42 // Otherwise, returns false. |
42 bool GetResourceTypeName(ContentSettingsType content_type, | 43 bool GetResourceTypeName(ContentSettingsType content_type, |
43 std::string* pref_key) { | 44 std::string* pref_key) { |
44 if (content_type == CONTENT_SETTINGS_TYPE_PLUGINS) { | 45 if (content_type == CONTENT_SETTINGS_TYPE_PLUGINS) { |
45 *pref_key = kPerPluginPrefName; | 46 *pref_key = kPerResourceIdentifierPrefName; |
46 return true; | 47 return true; |
47 } | 48 } |
48 return false; | 49 return false; |
49 } | 50 } |
50 | 51 |
51 } // namespace | 52 } // namespace |
52 | 53 |
53 namespace content_settings { | 54 namespace content_settings { |
54 | 55 |
55 ContentSettingsPref::ContentSettingsPref( | 56 ContentSettingsPref::ContentSettingsPref( |
57 ContentSettingsType content_type, | |
56 PrefService* prefs, | 58 PrefService* prefs, |
57 PrefChangeRegistrar* registrar, | 59 PrefChangeRegistrar* registrar, |
58 base::Clock* clock, | 60 const char* pref_name, |
59 bool incognito, | 61 bool incognito, |
62 bool* updating_old_preferences_flag, | |
60 NotifyObserversCallback notify_callback) | 63 NotifyObserversCallback notify_callback) |
61 : prefs_(prefs), | 64 : content_type_(content_type), |
62 clock_(clock), | 65 prefs_(prefs), |
63 registrar_(registrar), | 66 registrar_(registrar), |
67 pref_name_(pref_name), | |
64 is_incognito_(incognito), | 68 is_incognito_(incognito), |
65 updating_preferences_(false), | 69 updating_preferences_(false), |
70 updating_old_preferences_(updating_old_preferences_flag), | |
66 notify_callback_(notify_callback) { | 71 notify_callback_(notify_callback) { |
67 DCHECK(prefs_); | 72 DCHECK(prefs_); |
68 | 73 |
69 // Read content settings exceptions. | 74 // Read content settings exceptions. At initialization, we do not need to |
75 // propagate the read settings back to the old dictionary, as the dictionary | |
76 // | |
70 ReadContentSettingsFromPref(); | 77 ReadContentSettingsFromPref(); |
71 | 78 |
72 registrar->Add( | 79 registrar_->Add( |
73 prefs::kContentSettingsPatternPairs, | 80 pref_name_, |
74 base::Bind(&ContentSettingsPref::OnContentSettingsPatternPairsChanged, | 81 base::Bind(&ContentSettingsPref::OnPrefChanged, base::Unretained(this))); |
75 base::Unretained(this))); | |
76 } | 82 } |
77 | 83 |
78 ContentSettingsPref::~ContentSettingsPref() { | 84 ContentSettingsPref::~ContentSettingsPref() { |
79 } | 85 } |
80 | 86 |
81 RuleIterator* ContentSettingsPref::GetRuleIterator( | 87 RuleIterator* ContentSettingsPref::GetRuleIterator( |
82 ContentSettingsType content_type, | |
83 const ResourceIdentifier& resource_identifier, | 88 const ResourceIdentifier& resource_identifier, |
84 bool incognito) const { | 89 bool incognito) const { |
85 if (incognito) | 90 if (incognito) |
86 return incognito_value_map_.GetRuleIterator(content_type, | 91 return incognito_value_map_.GetRuleIterator(content_type_, |
87 resource_identifier, | 92 resource_identifier, |
88 &lock_); | 93 &lock_); |
89 return value_map_.GetRuleIterator(content_type, resource_identifier, &lock_); | 94 return value_map_.GetRuleIterator(content_type_, resource_identifier, &lock_); |
90 } | 95 } |
91 | 96 |
92 bool ContentSettingsPref::SetWebsiteSetting( | 97 bool ContentSettingsPref::SetWebsiteSetting( |
93 const ContentSettingsPattern& primary_pattern, | 98 const ContentSettingsPattern& primary_pattern, |
94 const ContentSettingsPattern& secondary_pattern, | 99 const ContentSettingsPattern& secondary_pattern, |
95 ContentSettingsType content_type, | |
96 const ResourceIdentifier& resource_identifier, | 100 const ResourceIdentifier& resource_identifier, |
97 base::Value* in_value) { | 101 base::Value* in_value) { |
98 DCHECK(thread_checker_.CalledOnValidThread()); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
99 DCHECK(prefs_); | 103 DCHECK(prefs_); |
100 | 104 DCHECK(primary_pattern != ContentSettingsPattern::Wildcard() || |
101 // Default settings are set using a wildcard pattern for both | 105 secondary_pattern != ContentSettingsPattern::Wildcard() || |
102 // |primary_pattern| and |secondary_pattern|. Don't store default settings in | 106 !resource_identifier.empty()); |
103 // the |PrefProvider|. The |PrefProvider| handles settings for specific | |
104 // sites/origins defined by the |primary_pattern| and the |secondary_pattern|. | |
105 // Default settings are handled by the |DefaultProvider|. | |
106 if (primary_pattern == ContentSettingsPattern::Wildcard() && | |
107 secondary_pattern == ContentSettingsPattern::Wildcard() && | |
108 resource_identifier.empty()) { | |
109 return false; | |
110 } | |
111 | 107 |
112 // At this point take the ownership of the |in_value|. | 108 // At this point take the ownership of the |in_value|. |
113 scoped_ptr<base::Value> value(in_value); | 109 scoped_ptr<base::Value> value(in_value); |
110 | |
114 // Update in memory value map. | 111 // Update in memory value map. |
115 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; | 112 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
116 if (!is_incognito_) | 113 if (!is_incognito_) |
117 map_to_modify = &value_map_; | 114 map_to_modify = &value_map_; |
118 | 115 |
119 { | 116 { |
120 base::AutoLock auto_lock(lock_); | 117 base::AutoLock auto_lock(lock_); |
121 if (value.get()) { | 118 if (value.get()) { |
122 map_to_modify->SetValue( | 119 map_to_modify->SetValue( |
123 primary_pattern, | 120 primary_pattern, |
124 secondary_pattern, | 121 secondary_pattern, |
125 content_type, | 122 content_type_, |
126 resource_identifier, | 123 resource_identifier, |
127 value->DeepCopy()); | 124 value->DeepCopy()); |
128 } else { | 125 } else { |
129 map_to_modify->DeleteValue( | 126 map_to_modify->DeleteValue( |
130 primary_pattern, | 127 primary_pattern, |
131 secondary_pattern, | 128 secondary_pattern, |
132 content_type, | 129 content_type_, |
133 resource_identifier); | 130 resource_identifier); |
134 } | 131 } |
135 } | 132 } |
136 // Update the content settings preference. | 133 // Update the content settings preference. |
137 if (!is_incognito_) { | 134 if (!is_incognito_) { |
138 UpdatePref(primary_pattern, | 135 UpdatePref(primary_pattern, |
139 secondary_pattern, | 136 secondary_pattern, |
140 content_type, | |
141 resource_identifier, | 137 resource_identifier, |
142 value.get()); | 138 value.get()); |
139 if (IsContentSettingsTypeSyncable(content_type_)) | |
140 UpdateOldPref(primary_pattern, | |
141 secondary_pattern, | |
142 resource_identifier, | |
143 value.get()); | |
143 } | 144 } |
144 | 145 |
145 notify_callback_.Run( | 146 notify_callback_.Run( |
146 primary_pattern, secondary_pattern, content_type, resource_identifier); | 147 primary_pattern, secondary_pattern, content_type_, resource_identifier); |
147 | 148 |
148 return true; | 149 return true; |
149 } | 150 } |
150 | 151 |
151 void ContentSettingsPref::ClearAllContentSettingsRules( | 152 void ContentSettingsPref::ClearAllContentSettingsRules() { |
msramek
2015/03/24 15:11:45
I just noticed this: There doesn't seem to be a go
| |
152 ContentSettingsType content_type) { | |
153 DCHECK(thread_checker_.CalledOnValidThread()); | 153 DCHECK(thread_checker_.CalledOnValidThread()); |
154 DCHECK(prefs_); | 154 DCHECK(prefs_); |
155 | 155 |
156 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; | 156 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
157 if (!is_incognito_) | 157 if (!is_incognito_) |
158 map_to_modify = &value_map_; | 158 map_to_modify = &value_map_; |
159 | 159 |
160 std::vector<Rule> rules_to_delete; | 160 std::vector<Rule> rules_to_delete; |
161 { | 161 { |
162 base::AutoLock auto_lock(lock_); | 162 base::AutoLock auto_lock(lock_); |
163 scoped_ptr<RuleIterator> rule_iterator( | 163 scoped_ptr<RuleIterator> rule_iterator( |
164 map_to_modify->GetRuleIterator(content_type, std::string(), NULL)); | 164 map_to_modify->GetRuleIterator(content_type_, std::string(), NULL)); |
165 // Copy the rules; we cannot call |UpdatePref| while holding |lock_|. | 165 // Copy the rules; we cannot call |UpdatePref| while holding |lock_|. |
166 while (rule_iterator->HasNext()) | 166 while (rule_iterator->HasNext()) |
167 rules_to_delete.push_back(rule_iterator->Next()); | 167 rules_to_delete.push_back(rule_iterator->Next()); |
168 | 168 |
169 map_to_modify->DeleteValues(content_type, std::string()); | 169 map_to_modify->DeleteValues(content_type_, std::string()); |
170 } | 170 } |
171 | 171 |
172 for (std::vector<Rule>::const_iterator it = rules_to_delete.begin(); | 172 for (std::vector<Rule>::const_iterator it = rules_to_delete.begin(); |
173 it != rules_to_delete.end(); ++it) { | 173 it != rules_to_delete.end(); ++it) { |
174 UpdatePref(it->primary_pattern, | 174 UpdatePref(it->primary_pattern, |
175 it->secondary_pattern, | 175 it->secondary_pattern, |
176 content_type, | |
177 std::string(), | 176 std::string(), |
178 NULL); | 177 NULL); |
178 if (IsContentSettingsTypeSyncable(content_type_)) | |
179 UpdateOldPref(it->primary_pattern, | |
180 it->secondary_pattern, | |
181 std::string(), | |
182 NULL); | |
179 } | 183 } |
180 notify_callback_.Run(ContentSettingsPattern(), | 184 notify_callback_.Run(ContentSettingsPattern(), |
181 ContentSettingsPattern(), | 185 ContentSettingsPattern(), |
182 content_type, | 186 content_type_, |
183 std::string()); | 187 std::string()); |
184 } | 188 } |
185 | 189 |
186 void ContentSettingsPref::UpdateLastUsage( | 190 void ContentSettingsPref::UpdateLastUsage( |
187 const ContentSettingsPattern& primary_pattern, | 191 const ContentSettingsPattern& primary_pattern, |
188 const ContentSettingsPattern& secondary_pattern, | 192 const ContentSettingsPattern& secondary_pattern, |
189 ContentSettingsType content_type) { | 193 base::Clock* clock) { |
190 // Don't write if in incognito. | 194 // Don't write if in incognito. |
191 if (is_incognito_) { | 195 if (is_incognito_) { |
192 return; | 196 return; |
193 } | 197 } |
194 | 198 |
195 // Ensure that |lock_| is not held by this thread, since this function will | 199 // Ensure that |lock_| is not held by this thread, since this function will |
196 // send out notifications (by |~DictionaryPrefUpdate|). | 200 // send out notifications (by |~DictionaryPrefUpdate|). |
197 AssertLockNotHeld(); | 201 AssertLockNotHeld(); |
198 | 202 |
199 base::AutoReset<bool> auto_reset(&updating_preferences_, true); | 203 base::AutoReset<bool> auto_reset(&updating_preferences_, true); |
200 { | 204 { |
201 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); | 205 DictionaryPrefUpdate update(prefs_, pref_name_); |
202 base::DictionaryValue* pattern_pairs_settings = update.Get(); | 206 base::DictionaryValue* pattern_pairs_settings = update.Get(); |
203 | 207 |
204 std::string pattern_str( | 208 std::string pattern_str( |
205 CreatePatternString(primary_pattern, secondary_pattern)); | 209 CreatePatternString(primary_pattern, secondary_pattern)); |
206 base::DictionaryValue* settings_dictionary = NULL; | 210 base::DictionaryValue* settings_dictionary = NULL; |
207 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | 211 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( |
208 pattern_str, &settings_dictionary); | 212 pattern_str, &settings_dictionary); |
209 | 213 |
210 if (!found) { | 214 if (!found) { |
211 settings_dictionary = new base::DictionaryValue; | 215 settings_dictionary = new base::DictionaryValue; |
212 pattern_pairs_settings->SetWithoutPathExpansion(pattern_str, | 216 pattern_pairs_settings->SetWithoutPathExpansion(pattern_str, |
213 settings_dictionary); | 217 settings_dictionary); |
214 } | 218 } |
215 | 219 |
216 base::DictionaryValue* last_used_dictionary = NULL; | 220 settings_dictionary->SetWithoutPathExpansion( |
217 found = settings_dictionary->GetDictionaryWithoutPathExpansion( | 221 kLastUsed, new base::FundamentalValue(clock->Now().ToDoubleT())); |
218 kLastUsed, &last_used_dictionary); | |
219 | |
220 if (!found) { | |
221 last_used_dictionary = new base::DictionaryValue; | |
222 settings_dictionary->SetWithoutPathExpansion(kLastUsed, | |
223 last_used_dictionary); | |
224 } | |
225 | |
226 std::string settings_path = GetTypeName(content_type); | |
227 last_used_dictionary->Set( | |
228 settings_path, new base::FundamentalValue(clock_->Now().ToDoubleT())); | |
229 } | 222 } |
230 } | 223 } |
231 | 224 |
232 base::Time ContentSettingsPref::GetLastUsage( | 225 base::Time ContentSettingsPref::GetLastUsage( |
233 const ContentSettingsPattern& primary_pattern, | 226 const ContentSettingsPattern& primary_pattern, |
234 const ContentSettingsPattern& secondary_pattern, | 227 const ContentSettingsPattern& secondary_pattern) { |
235 ContentSettingsType content_type) { | |
236 const base::DictionaryValue* pattern_pairs_settings = | 228 const base::DictionaryValue* pattern_pairs_settings = |
237 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | 229 prefs_->GetDictionary(pref_name_); |
238 std::string pattern_str( | 230 std::string pattern_str( |
239 CreatePatternString(primary_pattern, secondary_pattern)); | 231 CreatePatternString(primary_pattern, secondary_pattern)); |
240 | 232 |
241 const base::DictionaryValue* settings_dictionary = NULL; | 233 const base::DictionaryValue* settings_dictionary = NULL; |
242 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | 234 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( |
243 pattern_str, &settings_dictionary); | 235 pattern_str, &settings_dictionary); |
244 | 236 |
245 if (!found) | 237 if (!found) |
246 return base::Time(); | 238 return base::Time(); |
247 | 239 |
248 const base::DictionaryValue* last_used_dictionary = NULL; | |
249 found = settings_dictionary->GetDictionaryWithoutPathExpansion( | |
250 kLastUsed, &last_used_dictionary); | |
251 | |
252 if (!found) | |
253 return base::Time(); | |
254 | |
255 double last_used_time; | 240 double last_used_time; |
256 found = last_used_dictionary->GetDoubleWithoutPathExpansion( | 241 found = settings_dictionary->GetDoubleWithoutPathExpansion( |
257 GetTypeName(content_type), &last_used_time); | 242 kLastUsed, &last_used_time); |
258 | 243 |
259 if (!found) | 244 if (!found) |
260 return base::Time(); | 245 return base::Time(); |
261 | 246 |
262 return base::Time::FromDoubleT(last_used_time); | 247 return base::Time::FromDoubleT(last_used_time); |
263 } | 248 } |
264 | 249 |
265 size_t ContentSettingsPref::GetNumExceptions() { | 250 size_t ContentSettingsPref::GetNumExceptions() { |
266 return value_map_.size(); | 251 return value_map_.size(); |
267 } | 252 } |
268 | 253 |
269 void ContentSettingsPref::SetClockForTesting(base::Clock* clock) { | |
270 clock_ = clock; | |
271 } | |
272 | |
273 void ContentSettingsPref::ReadContentSettingsFromPref() { | 254 void ContentSettingsPref::ReadContentSettingsFromPref() { |
274 // |DictionaryPrefUpdate| sends out notifications when destructed. This | 255 // |DictionaryPrefUpdate| sends out notifications when destructed. This |
275 // construction order ensures |AutoLock| gets destroyed first and |lock_| is | 256 // construction order ensures |AutoLock| gets destroyed first and |lock_| is |
276 // not held when the notifications are sent. Also, |auto_reset| must be still | 257 // not held when the notifications are sent. Also, |auto_reset| must be still |
277 // valid when the notifications are sent, so that |Observe| skips the | 258 // valid when the notifications are sent, so that |Observe| skips the |
278 // notification. | 259 // notification. |
279 base::AutoReset<bool> auto_reset(&updating_preferences_, true); | 260 base::AutoReset<bool> auto_reset(&updating_preferences_, true); |
280 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); | 261 DictionaryPrefUpdate update(prefs_, pref_name_); |
281 base::AutoLock auto_lock(lock_); | 262 base::AutoLock auto_lock(lock_); |
282 | 263 |
283 const base::DictionaryValue* all_settings_dictionary = | 264 const base::DictionaryValue* all_settings_dictionary = |
284 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | 265 prefs_->GetDictionary(pref_name_); |
285 | 266 |
286 value_map_.clear(); | 267 value_map_.clear(); |
287 | 268 |
288 // Careful: The returned value could be NULL if the pref has never been set. | 269 // Careful: The returned value could be NULL if the pref has never been set. |
289 if (!all_settings_dictionary) | 270 if (!all_settings_dictionary) |
290 return; | 271 return; |
291 | 272 |
292 base::DictionaryValue* mutable_settings; | 273 base::DictionaryValue* mutable_settings; |
293 scoped_ptr<base::DictionaryValue> mutable_settings_scope; | 274 scoped_ptr<base::DictionaryValue> mutable_settings_scope; |
294 | 275 |
(...skipping 21 matching lines...) Expand all Loading... | |
316 LOG(ERROR) << "Invalid pattern strings: " << pattern_str; | 297 LOG(ERROR) << "Invalid pattern strings: " << pattern_str; |
317 continue; | 298 continue; |
318 } | 299 } |
319 | 300 |
320 // Get settings dictionary for the current pattern string, and read | 301 // Get settings dictionary for the current pattern string, and read |
321 // settings from the dictionary. | 302 // settings from the dictionary. |
322 const base::DictionaryValue* settings_dictionary = NULL; | 303 const base::DictionaryValue* settings_dictionary = NULL; |
323 bool is_dictionary = i.value().GetAsDictionary(&settings_dictionary); | 304 bool is_dictionary = i.value().GetAsDictionary(&settings_dictionary); |
324 DCHECK(is_dictionary); | 305 DCHECK(is_dictionary); |
325 | 306 |
326 for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 307 std::string res_dictionary_path; |
327 ContentSettingsType content_type = static_cast<ContentSettingsType>(i); | 308 if (GetResourceTypeName(content_type_, &res_dictionary_path)) { |
328 | 309 const base::DictionaryValue* resource_dictionary = NULL; |
329 std::string res_dictionary_path; | 310 if (settings_dictionary->GetDictionary( |
330 if (GetResourceTypeName(content_type, &res_dictionary_path)) { | 311 res_dictionary_path, &resource_dictionary)) { |
331 const base::DictionaryValue* resource_dictionary = NULL; | 312 for (base::DictionaryValue::Iterator j(*resource_dictionary); |
332 if (settings_dictionary->GetDictionary( | 313 !j.IsAtEnd(); |
333 res_dictionary_path, &resource_dictionary)) { | 314 j.Advance()) { |
334 for (base::DictionaryValue::Iterator j(*resource_dictionary); | 315 const std::string& resource_identifier(j.key()); |
335 !j.IsAtEnd(); | 316 int setting = CONTENT_SETTING_DEFAULT; |
336 j.Advance()) { | 317 bool is_integer = j.value().GetAsInteger(&setting); |
337 const std::string& resource_identifier(j.key()); | 318 DCHECK(is_integer); |
338 int setting = CONTENT_SETTING_DEFAULT; | |
339 bool is_integer = j.value().GetAsInteger(&setting); | |
340 DCHECK(is_integer); | |
341 DCHECK_NE(CONTENT_SETTING_DEFAULT, setting); | |
342 value_map_.SetValue(pattern_pair.first, | |
343 pattern_pair.second, | |
344 content_type, | |
345 resource_identifier, | |
346 new base::FundamentalValue(setting)); | |
347 } | |
348 } | |
349 } | |
350 base::Value* value = NULL; | |
351 if (HostContentSettingsMap::ContentTypeHasCompoundValue(content_type)) { | |
352 const base::DictionaryValue* setting = NULL; | |
353 // TODO(xians): Handle the non-dictionary types. | |
354 if (settings_dictionary->GetDictionaryWithoutPathExpansion( | |
355 GetTypeName(ContentSettingsType(i)), &setting)) { | |
356 DCHECK(!setting->empty()); | |
357 value = setting->DeepCopy(); | |
358 } | |
359 } else { | |
360 int setting = CONTENT_SETTING_DEFAULT; | |
361 if (settings_dictionary->GetIntegerWithoutPathExpansion( | |
362 GetTypeName(ContentSettingsType(i)), &setting)) { | |
363 DCHECK_NE(CONTENT_SETTING_DEFAULT, setting); | 319 DCHECK_NE(CONTENT_SETTING_DEFAULT, setting); |
364 setting = FixObsoleteCookiePromptMode(content_type, | 320 scoped_ptr<base::Value> setting_ptr = make_scoped_ptr( |
365 ContentSetting(setting)); | 321 new base::FundamentalValue(setting)); |
366 value = new base::FundamentalValue(setting); | 322 value_map_.SetValue(pattern_pair.first, |
367 } | 323 pattern_pair.second, |
368 } | 324 content_type_, |
369 | 325 resource_identifier, |
370 // |value_map_| will take the ownership of |value|. | 326 setting_ptr.get()); |
371 if (value != NULL) { | 327 if (IsContentSettingsTypeSyncable(content_type_)) |
372 value_map_.SetValue(pattern_pair.first, | 328 UpdateOldPref(pattern_pair.first, |
373 pattern_pair.second, | 329 pattern_pair.second, |
374 content_type, | 330 resource_identifier, |
375 ResourceIdentifier(), | 331 setting_ptr.get()); |
376 value); | |
377 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) { | |
378 ContentSetting s = ValueToContentSetting(value); | |
379 switch (s) { | |
380 case CONTENT_SETTING_ALLOW : | |
381 ++cookies_allow_exception_count; | |
382 break; | |
383 case CONTENT_SETTING_BLOCK : | |
384 ++cookies_block_exception_count; | |
385 break; | |
386 case CONTENT_SETTING_SESSION_ONLY : | |
387 ++cookies_session_only_exception_count; | |
388 break; | |
389 default: | |
390 NOTREACHED(); | |
391 break; | |
392 } | |
393 } | 332 } |
394 } | 333 } |
395 } | 334 } |
335 base::Value* value = NULL; | |
336 if (HostContentSettingsMap::ContentTypeHasCompoundValue(content_type_)) { | |
337 const base::DictionaryValue* setting = NULL; | |
338 // TODO(xians): Handle the non-dictionary types. | |
339 if (settings_dictionary->GetDictionaryWithoutPathExpansion( | |
340 kSettingPath, &setting)) { | |
341 DCHECK(!setting->empty()); | |
342 value = setting->DeepCopy(); | |
343 } | |
344 } else { | |
345 int setting = CONTENT_SETTING_DEFAULT; | |
346 if (settings_dictionary->GetIntegerWithoutPathExpansion( | |
347 kSettingPath, &setting)) { | |
348 DCHECK_NE(CONTENT_SETTING_DEFAULT, setting); | |
349 setting = FixObsoleteCookiePromptMode(content_type_, | |
350 ContentSetting(setting)); | |
351 value = new base::FundamentalValue(setting); | |
352 } | |
353 } | |
354 | |
355 // |value_map_| will take the ownership of |value|. | |
356 if (value != NULL) { | |
357 value_map_.SetValue(pattern_pair.first, | |
358 pattern_pair.second, | |
359 content_type_, | |
360 ResourceIdentifier(), | |
361 value); | |
362 if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES) { | |
363 ContentSetting s = ValueToContentSetting(value); | |
364 switch (s) { | |
365 case CONTENT_SETTING_ALLOW : | |
366 ++cookies_allow_exception_count; | |
367 break; | |
368 case CONTENT_SETTING_BLOCK : | |
369 ++cookies_block_exception_count; | |
370 break; | |
371 case CONTENT_SETTING_SESSION_ONLY : | |
372 ++cookies_session_only_exception_count; | |
373 break; | |
374 default: | |
375 NOTREACHED(); | |
376 break; | |
377 } | |
378 } | |
379 } | |
380 | |
396 } | 381 } |
397 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfBlockCookiesExceptions", | 382 |
398 cookies_block_exception_count); | 383 if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES) { |
399 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfAllowCookiesExceptions", | 384 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfBlockCookiesExceptions", |
400 cookies_allow_exception_count); | 385 cookies_block_exception_count); |
401 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfSessionOnlyCookiesExceptions", | 386 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfAllowCookiesExceptions", |
402 cookies_session_only_exception_count); | 387 cookies_allow_exception_count); |
388 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfSessionOnlyCookiesExceptions", | |
389 cookies_session_only_exception_count); | |
390 } | |
403 } | 391 } |
404 | 392 |
405 void ContentSettingsPref::OnContentSettingsPatternPairsChanged() { | 393 void ContentSettingsPref::OnPrefChanged() { |
406 DCHECK(thread_checker_.CalledOnValidThread()); | 394 DCHECK(thread_checker_.CalledOnValidThread()); |
407 | 395 |
408 if (updating_preferences_) | 396 if (updating_preferences_) |
409 return; | 397 return; |
410 | 398 |
411 ReadContentSettingsFromPref(); | 399 ReadContentSettingsFromPref(); |
412 | 400 |
413 notify_callback_.Run(ContentSettingsPattern(), | 401 notify_callback_.Run(ContentSettingsPattern(), |
414 ContentSettingsPattern(), | 402 ContentSettingsPattern(), |
415 CONTENT_SETTINGS_TYPE_DEFAULT, | 403 content_type_, |
416 std::string()); | 404 std::string()); |
417 } | 405 } |
418 | 406 |
419 void ContentSettingsPref::UpdatePref( | 407 void ContentSettingsPref::UpdatePref( |
420 const ContentSettingsPattern& primary_pattern, | 408 const ContentSettingsPattern& primary_pattern, |
421 const ContentSettingsPattern& secondary_pattern, | 409 const ContentSettingsPattern& secondary_pattern, |
422 ContentSettingsType content_type, | |
423 const ResourceIdentifier& resource_identifier, | 410 const ResourceIdentifier& resource_identifier, |
424 const base::Value* value) { | 411 const base::Value* value) { |
425 // Ensure that |lock_| is not held by this thread, since this function will | 412 // Ensure that |lock_| is not held by this thread, since this function will |
426 // send out notifications (by |~DictionaryPrefUpdate|). | 413 // send out notifications (by |~DictionaryPrefUpdate|). |
427 AssertLockNotHeld(); | 414 AssertLockNotHeld(); |
428 | 415 |
429 base::AutoReset<bool> auto_reset(&updating_preferences_, true); | 416 base::AutoReset<bool> auto_reset(&updating_preferences_, true); |
430 { | 417 { |
418 DictionaryPrefUpdate update(prefs_, pref_name_); | |
419 base::DictionaryValue* pattern_pairs_settings = update.Get(); | |
420 | |
421 // Get settings dictionary for the given patterns. | |
422 std::string pattern_str(CreatePatternString(primary_pattern, | |
423 secondary_pattern)); | |
424 base::DictionaryValue* settings_dictionary = NULL; | |
425 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | |
426 pattern_str, &settings_dictionary); | |
427 | |
428 if (!found && value) { | |
429 settings_dictionary = new base::DictionaryValue; | |
430 pattern_pairs_settings->SetWithoutPathExpansion( | |
431 pattern_str, settings_dictionary); | |
432 } | |
433 | |
434 if (settings_dictionary) { | |
435 std::string res_dictionary_path; | |
436 if (GetResourceTypeName(content_type_, &res_dictionary_path) && | |
437 !resource_identifier.empty()) { | |
438 base::DictionaryValue* resource_dictionary = NULL; | |
439 found = settings_dictionary->GetDictionary( | |
440 res_dictionary_path, &resource_dictionary); | |
441 if (!found) { | |
442 if (value == NULL) | |
443 return; // Nothing to remove. Exit early. | |
444 resource_dictionary = new base::DictionaryValue; | |
445 settings_dictionary->Set(res_dictionary_path, resource_dictionary); | |
446 } | |
447 // Update resource dictionary. | |
448 if (value == NULL) { | |
449 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, | |
450 NULL); | |
451 if (resource_dictionary->empty()) { | |
452 settings_dictionary->RemoveWithoutPathExpansion( | |
453 res_dictionary_path, NULL); | |
454 } | |
455 } else { | |
456 resource_dictionary->SetWithoutPathExpansion( | |
457 resource_identifier, value->DeepCopy()); | |
458 } | |
459 } else { | |
460 // Update settings dictionary. | |
461 if (value == NULL) { | |
462 settings_dictionary->RemoveWithoutPathExpansion(kSettingPath, NULL); | |
463 settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL); | |
464 } else { | |
465 settings_dictionary->SetWithoutPathExpansion( | |
466 kSettingPath, value->DeepCopy()); | |
467 } | |
468 } | |
469 // Remove the settings dictionary if it is empty. | |
470 if (settings_dictionary->empty()) { | |
471 pattern_pairs_settings->RemoveWithoutPathExpansion( | |
472 pattern_str, NULL); | |
473 } | |
474 } | |
475 } | |
476 } | |
477 | |
478 void ContentSettingsPref::UpdateOldPref( | |
479 const ContentSettingsPattern& primary_pattern, | |
480 const ContentSettingsPattern& secondary_pattern, | |
481 const ResourceIdentifier& resource_identifier, | |
482 const base::Value* value) { | |
483 DCHECK(IsContentSettingsTypeSyncable(content_type_)); | |
484 | |
485 if (*updating_old_preferences_) | |
486 return; | |
487 | |
488 base::AutoReset<bool> auto_reset(updating_old_preferences_, true); | |
489 { | |
431 DictionaryPrefUpdate update(prefs_, | 490 DictionaryPrefUpdate update(prefs_, |
432 prefs::kContentSettingsPatternPairs); | 491 prefs::kContentSettingsPatternPairs); |
433 base::DictionaryValue* pattern_pairs_settings = update.Get(); | 492 base::DictionaryValue* pattern_pairs_settings = update.Get(); |
434 | 493 |
435 // Get settings dictionary for the given patterns. | 494 // Get settings dictionary for the given patterns. |
436 std::string pattern_str(CreatePatternString(primary_pattern, | 495 std::string pattern_str(CreatePatternString(primary_pattern, |
437 secondary_pattern)); | 496 secondary_pattern)); |
438 base::DictionaryValue* settings_dictionary = NULL; | 497 base::DictionaryValue* settings_dictionary = NULL; |
439 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | 498 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( |
440 pattern_str, &settings_dictionary); | 499 pattern_str, &settings_dictionary); |
441 | 500 |
442 if (!found && value) { | 501 if (!found && value) { |
443 settings_dictionary = new base::DictionaryValue; | 502 settings_dictionary = new base::DictionaryValue; |
444 pattern_pairs_settings->SetWithoutPathExpansion( | 503 pattern_pairs_settings->SetWithoutPathExpansion( |
445 pattern_str, settings_dictionary); | 504 pattern_str, settings_dictionary); |
446 } | 505 } |
447 | 506 |
448 if (settings_dictionary) { | 507 if (settings_dictionary) { |
449 std::string res_dictionary_path; | 508 if (content_type_ == CONTENT_SETTINGS_TYPE_PLUGINS && |
450 if (GetResourceTypeName(content_type, &res_dictionary_path) && | |
451 !resource_identifier.empty()) { | 509 !resource_identifier.empty()) { |
510 std::string res_dictionary_path = kPerResourceIdentifierPrefName; | |
452 base::DictionaryValue* resource_dictionary = NULL; | 511 base::DictionaryValue* resource_dictionary = NULL; |
453 found = settings_dictionary->GetDictionary( | 512 found = settings_dictionary->GetDictionary( |
454 res_dictionary_path, &resource_dictionary); | 513 res_dictionary_path, &resource_dictionary); |
455 if (!found) { | 514 if (!found) { |
456 if (value == NULL) | 515 if (value == NULL) |
457 return; // Nothing to remove. Exit early. | 516 return; // Nothing to remove. Exit early. |
458 resource_dictionary = new base::DictionaryValue; | 517 resource_dictionary = new base::DictionaryValue; |
459 settings_dictionary->Set(res_dictionary_path, resource_dictionary); | 518 settings_dictionary->Set(res_dictionary_path, resource_dictionary); |
460 } | 519 } |
461 // Update resource dictionary. | 520 // Update resource dictionary. |
462 if (value == NULL) { | 521 if (value == NULL) { |
463 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, | 522 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, |
464 NULL); | 523 NULL); |
465 if (resource_dictionary->empty()) { | 524 if (resource_dictionary->empty()) { |
466 settings_dictionary->RemoveWithoutPathExpansion( | 525 settings_dictionary->RemoveWithoutPathExpansion( |
467 res_dictionary_path, NULL); | 526 res_dictionary_path, NULL); |
468 } | 527 } |
469 } else { | 528 } else { |
470 resource_dictionary->SetWithoutPathExpansion( | 529 resource_dictionary->SetWithoutPathExpansion( |
471 resource_identifier, value->DeepCopy()); | 530 resource_identifier, value->DeepCopy()); |
472 } | 531 } |
473 } else { | 532 } else { |
474 // Update settings dictionary. | 533 // Update settings dictionary. |
475 std::string setting_path = GetTypeName(content_type); | 534 std::string setting_path = GetTypeName(content_type_); |
476 if (value == NULL) { | 535 if (value == NULL) { |
477 settings_dictionary->RemoveWithoutPathExpansion(setting_path, | 536 settings_dictionary->RemoveWithoutPathExpansion(setting_path, |
478 NULL); | 537 NULL); |
479 settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL); | 538 settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL); |
480 } else { | 539 } else { |
481 settings_dictionary->SetWithoutPathExpansion( | 540 settings_dictionary->SetWithoutPathExpansion( |
482 setting_path, value->DeepCopy()); | 541 setting_path, value->DeepCopy()); |
483 } | 542 } |
484 } | 543 } |
485 // Remove the settings dictionary if it is empty. | 544 // Remove the settings dictionary if it is empty. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
549 | 608 |
550 void ContentSettingsPref::AssertLockNotHeld() const { | 609 void ContentSettingsPref::AssertLockNotHeld() const { |
551 #if !defined(NDEBUG) | 610 #if !defined(NDEBUG) |
552 // |Lock::Acquire()| will assert if the lock is held by this thread. | 611 // |Lock::Acquire()| will assert if the lock is held by this thread. |
553 lock_.Acquire(); | 612 lock_.Acquire(); |
554 lock_.Release(); | 613 lock_.Release(); |
555 #endif | 614 #endif |
556 } | 615 } |
557 | 616 |
558 } // namespace content_settings | 617 } // namespace content_settings |
OLD | NEW |