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> |
11 | 11 |
12 #include "base/auto_reset.h" | 12 #include "base/auto_reset.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "chrome/browser/content_settings/content_settings_details.h" | 15 #include "chrome/browser/content_settings/content_settings_details.h" |
16 #include "chrome/browser/content_settings/content_settings_pattern.h" | 16 #include "chrome/browser/content_settings/content_settings_pattern.h" |
17 #include "chrome/browser/content_settings/content_settings_utils.h" | 17 #include "chrome/browser/content_settings/content_settings_utils.h" |
18 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
19 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
23 #include "chrome/common/content_settings.h" | 23 #include "chrome/common/content_settings.h" |
24 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
25 #include "content/browser/browser_thread.h" | 25 #include "content/browser/browser_thread.h" |
26 #include "content/browser/user_metrics.h" | 26 #include "content/browser/user_metrics.h" |
27 #include "content/common/notification_details.h" | |
28 #include "content/common/notification_service.h" | 27 #include "content/common/notification_service.h" |
29 #include "content/common/notification_source.h" | 28 #include "content/common/notification_source.h" |
30 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" |
31 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
32 | 31 |
33 namespace { | 32 namespace { |
34 | 33 |
35 // The preference keys where resource identifiers are stored for | 34 // The preference keys where resource identifiers are stored for |
36 // ContentSettingsType values that support resource identifiers. | 35 // ContentSettingsType values that support resource identifiers. |
37 const char* kResourceTypeNames[] = { | 36 const char* kResourceTypeNames[] = { |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 | 376 |
378 // Obsolete prefs, for migration: | 377 // Obsolete prefs, for migration: |
379 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, | 378 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns, |
380 PrefService::SYNCABLE_PREF); | 379 PrefService::SYNCABLE_PREF); |
381 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, | 380 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, |
382 PrefService::UNSYNCABLE_PREF); | 381 PrefService::UNSYNCABLE_PREF); |
383 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, | 382 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, |
384 PrefService::UNSYNCABLE_PREF); | 383 PrefService::UNSYNCABLE_PREF); |
385 } | 384 } |
386 | 385 |
387 PrefProvider::PrefProvider(HostContentSettingsMap* map, | 386 PrefProvider::PrefProvider(PrefService* prefs, |
388 PrefService* prefs, | |
389 bool incognito) | 387 bool incognito) |
390 : prefs_(prefs), | 388 : prefs_(prefs), |
391 host_content_settings_map_(map), | |
392 is_incognito_(incognito), | 389 is_incognito_(incognito), |
393 updating_preferences_(false) { | 390 updating_preferences_(false) { |
394 DCHECK(prefs_); | 391 DCHECK(prefs_); |
395 if (!is_incognito_) { | 392 if (!is_incognito_) { |
396 // Migrate obsolete preferences. | 393 // Migrate obsolete preferences. |
397 MigrateObsoletePerhostPref(); | 394 MigrateObsoletePerhostPref(); |
398 MigrateObsoletePopupsPref(); | 395 MigrateObsoletePopupsPref(); |
399 MigrateObsoleteContentSettingsPatternPref(); | 396 MigrateObsoleteContentSettingsPatternPref(); |
400 } | 397 } |
401 | 398 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 | 510 |
514 // Update the content settings preference. | 511 // Update the content settings preference. |
515 if (!is_incognito_) { | 512 if (!is_incognito_) { |
516 UpdatePref(primary_pattern, | 513 UpdatePref(primary_pattern, |
517 secondary_pattern, | 514 secondary_pattern, |
518 content_type, | 515 content_type, |
519 resource_identifier, | 516 resource_identifier, |
520 setting); | 517 setting); |
521 } | 518 } |
522 | 519 |
523 ContentSettingsDetails details( | 520 NotifyObservers( |
524 primary_pattern, secondary_pattern, content_type, resource_identifier); | 521 primary_pattern, secondary_pattern, content_type, resource_identifier); |
525 NotifyObservers(details); | |
526 } | 522 } |
527 | 523 |
528 void PrefProvider::ClearAllContentSettingsRules( | 524 void PrefProvider::ClearAllContentSettingsRules( |
529 ContentSettingsType content_type) { | 525 ContentSettingsType content_type) { |
530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 526 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
531 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 527 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
532 DCHECK(prefs_); | 528 DCHECK(prefs_); |
533 | 529 |
534 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; | 530 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
535 if (!is_incognito_) | 531 if (!is_incognito_) |
(...skipping 11 matching lines...) Expand all Loading... |
547 entry->identifier, | 543 entry->identifier, |
548 CONTENT_SETTING_DEFAULT); | 544 CONTENT_SETTING_DEFAULT); |
549 // Delete current |entry| and set |entry| to the next value map entry. | 545 // Delete current |entry| and set |entry| to the next value map entry. |
550 entry = map_to_modify->erase(entry); | 546 entry = map_to_modify->erase(entry); |
551 } else { | 547 } else { |
552 ++entry; | 548 ++entry; |
553 } | 549 } |
554 } | 550 } |
555 } | 551 } |
556 | 552 |
557 ContentSettingsDetails details( | 553 NotifyObservers(ContentSettingsPattern(), |
558 ContentSettingsPattern(), | 554 ContentSettingsPattern(), |
559 ContentSettingsPattern(), | 555 content_type, |
560 content_type, | 556 std::string()); |
561 std::string()); | |
562 NotifyObservers(details); | |
563 } | 557 } |
564 | 558 |
565 void PrefProvider::Observe( | 559 void PrefProvider::Observe( |
566 int type, | 560 int type, |
567 const NotificationSource& source, | 561 const NotificationSource& source, |
568 const NotificationDetails& details) { | 562 const NotificationDetails& details) { |
569 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 563 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
570 | 564 |
571 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | 565 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
572 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); | 566 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); |
573 if (updating_preferences_) | 567 if (updating_preferences_) |
574 return; | 568 return; |
575 | 569 |
576 std::string* name = Details<std::string>(details).ptr(); | 570 std::string* name = Details<std::string>(details).ptr(); |
577 if (*name == prefs::kContentSettingsPatternPairs) { | 571 if (*name == prefs::kContentSettingsPatternPairs) { |
578 SyncObsoletePref(); | 572 SyncObsoletePref(); |
579 ReadContentSettingsFromPref(true); | 573 ReadContentSettingsFromPref(true); |
580 } else if (*name == prefs::kContentSettingsPatterns) { | 574 } else if (*name == prefs::kContentSettingsPatterns) { |
581 AutoReset<bool> auto_reset(&updating_preferences_, true); | 575 AutoReset<bool> auto_reset(&updating_preferences_, true); |
582 MigrateObsoleteContentSettingsPatternPref(); | 576 MigrateObsoleteContentSettingsPatternPref(); |
583 ReadContentSettingsFromPref(true); | 577 ReadContentSettingsFromPref(true); |
584 } else { | 578 } else { |
585 NOTREACHED() << "Unexpected preference observed"; | 579 NOTREACHED() << "Unexpected preference observed"; |
586 return; | 580 return; |
587 } | 581 } |
588 | 582 |
589 ContentSettingsDetails details(ContentSettingsPattern(), | 583 NotifyObservers(ContentSettingsPattern(), |
590 ContentSettingsPattern(), | 584 ContentSettingsPattern(), |
591 CONTENT_SETTINGS_TYPE_DEFAULT, | 585 CONTENT_SETTINGS_TYPE_DEFAULT, |
592 std::string()); | 586 std::string()); |
593 NotifyObservers(details); | |
594 } else { | 587 } else { |
595 NOTREACHED() << "Unexpected notification"; | 588 NOTREACHED() << "Unexpected notification"; |
596 } | 589 } |
597 } | 590 } |
598 | 591 |
599 PrefProvider::~PrefProvider() { | 592 PrefProvider::~PrefProvider() { |
600 DCHECK(!prefs_); | 593 DCHECK(!prefs_); |
601 } | 594 } |
602 | 595 |
603 // //////////////////////////////////////////////////////////////////////////// | 596 // //////////////////////////////////////////////////////////////////////////// |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
890 | 883 |
891 for (size_t i = 0; i < move_items.size(); ++i) { | 884 for (size_t i = 0; i < move_items.size(); ++i) { |
892 Value* pattern_settings_dictionary = NULL; | 885 Value* pattern_settings_dictionary = NULL; |
893 all_settings_dictionary->RemoveWithoutPathExpansion( | 886 all_settings_dictionary->RemoveWithoutPathExpansion( |
894 move_items[i].first, &pattern_settings_dictionary); | 887 move_items[i].first, &pattern_settings_dictionary); |
895 all_settings_dictionary->SetWithoutPathExpansion( | 888 all_settings_dictionary->SetWithoutPathExpansion( |
896 move_items[i].second, pattern_settings_dictionary); | 889 move_items[i].second, pattern_settings_dictionary); |
897 } | 890 } |
898 } | 891 } |
899 | 892 |
900 void PrefProvider::NotifyObservers( | |
901 const ContentSettingsDetails& details) { | |
902 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
903 DCHECK(host_content_settings_map_); | |
904 NotificationService::current()->Notify( | |
905 chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED, | |
906 Source<HostContentSettingsMap>(host_content_settings_map_), | |
907 Details<const ContentSettingsDetails>(&details)); | |
908 } | |
909 | |
910 void PrefProvider::ShutdownOnUIThread() { | 893 void PrefProvider::ShutdownOnUIThread() { |
911 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 894 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
912 DCHECK(prefs_); | 895 DCHECK(prefs_); |
| 896 RemoveAllObservers(); |
913 pref_change_registrar_.RemoveAll(); | 897 pref_change_registrar_.RemoveAll(); |
914 prefs_ = NULL; | 898 prefs_ = NULL; |
915 host_content_settings_map_ = NULL; | |
916 } | 899 } |
917 | 900 |
918 void PrefProvider::MigrateObsoletePerhostPref() { | 901 void PrefProvider::MigrateObsoletePerhostPref() { |
919 if (prefs_->HasPrefPath(prefs::kPerHostContentSettings)) { | 902 if (prefs_->HasPrefPath(prefs::kPerHostContentSettings)) { |
920 const DictionaryValue* all_settings_dictionary = | 903 const DictionaryValue* all_settings_dictionary = |
921 prefs_->GetDictionary(prefs::kPerHostContentSettings); | 904 prefs_->GetDictionary(prefs::kPerHostContentSettings); |
922 DCHECK(all_settings_dictionary); | 905 DCHECK(all_settings_dictionary); |
923 for (DictionaryValue::key_iterator | 906 for (DictionaryValue::key_iterator |
924 i(all_settings_dictionary->begin_keys()); | 907 i(all_settings_dictionary->begin_keys()); |
925 i != all_settings_dictionary->end_keys(); ++i) { | 908 i != all_settings_dictionary->end_keys(); ++i) { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 DCHECK(found); | 1041 DCHECK(found); |
1059 std::string new_key = pattern_pair.first.ToString(); | 1042 std::string new_key = pattern_pair.first.ToString(); |
1060 // Existing values are overwritten. | 1043 // Existing values are overwritten. |
1061 obsolete_settings_dictionary->SetWithoutPathExpansion( | 1044 obsolete_settings_dictionary->SetWithoutPathExpansion( |
1062 new_key, dictionary->DeepCopy()); | 1045 new_key, dictionary->DeepCopy()); |
1063 } | 1046 } |
1064 } | 1047 } |
1065 } | 1048 } |
1066 | 1049 |
1067 } // namespace content_settings | 1050 } // namespace content_settings |
OLD | NEW |