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

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: Fix bad file header. Created 8 years, 8 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::kContentSettingsPatterns) {
262 SyncObsoletePatternPref();
263 SyncObsoletePrefs();
264 } else if (*name == prefs::kContentSettingsPatterns) {
265 MigrateObsoleteContentSettingsPatternPref(); 262 MigrateObsoleteContentSettingsPatternPref();
266 } else if (*name == prefs::kGeolocationContentSettings) { 263 } else if (*name == prefs::kGeolocationContentSettings) {
267 MigrateObsoleteGeolocationPref(); 264 MigrateObsoleteGeolocationPref();
268 } else if (*name == prefs::kDesktopNotificationAllowedOrigins || 265 } else if (*name == prefs::kDesktopNotificationAllowedOrigins ||
269 *name == prefs::kDesktopNotificationDeniedOrigins) { 266 *name == prefs::kDesktopNotificationDeniedOrigins) {
270 MigrateObsoleteNotificationsPrefs(); 267 MigrateObsoleteNotificationsPrefs();
271 } else { 268 } else if (*name != prefs::kContentSettingsPatternPairs) {
272 NOTREACHED() << "Unexpected preference observed"; 269 NOTREACHED() << "Unexpected preference observed";
273 return; 270 return;
274 } 271 }
275 } 272 }
276 ReadContentSettingsFromPref(true); 273 ReadContentSettingsFromPref(true);
277 274
278 NotifyObservers(ContentSettingsPattern(), 275 NotifyObservers(ContentSettingsPattern(),
279 ContentSettingsPattern(), 276 ContentSettingsPattern(),
280 CONTENT_SETTINGS_TYPE_DEFAULT, 277 CONTENT_SETTINGS_TYPE_DEFAULT,
281 std::string()); 278 std::string());
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 DictionaryPrefUpdate update(prefs_, 314 DictionaryPrefUpdate update(prefs_,
318 prefs::kContentSettingsPatternPairs); 315 prefs::kContentSettingsPatternPairs);
319 DictionaryValue* pattern_pairs_settings = update.Get(); 316 DictionaryValue* pattern_pairs_settings = update.Get();
320 UpdatePatternPairsSettings(primary_pattern, 317 UpdatePatternPairsSettings(primary_pattern,
321 secondary_pattern, 318 secondary_pattern,
322 content_type, 319 content_type,
323 resource_identifier, 320 resource_identifier,
324 value, 321 value,
325 pattern_pairs_settings); 322 pattern_pairs_settings);
326 } 323 }
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 } 324 }
351 325
352 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { 326 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) {
353 // |DictionaryPrefUpdate| sends out notifications when destructed. This 327 // |DictionaryPrefUpdate| sends out notifications when destructed. This
354 // construction order ensures |AutoLock| gets destroyed first and |lock_| is 328 // construction order ensures |AutoLock| gets destroyed first and |lock_| is
355 // not held when the notifications are sent. Also, |auto_reset| must be still 329 // 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 330 // valid when the notifications are sent, so that |Observe| skips the
357 // notification. 331 // notification.
358 AutoReset<bool> auto_reset(&updating_preferences_, true); 332 AutoReset<bool> auto_reset(&updating_preferences_, true);
359 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); 333 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 value_map_.SetValue(pattern_pair.first, 408 value_map_.SetValue(pattern_pair.first,
435 pattern_pair.second, 409 pattern_pair.second,
436 content_type, 410 content_type,
437 ResourceIdentifier(""), 411 ResourceIdentifier(""),
438 Value::CreateIntegerValue(setting)); 412 Value::CreateIntegerValue(setting));
439 } 413 }
440 } 414 }
441 } 415 }
442 } 416 }
443 417
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( 418 void PrefProvider::UpdatePatternPairsSettings(
515 const ContentSettingsPattern& primary_pattern, 419 const ContentSettingsPattern& primary_pattern,
516 const ContentSettingsPattern& secondary_pattern, 420 const ContentSettingsPattern& secondary_pattern,
517 ContentSettingsType content_type, 421 ContentSettingsType content_type,
518 const ResourceIdentifier& resource_identifier, 422 const ResourceIdentifier& resource_identifier,
519 const base::Value* value, 423 const base::Value* value,
520 DictionaryValue* pattern_pairs_settings) { 424 DictionaryValue* pattern_pairs_settings) {
521 // Get settings dictionary for the given patterns. 425 // Get settings dictionary for the given patterns.
522 std::string pattern_str(CreatePatternString(primary_pattern, 426 std::string pattern_str(CreatePatternString(primary_pattern,
523 secondary_pattern)); 427 secondary_pattern));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 } 472 }
569 } 473 }
570 // Remove the settings dictionary if it is empty. 474 // Remove the settings dictionary if it is empty.
571 if (settings_dictionary->empty()) { 475 if (settings_dictionary->empty()) {
572 pattern_pairs_settings->RemoveWithoutPathExpansion( 476 pattern_pairs_settings->RemoveWithoutPathExpansion(
573 pattern_str, NULL); 477 pattern_str, NULL);
574 } 478 }
575 } 479 }
576 } 480 }
577 481
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 482 // static
653 void PrefProvider::CanonicalizeContentSettingsExceptions( 483 void PrefProvider::CanonicalizeContentSettingsExceptions(
654 DictionaryValue* all_settings_dictionary) { 484 DictionaryValue* all_settings_dictionary) {
655 DCHECK(all_settings_dictionary); 485 DCHECK(all_settings_dictionary);
656 486
657 std::vector<std::string> remove_items; 487 std::vector<std::string> remove_items;
658 std::vector<std::pair<std::string, std::string> > move_items; 488 std::vector<std::pair<std::string, std::string> > move_items;
659 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); 489 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
660 i != all_settings_dictionary->end_keys(); ++i) { 490 i != all_settings_dictionary->end_keys(); ++i) {
661 const std::string& pattern_str(*i); 491 const std::string& pattern_str(*i);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 DCHECK(found); 679 DCHECK(found);
850 if (!pattern_str_pair.second.empty()) { 680 if (!pattern_str_pair.second.empty()) {
851 mutable_patterns_dictionary->SetWithoutPathExpansion( 681 mutable_patterns_dictionary->SetWithoutPathExpansion(
852 pattern_str_pair.second, dict.release()); 682 pattern_str_pair.second, dict.release());
853 } 683 }
854 } 684 }
855 } 685 }
856 } 686 }
857 } 687 }
858 688
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() { 689 void PrefProvider::MigrateObsoleteGeolocationPref() {
917 // Ensure that |lock_| is not held by this thread, since this function will 690 // Ensure that |lock_| is not held by this thread, since this function will
918 // send out notifications (by |~DictionaryPrefUpdate|). 691 // send out notifications (by |~DictionaryPrefUpdate|).
919 AssertLockNotHeld(); 692 AssertLockNotHeld();
920 693
921 if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings)) 694 if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings))
922 return; 695 return;
923 696
924 DictionaryPrefUpdate update(prefs_, 697 DictionaryPrefUpdate update(prefs_,
925 prefs::kContentSettingsPatternPairs); 698 prefs::kContentSettingsPatternPairs);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK)); 810 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
1038 UpdatePatternPairsSettings(primary_pattern, 811 UpdatePatternPairsSettings(primary_pattern,
1039 ContentSettingsPattern::Wildcard(), 812 ContentSettingsPattern::Wildcard(),
1040 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 813 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1041 std::string(), 814 std::string(),
1042 value.get(), 815 value.get(),
1043 pattern_pairs_settings); 816 pattern_pairs_settings);
1044 } 817 }
1045 } 818 }
1046 819
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 { 820 void PrefProvider::AssertLockNotHeld() const {
1106 #if !defined(NDEBUG) 821 #if !defined(NDEBUG)
1107 // |Lock::Acquire()| will assert if the lock is held by this thread. 822 // |Lock::Acquire()| will assert if the lock is held by this thread.
1108 lock_.Acquire(); 823 lock_.Acquire();
1109 lock_.Release(); 824 lock_.Release();
1110 #endif 825 #endif
1111 } 826 }
1112 827
1113 } // namespace content_settings 828 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698