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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
13 #include "components/content_settings/core/browser/content_settings_info.h" | 14 #include "components/content_settings/core/browser/content_settings_info.h" |
14 #include "components/content_settings/core/browser/content_settings_registry.h" | 15 #include "components/content_settings/core/browser/content_settings_registry.h" |
15 #include "components/content_settings/core/browser/content_settings_rule.h" | 16 #include "components/content_settings/core/browser/content_settings_rule.h" |
16 #include "components/content_settings/core/browser/content_settings_utils.h" | 17 #include "components/content_settings/core/browser/content_settings_utils.h" |
17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 18 #include "components/content_settings/core/browser/host_content_settings_map.h" |
18 #include "components/content_settings/core/common/content_settings.h" | 19 #include "components/content_settings/core/common/content_settings.h" |
19 #include "components/content_settings/core/common/content_settings_pattern.h" | 20 #include "components/content_settings/core/common/content_settings_pattern.h" |
20 #include "components/content_settings/core/common/pref_names.h" | 21 #include "components/content_settings/core/common/pref_names.h" |
21 #include "components/prefs/scoped_user_pref_update.h" | 22 #include "components/prefs/scoped_user_pref_update.h" |
22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 const char kSettingPath[] = "setting"; | 27 const char kSettingPath[] = "setting"; |
28 const char kLastModifiedPath[] = "last_modified"; | |
27 const char kPerResourceIdentifierPrefName[] = "per_resource"; | 29 const char kPerResourceIdentifierPrefName[] = "per_resource"; |
28 | 30 |
29 // If the given content type supports resource identifiers in user preferences, | 31 // If the given content type supports resource identifiers in user preferences, |
30 // returns true and sets |pref_key| to the key in the content settings | 32 // returns true and sets |pref_key| to the key in the content settings |
31 // dictionary under which per-resource content settings are stored. | 33 // dictionary under which per-resource content settings are stored. |
32 // Otherwise, returns false. | 34 // Otherwise, returns false. |
33 bool SupportsResourceIdentifiers(ContentSettingsType content_type) { | 35 bool SupportsResourceIdentifiers(ContentSettingsType content_type) { |
34 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS; | 36 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS; |
35 } | 37 } |
36 | 38 |
(...skipping 17 matching lines...) Expand all Loading... | |
54 } // namespace | 56 } // namespace |
55 | 57 |
56 namespace content_settings { | 58 namespace content_settings { |
57 | 59 |
58 ContentSettingsPref::ContentSettingsPref( | 60 ContentSettingsPref::ContentSettingsPref( |
59 ContentSettingsType content_type, | 61 ContentSettingsType content_type, |
60 PrefService* prefs, | 62 PrefService* prefs, |
61 PrefChangeRegistrar* registrar, | 63 PrefChangeRegistrar* registrar, |
62 const std::string& pref_name, | 64 const std::string& pref_name, |
63 bool incognito, | 65 bool incognito, |
66 bool store_last_modified, | |
64 NotifyObserversCallback notify_callback) | 67 NotifyObserversCallback notify_callback) |
65 : content_type_(content_type), | 68 : content_type_(content_type), |
66 prefs_(prefs), | 69 prefs_(prefs), |
67 registrar_(registrar), | 70 registrar_(registrar), |
68 pref_name_(pref_name), | 71 pref_name_(pref_name), |
69 is_incognito_(incognito), | 72 is_incognito_(incognito), |
73 store_last_modified_(store_last_modified), | |
70 updating_preferences_(false), | 74 updating_preferences_(false), |
71 notify_callback_(notify_callback) { | 75 notify_callback_(notify_callback) { |
72 DCHECK(prefs_); | 76 DCHECK(prefs_); |
73 | 77 |
74 ReadContentSettingsFromPref(); | 78 ReadContentSettingsFromPref(); |
75 | 79 |
76 registrar_->Add( | 80 registrar_->Add( |
77 pref_name_, | 81 pref_name_, |
78 base::Bind(&ContentSettingsPref::OnPrefChanged, base::Unretained(this))); | 82 base::Bind(&ContentSettingsPref::OnPrefChanged, base::Unretained(this))); |
79 } | 83 } |
(...skipping 24 matching lines...) Expand all Loading... | |
104 !resource_identifier.empty()); | 108 !resource_identifier.empty()); |
105 | 109 |
106 // At this point take the ownership of the |in_value|. | 110 // At this point take the ownership of the |in_value|. |
107 std::unique_ptr<base::Value> value(in_value); | 111 std::unique_ptr<base::Value> value(in_value); |
108 | 112 |
109 // Update in memory value map. | 113 // Update in memory value map. |
110 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; | 114 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
111 if (!is_incognito_) | 115 if (!is_incognito_) |
112 map_to_modify = &value_map_; | 116 map_to_modify = &value_map_; |
113 | 117 |
118 base::Time modified_time = | |
119 store_last_modified_ ? base::Time::Now() : base::Time(); | |
120 | |
114 { | 121 { |
115 base::AutoLock auto_lock(lock_); | 122 base::AutoLock auto_lock(lock_); |
116 if (value.get()) { | 123 if (value.get()) { |
117 map_to_modify->SetValue( | 124 map_to_modify->SetValue(primary_pattern, secondary_pattern, content_type_, |
118 primary_pattern, | 125 resource_identifier, modified_time, |
119 secondary_pattern, | 126 value->DeepCopy()); |
120 content_type_, | |
121 resource_identifier, | |
122 value->DeepCopy()); | |
123 } else { | 127 } else { |
124 map_to_modify->DeleteValue( | 128 map_to_modify->DeleteValue( |
125 primary_pattern, | 129 primary_pattern, |
126 secondary_pattern, | 130 secondary_pattern, |
127 content_type_, | 131 content_type_, |
128 resource_identifier); | 132 resource_identifier); |
129 } | 133 } |
130 } | 134 } |
131 // Update the content settings preference. | 135 // Update the content settings preference. |
132 if (!is_incognito_) { | 136 if (!is_incognito_) { |
133 UpdatePref(primary_pattern, | 137 UpdatePref(primary_pattern, secondary_pattern, resource_identifier, |
134 secondary_pattern, | 138 modified_time, value.get()); |
135 resource_identifier, | |
136 value.get()); | |
137 } | 139 } |
138 | 140 |
139 notify_callback_.Run( | 141 notify_callback_.Run( |
140 primary_pattern, secondary_pattern, content_type_, resource_identifier); | 142 primary_pattern, secondary_pattern, content_type_, resource_identifier); |
141 | 143 |
142 return true; | 144 return true; |
143 } | 145 } |
144 | 146 |
145 void ContentSettingsPref::ClearPref() { | 147 void ContentSettingsPref::ClearPref() { |
146 DCHECK(thread_checker_.CalledOnValidThread()); | 148 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 LOG(ERROR) << "Invalid pattern strings: " << pattern_str; | 235 LOG(ERROR) << "Invalid pattern strings: " << pattern_str; |
234 continue; | 236 continue; |
235 } | 237 } |
236 | 238 |
237 // Get settings dictionary for the current pattern string, and read | 239 // Get settings dictionary for the current pattern string, and read |
238 // settings from the dictionary. | 240 // settings from the dictionary. |
239 const base::DictionaryValue* settings_dictionary = NULL; | 241 const base::DictionaryValue* settings_dictionary = NULL; |
240 bool is_dictionary = i.value().GetAsDictionary(&settings_dictionary); | 242 bool is_dictionary = i.value().GetAsDictionary(&settings_dictionary); |
241 DCHECK(is_dictionary); | 243 DCHECK(is_dictionary); |
242 | 244 |
245 // Get last modified timestamp. | |
msramek
2017/04/19 10:49:16
We're using one last modified timestamp per patter
dullweber
2017/04/19 15:02:45
I just changed it to store a timestamp per resourc
raymes
2017/04/20 00:10:13
I think we'd like to remove the per-resource ident
| |
246 std::string time_stamp_str; | |
247 settings_dictionary->GetStringWithoutPathExpansion(kLastModifiedPath, | |
248 &time_stamp_str); | |
249 int64_t time_stamp = 0; | |
250 base::StringToInt64(time_stamp_str, &time_stamp); | |
251 base::Time last_modified = base::Time::FromInternalValue(time_stamp); | |
252 | |
243 if (SupportsResourceIdentifiers(content_type_)) { | 253 if (SupportsResourceIdentifiers(content_type_)) { |
244 const base::DictionaryValue* resource_dictionary = NULL; | 254 const base::DictionaryValue* resource_dictionary = NULL; |
245 if (settings_dictionary->GetDictionary( | 255 if (settings_dictionary->GetDictionary( |
246 kPerResourceIdentifierPrefName, &resource_dictionary)) { | 256 kPerResourceIdentifierPrefName, &resource_dictionary)) { |
247 for (base::DictionaryValue::Iterator j(*resource_dictionary); | 257 for (base::DictionaryValue::Iterator j(*resource_dictionary); |
248 !j.IsAtEnd(); | 258 !j.IsAtEnd(); |
249 j.Advance()) { | 259 j.Advance()) { |
250 const std::string& resource_identifier(j.key()); | 260 const std::string& resource_identifier(j.key()); |
251 int setting = CONTENT_SETTING_DEFAULT; | 261 int setting = CONTENT_SETTING_DEFAULT; |
252 bool is_integer = j.value().GetAsInteger(&setting); | 262 bool is_integer = j.value().GetAsInteger(&setting); |
253 DCHECK(is_integer); | 263 DCHECK(is_integer); |
254 DCHECK_NE(CONTENT_SETTING_DEFAULT, setting); | 264 DCHECK_NE(CONTENT_SETTING_DEFAULT, setting); |
255 std::unique_ptr<base::Value> setting_ptr(new base::Value(setting)); | 265 std::unique_ptr<base::Value> setting_ptr(new base::Value(setting)); |
256 value_map_.SetValue(pattern_pair.first, | 266 value_map_.SetValue(pattern_pair.first, pattern_pair.second, |
257 pattern_pair.second, | 267 content_type_, resource_identifier, last_modified, |
258 content_type_, | |
259 resource_identifier, | |
260 setting_ptr->DeepCopy()); | 268 setting_ptr->DeepCopy()); |
261 } | 269 } |
262 } | 270 } |
263 } | 271 } |
264 | 272 |
265 const base::Value* value = nullptr; | 273 const base::Value* value = nullptr; |
266 settings_dictionary->GetWithoutPathExpansion(kSettingPath, &value); | 274 settings_dictionary->GetWithoutPathExpansion(kSettingPath, &value); |
267 | |
268 if (value) { | 275 if (value) { |
269 DCHECK(IsValueAllowedForType(value, content_type_)); | 276 DCHECK(IsValueAllowedForType(value, content_type_)); |
270 value_map_.SetValue(pattern_pair.first, | 277 value_map_.SetValue(pattern_pair.first, pattern_pair.second, |
271 pattern_pair.second, | 278 content_type_, ResourceIdentifier(), last_modified, |
272 content_type_, | |
273 ResourceIdentifier(), | |
274 value->DeepCopy()); | 279 value->DeepCopy()); |
275 if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES) { | 280 if (content_type_ == CONTENT_SETTINGS_TYPE_COOKIES) { |
276 ContentSetting s = ValueToContentSetting(value); | 281 ContentSetting s = ValueToContentSetting(value); |
277 switch (s) { | 282 switch (s) { |
278 case CONTENT_SETTING_ALLOW : | 283 case CONTENT_SETTING_ALLOW : |
279 ++cookies_allow_exception_count; | 284 ++cookies_allow_exception_count; |
280 break; | 285 break; |
281 case CONTENT_SETTING_BLOCK : | 286 case CONTENT_SETTING_BLOCK : |
282 ++cookies_block_exception_count; | 287 ++cookies_block_exception_count; |
283 break; | 288 break; |
(...skipping 30 matching lines...) Expand all Loading... | |
314 notify_callback_.Run(ContentSettingsPattern(), | 319 notify_callback_.Run(ContentSettingsPattern(), |
315 ContentSettingsPattern(), | 320 ContentSettingsPattern(), |
316 content_type_, | 321 content_type_, |
317 ResourceIdentifier()); | 322 ResourceIdentifier()); |
318 } | 323 } |
319 | 324 |
320 void ContentSettingsPref::UpdatePref( | 325 void ContentSettingsPref::UpdatePref( |
321 const ContentSettingsPattern& primary_pattern, | 326 const ContentSettingsPattern& primary_pattern, |
322 const ContentSettingsPattern& secondary_pattern, | 327 const ContentSettingsPattern& secondary_pattern, |
323 const ResourceIdentifier& resource_identifier, | 328 const ResourceIdentifier& resource_identifier, |
329 const base::Time last_modified, | |
324 const base::Value* value) { | 330 const base::Value* value) { |
325 // Ensure that |lock_| is not held by this thread, since this function will | 331 // Ensure that |lock_| is not held by this thread, since this function will |
326 // send out notifications (by |~DictionaryPrefUpdate|). | 332 // send out notifications (by |~DictionaryPrefUpdate|). |
327 AssertLockNotHeld(); | 333 AssertLockNotHeld(); |
328 | 334 |
329 base::AutoReset<bool> auto_reset(&updating_preferences_, true); | 335 base::AutoReset<bool> auto_reset(&updating_preferences_, true); |
330 { | 336 { |
331 DictionaryPrefUpdate update(prefs_, pref_name_); | 337 DictionaryPrefUpdate update(prefs_, pref_name_); |
332 base::DictionaryValue* pattern_pairs_settings = update.Get(); | 338 base::DictionaryValue* pattern_pairs_settings = update.Get(); |
333 | 339 |
(...skipping 23 matching lines...) Expand all Loading... | |
357 settings_dictionary->Set( | 363 settings_dictionary->Set( |
358 kPerResourceIdentifierPrefName, resource_dictionary); | 364 kPerResourceIdentifierPrefName, resource_dictionary); |
359 } | 365 } |
360 // Update resource dictionary. | 366 // Update resource dictionary. |
361 if (value == NULL) { | 367 if (value == NULL) { |
362 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, | 368 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, |
363 NULL); | 369 NULL); |
364 if (resource_dictionary->empty()) { | 370 if (resource_dictionary->empty()) { |
365 settings_dictionary->RemoveWithoutPathExpansion( | 371 settings_dictionary->RemoveWithoutPathExpansion( |
366 kPerResourceIdentifierPrefName, NULL); | 372 kPerResourceIdentifierPrefName, NULL); |
373 settings_dictionary->RemoveWithoutPathExpansion(kLastModifiedPath, | |
374 NULL); | |
367 } | 375 } |
368 } else { | 376 } else { |
369 resource_dictionary->SetWithoutPathExpansion( | 377 resource_dictionary->SetWithoutPathExpansion( |
370 resource_identifier, value->DeepCopy()); | 378 resource_identifier, value->DeepCopy()); |
379 // Update timestamp for whole resource dictionary. | |
380 settings_dictionary->SetStringWithoutPathExpansion( | |
381 kLastModifiedPath, | |
382 base::Int64ToString(last_modified.ToInternalValue())); | |
371 } | 383 } |
372 } else { | 384 } else { |
373 // Update settings dictionary. | 385 // Update settings dictionary. |
374 if (value == NULL) { | 386 if (value == NULL) { |
375 settings_dictionary->RemoveWithoutPathExpansion(kSettingPath, NULL); | 387 settings_dictionary->RemoveWithoutPathExpansion(kSettingPath, NULL); |
388 settings_dictionary->RemoveWithoutPathExpansion(kLastModifiedPath, | |
389 NULL); | |
376 } else { | 390 } else { |
377 settings_dictionary->SetWithoutPathExpansion( | 391 settings_dictionary->SetWithoutPathExpansion( |
378 kSettingPath, value->DeepCopy()); | 392 kSettingPath, value->DeepCopy()); |
393 settings_dictionary->SetStringWithoutPathExpansion( | |
394 kLastModifiedPath, | |
395 base::Int64ToString(last_modified.ToInternalValue())); | |
379 } | 396 } |
380 } | 397 } |
381 // Remove the settings dictionary if it is empty. | 398 // Remove the settings dictionary if it is empty. |
382 if (settings_dictionary->empty()) { | 399 if (settings_dictionary->empty()) { |
383 pattern_pairs_settings->RemoveWithoutPathExpansion( | 400 pattern_pairs_settings->RemoveWithoutPathExpansion( |
384 pattern_str, NULL); | 401 pattern_str, NULL); |
385 } | 402 } |
386 } | 403 } |
387 } | 404 } |
388 } | 405 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
445 | 462 |
446 void ContentSettingsPref::AssertLockNotHeld() const { | 463 void ContentSettingsPref::AssertLockNotHeld() const { |
447 #if !defined(NDEBUG) | 464 #if !defined(NDEBUG) |
448 // |Lock::Acquire()| will assert if the lock is held by this thread. | 465 // |Lock::Acquire()| will assert if the lock is held by this thread. |
449 lock_.Acquire(); | 466 lock_.Acquire(); |
450 lock_.Release(); | 467 lock_.Release(); |
451 #endif | 468 #endif |
452 } | 469 } |
453 | 470 |
454 } // namespace content_settings | 471 } // namespace content_settings |
OLD | NEW |