| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/host_content_settings_map.h" | 5 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 switches::kEnableResourceContentSettings)) { | 218 switches::kEnableResourceContentSettings)) { |
| 219 DCHECK(!resource_identifier.empty()); | 219 DCHECK(!resource_identifier.empty()); |
| 220 } | 220 } |
| 221 | 221 |
| 222 // Host content settings are ignored if the default_content_setting is | 222 // Host content settings are ignored if the default_content_setting is |
| 223 // managed. | 223 // managed. |
| 224 if (IsDefaultContentSettingManaged(content_type)) { | 224 if (IsDefaultContentSettingManaged(content_type)) { |
| 225 return GetDefaultContentSetting(content_type); | 225 return GetDefaultContentSetting(content_type); |
| 226 } | 226 } |
| 227 | 227 |
| 228 AutoLock auto_lock(lock_); | 228 base::AutoLock auto_lock(lock_); |
| 229 | 229 |
| 230 const std::string host(net::GetHostOrSpecFromURL(url)); | 230 const std::string host(net::GetHostOrSpecFromURL(url)); |
| 231 ContentSettingsTypeResourceIdentifierPair | 231 ContentSettingsTypeResourceIdentifierPair |
| 232 requested_setting(content_type, resource_identifier); | 232 requested_setting(content_type, resource_identifier); |
| 233 | 233 |
| 234 // Check for exact matches first. | 234 // Check for exact matches first. |
| 235 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | 235 HostContentSettings::const_iterator i(host_content_settings_.find(host)); |
| 236 if (i != host_content_settings_.end() && | 236 if (i != host_content_settings_.end() && |
| 237 i->second.content_settings_for_resources.find(requested_setting) != | 237 i->second.content_settings_for_resources.find(requested_setting) != |
| 238 i->second.content_settings_for_resources.end()) { | 238 i->second.content_settings_for_resources.end()) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 } | 300 } |
| 301 } | 301 } |
| 302 return output; | 302 return output; |
| 303 } | 303 } |
| 304 | 304 |
| 305 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( | 305 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( |
| 306 const GURL& url) const { | 306 const GURL& url) const { |
| 307 if (ShouldAllowAllContent(url)) | 307 if (ShouldAllowAllContent(url)) |
| 308 return ContentSettings(CONTENT_SETTING_ALLOW); | 308 return ContentSettings(CONTENT_SETTING_ALLOW); |
| 309 | 309 |
| 310 AutoLock auto_lock(lock_); | 310 base::AutoLock auto_lock(lock_); |
| 311 | 311 |
| 312 const std::string host(net::GetHostOrSpecFromURL(url)); | 312 const std::string host(net::GetHostOrSpecFromURL(url)); |
| 313 ContentSettings output; | 313 ContentSettings output; |
| 314 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) | 314 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) |
| 315 output.settings[j] = CONTENT_SETTING_DEFAULT; | 315 output.settings[j] = CONTENT_SETTING_DEFAULT; |
| 316 | 316 |
| 317 // Check for exact matches first. | 317 // Check for exact matches first. |
| 318 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | 318 HostContentSettings::const_iterator i(host_content_settings_.find(host)); |
| 319 if (i != host_content_settings_.end()) | 319 if (i != host_content_settings_.end()) |
| 320 output = i->second.content_settings; | 320 output = i->second.content_settings; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 DCHECK(RequiresResourceIdentifier(content_type) != | 364 DCHECK(RequiresResourceIdentifier(content_type) != |
| 365 resource_identifier.empty()); | 365 resource_identifier.empty()); |
| 366 DCHECK(settings); | 366 DCHECK(settings); |
| 367 settings->clear(); | 367 settings->clear(); |
| 368 | 368 |
| 369 const HostContentSettings* map_to_return = | 369 const HostContentSettings* map_to_return = |
| 370 is_off_the_record_ ? &off_the_record_settings_ : &host_content_settings_; | 370 is_off_the_record_ ? &off_the_record_settings_ : &host_content_settings_; |
| 371 ContentSettingsTypeResourceIdentifierPair | 371 ContentSettingsTypeResourceIdentifierPair |
| 372 requested_setting(content_type, resource_identifier); | 372 requested_setting(content_type, resource_identifier); |
| 373 | 373 |
| 374 AutoLock auto_lock(lock_); | 374 base::AutoLock auto_lock(lock_); |
| 375 for (HostContentSettings::const_iterator i(map_to_return->begin()); | 375 for (HostContentSettings::const_iterator i(map_to_return->begin()); |
| 376 i != map_to_return->end(); ++i) { | 376 i != map_to_return->end(); ++i) { |
| 377 ContentSetting setting; | 377 ContentSetting setting; |
| 378 if (RequiresResourceIdentifier(content_type)) { | 378 if (RequiresResourceIdentifier(content_type)) { |
| 379 if (i->second.content_settings_for_resources.find(requested_setting) != | 379 if (i->second.content_settings_for_resources.find(requested_setting) != |
| 380 i->second.content_settings_for_resources.end()) | 380 i->second.content_settings_for_resources.end()) |
| 381 setting = i->second.content_settings_for_resources.find( | 381 setting = i->second.content_settings_for_resources.find( |
| 382 requested_setting)->second; | 382 requested_setting)->second; |
| 383 else | 383 else |
| 384 setting = CONTENT_SETTING_DEFAULT; | 384 setting = CONTENT_SETTING_DEFAULT; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 DictionaryValue* all_settings_dictionary = NULL; | 439 DictionaryValue* all_settings_dictionary = NULL; |
| 440 HostContentSettings* map_to_modify = &off_the_record_settings_; | 440 HostContentSettings* map_to_modify = &off_the_record_settings_; |
| 441 if (!is_off_the_record_) { | 441 if (!is_off_the_record_) { |
| 442 prefs = profile_->GetPrefs(); | 442 prefs = profile_->GetPrefs(); |
| 443 all_settings_dictionary = | 443 all_settings_dictionary = |
| 444 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | 444 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); |
| 445 map_to_modify = &host_content_settings_; | 445 map_to_modify = &host_content_settings_; |
| 446 } | 446 } |
| 447 | 447 |
| 448 { | 448 { |
| 449 AutoLock auto_lock(lock_); | 449 base::AutoLock auto_lock(lock_); |
| 450 if (!map_to_modify->count(pattern_str)) | 450 if (!map_to_modify->count(pattern_str)) |
| 451 (*map_to_modify)[pattern_str].content_settings = ContentSettings(); | 451 (*map_to_modify)[pattern_str].content_settings = ContentSettings(); |
| 452 HostContentSettings::iterator | 452 HostContentSettings::iterator |
| 453 i(map_to_modify->find(pattern_str)); | 453 i(map_to_modify->find(pattern_str)); |
| 454 ContentSettings& settings = i->second.content_settings; | 454 ContentSettings& settings = i->second.content_settings; |
| 455 if (RequiresResourceIdentifier(content_type)) { | 455 if (RequiresResourceIdentifier(content_type)) { |
| 456 settings.settings[content_type] = CONTENT_SETTING_DEFAULT; | 456 settings.settings[content_type] = CONTENT_SETTING_DEFAULT; |
| 457 if (setting != CONTENT_SETTING_DEFAULT) { | 457 if (setting != CONTENT_SETTING_DEFAULT) { |
| 458 i->second.content_settings_for_resources[ | 458 i->second.content_settings_for_resources[ |
| 459 ContentSettingsTypeResourceIdentifierPair(content_type, | 459 ContentSettingsTypeResourceIdentifierPair(content_type, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 HostContentSettings* map_to_modify = &off_the_record_settings_; | 549 HostContentSettings* map_to_modify = &off_the_record_settings_; |
| 550 | 550 |
| 551 if (!is_off_the_record_) { | 551 if (!is_off_the_record_) { |
| 552 prefs = profile_->GetPrefs(); | 552 prefs = profile_->GetPrefs(); |
| 553 all_settings_dictionary = | 553 all_settings_dictionary = |
| 554 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | 554 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); |
| 555 map_to_modify = &host_content_settings_; | 555 map_to_modify = &host_content_settings_; |
| 556 } | 556 } |
| 557 | 557 |
| 558 { | 558 { |
| 559 AutoLock auto_lock(lock_); | 559 base::AutoLock auto_lock(lock_); |
| 560 for (HostContentSettings::iterator i(map_to_modify->begin()); | 560 for (HostContentSettings::iterator i(map_to_modify->begin()); |
| 561 i != map_to_modify->end(); ) { | 561 i != map_to_modify->end(); ) { |
| 562 if (RequiresResourceIdentifier(content_type) || | 562 if (RequiresResourceIdentifier(content_type) || |
| 563 i->second.content_settings.settings[content_type] != | 563 i->second.content_settings.settings[content_type] != |
| 564 CONTENT_SETTING_DEFAULT) { | 564 CONTENT_SETTING_DEFAULT) { |
| 565 if (RequiresResourceIdentifier(content_type)) | 565 if (RequiresResourceIdentifier(content_type)) |
| 566 i->second.content_settings_for_resources.clear(); | 566 i->second.content_settings_for_resources.clear(); |
| 567 i->second.content_settings.settings[content_type] = | 567 i->second.content_settings.settings[content_type] = |
| 568 CONTENT_SETTING_DEFAULT; | 568 CONTENT_SETTING_DEFAULT; |
| 569 std::string host(i->first); | 569 std::string host(i->first); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 | 619 |
| 620 PrefService* prefs = profile_->GetPrefs(); | 620 PrefService* prefs = profile_->GetPrefs(); |
| 621 // If the preference block-third-party-cookies is managed then do not allow to | 621 // If the preference block-third-party-cookies is managed then do not allow to |
| 622 // change it. | 622 // change it. |
| 623 if (prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies)) { | 623 if (prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies)) { |
| 624 NOTREACHED(); | 624 NOTREACHED(); |
| 625 return; | 625 return; |
| 626 } | 626 } |
| 627 | 627 |
| 628 { | 628 { |
| 629 AutoLock auto_lock(lock_); | 629 base::AutoLock auto_lock(lock_); |
| 630 block_third_party_cookies_ = block; | 630 block_third_party_cookies_ = block; |
| 631 } | 631 } |
| 632 | 632 |
| 633 if (block) | 633 if (block) |
| 634 prefs->SetBoolean(prefs::kBlockThirdPartyCookies, true); | 634 prefs->SetBoolean(prefs::kBlockThirdPartyCookies, true); |
| 635 else | 635 else |
| 636 prefs->ClearPref(prefs::kBlockThirdPartyCookies); | 636 prefs->ClearPref(prefs::kBlockThirdPartyCookies); |
| 637 } | 637 } |
| 638 | 638 |
| 639 void HostContentSettingsMap::SetBlockNonsandboxedPlugins(bool block) { | 639 void HostContentSettingsMap::SetBlockNonsandboxedPlugins(bool block) { |
| 640 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 640 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 641 | 641 |
| 642 // This setting may not be directly modified for OTR sessions. Instead, it | 642 // This setting may not be directly modified for OTR sessions. Instead, it |
| 643 // is synced to the main profile's setting. | 643 // is synced to the main profile's setting. |
| 644 if (is_off_the_record_) { | 644 if (is_off_the_record_) { |
| 645 NOTREACHED(); | 645 NOTREACHED(); |
| 646 return; | 646 return; |
| 647 } | 647 } |
| 648 | 648 |
| 649 { | 649 { |
| 650 AutoLock auto_lock(lock_); | 650 base::AutoLock auto_lock(lock_); |
| 651 block_nonsandboxed_plugins_ = block; | 651 block_nonsandboxed_plugins_ = block; |
| 652 } | 652 } |
| 653 | 653 |
| 654 | 654 |
| 655 PrefService* prefs = profile_->GetPrefs(); | 655 PrefService* prefs = profile_->GetPrefs(); |
| 656 if (block) { | 656 if (block) { |
| 657 UserMetrics::RecordAction( | 657 UserMetrics::RecordAction( |
| 658 UserMetricsAction("BlockNonsandboxedPlugins_Enable")); | 658 UserMetricsAction("BlockNonsandboxedPlugins_Enable")); |
| 659 prefs->SetBoolean(prefs::kBlockNonsandboxedPlugins, true); | 659 prefs->SetBoolean(prefs::kBlockNonsandboxedPlugins, true); |
| 660 } else { | 660 } else { |
| 661 UserMetrics::RecordAction( | 661 UserMetrics::RecordAction( |
| 662 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); | 662 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); |
| 663 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); | 663 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); |
| 664 } | 664 } |
| 665 } | 665 } |
| 666 | 666 |
| 667 void HostContentSettingsMap::ResetToDefaults() { | 667 void HostContentSettingsMap::ResetToDefaults() { |
| 668 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 668 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 669 | 669 |
| 670 { | 670 { |
| 671 AutoLock auto_lock(lock_); | 671 base::AutoLock auto_lock(lock_); |
| 672 for (provider_iterator provider = content_settings_providers_.begin(); | 672 for (provider_iterator provider = content_settings_providers_.begin(); |
| 673 provider != content_settings_providers_.end(); ++provider) { | 673 provider != content_settings_providers_.end(); ++provider) { |
| 674 (*provider)->ResetToDefaults(); | 674 (*provider)->ResetToDefaults(); |
| 675 } | 675 } |
| 676 host_content_settings_.clear(); | 676 host_content_settings_.clear(); |
| 677 off_the_record_settings_.clear(); | 677 off_the_record_settings_.clear(); |
| 678 // Don't reset block third party cookies if they are managed. | 678 // Don't reset block third party cookies if they are managed. |
| 679 if (!IsBlockThirdPartyCookiesManaged()) | 679 if (!IsBlockThirdPartyCookiesManaged()) |
| 680 block_third_party_cookies_ = false; | 680 block_third_party_cookies_ = false; |
| 681 block_nonsandboxed_plugins_ = false; | 681 block_nonsandboxed_plugins_ = false; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 704 | 704 |
| 705 if (type == NotificationType::PREF_CHANGED) { | 705 if (type == NotificationType::PREF_CHANGED) { |
| 706 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 706 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); |
| 707 if (updating_preferences_) | 707 if (updating_preferences_) |
| 708 return; | 708 return; |
| 709 | 709 |
| 710 std::string* name = Details<std::string>(details).ptr(); | 710 std::string* name = Details<std::string>(details).ptr(); |
| 711 if (*name == prefs::kContentSettingsPatterns) { | 711 if (*name == prefs::kContentSettingsPatterns) { |
| 712 ReadExceptions(true); | 712 ReadExceptions(true); |
| 713 } else if (*name == prefs::kBlockThirdPartyCookies) { | 713 } else if (*name == prefs::kBlockThirdPartyCookies) { |
| 714 AutoLock auto_lock(lock_); | 714 base::AutoLock auto_lock(lock_); |
| 715 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( | 715 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( |
| 716 prefs::kBlockThirdPartyCookies); | 716 prefs::kBlockThirdPartyCookies); |
| 717 is_block_third_party_cookies_managed_ = | 717 is_block_third_party_cookies_managed_ = |
| 718 profile_->GetPrefs()->IsManagedPreference( | 718 profile_->GetPrefs()->IsManagedPreference( |
| 719 prefs::kBlockThirdPartyCookies); | 719 prefs::kBlockThirdPartyCookies); |
| 720 } else if (*name == prefs::kBlockNonsandboxedPlugins) { | 720 } else if (*name == prefs::kBlockNonsandboxedPlugins) { |
| 721 AutoLock auto_lock(lock_); | 721 base::AutoLock auto_lock(lock_); |
| 722 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( | 722 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( |
| 723 prefs::kBlockNonsandboxedPlugins); | 723 prefs::kBlockNonsandboxedPlugins); |
| 724 } else { | 724 } else { |
| 725 NOTREACHED() << "Unexpected preference observed"; | 725 NOTREACHED() << "Unexpected preference observed"; |
| 726 return; | 726 return; |
| 727 } | 727 } |
| 728 | 728 |
| 729 if (!is_off_the_record_) { | 729 if (!is_off_the_record_) { |
| 730 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), | 730 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |
| 731 CONTENT_SETTINGS_TYPE_DEFAULT, | 731 CONTENT_SETTINGS_TYPE_DEFAULT, |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 ContentSettingsType content_type) const { | 815 ContentSettingsType content_type) const { |
| 816 for (const_provider_iterator provider = content_settings_providers_.begin(); | 816 for (const_provider_iterator provider = content_settings_providers_.begin(); |
| 817 provider != content_settings_providers_.end(); ++provider) { | 817 provider != content_settings_providers_.end(); ++provider) { |
| 818 if ((*provider)->DefaultSettingIsManaged(content_type)) | 818 if ((*provider)->DefaultSettingIsManaged(content_type)) |
| 819 return true; | 819 return true; |
| 820 } | 820 } |
| 821 return false; | 821 return false; |
| 822 } | 822 } |
| 823 | 823 |
| 824 void HostContentSettingsMap::ReadExceptions(bool overwrite) { | 824 void HostContentSettingsMap::ReadExceptions(bool overwrite) { |
| 825 AutoLock lock(lock_); | 825 base::AutoLock lock(lock_); |
| 826 | 826 |
| 827 PrefService* prefs = profile_->GetPrefs(); | 827 PrefService* prefs = profile_->GetPrefs(); |
| 828 DictionaryValue* all_settings_dictionary = | 828 DictionaryValue* all_settings_dictionary = |
| 829 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | 829 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); |
| 830 | 830 |
| 831 if (overwrite) | 831 if (overwrite) |
| 832 host_content_settings_.clear(); | 832 host_content_settings_.clear(); |
| 833 | 833 |
| 834 // Careful: The returned value could be NULL if the pref has never been set. | 834 // Careful: The returned value could be NULL if the pref has never been set. |
| 835 if (all_settings_dictionary != NULL) { | 835 if (all_settings_dictionary != NULL) { |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 968 } | 968 } |
| 969 | 969 |
| 970 for (size_t i = 0; i < move_items.size(); ++i) { | 970 for (size_t i = 0; i < move_items.size(); ++i) { |
| 971 Value* pattern_settings_dictionary = NULL; | 971 Value* pattern_settings_dictionary = NULL; |
| 972 all_settings_dictionary->RemoveWithoutPathExpansion( | 972 all_settings_dictionary->RemoveWithoutPathExpansion( |
| 973 move_items[i].first, &pattern_settings_dictionary); | 973 move_items[i].first, &pattern_settings_dictionary); |
| 974 all_settings_dictionary->SetWithoutPathExpansion( | 974 all_settings_dictionary->SetWithoutPathExpansion( |
| 975 move_items[i].second, pattern_settings_dictionary); | 975 move_items[i].second, pattern_settings_dictionary); |
| 976 } | 976 } |
| 977 } | 977 } |
| OLD | NEW |