Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: chrome/browser/content_settings/content_settings_pref_provider.cc

Issue 9826020: Remove code to keep obsolete content settings patterns pref in sync. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/content_settings/content_settings_pref_provider.h" 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
252 252
253 if (type == chrome::NOTIFICATION_PREF_CHANGED) { 253 if (type == chrome::NOTIFICATION_PREF_CHANGED) {
254 DCHECK_EQ(prefs_, content::Source<PrefService>(source).ptr()); 254 DCHECK_EQ(prefs_, content::Source<PrefService>(source).ptr());
255 if (updating_preferences_) 255 if (updating_preferences_)
256 return; 256 return;
257 257
258 if (!is_incognito_) { 258 if (!is_incognito_) {
259 AutoReset<bool> auto_reset(&updating_preferences_, true); 259 AutoReset<bool> auto_reset(&updating_preferences_, true);
260 std::string* name = content::Details<std::string>(details).ptr(); 260 std::string* name = content::Details<std::string>(details).ptr();
261 if (*name == prefs::kContentSettingsPatternPairs) { 261 if (*name == prefs::kContentSettingsPatternPairs) {
Bernhard Bauer 2012/03/23 11:40:15 If there's no need to do anything special, you can
markusheintz_ 2012/03/23 11:56:51 I moved the inverted condition to the final else.
262 SyncObsoletePatternPref(); 262 // No need to do anything special.
263 SyncObsoletePrefs();
264 } else if (*name == prefs::kContentSettingsPatterns) { 263 } else if (*name == prefs::kContentSettingsPatterns) {
265 MigrateObsoleteContentSettingsPatternPref(); 264 MigrateObsoleteContentSettingsPatternPref();
266 } else if (*name == prefs::kGeolocationContentSettings) { 265 } else if (*name == prefs::kGeolocationContentSettings) {
267 MigrateObsoleteGeolocationPref(); 266 MigrateObsoleteGeolocationPref();
268 } else if (*name == prefs::kDesktopNotificationAllowedOrigins || 267 } else if (*name == prefs::kDesktopNotificationAllowedOrigins ||
269 *name == prefs::kDesktopNotificationDeniedOrigins) { 268 *name == prefs::kDesktopNotificationDeniedOrigins) {
270 MigrateObsoleteNotificationsPrefs(); 269 MigrateObsoleteNotificationsPrefs();
271 } else { 270 } else {
272 NOTREACHED() << "Unexpected preference observed"; 271 NOTREACHED() << "Unexpected preference observed";
273 return; 272 return;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 DictionaryPrefUpdate update(prefs_, 316 DictionaryPrefUpdate update(prefs_,
318 prefs::kContentSettingsPatternPairs); 317 prefs::kContentSettingsPatternPairs);
319 DictionaryValue* pattern_pairs_settings = update.Get(); 318 DictionaryValue* pattern_pairs_settings = update.Get();
320 UpdatePatternPairsSettings(primary_pattern, 319 UpdatePatternPairsSettings(primary_pattern,
321 secondary_pattern, 320 secondary_pattern,
322 content_type, 321 content_type,
323 resource_identifier, 322 resource_identifier,
324 value, 323 value,
325 pattern_pairs_settings); 324 pattern_pairs_settings);
326 } 325 }
327 if (content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION &&
328 content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
329 UpdateObsoletePatternsPref(primary_pattern,
330 secondary_pattern,
331 content_type,
332 resource_identifier,
333 ValueToContentSetting(value));
334 } else if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) {
335 UpdateObsoleteGeolocationPref(
336 primary_pattern,
337 secondary_pattern,
338 ValueToContentSetting(value));
339 } else if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
340 ListPrefUpdate update_allowed_sites(
341 prefs_, prefs::kDesktopNotificationAllowedOrigins);
342 ListPrefUpdate update_denied_sites(
343 prefs_, prefs::kDesktopNotificationDeniedOrigins);
344 UpdateObsoleteNotificationsSettings(primary_pattern,
345 secondary_pattern,
346 ValueToContentSetting(value),
347 update_allowed_sites.Get(),
348 update_denied_sites.Get());
349 }
350 } 326 }
351 327
352 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { 328 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) {
353 // |DictionaryPrefUpdate| sends out notifications when destructed. This 329 // |DictionaryPrefUpdate| sends out notifications when destructed. This
354 // construction order ensures |AutoLock| gets destroyed first and |lock_| is 330 // construction order ensures |AutoLock| gets destroyed first and |lock_| is
355 // not held when the notifications are sent. Also, |auto_reset| must be still 331 // not held when the notifications are sent. Also, |auto_reset| must be still
356 // valid when the notifications are sent, so that |Observe| skips the 332 // valid when the notifications are sent, so that |Observe| skips the
357 // notification. 333 // notification.
358 AutoReset<bool> auto_reset(&updating_preferences_, true); 334 AutoReset<bool> auto_reset(&updating_preferences_, true);
359 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); 335 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 value_map_.SetValue(pattern_pair.first, 410 value_map_.SetValue(pattern_pair.first,
435 pattern_pair.second, 411 pattern_pair.second,
436 content_type, 412 content_type,
437 ResourceIdentifier(""), 413 ResourceIdentifier(""),
438 Value::CreateIntegerValue(setting)); 414 Value::CreateIntegerValue(setting));
439 } 415 }
440 } 416 }
441 } 417 }
442 } 418 }
443 419
444 void PrefProvider::UpdateObsoletePatternsPref(
445 const ContentSettingsPattern& primary_pattern,
446 const ContentSettingsPattern& secondary_pattern,
447 ContentSettingsType content_type,
448 const ResourceIdentifier& resource_identifier,
449 ContentSetting setting) {
450 // Ensure that |lock_| is not held by this thread, since this function will
451 // send out notifications (by |~DictionaryPrefUpdate|).
452 AssertLockNotHeld();
453
454 DictionaryPrefUpdate update(prefs_,
455 prefs::kContentSettingsPatterns);
456 DictionaryValue* all_settings_dictionary = update.Get();
457
458 // Get settings dictionary for |primary_pattern|.
459 std::string pattern_str(primary_pattern.ToString());
460 DictionaryValue* settings_dictionary = NULL;
461 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
462 pattern_str, &settings_dictionary);
463
464 if (!found && (setting != CONTENT_SETTING_DEFAULT)) {
465 settings_dictionary = new DictionaryValue;
466 all_settings_dictionary->SetWithoutPathExpansion(
467 pattern_str, settings_dictionary);
468 }
469
470 if (settings_dictionary) {
471 std::string res_dictionary_path;
472 if (GetResourceTypeName(content_type, &res_dictionary_path) &&
473 !resource_identifier.empty()) {
474 DictionaryValue* resource_dictionary = NULL;
475 found = settings_dictionary->GetDictionary(
476 res_dictionary_path, &resource_dictionary);
477 if (!found) {
478 if (setting == CONTENT_SETTING_DEFAULT)
479 return; // Nothing to remove. Exit early.
480 resource_dictionary = new DictionaryValue;
481 settings_dictionary->Set(res_dictionary_path, resource_dictionary);
482 }
483 // Update resource dictionary.
484 if (setting == CONTENT_SETTING_DEFAULT) {
485 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier,
486 NULL);
487 if (resource_dictionary->empty()) {
488 settings_dictionary->RemoveWithoutPathExpansion(
489 res_dictionary_path, NULL);
490 }
491 } else {
492 resource_dictionary->SetWithoutPathExpansion(
493 resource_identifier, Value::CreateIntegerValue(setting));
494 }
495 } else {
496 // Update settings dictionary.
497 std::string setting_path = GetTypeName(content_type);
498 if (setting == CONTENT_SETTING_DEFAULT) {
499 settings_dictionary->RemoveWithoutPathExpansion(setting_path,
500 NULL);
501 } else {
502 settings_dictionary->SetWithoutPathExpansion(
503 setting_path, Value::CreateIntegerValue(setting));
504 }
505 }
506 // Remove the settings dictionary if it is empty.
507 if (settings_dictionary->empty()) {
508 all_settings_dictionary->RemoveWithoutPathExpansion(
509 pattern_str, NULL);
510 }
511 }
512 }
513
514 void PrefProvider::UpdatePatternPairsSettings( 420 void PrefProvider::UpdatePatternPairsSettings(
515 const ContentSettingsPattern& primary_pattern, 421 const ContentSettingsPattern& primary_pattern,
516 const ContentSettingsPattern& secondary_pattern, 422 const ContentSettingsPattern& secondary_pattern,
517 ContentSettingsType content_type, 423 ContentSettingsType content_type,
518 const ResourceIdentifier& resource_identifier, 424 const ResourceIdentifier& resource_identifier,
519 const base::Value* value, 425 const base::Value* value,
520 DictionaryValue* pattern_pairs_settings) { 426 DictionaryValue* pattern_pairs_settings) {
521 // Get settings dictionary for the given patterns. 427 // Get settings dictionary for the given patterns.
522 std::string pattern_str(CreatePatternString(primary_pattern, 428 std::string pattern_str(CreatePatternString(primary_pattern,
523 secondary_pattern)); 429 secondary_pattern));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 } 474 }
569 } 475 }
570 // Remove the settings dictionary if it is empty. 476 // Remove the settings dictionary if it is empty.
571 if (settings_dictionary->empty()) { 477 if (settings_dictionary->empty()) {
572 pattern_pairs_settings->RemoveWithoutPathExpansion( 478 pattern_pairs_settings->RemoveWithoutPathExpansion(
573 pattern_str, NULL); 479 pattern_str, NULL);
574 } 480 }
575 } 481 }
576 } 482 }
577 483
578 void PrefProvider::UpdateObsoleteGeolocationPref(
579 const ContentSettingsPattern& primary_pattern,
580 const ContentSettingsPattern& secondary_pattern,
581 ContentSetting setting) {
582 // Ensure that |lock_| is not held by this thread, since this function will
583 // send out notifications (by |~DictionaryPrefUpdate|).
584 AssertLockNotHeld();
585
586 if (!prefs_)
587 return;
588
589 // Ignore settings with wildcard patterns as they are not supported by the
590 // obsolete preference.
591 if (primary_pattern == ContentSettingsPattern::Wildcard() ||
592 secondary_pattern == ContentSettingsPattern::Wildcard()) {
593 return;
594 }
595
596 const GURL requesting_origin(primary_pattern.ToString());
597 const GURL embedding_origin(secondary_pattern.ToString());
598 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid());
599
600 DictionaryPrefUpdate update(prefs_, prefs::kGeolocationContentSettings);
601 DictionaryValue* obsolete_geolocation_settings = update.Get();
602 DictionaryValue* requesting_origin_settings_dictionary = NULL;
603 bool settings_found =
604 obsolete_geolocation_settings->GetDictionaryWithoutPathExpansion(
605 requesting_origin.spec(), &requesting_origin_settings_dictionary);
606 if (setting == CONTENT_SETTING_DEFAULT) {
607 if (settings_found) {
608 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion(
609 embedding_origin.spec(), NULL);
610 if (requesting_origin_settings_dictionary->empty()) {
611 obsolete_geolocation_settings->RemoveWithoutPathExpansion(
612 requesting_origin.spec(), NULL);
613 }
614 }
615 } else {
616 if (!settings_found) {
617 requesting_origin_settings_dictionary = new DictionaryValue;
618 obsolete_geolocation_settings->SetWithoutPathExpansion(
619 requesting_origin.spec(), requesting_origin_settings_dictionary);
620 }
621 DCHECK(requesting_origin_settings_dictionary);
622 requesting_origin_settings_dictionary->SetWithoutPathExpansion(
623 embedding_origin.spec(), Value::CreateIntegerValue(setting));
624 }
625 }
626
627 void PrefProvider::UpdateObsoleteNotificationsSettings(
628 const ContentSettingsPattern& primary_pattern,
629 const ContentSettingsPattern& secondary_pattern,
630 ContentSetting setting,
631 ListValue* allowed_sites,
632 ListValue* denied_sites) {
633 DCHECK_EQ(secondary_pattern, ContentSettingsPattern::Wildcard());
634 GURL origin(primary_pattern.ToString());
635 DCHECK(origin.is_valid());
636 scoped_ptr<StringValue> value(new StringValue(origin.spec()));
637 if (setting == CONTENT_SETTING_ALLOW) {
638 denied_sites->Remove(*value, NULL);
639 allowed_sites->AppendIfNotPresent(value.release());
640 } else if (setting == CONTENT_SETTING_BLOCK) {
641 allowed_sites->Remove(*value, NULL);
642 denied_sites->AppendIfNotPresent(value.release());
643 } else if (setting == CONTENT_SETTING_DEFAULT) {
644 denied_sites->Remove(*value, NULL);
645 allowed_sites->Remove(*value, NULL);
646 } else {
647 NOTREACHED() << "Setting value: " << setting
648 << " is not supported for notifications";
649 }
650 }
651
652 // static 484 // static
653 void PrefProvider::CanonicalizeContentSettingsExceptions( 485 void PrefProvider::CanonicalizeContentSettingsExceptions(
654 DictionaryValue* all_settings_dictionary) { 486 DictionaryValue* all_settings_dictionary) {
655 DCHECK(all_settings_dictionary); 487 DCHECK(all_settings_dictionary);
656 488
657 std::vector<std::string> remove_items; 489 std::vector<std::string> remove_items;
658 std::vector<std::pair<std::string, std::string> > move_items; 490 std::vector<std::pair<std::string, std::string> > move_items;
659 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); 491 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
660 i != all_settings_dictionary->end_keys(); ++i) { 492 i != all_settings_dictionary->end_keys(); ++i) {
661 const std::string& pattern_str(*i); 493 const std::string& pattern_str(*i);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 DCHECK(found); 681 DCHECK(found);
850 if (!pattern_str_pair.second.empty()) { 682 if (!pattern_str_pair.second.empty()) {
851 mutable_patterns_dictionary->SetWithoutPathExpansion( 683 mutable_patterns_dictionary->SetWithoutPathExpansion(
852 pattern_str_pair.second, dict.release()); 684 pattern_str_pair.second, dict.release());
853 } 685 }
854 } 686 }
855 } 687 }
856 } 688 }
857 } 689 }
858 690
859 void PrefProvider::SyncObsoletePatternPref() {
860 // Ensure that |lock_| is not held by this thread, since this function will
861 // send out notifications (by |~DictionaryPrefUpdate|).
862 AssertLockNotHeld();
863
864 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) &&
865 !is_incognito_) {
866 const DictionaryValue* pattern_pairs_dictionary =
867 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
868
869 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns);
870 DictionaryValue* obsolete_settings_dictionary = update.Get();
871
872 for (DictionaryValue::key_iterator i =
873 pattern_pairs_dictionary->begin_keys();
874 i != pattern_pairs_dictionary->end_keys();
875 ++i) {
876 const std::string& key(*i);
877 // Validate pattern string and skip it if it is invalid.
878 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
879 ParsePatternString(key);
880 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid()) {
881 LOG(DFATAL) << "Invalid pattern strings: " << key;
882 continue;
883 }
884
885 DictionaryValue* settings_dictionary = NULL;
886 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion(
887 key, &settings_dictionary);
888 DCHECK(found);
889 scoped_ptr<DictionaryValue> settings_dictionary_copy(
890 new DictionaryValue());
891 for (size_t i = CONTENT_SETTINGS_TYPE_COOKIES;
892 i <= CONTENT_SETTINGS_TYPE_POPUPS;
893 ++i) {
894 std::string type_name = GetTypeName(ContentSettingsType(i));
895 if (settings_dictionary->HasKey(type_name)) {
896 Value* value = NULL;
897 bool found = settings_dictionary->GetWithoutPathExpansion(
898 type_name, &value);
899 DCHECK(found);
900 settings_dictionary_copy->SetWithoutPathExpansion(
901 type_name, value->DeepCopy());
902 }
903 }
904
905 // Ignore empty dictionaryies.
906 if (!settings_dictionary_copy->empty()) {
907 std::string new_key = pattern_pair.first.ToString();
908 // Existing values are overwritten.
909 obsolete_settings_dictionary->SetWithoutPathExpansion(
910 new_key, settings_dictionary_copy.release());
911 }
912 }
913 }
914 }
915
916 void PrefProvider::MigrateObsoleteGeolocationPref() { 691 void PrefProvider::MigrateObsoleteGeolocationPref() {
917 // Ensure that |lock_| is not held by this thread, since this function will 692 // Ensure that |lock_| is not held by this thread, since this function will
918 // send out notifications (by |~DictionaryPrefUpdate|). 693 // send out notifications (by |~DictionaryPrefUpdate|).
919 AssertLockNotHeld(); 694 AssertLockNotHeld();
920 695
921 if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings)) 696 if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings))
922 return; 697 return;
923 698
924 DictionaryPrefUpdate update(prefs_, 699 DictionaryPrefUpdate update(prefs_,
925 prefs::kContentSettingsPatternPairs); 700 prefs::kContentSettingsPatternPairs);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK)); 812 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
1038 UpdatePatternPairsSettings(primary_pattern, 813 UpdatePatternPairsSettings(primary_pattern,
1039 ContentSettingsPattern::Wildcard(), 814 ContentSettingsPattern::Wildcard(),
1040 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 815 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1041 std::string(), 816 std::string(),
1042 value.get(), 817 value.get(),
1043 pattern_pairs_settings); 818 pattern_pairs_settings);
1044 } 819 }
1045 } 820 }
1046 821
1047 void PrefProvider::SyncObsoletePrefs() {
1048 // Ensure that |lock_| is not held by this thread, since this function will
1049 // send out notifications (by |~DictionaryPrefUpdate|).
1050 AssertLockNotHeld();
1051
1052 DCHECK(prefs_);
1053 DCHECK(prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs));
1054
1055 // Clear obsolete preferences first. Then copy the settings from the new
1056 // preference to the obsolete ones.
1057 prefs_->ClearPref(prefs::kGeolocationContentSettings);
1058 prefs_->ClearPref(prefs::kDesktopNotificationAllowedOrigins);
1059 prefs_->ClearPref(prefs::kDesktopNotificationDeniedOrigins);
1060
1061 ListPrefUpdate update_allowed_origins(
1062 prefs_, prefs::kDesktopNotificationAllowedOrigins);
1063 ListPrefUpdate update_denied_origins(
1064 prefs_, prefs::kDesktopNotificationDeniedOrigins);
1065 ListValue* allowed_origins = update_allowed_origins.Get();
1066 ListValue* denied_origins = update_denied_origins.Get();
1067
1068 const DictionaryValue* pattern_pairs_dictionary =
1069 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
1070 for (DictionaryValue::key_iterator i =
1071 pattern_pairs_dictionary->begin_keys();
1072 i != pattern_pairs_dictionary->end_keys();
1073 ++i) {
1074 const std::string& key(*i);
1075 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
1076 ParsePatternString(key);
1077 DCHECK(pattern_pair.first.IsValid() && pattern_pair.second.IsValid());
1078
1079 DictionaryValue* settings_dictionary = NULL;
1080 bool settings_found =
1081 pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion(
1082 key, &settings_dictionary);
1083 DCHECK(settings_found);
1084
1085 int setting_value = 0;
1086 if (settings_dictionary->GetInteger(
1087 GetTypeName(CONTENT_SETTINGS_TYPE_NOTIFICATIONS), &setting_value)) {
1088 UpdateObsoleteNotificationsSettings(pattern_pair.first,
1089 pattern_pair.second,
1090 ContentSetting(setting_value),
1091 allowed_origins,
1092 denied_origins);
1093 }
1094
1095 setting_value = 0;
1096 if (settings_dictionary->GetInteger(
1097 GetTypeName(CONTENT_SETTINGS_TYPE_GEOLOCATION), &setting_value)) {
1098 UpdateObsoleteGeolocationPref(pattern_pair.first,
1099 pattern_pair.second,
1100 ContentSetting(setting_value));
1101 }
1102 }
1103 }
1104
1105 void PrefProvider::AssertLockNotHeld() const { 822 void PrefProvider::AssertLockNotHeld() const {
1106 #if !defined(NDEBUG) 823 #if !defined(NDEBUG)
1107 // |Lock::Acquire()| will assert if the lock is held by this thread. 824 // |Lock::Acquire()| will assert if the lock is held by this thread.
1108 lock_.Acquire(); 825 lock_.Acquire();
1109 lock_.Release(); 826 lock_.Release();
1110 #endif 827 #endif
1111 } 828 }
1112 829
1113 } // namespace content_settings 830 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698