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

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: rename variables 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 <list> 7 #include <list>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 20 matching lines...) Expand all
31 namespace { 31 namespace {
32 32
33 // The preference keys where resource identifiers are stored for 33 // The preference keys where resource identifiers are stored for
34 // ContentSettingsType values that support resource identifiers. 34 // ContentSettingsType values that support resource identifiers.
35 const char* kResourceTypeNames[] = { 35 const char* kResourceTypeNames[] = {
36 NULL, 36 NULL,
37 NULL, 37 NULL,
38 NULL, 38 NULL,
39 "per_plugin", 39 "per_plugin",
40 NULL, 40 NULL,
41 NULL, // Not used for Geolocation 41 NULL,
42 NULL, // Not used for Notifications 42 NULL, // Not used for Notifications
43 }; 43 };
44 COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES, 44 COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
45 resource_type_names_incorrect_size); 45 resource_type_names_incorrect_size);
46 46
47 // The default setting for each content type. 47 // The default setting for each content type.
48 const ContentSetting kDefaultSettings[] = { 48 const ContentSetting kDefaultSettings[] = {
49 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES 49 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES
50 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES 50 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES
51 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT 51 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT
52 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS 52 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS
53 CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS 53 CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS
54 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_GEOLOCATION 54 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_GEOLOCATION
55 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_NOTIFICATIONS 55 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_NOTIFICATIONS
56 }; 56 };
57 COMPILE_ASSERT(arraysize(kDefaultSettings) == CONTENT_SETTINGS_NUM_TYPES, 57 COMPILE_ASSERT(arraysize(kDefaultSettings) == CONTENT_SETTINGS_NUM_TYPES,
58 default_settings_incorrect_size); 58 default_settings_incorrect_size);
59 59
60 // The names of the ContentSettingsType values, for use with dictionary prefs. 60 // The names of the ContentSettingsType values, for use with dictionary prefs.
61 const char* kTypeNames[] = { 61 const char* kTypeNames[] = {
62 "cookies", 62 "cookies",
63 "images", 63 "images",
64 "javascript", 64 "javascript",
65 "plugins", 65 "plugins",
66 "popups", 66 "popups",
67 "geolocation",
67 // TODO(markusheintz): Refactoring in progress. Content settings exceptions 68 // TODO(markusheintz): Refactoring in progress. Content settings exceptions
68 // for notifications and geolocation will be added next. 69 // for notifications added next.
69 "geolocation", // Only used for default Geolocation settings
70 "notifications", // Only used for default Notifications settings. 70 "notifications", // Only used for default Notifications settings.
71 }; 71 };
72 COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES, 72 COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
73 type_names_incorrect_size); 73 type_names_incorrect_size);
74 74
75 void SetDefaultContentSettings(DictionaryValue* default_settings) { 75 void SetDefaultContentSettings(DictionaryValue* default_settings) {
76 default_settings->Clear(); 76 default_settings->Clear();
77 77
78 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { 78 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
79 if (kTypeNames[i] != NULL) { 79 if (kTypeNames[i] != NULL) {
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 // static 350 // static
351 void PrefProvider::RegisterUserPrefs(PrefService* prefs) { 351 void PrefProvider::RegisterUserPrefs(PrefService* prefs) {
352 prefs->RegisterIntegerPref( 352 prefs->RegisterIntegerPref(
353 prefs::kContentSettingsVersion, 353 prefs::kContentSettingsVersion,
354 ContentSettingsPattern::kContentSettingsPatternVersion, 354 ContentSettingsPattern::kContentSettingsPatternVersion,
355 PrefService::UNSYNCABLE_PREF); 355 PrefService::UNSYNCABLE_PREF);
356 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatternPairs, 356 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatternPairs,
357 PrefService::SYNCABLE_PREF); 357 PrefService::SYNCABLE_PREF);
358 358
359 // Obsolete prefs, for migration: 359 // Obsolete prefs, for migration:
360 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings,
361 PrefService::SYNCABLE_PREF);
360 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, 362 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns,
361 PrefService::SYNCABLE_PREF); 363 PrefService::SYNCABLE_PREF);
362 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, 364 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts,
363 PrefService::UNSYNCABLE_PREF); 365 PrefService::UNSYNCABLE_PREF);
364 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, 366 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings,
365 PrefService::UNSYNCABLE_PREF); 367 PrefService::UNSYNCABLE_PREF);
366 } 368 }
367 369
368 PrefProvider::PrefProvider(PrefService* prefs, 370 PrefProvider::PrefProvider(PrefService* prefs,
369 bool incognito) 371 bool incognito)
370 : prefs_(prefs), 372 : prefs_(prefs),
371 is_incognito_(incognito), 373 is_incognito_(incognito),
372 updating_preferences_(false) { 374 updating_preferences_(false) {
373 DCHECK(prefs_); 375 DCHECK(prefs_);
374 if (!is_incognito_) { 376 if (!is_incognito_) {
375 // Migrate obsolete preferences. 377 // Migrate obsolete preferences.
376 MigrateObsoletePerhostPref(); 378 MigrateObsoletePerhostPref();
377 MigrateObsoletePopupsPref(); 379 MigrateObsoletePopupsPref();
378 MigrateObsoleteContentSettingsPatternPref(); 380 MigrateObsoleteContentSettingsPatternPref();
381 MigrateObsoleteGeolocationPref();
379 } 382 }
380 383
381 // Verify preferences version. 384 // Verify preferences version.
382 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { 385 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) {
383 prefs_->SetInteger(prefs::kContentSettingsVersion, 386 prefs_->SetInteger(prefs::kContentSettingsVersion,
384 ContentSettingsPattern::kContentSettingsPatternVersion); 387 ContentSettingsPattern::kContentSettingsPatternVersion);
385 } 388 }
386 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > 389 if (prefs_->GetInteger(prefs::kContentSettingsVersion) >
387 ContentSettingsPattern::kContentSettingsPatternVersion) { 390 ContentSettingsPattern::kContentSettingsPatternVersion) {
388 return; 391 return;
389 } 392 }
390 393
391 // Read content settings exceptions. 394 // Read content settings exceptions.
392 ReadContentSettingsFromPref(false); 395 ReadContentSettingsFromPref(false);
393 396
394 if (!is_incognito_) { 397 if (!is_incognito_) {
395 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", 398 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions",
396 value_map_.size()); 399 value_map_.size());
397 } 400 }
398 401
399 pref_change_registrar_.Init(prefs_); 402 pref_change_registrar_.Init(prefs_);
400 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); 403 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this);
401 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); 404 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this);
405 pref_change_registrar_.Add(prefs::kGeolocationContentSettings, this);
402 } 406 }
403 407
404 ContentSetting PrefProvider::GetContentSetting( 408 ContentSetting PrefProvider::GetContentSetting(
405 const GURL& primary_url, 409 const GURL& primary_url,
406 const GURL& secondary_url, 410 const GURL& secondary_url,
407 ContentSettingsType content_type, 411 ContentSettingsType content_type,
408 const ResourceIdentifier& resource_identifier) const { 412 const ResourceIdentifier& resource_identifier) const {
409 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito 413 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito
410 // profile, this will always return NULL. 414 // profile, this will always return NULL.
411 // TODO(markusheintz): I don't like this. I'd like to have an 415 // 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
458 } 462 }
459 463
460 void PrefProvider::SetContentSetting( 464 void PrefProvider::SetContentSetting(
461 const ContentSettingsPattern& primary_pattern, 465 const ContentSettingsPattern& primary_pattern,
462 const ContentSettingsPattern& secondary_pattern, 466 const ContentSettingsPattern& secondary_pattern,
463 ContentSettingsType content_type, 467 ContentSettingsType content_type,
464 const ResourceIdentifier& resource_identifier, 468 const ResourceIdentifier& resource_identifier,
465 ContentSetting setting) { 469 ContentSetting setting) {
466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
467 DCHECK(prefs_); 471 DCHECK(prefs_);
468 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 472 DCHECK(kTypeNames[content_type] != NULL);
469 473
470 // Update in memory value map. 474 // Update in memory value map.
471 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; 475 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
472 if (!is_incognito_) 476 if (!is_incognito_)
473 map_to_modify = &value_map_; 477 map_to_modify = &value_map_;
474 478
475 { 479 {
476 base::AutoLock auto_lock(lock_); 480 base::AutoLock auto_lock(lock_);
477 if (setting == CONTENT_SETTING_DEFAULT) { 481 if (setting == CONTENT_SETTING_DEFAULT) {
478 map_to_modify->DeleteValue( 482 map_to_modify->DeleteValue(
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 const NotificationDetails& details) { 548 const NotificationDetails& details) {
545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 549 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
546 550
547 if (type == chrome::NOTIFICATION_PREF_CHANGED) { 551 if (type == chrome::NOTIFICATION_PREF_CHANGED) {
548 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); 552 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
549 if (updating_preferences_) 553 if (updating_preferences_)
550 return; 554 return;
551 555
552 std::string* name = Details<std::string>(details).ptr(); 556 std::string* name = Details<std::string>(details).ptr();
553 if (*name == prefs::kContentSettingsPatternPairs) { 557 if (*name == prefs::kContentSettingsPatternPairs) {
554 SyncObsoletePref(); 558 SyncObsoletePatternPref();
559 SyncObsoleteGeolocationPref();
555 ReadContentSettingsFromPref(true); 560 ReadContentSettingsFromPref(true);
556 } else if (*name == prefs::kContentSettingsPatterns) { 561 } else if (*name == prefs::kContentSettingsPatterns) {
557 AutoReset<bool> auto_reset(&updating_preferences_, true); 562 AutoReset<bool> auto_reset(&updating_preferences_, true);
558 MigrateObsoleteContentSettingsPatternPref(); 563 MigrateObsoleteContentSettingsPatternPref();
559 ReadContentSettingsFromPref(true); 564 ReadContentSettingsFromPref(true);
565 } else if (*name == prefs::kGeolocationContentSettings) {
566 AutoReset<bool> auto_reset(&updating_preferences_, true);
567 MigrateObsoleteGeolocationPref();
568 ReadContentSettingsFromPref(true);
560 } else { 569 } else {
561 NOTREACHED() << "Unexpected preference observed"; 570 NOTREACHED() << "Unexpected preference observed";
562 return; 571 return;
563 } 572 }
564 573
565 NotifyObservers(ContentSettingsPattern(), 574 NotifyObservers(ContentSettingsPattern(),
566 ContentSettingsPattern(), 575 ContentSettingsPattern(),
567 CONTENT_SETTINGS_TYPE_DEFAULT, 576 CONTENT_SETTINGS_TYPE_DEFAULT,
568 std::string()); 577 std::string());
569 } else { 578 } else {
(...skipping 13 matching lines...) Expand all
583 const ContentSettingsPattern& secondary_pattern, 592 const ContentSettingsPattern& secondary_pattern,
584 ContentSettingsType content_type, 593 ContentSettingsType content_type,
585 const ResourceIdentifier& resource_identifier, 594 const ResourceIdentifier& resource_identifier,
586 ContentSetting setting) { 595 ContentSetting setting) {
587 AutoReset<bool> auto_reset(&updating_preferences_, true); 596 AutoReset<bool> auto_reset(&updating_preferences_, true);
588 UpdatePatternPairsPref(primary_pattern, 597 UpdatePatternPairsPref(primary_pattern,
589 secondary_pattern, 598 secondary_pattern,
590 content_type, 599 content_type,
591 resource_identifier, 600 resource_identifier,
592 setting); 601 setting);
593 UpdatePatternsPref(primary_pattern, 602 if (content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION &&
594 secondary_pattern, 603 content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
595 content_type, 604 UpdateObsoletePatternsPref(primary_pattern,
596 resource_identifier, 605 secondary_pattern,
597 setting); 606 content_type,
607 resource_identifier,
608 setting);
609 }
610 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) {
611 SyncObsoleteGeolocationPref();
612 }
598 } 613 }
599 614
600 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { 615 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) {
601 base::AutoLock auto_lock(lock_); 616 base::AutoLock auto_lock(lock_);
602 617
603 const DictionaryValue* all_settings_dictionary = 618 const DictionaryValue* all_settings_dictionary =
604 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); 619 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
605 620
606 if (overwrite) 621 if (overwrite)
607 value_map_.clear(); 622 value_map_.clear();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 content_type, 697 content_type,
683 ResourceIdentifier(""), 698 ResourceIdentifier(""),
684 Value::CreateIntegerValue(setting)); 699 Value::CreateIntegerValue(setting));
685 } 700 }
686 } 701 }
687 } 702 }
688 } 703 }
689 } 704 }
690 } 705 }
691 706
692 void PrefProvider::UpdatePatternsPref( 707 void PrefProvider::UpdateObsoletePatternsPref(
693 const ContentSettingsPattern& primary_pattern, 708 const ContentSettingsPattern& primary_pattern,
694 const ContentSettingsPattern& secondary_pattern, 709 const ContentSettingsPattern& secondary_pattern,
695 ContentSettingsType content_type, 710 ContentSettingsType content_type,
696 const ResourceIdentifier& resource_identifier, 711 const ResourceIdentifier& resource_identifier,
697 ContentSetting setting) { 712 ContentSetting setting) {
698 DictionaryPrefUpdate update(prefs_, 713 DictionaryPrefUpdate update(prefs_,
699 prefs::kContentSettingsPatterns); 714 prefs::kContentSettingsPatterns);
700 DictionaryValue* all_settings_dictionary = update.Get(); 715 DictionaryValue* all_settings_dictionary = update.Get();
701 716
702 // Get settings dictionary for |primary_pattern|. 717 // Get settings dictionary for |primary_pattern|.
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
957 CONTENT_SETTINGS_TYPE_POPUPS, 972 CONTENT_SETTINGS_TYPE_POPUPS,
958 "", 973 "",
959 CONTENT_SETTING_ALLOW); 974 CONTENT_SETTING_ALLOW);
960 } 975 }
961 prefs_->ClearPref(prefs::kPopupWhitelistedHosts); 976 prefs_->ClearPref(prefs::kPopupWhitelistedHosts);
962 } 977 }
963 } 978 }
964 979
965 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() { 980 void PrefProvider::MigrateObsoleteContentSettingsPatternPref() {
966 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) { 981 if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) {
967 const DictionaryValue* all_settings_dictionary = 982 const DictionaryValue* patterns_dictionary =
968 prefs_->GetDictionary(prefs::kContentSettingsPatterns); 983 prefs_->GetDictionary(prefs::kContentSettingsPatterns);
969 984
970 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); 985 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
971 DictionaryValue* exceptions_dictionary; 986 DictionaryValue* exceptions_dictionary;
972 exceptions_dictionary = update.Get(); 987 exceptions_dictionary = update.Get();
973 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); 988 for (DictionaryValue::key_iterator i(patterns_dictionary->begin_keys());
974 i != all_settings_dictionary->end_keys(); 989 i != patterns_dictionary->end_keys();
975 ++i) { 990 ++i) {
976 const std::string& key(*i); 991 const std::string& key(*i);
977 if (key.empty()) 992 if (key.empty())
978 continue; 993 continue;
979 994
980 // Validate pattern string and skip it if it is invalid. 995 // Validate pattern string and skip it if it is invalid.
981 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = 996 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
982 ParsePatternString(key); 997 ParsePatternString(key);
983 const ContentSettingsPattern& primary_pattern = pattern_pair.first; 998 const ContentSettingsPattern& primary_pattern = pattern_pair.first;
984 if (!primary_pattern.IsValid()) { 999 if (!primary_pattern.IsValid()) {
985 LOG(DFATAL) << "Invalid pattern strings: " << key; 1000 LOG(DFATAL) << "Invalid pattern strings: " << key;
986 continue; 1001 continue;
987 } 1002 }
988 1003
989 // Copy dictionary value. 1004 // Copy dictionary value.
990 // Get old settings. 1005 // Get old settings.
991 DictionaryValue* dictionary = NULL; 1006 DictionaryValue* dictionary = NULL;
992 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 1007 bool found = patterns_dictionary->GetDictionaryWithoutPathExpansion(
993 key, &dictionary); 1008 key, &dictionary);
994 DCHECK(found); 1009 DCHECK(found);
995 1010
996 // Create new dictionary key. 1011 // Create new dictionary key.
997 std::string new_pattern_str = CreatePatternString( 1012 std::string new_pattern_str = CreatePatternString(
998 primary_pattern, ContentSettingsPattern::Wildcard()); 1013 primary_pattern, ContentSettingsPattern::Wildcard());
999 1014
1000 // Existing values are overwritten. 1015 // Existing values are overwritten.
1001 exceptions_dictionary->SetWithoutPathExpansion( 1016 exceptions_dictionary->SetWithoutPathExpansion(
1002 new_pattern_str, dictionary->DeepCopy()); 1017 new_pattern_str, dictionary->DeepCopy());
1003 } 1018 }
1004 } 1019 }
1005 } 1020 }
1006 1021
1007 void PrefProvider::SyncObsoletePref() { 1022 void PrefProvider::SyncObsoletePatternPref() {
1008 AutoReset<bool> auto_reset(&updating_preferences_, true); 1023 AutoReset<bool> auto_reset(&updating_preferences_, true);
1009 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && 1024 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) &&
1010 !is_incognito_) { 1025 !is_incognito_) {
1011 const DictionaryValue* pattern_pairs_dictionary = 1026 const DictionaryValue* pattern_pairs_dictionary =
1012 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); 1027 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
1013 1028
1014 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns); 1029 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns);
1015 DictionaryValue* obsolete_settings_dictionary = update.Get(); 1030 DictionaryValue* obsolete_settings_dictionary = update.Get();
1016 1031
1017 for (DictionaryValue::key_iterator i = 1032 for (DictionaryValue::key_iterator i =
1018 pattern_pairs_dictionary->begin_keys(); 1033 pattern_pairs_dictionary->begin_keys();
1019 i != pattern_pairs_dictionary->end_keys(); 1034 i != pattern_pairs_dictionary->end_keys();
1020 ++i) { 1035 ++i) {
1021 const std::string& key(*i); 1036 const std::string& key(*i);
1022 // Validate pattern string and skip it if it is invalid. 1037 // Validate pattern string and skip it if it is invalid.
1023 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = 1038 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
1024 ParsePatternString(key); 1039 ParsePatternString(key);
1025 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid()) { 1040 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid()) {
1026 LOG(DFATAL) << "Invalid pattern strings: " << key; 1041 LOG(DFATAL) << "Invalid pattern strings: " << key;
1027 continue; 1042 continue;
1028 } 1043 }
1029 1044
1030 // Copy dictionary 1045 DictionaryValue* settings_dictionary = NULL;
1031 DictionaryValue* dictionary = NULL;
1032 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( 1046 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion(
1033 key, &dictionary); 1047 key, &settings_dictionary);
1034 DCHECK(found); 1048 DCHECK(found);
1035 std::string new_key = pattern_pair.first.ToString(); 1049 scoped_ptr<DictionaryValue> settings_dictionary_copy(
1036 // Existing values are overwritten. 1050 new DictionaryValue());
1037 obsolete_settings_dictionary->SetWithoutPathExpansion( 1051 for (size_t i = CONTENT_SETTINGS_TYPE_COOKIES;
1038 new_key, dictionary->DeepCopy()); 1052 i <= CONTENT_SETTINGS_TYPE_POPUPS;
1053 ++i) {
1054 DCHECK(kTypeNames[i]);
1055 std::string type_name(kTypeNames[i]);
1056 if (settings_dictionary->HasKey(type_name)) {
1057 Value* value = NULL;
1058 bool found = settings_dictionary->GetWithoutPathExpansion(
1059 type_name, &value);
1060 DCHECK(found);
1061 settings_dictionary_copy->SetWithoutPathExpansion(
1062 type_name, value->DeepCopy());
1063 }
1064 }
1065
1066 // Ignore empty dictionaryies.
1067 if (!settings_dictionary_copy->empty()) {
1068 std::string new_key = pattern_pair.first.ToString();
1069 // Existing values are overwritten.
1070 obsolete_settings_dictionary->SetWithoutPathExpansion(
1071 new_key, settings_dictionary_copy.release());
1072 }
1039 } 1073 }
1040 } 1074 }
1041 } 1075 }
1042 1076
1077 void PrefProvider::MigrateObsoleteGeolocationPref() {
1078 if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings))
1079 return;
1080
1081 const DictionaryValue* geolocation_settings =
1082 prefs_->GetDictionary(prefs::kGeolocationContentSettings);
1083 for (DictionaryValue::key_iterator i =
1084 geolocation_settings->begin_keys();
1085 i != geolocation_settings->end_keys();
1086 ++i) {
1087 const std::string& primary_key(*i);
1088 GURL primary_url(primary_key);
1089 DCHECK(primary_url.is_valid());
1090
1091 DictionaryValue* requesting_origin_settings = NULL;
1092 bool found = geolocation_settings->GetDictionaryWithoutPathExpansion(
1093 primary_key, &requesting_origin_settings);
Bernhard Bauer 2011/08/05 10:08:46 Nit: indent four spaces less now.
markusheintz_ 2011/08/09 13:45:48 Done.
1094 DCHECK(found);
1095
1096 for (DictionaryValue::key_iterator j =
1097 requesting_origin_settings->begin_keys();
1098 j != requesting_origin_settings->end_keys();
1099 ++j) {
1100 const std::string& secondary_key(*j);
1101 GURL secondary_url(secondary_key);
1102 DCHECK(secondary_url.is_valid());
1103
1104 int setting_value;
1105 found = requesting_origin_settings->GetIntegerWithoutPathExpansion(
1106 secondary_key, &setting_value);
Bernhard Bauer 2011/08/05 10:08:46 Nit: indent four spaces less now.
markusheintz_ 2011/08/09 13:45:48 Done.
1107 DCHECK(found);
1108
1109 ContentSettingsPattern primary_pattern =
1110 ContentSettingsPattern::FromURLNoWildcard(primary_url);
1111 ContentSettingsPattern secondary_pattern =
1112 ContentSettingsPattern::FromURLNoWildcard(secondary_url);
1113 DCHECK(primary_pattern.IsValid() && secondary_pattern.IsValid());
1114
1115 SetContentSetting(primary_pattern,
1116 secondary_pattern,
1117 CONTENT_SETTINGS_TYPE_GEOLOCATION,
1118 std::string(),
1119 IntToContentSetting(setting_value));
1120 }
1121 }
1122 }
1123
1124 void PrefProvider::SyncObsoleteGeolocationPref() {
1125 DCHECK(prefs_);
1126 DCHECK(prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs));
1127
1128 scoped_ptr<DictionaryValue> obsolete_geolocation_settings(
1129 new DictionaryValue());
1130
1131 const DictionaryValue* pattern_pairs_dictionary =
1132 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
1133 for (DictionaryValue::key_iterator i = pattern_pairs_dictionary->begin_keys();
1134 i != pattern_pairs_dictionary->end_keys();
1135 ++i) {
1136 const std::string& key(*i);
1137 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
1138 ParsePatternString(key);
1139 DCHECK(pattern_pair.first.IsValid() && pattern_pair.second.IsValid());
1140
1141 DictionaryValue* settings_dictionary = NULL;
1142 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion(
1143 key, &settings_dictionary);
1144 DCHECK(found);
1145
1146 if (settings_dictionary->HasKey(
1147 kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION])) {
1148 const GURL primary_url(pattern_pair.first.ToString());
1149 const GURL secondary_url(pattern_pair.second.ToString());
1150 DCHECK(primary_url.is_valid() && secondary_url.is_valid());
1151
1152 int setting_value;
1153 settings_dictionary->GetInteger(
1154 kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION], &setting_value);
1155
1156 // Add to obsolete pref.
1157 DictionaryValue* one_origin_settings = NULL;
1158 bool found =
1159 obsolete_geolocation_settings->GetDictionaryWithoutPathExpansion(
1160 primary_url.spec(), &one_origin_settings);
1161 if (!found) {
1162 one_origin_settings = new DictionaryValue();
1163 obsolete_geolocation_settings->SetWithoutPathExpansion(
1164 primary_url.spec(), one_origin_settings);
1165 }
1166
1167 one_origin_settings->SetWithoutPathExpansion(
1168 secondary_url.spec(), Value::CreateIntegerValue(setting_value));
1169 }
1170 }
1171
1172 prefs_->Set(prefs::kGeolocationContentSettings,
1173 *obsolete_geolocation_settings);
1174 }
1175
1043 } // namespace content_settings 1176 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698