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 <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 Loading... | |
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) { |
80 default_settings->SetInteger(kTypeNames[i], | 80 default_settings->SetInteger(kTypeNames[i], |
81 kDefaultSettings[i]); | 81 kDefaultSettings[i]); |
82 } | 82 } |
83 } | 83 } |
84 } | 84 } |
85 | 85 |
86 void CopyKeys(DictionaryValue* source, | |
Bernhard Bauer
2011/08/03 15:22:50
Not used?
markusheintz_
2011/08/04 12:45:56
Sorry. Removed!
| |
87 DictionaryValue* target, | |
88 const char** keys_to_copy, | |
89 size_t size) { | |
90 for (DictionaryValue::key_iterator i(source->begin_keys()); | |
91 i != source->end_keys(); ++i) { | |
92 const std::string& key(*i); | |
93 for (size_t i = 0; i < size; ++i) { | |
94 if ((keys_to_copy[i] != NULL) && (keys_to_copy[i] == key)) { | |
95 Value* value; | |
96 bool found = source->GetWithoutPathExpansion( | |
97 key, &value); | |
98 DCHECK(found); | |
99 target->SetWithoutPathExpansion(key, value->DeepCopy()); | |
100 break; | |
101 } | |
102 } | |
103 } | |
104 } | |
105 | |
86 ContentSetting ValueToContentSetting(Value* value) { | 106 ContentSetting ValueToContentSetting(Value* value) { |
87 int int_value; | 107 int int_value; |
88 value->GetAsInteger(&int_value); | 108 value->GetAsInteger(&int_value); |
89 return IntToContentSetting(int_value); | 109 return IntToContentSetting(int_value); |
90 } | 110 } |
91 | 111 |
92 ContentSettingsType StringToContentSettingsType( | 112 ContentSettingsType StringToContentSettingsType( |
93 const std::string& content_type_str) { | 113 const std::string& content_type_str) { |
94 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { | 114 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { |
95 if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type_str)) | 115 if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type_str)) |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
350 // static | 370 // static |
351 void PrefProvider::RegisterUserPrefs(PrefService* prefs) { | 371 void PrefProvider::RegisterUserPrefs(PrefService* prefs) { |
352 prefs->RegisterIntegerPref( | 372 prefs->RegisterIntegerPref( |
353 prefs::kContentSettingsVersion, | 373 prefs::kContentSettingsVersion, |
354 ContentSettingsPattern::kContentSettingsPatternVersion, | 374 ContentSettingsPattern::kContentSettingsPatternVersion, |
355 PrefService::UNSYNCABLE_PREF); | 375 PrefService::UNSYNCABLE_PREF); |
356 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatternPairs, | 376 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatternPairs, |
357 PrefService::SYNCABLE_PREF); | 377 PrefService::SYNCABLE_PREF); |
358 | 378 |
359 // Obsolete prefs, for migration: | 379 // Obsolete prefs, for migration: |
380 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings, | |
381 PrefService::SYNCABLE_PREF); | |
360 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, | 382 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, |
361 PrefService::SYNCABLE_PREF); | 383 PrefService::SYNCABLE_PREF); |
362 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, | 384 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, |
363 PrefService::UNSYNCABLE_PREF); | 385 PrefService::UNSYNCABLE_PREF); |
364 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, | 386 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, |
365 PrefService::UNSYNCABLE_PREF); | 387 PrefService::UNSYNCABLE_PREF); |
366 } | 388 } |
367 | 389 |
368 PrefProvider::PrefProvider(PrefService* prefs, | 390 PrefProvider::PrefProvider(PrefService* prefs, |
369 bool incognito) | 391 bool incognito) |
370 : prefs_(prefs), | 392 : prefs_(prefs), |
371 is_incognito_(incognito), | 393 is_incognito_(incognito), |
372 updating_preferences_(false) { | 394 updating_preferences_(false) { |
373 DCHECK(prefs_); | 395 DCHECK(prefs_); |
374 if (!is_incognito_) { | 396 if (!is_incognito_) { |
375 // Migrate obsolete preferences. | 397 // Migrate obsolete preferences. |
376 MigrateObsoletePerhostPref(); | 398 MigrateObsoletePerhostPref(); |
377 MigrateObsoletePopupsPref(); | 399 MigrateObsoletePopupsPref(); |
378 MigrateObsoleteContentSettingsPatternPref(); | 400 MigrateObsoleteContentSettingsPatternPref(); |
401 MigrateObsoleteGeolocationPref(); | |
379 } | 402 } |
380 | 403 |
381 // Verify preferences version. | 404 // Verify preferences version. |
382 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { | 405 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { |
383 prefs_->SetInteger(prefs::kContentSettingsVersion, | 406 prefs_->SetInteger(prefs::kContentSettingsVersion, |
384 ContentSettingsPattern::kContentSettingsPatternVersion); | 407 ContentSettingsPattern::kContentSettingsPatternVersion); |
385 } | 408 } |
386 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > | 409 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > |
387 ContentSettingsPattern::kContentSettingsPatternVersion) { | 410 ContentSettingsPattern::kContentSettingsPatternVersion) { |
388 return; | 411 return; |
389 } | 412 } |
390 | 413 |
391 // Read content settings exceptions. | 414 // Read content settings exceptions. |
392 ReadContentSettingsFromPref(false); | 415 ReadContentSettingsFromPref(false); |
393 | 416 |
394 if (!is_incognito_) { | 417 if (!is_incognito_) { |
395 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", | 418 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", |
396 value_map_.size()); | 419 value_map_.size()); |
397 } | 420 } |
398 | 421 |
399 pref_change_registrar_.Init(prefs_); | 422 pref_change_registrar_.Init(prefs_); |
400 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); | 423 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); |
401 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); | 424 pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this); |
425 pref_change_registrar_.Add(prefs::kGeolocationContentSettings, this); | |
402 } | 426 } |
403 | 427 |
404 ContentSetting PrefProvider::GetContentSetting( | 428 ContentSetting PrefProvider::GetContentSetting( |
405 const GURL& primary_url, | 429 const GURL& primary_url, |
406 const GURL& secondary_url, | 430 const GURL& secondary_url, |
407 ContentSettingsType content_type, | 431 ContentSettingsType content_type, |
408 const ResourceIdentifier& resource_identifier) const { | 432 const ResourceIdentifier& resource_identifier) const { |
409 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito | 433 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito |
410 // profile, this will always return NULL. | 434 // profile, this will always return NULL. |
411 // TODO(markusheintz): I don't like this. I'd like to have an | 435 // 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... | |
458 } | 482 } |
459 | 483 |
460 void PrefProvider::SetContentSetting( | 484 void PrefProvider::SetContentSetting( |
461 const ContentSettingsPattern& primary_pattern, | 485 const ContentSettingsPattern& primary_pattern, |
462 const ContentSettingsPattern& secondary_pattern, | 486 const ContentSettingsPattern& secondary_pattern, |
463 ContentSettingsType content_type, | 487 ContentSettingsType content_type, |
464 const ResourceIdentifier& resource_identifier, | 488 const ResourceIdentifier& resource_identifier, |
465 ContentSetting setting) { | 489 ContentSetting setting) { |
466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
467 DCHECK(prefs_); | 491 DCHECK(prefs_); |
468 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 492 DCHECK(kTypeNames[content_type] != NULL); |
469 | 493 |
470 // Update in memory value map. | 494 // Update in memory value map. |
471 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; | 495 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
472 if (!is_incognito_) | 496 if (!is_incognito_) |
473 map_to_modify = &value_map_; | 497 map_to_modify = &value_map_; |
474 | 498 |
475 { | 499 { |
476 base::AutoLock auto_lock(lock_); | 500 base::AutoLock auto_lock(lock_); |
477 if (setting == CONTENT_SETTING_DEFAULT) { | 501 if (setting == CONTENT_SETTING_DEFAULT) { |
478 map_to_modify->DeleteValue( | 502 map_to_modify->DeleteValue( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
544 const NotificationDetails& details) { | 568 const NotificationDetails& details) { |
545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 569 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
546 | 570 |
547 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | 571 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
548 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); | 572 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); |
549 if (updating_preferences_) | 573 if (updating_preferences_) |
550 return; | 574 return; |
551 | 575 |
552 std::string* name = Details<std::string>(details).ptr(); | 576 std::string* name = Details<std::string>(details).ptr(); |
553 if (*name == prefs::kContentSettingsPatternPairs) { | 577 if (*name == prefs::kContentSettingsPatternPairs) { |
554 SyncObsoletePref(); | 578 SyncObsoletePatternPref(); |
579 SyncObsoleteGeolocationPref(); | |
555 ReadContentSettingsFromPref(true); | 580 ReadContentSettingsFromPref(true); |
556 } else if (*name == prefs::kContentSettingsPatterns) { | 581 } else if (*name == prefs::kContentSettingsPatterns) { |
557 AutoReset<bool> auto_reset(&updating_preferences_, true); | 582 AutoReset<bool> auto_reset(&updating_preferences_, true); |
558 MigrateObsoleteContentSettingsPatternPref(); | 583 MigrateObsoleteContentSettingsPatternPref(); |
559 ReadContentSettingsFromPref(true); | 584 ReadContentSettingsFromPref(true); |
585 } else if (*name == prefs::kGeolocationContentSettings) { | |
586 AutoReset<bool> auto_reset(&updating_preferences_, true); | |
587 MigrateObsoleteGeolocationPref(); | |
588 ReadContentSettingsFromPref(true); | |
560 } else { | 589 } else { |
561 NOTREACHED() << "Unexpected preference observed"; | 590 NOTREACHED() << "Unexpected preference observed"; |
562 return; | 591 return; |
563 } | 592 } |
564 | 593 |
565 NotifyObservers(ContentSettingsPattern(), | 594 NotifyObservers(ContentSettingsPattern(), |
566 ContentSettingsPattern(), | 595 ContentSettingsPattern(), |
567 CONTENT_SETTINGS_TYPE_DEFAULT, | 596 CONTENT_SETTINGS_TYPE_DEFAULT, |
568 std::string()); | 597 std::string()); |
569 } else { | 598 } else { |
(...skipping 13 matching lines...) Expand all Loading... | |
583 const ContentSettingsPattern& secondary_pattern, | 612 const ContentSettingsPattern& secondary_pattern, |
584 ContentSettingsType content_type, | 613 ContentSettingsType content_type, |
585 const ResourceIdentifier& resource_identifier, | 614 const ResourceIdentifier& resource_identifier, |
586 ContentSetting setting) { | 615 ContentSetting setting) { |
587 AutoReset<bool> auto_reset(&updating_preferences_, true); | 616 AutoReset<bool> auto_reset(&updating_preferences_, true); |
588 UpdatePatternPairsPref(primary_pattern, | 617 UpdatePatternPairsPref(primary_pattern, |
589 secondary_pattern, | 618 secondary_pattern, |
590 content_type, | 619 content_type, |
591 resource_identifier, | 620 resource_identifier, |
592 setting); | 621 setting); |
593 UpdatePatternsPref(primary_pattern, | 622 if (content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION && |
594 secondary_pattern, | 623 content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) { |
595 content_type, | 624 UpdateObsoletePatternsPref(primary_pattern, |
596 resource_identifier, | 625 secondary_pattern, |
597 setting); | 626 content_type, |
627 resource_identifier, | |
628 setting); | |
629 } | |
630 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { | |
631 SyncObsoleteGeolocationPref(); | |
632 } | |
598 } | 633 } |
599 | 634 |
600 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { | 635 void PrefProvider::ReadContentSettingsFromPref(bool overwrite) { |
601 base::AutoLock auto_lock(lock_); | 636 base::AutoLock auto_lock(lock_); |
602 | 637 |
603 const DictionaryValue* all_settings_dictionary = | 638 const DictionaryValue* all_settings_dictionary = |
604 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | 639 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); |
605 | 640 |
606 if (overwrite) | 641 if (overwrite) |
607 value_map_.clear(); | 642 value_map_.clear(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
682 content_type, | 717 content_type, |
683 ResourceIdentifier(""), | 718 ResourceIdentifier(""), |
684 Value::CreateIntegerValue(setting)); | 719 Value::CreateIntegerValue(setting)); |
685 } | 720 } |
686 } | 721 } |
687 } | 722 } |
688 } | 723 } |
689 } | 724 } |
690 } | 725 } |
691 | 726 |
692 void PrefProvider::UpdatePatternsPref( | 727 void PrefProvider::UpdateObsoletePatternsPref( |
693 const ContentSettingsPattern& primary_pattern, | 728 const ContentSettingsPattern& primary_pattern, |
694 const ContentSettingsPattern& secondary_pattern, | 729 const ContentSettingsPattern& secondary_pattern, |
695 ContentSettingsType content_type, | 730 ContentSettingsType content_type, |
696 const ResourceIdentifier& resource_identifier, | 731 const ResourceIdentifier& resource_identifier, |
697 ContentSetting setting) { | 732 ContentSetting setting) { |
698 DictionaryPrefUpdate update(prefs_, | 733 DictionaryPrefUpdate update(prefs_, |
699 prefs::kContentSettingsPatterns); | 734 prefs::kContentSettingsPatterns); |
700 DictionaryValue* all_settings_dictionary = update.Get(); | 735 DictionaryValue* all_settings_dictionary = update.Get(); |
701 | 736 |
702 // Get settings dictionary for |primary_pattern|. | 737 // Get settings dictionary for |primary_pattern|. |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
997 std::string new_pattern_str = CreatePatternString( | 1032 std::string new_pattern_str = CreatePatternString( |
998 primary_pattern, ContentSettingsPattern::Wildcard()); | 1033 primary_pattern, ContentSettingsPattern::Wildcard()); |
999 | 1034 |
1000 // Existing values are overwritten. | 1035 // Existing values are overwritten. |
1001 exceptions_dictionary->SetWithoutPathExpansion( | 1036 exceptions_dictionary->SetWithoutPathExpansion( |
1002 new_pattern_str, dictionary->DeepCopy()); | 1037 new_pattern_str, dictionary->DeepCopy()); |
1003 } | 1038 } |
1004 } | 1039 } |
1005 } | 1040 } |
1006 | 1041 |
1007 void PrefProvider::SyncObsoletePref() { | 1042 void PrefProvider::SyncObsoletePatternPref() { |
1008 AutoReset<bool> auto_reset(&updating_preferences_, true); | 1043 AutoReset<bool> auto_reset(&updating_preferences_, true); |
1009 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && | 1044 if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) && |
1010 !is_incognito_) { | 1045 !is_incognito_) { |
1011 const DictionaryValue* pattern_pairs_dictionary = | 1046 const DictionaryValue* pattern_pairs_dictionary = |
1012 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | 1047 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); |
1013 | 1048 |
1014 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns); | 1049 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns); |
1015 DictionaryValue* obsolete_settings_dictionary = update.Get(); | 1050 DictionaryValue* obsolete_settings_dictionary = update.Get(); |
1016 | 1051 |
1017 for (DictionaryValue::key_iterator i = | 1052 for (DictionaryValue::key_iterator i = |
1018 pattern_pairs_dictionary->begin_keys(); | 1053 pattern_pairs_dictionary->begin_keys(); |
1019 i != pattern_pairs_dictionary->end_keys(); | 1054 i != pattern_pairs_dictionary->end_keys(); |
1020 ++i) { | 1055 ++i) { |
1021 const std::string& key(*i); | 1056 const std::string& key(*i); |
1022 // Validate pattern string and skip it if it is invalid. | 1057 // Validate pattern string and skip it if it is invalid. |
1023 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = | 1058 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = |
1024 ParsePatternString(key); | 1059 ParsePatternString(key); |
1025 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid()) { | 1060 if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid()) { |
1026 LOG(DFATAL) << "Invalid pattern strings: " << key; | 1061 LOG(DFATAL) << "Invalid pattern strings: " << key; |
1027 continue; | 1062 continue; |
1028 } | 1063 } |
1029 | 1064 |
1030 // Copy dictionary | 1065 DictionaryValue* settings_dictionary = NULL; |
1031 DictionaryValue* dictionary = NULL; | |
1032 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( | 1066 bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion( |
1033 key, &dictionary); | 1067 key, &settings_dictionary); |
1034 DCHECK(found); | 1068 DCHECK(found); |
1035 std::string new_key = pattern_pair.first.ToString(); | 1069 scoped_ptr<DictionaryValue> settings_dictionary_copy( |
1036 // Existing values are overwritten. | 1070 new DictionaryValue()); |
1037 obsolete_settings_dictionary->SetWithoutPathExpansion( | 1071 for (DictionaryValue::key_iterator i(settings_dictionary->begin_keys()); |
1038 new_key, dictionary->DeepCopy()); | 1072 i != settings_dictionary->end_keys(); |
1073 ++i) { | |
1074 const std::string& type_name(*i); | |
1075 // Copy only the content settings types that were stored in the obsolete | |
1076 // preference. | |
1077 for (size_t i = 0; i <= CONTENT_SETTINGS_TYPE_POPUPS; ++i) { | |
Bernhard Bauer
2011/08/03 15:22:50
Can you start the iteration at CONTENT_SETTINGS_TY
markusheintz_
2011/08/04 12:45:56
s/0/CONTENT_SETTINGS_TYPE_COOKIES/ and changed the
| |
1078 if ((kTypeNames[i] != NULL) && (kTypeNames[i] == type_name)) { | |
Bernhard Bauer
2011/08/03 15:22:50
kTypeNames[i] should never be NULL.
markusheintz_
2011/08/04 12:45:56
I replaced the (kTypeNames[i] != NULL)with a DCHEC
| |
1079 Value* value; | |
Bernhard Bauer
2011/08/03 15:22:50
Initialize to NULL please.
markusheintz_
2011/08/04 12:45:56
Done.
| |
1080 bool found = settings_dictionary->GetWithoutPathExpansion( | |
1081 type_name, &value); | |
1082 DCHECK(found); | |
1083 settings_dictionary_copy->SetWithoutPathExpansion( | |
1084 type_name, value->DeepCopy()); | |
1085 break; | |
1086 } | |
1087 } | |
1088 } | |
1089 | |
1090 // Ignore empty dictionaryies. | |
1091 if (!settings_dictionary_copy->empty()) { | |
1092 std::string new_key = pattern_pair.first.ToString(); | |
1093 // Existing values are overwritten. | |
1094 obsolete_settings_dictionary->SetWithoutPathExpansion( | |
1095 new_key, settings_dictionary_copy.release()); | |
1096 } | |
1039 } | 1097 } |
1040 } | 1098 } |
1041 } | 1099 } |
1042 | 1100 |
1101 void PrefProvider::MigrateObsoleteGeolocationPref() { | |
1102 if (!prefs_->HasPrefPath(prefs::kGeolocationContentSettings)) | |
1103 return; | |
1104 | |
1105 const DictionaryValue* geolocation_settings_dictionary = | |
1106 prefs_->GetDictionary(prefs::kGeolocationContentSettings); | |
1107 for (DictionaryValue::key_iterator i = | |
1108 geolocation_settings_dictionary->begin_keys(); | |
1109 i != geolocation_settings_dictionary->end_keys(); | |
1110 ++i) { | |
1111 const std::string& primary_key(*i); | |
1112 GURL primary_url(primary_key); | |
1113 DCHECK(primary_url.is_valid()); | |
1114 | |
1115 DictionaryValue* requesting_origin_settings_dictionary = NULL; | |
1116 bool found = | |
1117 geolocation_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
Bernhard Bauer
2011/08/03 15:22:50
Maybe this is an indication to use a shorter varia
markusheintz_
2011/08/04 12:45:56
I removed the trailing |_dictionary| from the name
| |
1118 primary_key, &requesting_origin_settings_dictionary); | |
1119 DCHECK(found); | |
1120 | |
1121 for (DictionaryValue::key_iterator j = | |
1122 requesting_origin_settings_dictionary->begin_keys(); | |
1123 j != requesting_origin_settings_dictionary->end_keys(); | |
1124 ++j) { | |
1125 const std::string& secondary_key(*j); | |
1126 GURL secondary_url(secondary_key); | |
1127 DCHECK(secondary_url.is_valid()); | |
1128 | |
1129 int setting_value; | |
1130 found = | |
1131 requesting_origin_settings_dictionary->GetIntegerWithoutPathExpansion( | |
1132 secondary_key, &setting_value); | |
1133 DCHECK(found); | |
1134 | |
1135 ContentSettingsPattern primary_pattern = | |
1136 ContentSettingsPattern::FromURLNoWildcard(primary_url); | |
1137 ContentSettingsPattern secondary_pattern = | |
1138 ContentSettingsPattern::FromURLNoWildcard(secondary_url); | |
1139 DCHECK(primary_pattern.IsValid() && secondary_pattern.IsValid()); | |
1140 | |
1141 SetContentSetting(primary_pattern, | |
1142 secondary_pattern, | |
1143 CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
1144 std::string(), | |
1145 IntToContentSetting(setting_value)); | |
1146 } | |
1147 } | |
1148 } | |
1149 | |
1150 void PrefProvider::SyncObsoleteGeolocationPref() { | |
1151 DCHECK(prefs_); | |
1152 DCHECK(prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs)); | |
1153 | |
1154 scoped_ptr<DictionaryValue> obsolete_geolocation_settings( | |
1155 new DictionaryValue()); | |
1156 | |
1157 const DictionaryValue* all_settings_dictionary = | |
1158 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); | |
1159 for (DictionaryValue::key_iterator i = all_settings_dictionary->begin_keys(); | |
1160 i != all_settings_dictionary->end_keys(); | |
1161 ++i) { | |
1162 const std::string& key(*i); | |
1163 std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair = | |
1164 ParsePatternString(key); | |
1165 DCHECK(pattern_pair.first.IsValid() && pattern_pair.second.IsValid()); | |
1166 | |
1167 DictionaryValue* settings_dictionary; | |
Bernhard Bauer
2011/08/03 15:22:50
Initialize with NULL please.
markusheintz_
2011/08/04 12:45:56
Done.
| |
1168 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
1169 key, &settings_dictionary); | |
1170 DCHECK(found); | |
1171 | |
1172 if (settings_dictionary->HasKey( | |
1173 kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION])) { | |
1174 const GURL primary_url(pattern_pair.first.ToString()); | |
1175 const GURL secondary_url(pattern_pair.second.ToString()); | |
1176 DCHECK(primary_url.is_valid() && secondary_url.is_valid()); | |
1177 | |
1178 int setting_value; | |
1179 settings_dictionary->GetInteger( | |
1180 kTypeNames[CONTENT_SETTINGS_TYPE_GEOLOCATION], &setting_value); | |
1181 | |
1182 // Add to obsolete pref. | |
1183 DictionaryValue* one_origin_settings; | |
Bernhard Bauer
2011/08/03 15:22:50
Initialize with NULL please (not as important here
markusheintz_
2011/08/04 12:45:56
Done.
| |
1184 bool found = | |
1185 obsolete_geolocation_settings->GetDictionaryWithoutPathExpansion( | |
1186 primary_url.spec(), &one_origin_settings); | |
1187 if (!found) { | |
1188 one_origin_settings = new DictionaryValue(); | |
1189 obsolete_geolocation_settings->SetWithoutPathExpansion( | |
1190 primary_url.spec(), one_origin_settings); | |
1191 } | |
1192 | |
1193 one_origin_settings->SetWithoutPathExpansion( | |
1194 secondary_url.spec(), Value::CreateIntegerValue(setting_value)); | |
1195 } | |
1196 } | |
1197 | |
1198 prefs_->Set(prefs::kGeolocationContentSettings, | |
Bernhard Bauer
2011/08/03 15:22:50
Is there a reason you're not using a DictionaryPre
markusheintz_
2011/08/04 12:45:56
I just found that shorter.
Bernhard Bauer
2011/08/05 10:08:46
OTOH, it makes a |DeepCopy| of the dictionary.
| |
1199 *obsolete_geolocation_settings); | |
1200 } | |
1201 | |
1043 } // namespace content_settings | 1202 } // namespace content_settings |
OLD | NEW |