OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 AutoReset<bool> auto_reset(&updating_preferences_, true); |
Bernhard Bauer
2011/08/10 11:55:29
Nit: Move this (and the ones below) out of the if-
markusheintz_
2011/08/10 12:47:58
Done.
| |
563 SyncObsoletePatternPref(); | |
564 SyncObsoleteGeolocationPref(); | |
559 ReadContentSettingsFromPref(true); | 565 ReadContentSettingsFromPref(true); |
560 } else if (*name == prefs::kContentSettingsPatterns) { | 566 } else if (*name == prefs::kContentSettingsPatterns) { |
561 AutoReset<bool> auto_reset(&updating_preferences_, true); | 567 AutoReset<bool> auto_reset(&updating_preferences_, true); |
562 MigrateObsoleteContentSettingsPatternPref(); | 568 MigrateObsoleteContentSettingsPatternPref(); |
563 ReadContentSettingsFromPref(true); | 569 ReadContentSettingsFromPref(true); |
570 } else if (*name == prefs::kGeolocationContentSettings) { | |
571 AutoReset<bool> auto_reset(&updating_preferences_, true); | |
572 MigrateObsoleteGeolocationPref(); | |
573 ReadContentSettingsFromPref(true); | |
564 } else { | 574 } else { |
565 NOTREACHED() << "Unexpected preference observed"; | 575 NOTREACHED() << "Unexpected preference observed"; |
566 return; | 576 return; |
567 } | 577 } |
568 | 578 |
569 NotifyObservers(ContentSettingsPattern(), | 579 NotifyObservers(ContentSettingsPattern(), |
570 ContentSettingsPattern(), | 580 ContentSettingsPattern(), |
571 CONTENT_SETTINGS_TYPE_DEFAULT, | 581 CONTENT_SETTINGS_TYPE_DEFAULT, |
572 std::string()); | 582 std::string()); |
573 } else { | 583 } else { |
(...skipping 13 matching lines...) Expand all Loading... | |
587 const ContentSettingsPattern& secondary_pattern, | 597 const ContentSettingsPattern& secondary_pattern, |
588 ContentSettingsType content_type, | 598 ContentSettingsType content_type, |
589 const ResourceIdentifier& resource_identifier, | 599 const ResourceIdentifier& resource_identifier, |
590 ContentSetting setting) { | 600 ContentSetting setting) { |
591 AutoReset<bool> auto_reset(&updating_preferences_, true); | 601 AutoReset<bool> auto_reset(&updating_preferences_, true); |
592 UpdatePatternPairsPref(primary_pattern, | 602 UpdatePatternPairsPref(primary_pattern, |
593 secondary_pattern, | 603 secondary_pattern, |
594 content_type, | 604 content_type, |
595 resource_identifier, | 605 resource_identifier, |
596 setting); | 606 setting); |
597 UpdatePatternsPref(primary_pattern, | 607 if (content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION && |
598 secondary_pattern, | 608 content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { |
599 content_type, | 609 UpdateObsoletePatternsPref(primary_pattern, |
600 resource_identifier, | 610 secondary_pattern, |
601 setting); | 611 content_type, |
612 resource_identifier, | |
613 setting); | |
614 } | |
615 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { | |
616 UpdateObsoleteGeolocationPref(primary_pattern, secondary_pattern, setting); | |
617 } | |
602 } | 618 } |
603 | 619 |
604 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { | 620 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { |
605 base::AutoLock auto_lock(lock_); | 621 base::AutoLock auto_lock(lock_); |
606 | 622 |
607 const DictionaryValue* all_settings_dictionary = | 623 const DictionaryValue* all_settings_dictionary = |
608 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | 624 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); |
609 | 625 |
610 if (overwrite) | 626 if (overwrite) |
611 value_map_.clear(); | 627 value_map_.clear(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
686 content_type, | 702 content_type, |
687 ResourceIdentifier(""), | 703 ResourceIdentifier(""), |
688 Value::CreateIntegerValue(setting)); | 704 Value::CreateIntegerValue(setting)); |
689 } | 705 } |
690 } | 706 } |
691 } | 707 } |
692 } | 708 } |
693 } | 709 } |
694 } | 710 } |
695 | 711 |
696 void PrefProvider::UpdatePatternsPref( | 712 void PrefProvider::UpdateObsoletePatternsPref( |
697 const ContentSettingsPattern& primary_pattern, | 713 const ContentSettingsPattern& primary_pattern, |
698 const ContentSettingsPattern& secondary_pattern, | 714 const ContentSettingsPattern& secondary_pattern, |
699 ContentSettingsType content_type, | 715 ContentSettingsType content_type, |
700 const ResourceIdentifier& resource_identifier, | 716 const ResourceIdentifier& resource_identifier, |
701 ContentSetting setting) { | 717 ContentSetting setting) { |
702 DictionaryPrefUpdate update(prefs_, | 718 DictionaryPrefUpdate update(prefs_, |
703 prefs::kContentSettingsPatterns); | 719 prefs::kContentSettingsPatterns); |
704 DictionaryValue* all_settings_dictionary = update.Get(); | 720 DictionaryValue* all_settings_dictionary = update.Get(); |
705 | 721 |
706 // Get settings dictionary for |primary_pattern|. | 722 // Get settings dictionary for |primary_pattern|. |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
819 } | 835 } |
820 } | 836 } |
821 // Remove the settings dictionary if it is empty. | 837 // Remove the settings dictionary if it is empty. |
822 if (settings_dictionary->empty()) { | 838 if (settings_dictionary->empty()) { |
823 all_settings_dictionary->RemoveWithoutPathExpansion( | 839 all_settings_dictionary->RemoveWithoutPathExpansion( |
824 pattern_str, NULL); | 840 pattern_str, NULL); |
825 } | 841 } |
826 } | 842 } |
827 } | 843 } |
828 | 844 |
845 void PrefProvider::UpdateObsoleteGeolocationPref( | |
846 const ContentSettingsPattern& primary_pattern, | |
847 const ContentSettingsPattern& secondary_pattern, | |
848 ContentSetting setting) { | |
849 if (!prefs_) | |
850 return; | |
851 | |
852 const GURL requesting_origin(primary_pattern.ToString()); | |
853 const GURL embedding_origin(secondary_pattern.ToString()); | |
854 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); | |
855 | |
856 DictionaryPrefUpdate update(prefs_, prefs::kGeolocationContentSettings); | |
857 DictionaryValue* obsolete_geolocation_settings = update.Get(); | |
858 DictionaryValue* requesting_origin_settings_dictionary = NULL; | |
859 obsolete_geolocation_settings->GetDictionaryWithoutPathExpansion( | |
860 requesting_origin.spec(), &requesting_origin_settings_dictionary); | |
861 if (setting == CONTENT_SETTING_DEFAULT) { | |
862 if (requesting_origin_settings_dictionary) { | |
863 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( | |
864 embedding_origin.spec(), NULL); | |
865 if (requesting_origin_settings_dictionary->empty()) { | |
866 obsolete_geolocation_settings->RemoveWithoutPathExpansion( | |
867 requesting_origin.spec(), NULL); | |
868 } | |
869 } | |
870 } else { | |
871 if (!requesting_origin_settings_dictionary) { | |
872 requesting_origin_settings_dictionary = new DictionaryValue; | |
873 obsolete_geolocation_settings->SetWithoutPathExpansion( | |
874 requesting_origin.spec(), requesting_origin_settings_dictionary); | |
875 } | |
876 DCHECK(requesting_origin_settings_dictionary); | |
877 requesting_origin_settings_dictionary->SetWithoutPathExpansion( | |
878 embedding_origin.spec(), Value::CreateIntegerValue(setting)); | |
879 } | |
880 } | |
881 | |
829 // static | 882 // static |
830 void PrefProvider::CanonicalizeContentSettingsExceptions( | 883 void PrefProvider::CanonicalizeContentSettingsExceptions( |
831 DictionaryValue* all_settings_dictionary) { | 884 DictionaryValue* all_settings_dictionary) { |
832 DCHECK(all_settings_dictionary); | 885 DCHECK(all_settings_dictionary); |
833 | 886 |
834 std::vector<std::string> remove_items; | 887 std::vector<std::string> remove_items; |
835 std::vector<std::pair<std::string, std::string> > move_items; | 888 std::vector<std::pair<std::string, std::string> > move_items; |
836 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | 889 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
837 i != all_settings_dictionary->end_keys(); ++i) { | 890 i != all_settings_dictionary->end_keys(); ++i) { |
838 const std::string& pattern_str(*i); | 891 const std::string& pattern_str(*i); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
962 "", | 1015 "", |
963 CONTENT_SETTING_ALLOW); | 1016 CONTENT_SETTING_ALLOW); |
964 } | 1017 } |
965 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); | 1018 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); |
966 } | 1019 } |
967 } | 1020 } |
968 | 1021 |
969 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { | 1022 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { |
970 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { | 1023 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { |
971 const DictionaryValue* patterns_dictionary = | 1024 const DictionaryValue* patterns_dictionary = |
972 prefs_->GetDictionary(prefs::kContentSettingsPatterns); | 1025 prefs_->GetDictionary(prefs::kContentSettingsPatterns); |
973 | 1026 |
974 // A map with an old key, new key mapping. If the new key is empty then the | 1027 // 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. | 1028 // value for the old key will be removed. |
976 StringMap keys_to_change; | 1029 StringMap keys_to_change; |
977 | |
978 { | 1030 { |
979 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); | 1031 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); |
980 DictionaryValue* pattern_pairs_dictionary = update.Get(); | 1032 DictionaryValue* pattern_pairs_dictionary = update.Get(); |
981 for (DictionaryValue::key_iterator i( | 1033 for (DictionaryValue::key_iterator i( |
982 patterns_dictionary->begin_keys()); | 1034 patterns_dictionary->begin_keys()); |
983 i != patterns_dictionary->end_keys(); | 1035 i != patterns_dictionary->end_keys(); |
984 ++i) { | 1036 ++i) { |
985 const std::string& key(*i); | 1037 const std::string& key(*i); |
986 // Remove broken pattern keys and fix keys with pattern pairs. | 1038 // Remove broken pattern keys and fix keys with pattern pairs. |
987 size_t sep_pos = key.find(","); | 1039 size_t sep_pos = key.find(","); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1039 DCHECK(found); | 1091 DCHECK(found); |
1040 if (!pattern_str_pair.second.empty()) { | 1092 if (!pattern_str_pair.second.empty()) { |
1041 mutable_patterns_dictionary->SetWithoutPathExpansion( | 1093 mutable_patterns_dictionary->SetWithoutPathExpansion( |
1042 pattern_str_pair.second, dict.release()); | 1094 pattern_str_pair.second, dict.release()); |
1043 } | 1095 } |
1044 } | 1096 } |
1045 } | 1097 } |
1046 } | 1098 } |
1047 } | 1099 } |
1048 | 1100 |
1049 void PrefProvider::SyncObsoletePref() { | 1101 void PrefProvider::SyncObsoletePatternPref() { |
1050 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 prefs_->ClearPref(prefs::kGeolocationContentSettings); | |
1209 const DictionaryValue* pattern_pairs_dictionary = | |
1210 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | |
1211 for (DictionaryValue::key_iterator i = | |
1212 pattern_pairs_dictionary->begin_keys(); | |
1213 i != pattern_pairs_dictionary->end_keys(); | |
1214 ++i) { | |
1215 const std::string& key(*i); | |
1216 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = | |
1217 ParsePatternString(key); | |
1218 DCHECK(pattern_pair.first.IsValid() && pattern_pair.second.IsValid()); | |
1219 | |
1220 DictionaryValue* settings_dictionary = NULL; | |
1221 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( | |
1222 key, &settings_dictionary); | |
1223 DCHECK(found); | |
1224 | |
1225 if (settings_dictionary->HasKey( | |
1226 kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION])) { | |
1227 int setting_value; | |
1228 settings_dictionary->GetInteger( | |
1229 kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION], &setting_value); | |
1230 | |
1231 UpdateObsoleteGeolocationPref(pattern_pair.first, | |
1232 pattern_pair.second, | |
1233 ContentSetting(setting_value)); | |
1234 } | |
1235 } | |
1236 } | |
1084 | 1237 |
1085 } // namespace content_settings | 1238 } // namespace content_settings |
OLD | NEW |