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

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

Issue 7484072: Migrate geolocation settings to host content settings map. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 4 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include <vector> 10 #include <vector>
(...skipping 24 matching lines...) Expand all
35 typedef std::map<std::string, std::string> StringMap; 35 typedef std::map<std::string, std::string> StringMap;
36 36
37 // The preference keys where resource identifiers are stored for 37 // The preference keys where resource identifiers are stored for
38 // ContentSettingsType values that support resource identifiers. 38 // ContentSettingsType values that support resource identifiers.
39 const char* kResourceTypeNames[] = { 39 const char* kResourceTypeNames[] = {
40 NULL, 40 NULL,
41 NULL, 41 NULL,
42 NULL, 42 NULL,
43 "per_plugin", 43 "per_plugin",
44 NULL, 44 NULL,
45 NULL, // Not used for Geolocation 45 NULL,
46 NULL, // Not used for Notifications 46 NULL, // Not used for Notifications
47 }; 47 };
48 COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES, 48 COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
49 resource_type_names_incorrect_size); 49 resource_type_names_incorrect_size);
50 50
51 // The default setting for each content type. 51 // The default setting for each content type.
52 const ContentSetting kDefaultSettings[] = { 52 const ContentSetting kDefaultSettings[] = {
53 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES 53 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES
54 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES 54 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES
55 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT 55 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT
56 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS 56 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS
57 CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS 57 CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS
58 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_GEOLOCATION 58 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_GEOLOCATION
59 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_NOTIFICATIONS 59 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_NOTIFICATIONS
60 }; 60 };
61 COMPILE_ASSERT(arraysize(kDefaultSettings) == CONTENT_SETTINGS_NUM_TYPES, 61 COMPILE_ASSERT(arraysize(kDefaultSettings) == CONTENT_SETTINGS_NUM_TYPES,
62 default_settings_incorrect_size); 62 default_settings_incorrect_size);
63 63
64 // The names of the ContentSettingsType values, for use with dictionary prefs. 64 // The names of the ContentSettingsType values, for use with dictionary prefs.
65 const char* kTypeNames[] = { 65 const char* kTypeNames[] = {
66 "cookies", 66 "cookies",
67 "images", 67 "images",
68 "javascript", 68 "javascript",
69 "plugins", 69 "plugins",
70 "popups", 70 "popups",
71 "geolocation",
71 // TODO(markusheintz): Refactoring in progress. Content settings exceptions 72 // TODO(markusheintz): Refactoring in progress. Content settings exceptions
72 // for notifications and geolocation will be added next. 73 // for notifications added next.
73 "geolocation", // Only used for default Geolocation settings
74 "notifications", // Only used for default Notifications settings. 74 "notifications", // Only used for default Notifications settings.
75 }; 75 };
76 COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES, 76 COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
77 type_names_incorrect_size); 77 type_names_incorrect_size);
78 78
79 void SetDefaultContentSettings(DictionaryValue* default_settings) { 79 void SetDefaultContentSettings(DictionaryValue* default_settings) {
80 default_settings->Clear(); 80 default_settings->Clear();
81 81
82 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { 82 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
83 if (kTypeNames[i] != NULL) { 83 if (kTypeNames[i] != NULL) {
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 // static 354 // static
355 void PrefProvider::RegisterUserPrefs(PrefService* prefs) { 355 void PrefProvider::RegisterUserPrefs(PrefService* prefs) {
356 prefs->RegisterIntegerPref( 356 prefs->RegisterIntegerPref(
357 prefs::kContentSettingsVersion, 357 prefs::kContentSettingsVersion,
358 ContentSettingsPattern::kContentSettingsPatternVersion, 358 ContentSettingsPattern::kContentSettingsPatternVersion,
359 PrefService::UNSYNCABLE_PREF); 359 PrefService::UNSYNCABLE_PREF);
360 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatternPairs, 360 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatternPairs,
361 PrefService::SYNCABLE_PREF); 361 PrefService::SYNCABLE_PREF);
362 362
363 // Obsolete prefs, for migration: 363 // Obsolete prefs, for migration:
364 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings,
365 PrefService::SYNCABLE_PREF);
364 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, 366 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns,
365 PrefService::SYNCABLE_PREF); 367 PrefService::SYNCABLE_PREF);
366 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, 368 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts,
367 PrefService::UNSYNCABLE_PREF); 369 PrefService::UNSYNCABLE_PREF);
368 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, 370 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings,
369 PrefService::UNSYNCABLE_PREF); 371 PrefService::UNSYNCABLE_PREF);
370 } 372 }
371 373
372 PrefProvider::PrefProvider(PrefService* prefs, 374 PrefProvider::PrefProvider(PrefService* prefs,
373 bool incognito) 375 bool incognito)
374 : prefs_(prefs), 376 : prefs_(prefs),
375 is_incognito_(incognito), 377 is_incognito_(incognito),
376 updating_preferences_(false) { 378 updating_preferences_(false) {
377 DCHECK(prefs_); 379 DCHECK(prefs_);
378 if (!is_incognito_) { 380 if (!is_incognito_) {
379 // Migrate obsolete preferences. 381 // Migrate obsolete preferences.
380 MigrateObsoletePerhostPref(); 382 MigrateObsoletePerhostPref();
381 MigrateObsoletePopupsPref(); 383 MigrateObsoletePopupsPref();
382 MigrateObsoleteContentSettingsPatternPref(); 384 MigrateObsoleteContentSettingsPatternPref();
385 MigrateObsoleteGeolocationPref();
383 } 386 }
384 387
385 // Verify preferences version. 388 // Verify preferences version.
386 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { 389 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) {
387 prefs_->SetInteger(prefs::kContentSettingsVersion, 390 prefs_->SetInteger(prefs::kContentSettingsVersion,
388 ContentSettingsPattern::kContentSettingsPatternVersion); 391 ContentSettingsPattern::kContentSettingsPatternVersion);
389 } 392 }
390 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > 393 if (prefs_->GetInteger(prefs::kContentSettingsVersion) >
391 ContentSettingsPattern::kContentSettingsPatternVersion) { 394 ContentSettingsPattern::kContentSettingsPatternVersion) {
392 return; 395 return;
393 } 396 }
394 397
395 // Read content settings exceptions. 398 // Read content settings exceptions.
396 ReadContentSettingsFromPref(false); 399 ReadContentSettingsFromPref(false);
397 400
398 if (!is_incognito_) { 401 if (!is_incognito_) {
399 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", 402 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions",
400 value_map_.size()); 403 value_map_.size());
401 } 404 }
402 405
403 pref_change_registrar_.Init(prefs_); 406 pref_change_registrar_.Init(prefs_);
404 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); 407 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this);
405 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); 408 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this);
409 pref_change_registrar_.Add(prefs::kGeolocationContentSettings, this);
406 } 410 }
407 411
408 ContentSetting PrefProvider::GetContentSetting( 412 ContentSetting PrefProvider::GetContentSetting(
409 const GURL& primary_url, 413 const GURL& primary_url,
410 const GURL& secondary_url, 414 const GURL& secondary_url,
411 ContentSettingsType content_type, 415 ContentSettingsType content_type,
412 const ResourceIdentifier& resource_identifier) const { 416 const ResourceIdentifier& resource_identifier) const {
413 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito 417 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito
414 // profile, this will always return NULL. 418 // profile, this will always return NULL.
415 // TODO(markusheintz): I don't like this. I'd like to have an 419 // TODO(markusheintz): I don't like this. I'd like to have an
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 } 466 }
463 467
464 void PrefProvider::SetContentSetting( 468 void PrefProvider::SetContentSetting(
465 const ContentSettingsPattern& primary_pattern, 469 const ContentSettingsPattern& primary_pattern,
466 const ContentSettingsPattern& secondary_pattern, 470 const ContentSettingsPattern& secondary_pattern,
467 ContentSettingsType content_type, 471 ContentSettingsType content_type,
468 const ResourceIdentifier& resource_identifier, 472 const ResourceIdentifier& resource_identifier,
469 ContentSetting setting) { 473 ContentSetting setting) {
470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
471 DCHECK(prefs_); 475 DCHECK(prefs_);
472 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 476 DCHECK(kTypeNames[content_type] != NULL);
473 477
474 // Update in memory value map. 478 // Update in memory value map.
475 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; 479 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
476 if (!is_incognito_) 480 if (!is_incognito_)
477 map_to_modify = &value_map_; 481 map_to_modify = &value_map_;
478 482
479 { 483 {
480 base::AutoLock auto_lock(lock_); 484 base::AutoLock auto_lock(lock_);
481 if (setting == CONTENT_SETTING_DEFAULT) { 485 if (setting == CONTENT_SETTING_DEFAULT) {
482 map_to_modify->DeleteValue( 486 map_to_modify->DeleteValue(
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 const NotificationDetails& details) { 552 const NotificationDetails& details) {
549 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 553 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
550 554
551 if (type == chrome::NOTIFICATION_PREF_CHANGED) { 555 if (type == chrome::NOTIFICATION_PREF_CHANGED) {
552 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); 556 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
553 if (updating_preferences_) 557 if (updating_preferences_)
554 return; 558 return;
555 559
556 std::string* name = Details<std::string>(details).ptr(); 560 std::string* name = Details<std::string>(details).ptr();
557 if (*name == prefs::kContentSettingsPatternPairs) { 561 if (*name == prefs::kContentSettingsPatternPairs) {
558 SyncObsoletePref(); 562 SyncObsoletePatternPref();
563 SyncObsoleteGeolocationPref();
559 ReadContentSettingsFromPref(true); 564 ReadContentSettingsFromPref(true);
560 } else if (*name == prefs::kContentSettingsPatterns) { 565 } else if (*name == prefs::kContentSettingsPatterns) {
561 AutoReset<bool> auto_reset(&updating_preferences_, true); 566 AutoReset<bool> auto_reset(&updating_preferences_, true);
562 MigrateObsoleteContentSettingsPatternPref(); 567 MigrateObsoleteContentSettingsPatternPref();
563 ReadContentSettingsFromPref(true); 568 ReadContentSettingsFromPref(true);
569 } else if (*name == prefs::kGeolocationContentSettings) {
570 AutoReset<bool> auto_reset(&updating_preferences_, true);
571 MigrateObsoleteGeolocationPref();
572 ReadContentSettingsFromPref(true);
564 } else { 573 } else {
565 NOTREACHED() << "Unexpected preference observed"; 574 NOTREACHED() << "Unexpected preference observed";
566 return; 575 return;
567 } 576 }
568 577
569 NotifyObservers(ContentSettingsPattern(), 578 NotifyObservers(ContentSettingsPattern(),
570 ContentSettingsPattern(), 579 ContentSettingsPattern(),
571 CONTENT_SETTINGS_TYPE_DEFAULT, 580 CONTENT_SETTINGS_TYPE_DEFAULT,
572 std::string()); 581 std::string());
573 } else { 582 } else {
(...skipping 13 matching lines...) Expand all
587 const ContentSettingsPattern& secondary_pattern, 596 const ContentSettingsPattern& secondary_pattern,
588 ContentSettingsType content_type, 597 ContentSettingsType content_type,
589 const ResourceIdentifier& resource_identifier, 598 const ResourceIdentifier& resource_identifier,
590 ContentSetting setting) { 599 ContentSetting setting) {
591 AutoReset<bool> auto_reset(&updating_preferences_, true); 600 AutoReset<bool> auto_reset(&updating_preferences_, true);
592 UpdatePatternPairsPref(primary_pattern, 601 UpdatePatternPairsPref(primary_pattern,
593 secondary_pattern, 602 secondary_pattern,
594 content_type, 603 content_type,
595 resource_identifier, 604 resource_identifier,
596 setting); 605 setting);
597 UpdatePatternsPref(primary_pattern, 606 if (content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION &&
598 secondary_pattern, 607 content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
599 content_type, 608 UpdateObsoletePatternsPref(primary_pattern,
600 resource_identifier, 609 secondary_pattern,
601 setting); 610 content_type,
611 resource_identifier,
612 setting);
613 }
614 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) {
615 UpdateObsoleteGeolocationPref(primary_pattern, secondary_pattern, setting);
616 }
602 } 617 }
603 618
604 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { 619 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) {
605 base::AutoLock auto_lock(lock_); 620 base::AutoLock auto_lock(lock_);
606 621
607 const DictionaryValue* all_settings_dictionary = 622 const DictionaryValue* all_settings_dictionary =
608 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); 623 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
609 624
610 if (overwrite) 625 if (overwrite)
611 value_map_.clear(); 626 value_map_.clear();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 content_type, 701 content_type,
687 ResourceIdentifier(""), 702 ResourceIdentifier(""),
688 Value::CreateIntegerValue(setting)); 703 Value::CreateIntegerValue(setting));
689 } 704 }
690 } 705 }
691 } 706 }
692 } 707 }
693 } 708 }
694 } 709 }
695 710
696 void PrefProvider::UpdatePatternsPref( 711 void PrefProvider::UpdateObsoletePatternsPref(
697 const ContentSettingsPattern& primary_pattern, 712 const ContentSettingsPattern& primary_pattern,
698 const ContentSettingsPattern& secondary_pattern, 713 const ContentSettingsPattern& secondary_pattern,
699 ContentSettingsType content_type, 714 ContentSettingsType content_type,
700 const ResourceIdentifier& resource_identifier, 715 const ResourceIdentifier& resource_identifier,
701 ContentSetting setting) { 716 ContentSetting setting) {
702 DictionaryPrefUpdate update(prefs_, 717 DictionaryPrefUpdate update(prefs_,
703 prefs::kContentSettingsPatterns); 718 prefs::kContentSettingsPatterns);
704 DictionaryValue* all_settings_dictionary = update.Get(); 719 DictionaryValue* all_settings_dictionary = update.Get();
705 720
706 // Get settings dictionary for |primary_pattern|. 721 // Get settings dictionary for |primary_pattern|.
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
819 } 834 }
820 } 835 }
821 // Remove the settings dictionary if it is empty. 836 // Remove the settings dictionary if it is empty.
822 if (settings_dictionary->empty()) { 837 if (settings_dictionary->empty()) {
823 all_settings_dictionary->RemoveWithoutPathExpansion( 838 all_settings_dictionary->RemoveWithoutPathExpansion(
824 pattern_str, NULL); 839 pattern_str, NULL);
825 } 840 }
826 } 841 }
827 } 842 }
828 843
844 void PrefProvider::UpdateObsoleteGeolocationPref(
845 const ContentSettingsPattern& primary_pattern,
846 const ContentSettingsPattern& secondary_pattern,
847 ContentSetting setting) {
848 if (!prefs_)
849 return;
850
851 const GURL requesting_origin(primary_pattern.ToString());
852 const GURL embedding_origin(secondary_pattern.ToString());
853 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid());
854
855 DictionaryPrefUpdate update(prefs_, prefs::kGeolocationContentSettings);
856 DictionaryValue* obsolete_geolocation_settings = update.Get();
857 DictionaryValue* requesting_origin_settings_dictionary = NULL;
858 obsolete_geolocation_settings->GetDictionaryWithoutPathExpansion(
859 requesting_origin.spec(), &requesting_origin_settings_dictionary);
860 if (setting == CONTENT_SETTING_DEFAULT) {
861 if (requesting_origin_settings_dictionary) {
862 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion(
863 embedding_origin.spec(), NULL);
864 if (requesting_origin_settings_dictionary->empty()) {
865 obsolete_geolocation_settings->RemoveWithoutPathExpansion(
866 requesting_origin.spec(), NULL);
867 }
868 }
869 } else {
870 if (!requesting_origin_settings_dictionary) {
871 requesting_origin_settings_dictionary = new DictionaryValue;
872 obsolete_geolocation_settings->SetWithoutPathExpansion(
873 requesting_origin.spec(), requesting_origin_settings_dictionary);
874 }
875 DCHECK(requesting_origin_settings_dictionary);
876 requesting_origin_settings_dictionary->SetWithoutPathExpansion(
877 embedding_origin.spec(), Value::CreateIntegerValue(setting));
878 }
879 }
880
829 // static 881 // static
830 void PrefProvider::CanonicalizeContentSettingsExceptions( 882 void PrefProvider::CanonicalizeContentSettingsExceptions(
831 DictionaryValue* all_settings_dictionary) { 883 DictionaryValue* all_settings_dictionary) {
832 DCHECK(all_settings_dictionary); 884 DCHECK(all_settings_dictionary);
833 885
834 std::vector<std::string> remove_items; 886 std::vector<std::string> remove_items;
835 std::vector<std::pair<std::string, std::string> > move_items; 887 std::vector<std::pair<std::string, std::string> > move_items;
836 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); 888 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
837 i != all_settings_dictionary->end_keys(); ++i) { 889 i != all_settings_dictionary->end_keys(); ++i) {
838 const std::string& pattern_str(*i); 890 const std::string& pattern_str(*i);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 "", 1014 "",
963 CONTENT_SETTING_ALLOW); 1015 CONTENT_SETTING_ALLOW);
964 } 1016 }
965 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); 1017 prefs_->ClearPref(prefs::kPopupWhitelistedHosts);
966 } 1018 }
967 } 1019 }
968 1020
969 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { 1021 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() {
970 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { 1022 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) {
971 const DictionaryValue* patterns_dictionary = 1023 const DictionaryValue* patterns_dictionary =
972 prefs_->GetDictionary(prefs::kContentSettingsPatterns); 1024 prefs_->GetDictionary(prefs::kContentSettingsPatterns);
973 1025
974 // A map with an old key, new key mapping. If the new key is empty then the 1026 // A map with an old key, new key mapping. If the new key is empty then the
975 // value for the old key will be removed. 1027 // value for the old key will be removed.
976 StringMap keys_to_change; 1028 StringMap keys_to_change;
977
978 { 1029 {
979 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); 1030 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
980 DictionaryValue* pattern_pairs_dictionary = update.Get(); 1031 DictionaryValue* pattern_pairs_dictionary = update.Get();
981 for (DictionaryValue::key_iterator i( 1032 for (DictionaryValue::key_iterator i(
982 patterns_dictionary->begin_keys()); 1033 patterns_dictionary->begin_keys());
983 i != patterns_dictionary->end_keys(); 1034 i != patterns_dictionary->end_keys();
984 ++i) { 1035 ++i) {
985 const std::string& key(*i); 1036 const std::string& key(*i);
986 // Remove broken pattern keys and fix keys with pattern pairs. 1037 // Remove broken pattern keys and fix keys with pattern pairs.
987 size_t sep_pos = key.find(","); 1038 size_t sep_pos = key.find(",");
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 DCHECK(found); 1090 DCHECK(found);
1040 if (!pattern_str_pair.second.empty()) { 1091 if (!pattern_str_pair.second.empty()) {
1041 mutable_patterns_dictionary->SetWithoutPathExpansion( 1092 mutable_patterns_dictionary->SetWithoutPathExpansion(
1042 pattern_str_pair.second, dict.release()); 1093 pattern_str_pair.second, dict.release());
1043 } 1094 }
1044 } 1095 }
1045 } 1096 }
1046 } 1097 }
1047 } 1098 }
1048 1099
1049 void PrefProvider::SyncObsoletePref() { 1100 void PrefProvider::SyncObsoletePatternPref() {
1050 AutoReset<bool> auto_reset(&updating_preferences_, true); 1101 AutoReset<bool> auto_reset(&updating_preferences_, true);
1051 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && 1102 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) &&
1052 !is_incognito_) { 1103 !is_incognito_) {
1053 const DictionaryValue* pattern_pairs_dictionary = 1104 const DictionaryValue* pattern_pairs_dictionary =
1054 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); 1105 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
1055 1106
1056 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns); 1107 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns);
1057 DictionaryValue* obsolete_settings_dictionary = update.Get(); 1108 DictionaryValue* obsolete_settings_dictionary = update.Get();
1058 1109
1059 for (DictionaryValue::key_iterator i = 1110 for (DictionaryValue::key_iterator i =
1060 pattern_pairs_dictionary->begin_keys(); 1111 pattern_pairs_dictionary->begin_keys();
1061 i != pattern_pairs_dictionary->end_keys(); 1112 i != pattern_pairs_dictionary->end_keys();
1062 ++i) { 1113 ++i) {
1063 const std::string& key(*i); 1114 const std::string& key(*i);
1064 // Validate pattern string and skip it if it is invalid. 1115 // Validate pattern string and skip it if it is invalid.
1065 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = 1116 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
1066 ParsePatternString(key); 1117 ParsePatternString(key);
1067 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid()) { 1118 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid()) {
1068 LOG(DFATAL) << "Invalid pattern strings: " << key; 1119 LOG(DFATAL) << "Invalid pattern strings: " << key;
1069 continue; 1120 continue;
1070 } 1121 }
1071 1122
1072 // Copy dictionary 1123 DictionaryValue* settings_dictionary = NULL;
1073 DictionaryValue* dictionary = NULL;
1074 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( 1124 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion(
1075 key, &dictionary); 1125 key, &settings_dictionary);
1076 DCHECK(found); 1126 DCHECK(found);
1077 std::string new_key = pattern_pair.first.ToString(); 1127 scoped_ptr<DictionaryValue> settings_dictionary_copy(
1078 // Existing values are overwritten. 1128 new DictionaryValue());
1079 obsolete_settings_dictionary->SetWithoutPathExpansion( 1129 for (size_t i = CONTENT_SETTINGS_TYPE_COOKIES;
1080 new_key, dictionary->DeepCopy()); 1130 i <= CONTENT_SETTINGS_TYPE_POPUPS;
1131 ++i) {
1132 DCHECK(kTypeNames[i]);
1133 std::string type_name(kTypeNames[i]);
1134 if (settings_dictionary->HasKey(type_name)) {
1135 Value* value = NULL;
1136 bool found = settings_dictionary->GetWithoutPathExpansion(
1137 type_name, &value);
1138 DCHECK(found);
1139 settings_dictionary_copy->SetWithoutPathExpansion(
1140 type_name, value->DeepCopy());
1141 }
1142 }
1143
1144 // Ignore empty dictionaryies.
1145 if (!settings_dictionary_copy->empty()) {
1146 std::string new_key = pattern_pair.first.ToString();
1147 // Existing values are overwritten.
1148 obsolete_settings_dictionary->SetWithoutPathExpansion(
1149 new_key, settings_dictionary_copy.release());
1150 }
1081 } 1151 }
1082 } 1152 }
1083 } 1153 }
1154
1155 void PrefProvider::MigrateObsoleteGeolocationPref() {
1156 if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings))
1157 return;
1158
1159 const DictionaryValue* geolocation_settings =
1160 prefs_->GetDictionary(prefs::kGeolocationContentSettings);
1161 for (DictionaryValue::key_iterator i =
1162 geolocation_settings->begin_keys();
1163 i != geolocation_settings->end_keys();
1164 ++i) {
1165 const std::string& primary_key(*i);
1166 GURL primary_url(primary_key);
1167 DCHECK(primary_url.is_valid());
1168
1169 DictionaryValue* requesting_origin_settings = NULL;
1170 bool found = geolocation_settings->GetDictionaryWithoutPathExpansion(
1171 primary_key, &requesting_origin_settings);
1172 DCHECK(found);
1173
1174 for (DictionaryValue::key_iterator j =
1175 requesting_origin_settings->begin_keys();
1176 j != requesting_origin_settings->end_keys();
1177 ++j) {
1178 const std::string& secondary_key(*j);
1179 GURL secondary_url(secondary_key);
1180 DCHECK(secondary_url.is_valid());
1181
1182 int setting_value;
1183 found = requesting_origin_settings->GetIntegerWithoutPathExpansion(
1184 secondary_key, &setting_value);
1185 DCHECK(found);
1186
1187 ContentSettingsPattern primary_pattern =
1188 ContentSettingsPattern::FromURLNoWildcard(primary_url);
1189 ContentSettingsPattern secondary_pattern =
1190 ContentSettingsPattern::FromURLNoWildcard(secondary_url);
1191 DCHECK(primary_pattern.IsValid() && secondary_pattern.IsValid());
1192
1193 SetContentSetting(primary_pattern,
1194 secondary_pattern,
1195 CONTENT_SETTINGS_TYPE_GEOLOCATION,
1196 std::string(),
1197 IntToContentSetting(setting_value));
1198 }
1199 }
1200 }
1201
1202 void PrefProvider::SyncObsoleteGeolocationPref() {
1203 DCHECK(prefs_);
1204 DCHECK(prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs));
1205
1206 // Clear the obsolete preference for geolocation settings. Then copy all
1207 // geolocation settings from the new preference to the obsolete one.
1208 {
1209 DictionaryPrefUpdate update(prefs_, prefs::kGeolocationContentSettings);
1210 DictionaryValue* obsolete_geolocation_settings = update.Get();
1211 obsolete_geolocation_settings->Clear();
Bernhard Bauer 2011/08/10 10:40:03 Oh, wait, we use UpdateObsoleteGeolocationPref now
markusheintz_ 2011/08/10 11:21:42 Done.
1212 }
1213
1214 const DictionaryValue* pattern_pairs_dictionary =
1215 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
1216 for (DictionaryValue::key_iterator i =
1217 pattern_pairs_dictionary->begin_keys();
1218 i != pattern_pairs_dictionary->end_keys();
1219 ++i) {
1220 const std::string& key(*i);
1221 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
1222 ParsePatternString(key);
1223 DCHECK(pattern_pair.first.IsValid() && pattern_pair.second.IsValid());
1224
1225 DictionaryValue* settings_dictionary = NULL;
1226 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion(
1227 key, &settings_dictionary);
Bernhard Bauer 2011/08/10 10:40:03 Nit: indentation
markusheintz_ 2011/08/10 11:21:42 Done.
1228 DCHECK(found);
1229
1230 if (settings_dictionary->HasKey(
1231 kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION])) {
1232 int setting_value;
1233 settings_dictionary->GetInteger(
1234 kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION], &setting_value);
1235
1236 UpdateObsoleteGeolocationPref(
Bernhard Bauer 2011/08/10 10:40:03 Nit: indentation.
markusheintz_ 2011/08/10 11:21:42 Done.
1237 pattern_pair.first,
1238 pattern_pair.second,
1239 ContentSetting(setting_value));
1240 }
1241 }
1242 }
1084 1243
1085 } // namespace content_settings 1244 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698