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

Side by Side Diff: chrome/browser/content_settings/host_content_settings_map.cc

Issue 542253003: Add a global on/off switch for content settings and expose a toggle on the Website Settings options… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@global-settings
Patch Set: Created 6 years, 3 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <utility> 7 #include <utility>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 52
53 typedef std::vector<content_settings::Rule> Rules; 53 typedef std::vector<content_settings::Rule> Rules;
54 54
55 typedef std::pair<std::string, std::string> StringPair; 55 typedef std::pair<std::string, std::string> StringPair;
56 56
57 // TODO(bauerb): Expose constants. 57 // TODO(bauerb): Expose constants.
58 const char* kProviderNames[] = { 58 const char* kProviderNames[] = {
59 "platform_app", 59 "platform_app",
60 "policy", 60 "policy",
61 "extension", 61 "extension",
62 "override",
62 "preference", 63 "preference",
63 "default" 64 "default"
64 }; 65 };
65 66
66 content_settings::SettingSource kProviderSourceMap[] = { 67 content_settings::SettingSource kProviderSourceMap[] = {
67 content_settings::SETTING_SOURCE_EXTENSION, 68 content_settings::SETTING_SOURCE_EXTENSION,
68 content_settings::SETTING_SOURCE_POLICY, 69 content_settings::SETTING_SOURCE_POLICY,
69 content_settings::SETTING_SOURCE_EXTENSION, 70 content_settings::SETTING_SOURCE_EXTENSION,
70 content_settings::SETTING_SOURCE_USER, 71 content_settings::SETTING_SOURCE_USER,
71 content_settings::SETTING_SOURCE_USER, 72 content_settings::SETTING_SOURCE_USER,
73 content_settings::SETTING_SOURCE_USER,
72 }; 74 };
73 COMPILE_ASSERT(arraysize(kProviderSourceMap) == 75 COMPILE_ASSERT(arraysize(kProviderSourceMap) ==
74 HostContentSettingsMap::NUM_PROVIDER_TYPES, 76 HostContentSettingsMap::NUM_PROVIDER_TYPES,
75 kProviderSourceMap_has_incorrect_size); 77 kProviderSourceMap_has_incorrect_size);
76 78
77 // Returns true if the |content_type| supports a resource identifier. 79 // Returns true if the |content_type| supports a resource identifier.
78 // Resource identifiers are supported (but not required) for plug-ins. 80 // Resource identifiers are supported (but not required) for plug-ins.
79 bool SupportsResourceIdentifier(ContentSettingsType content_type) { 81 bool SupportsResourceIdentifier(ContentSettingsType content_type) {
80 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS; 82 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS;
81 } 83 }
82 84
83 } // namespace 85 } // namespace
84 86
85 HostContentSettingsMap::HostContentSettingsMap( 87 HostContentSettingsMap::HostContentSettingsMap(PrefService* prefs,
86 PrefService* prefs, 88 bool incognito)
87 bool incognito) : 89 :
88 #ifndef NDEBUG 90 #ifndef NDEBUG
89 used_from_thread_id_(base::PlatformThread::CurrentId()), 91 used_from_thread_id_(base::PlatformThread::CurrentId()),
90 #endif 92 #endif
91 prefs_(prefs), 93 prefs_(prefs),
92 is_off_the_record_(incognito) { 94 is_off_the_record_(incognito) {
93 content_settings::ObservableProvider* policy_provider = 95 content_settings::ObservableProvider* policy_provider =
94 new content_settings::PolicyProvider(prefs_); 96 new content_settings::PolicyProvider(prefs_);
95 policy_provider->AddObserver(this); 97 policy_provider->AddObserver(this);
96 content_settings_providers_[POLICY_PROVIDER] = policy_provider; 98 content_settings_providers_[POLICY_PROVIDER] = policy_provider;
97 99
98 content_settings::ObservableProvider* pref_provider = 100 content_settings::ObservableProvider* pref_provider =
99 new content_settings::PrefProvider(prefs_, is_off_the_record_); 101 new content_settings::PrefProvider(prefs_, is_off_the_record_);
100 pref_provider->AddObserver(this); 102 pref_provider->AddObserver(this);
101 content_settings_providers_[PREF_PROVIDER] = pref_provider; 103 content_settings_providers_[PREF_PROVIDER] = pref_provider;
102 104
103 content_settings::ObservableProvider* default_provider = 105 content_settings::ObservableProvider* default_provider =
104 new content_settings::DefaultProvider(prefs_, is_off_the_record_); 106 new content_settings::DefaultProvider(prefs_, is_off_the_record_);
105 default_provider->AddObserver(this); 107 default_provider->AddObserver(this);
106 content_settings_providers_[DEFAULT_PROVIDER] = default_provider; 108 content_settings_providers_[DEFAULT_PROVIDER] = default_provider;
107 109
110 content_settings_providers_[OVERRIDE_PROVIDER] =
111 new content_settings::OverrideProvider(prefs_, is_off_the_record_);
112
108 if (!is_off_the_record_) { 113 if (!is_off_the_record_) {
109 // Migrate obsolete preferences. 114 // Migrate obsolete preferences.
110 MigrateObsoleteClearOnExitPref(); 115 MigrateObsoleteClearOnExitPref();
111 } 116 }
112 } 117 }
113 118
114 #if defined(ENABLE_EXTENSIONS) 119 #if defined(ENABLE_EXTENSIONS)
115 void HostContentSettingsMap::RegisterExtensionService( 120 void HostContentSettingsMap::RegisterExtensionService(
116 ExtensionService* extension_service) { 121 ExtensionService* extension_service) {
117 DCHECK(extension_service); 122 DCHECK(extension_service);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 159 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
155 registry->RegisterBooleanPref( 160 registry->RegisterBooleanPref(
156 prefs::kContentSettingsClearOnExitMigrated, 161 prefs::kContentSettingsClearOnExitMigrated,
157 false, 162 false,
158 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 163 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
159 164
160 // Register the prefs for the content settings providers. 165 // Register the prefs for the content settings providers.
161 content_settings::DefaultProvider::RegisterProfilePrefs(registry); 166 content_settings::DefaultProvider::RegisterProfilePrefs(registry);
162 content_settings::PrefProvider::RegisterProfilePrefs(registry); 167 content_settings::PrefProvider::RegisterProfilePrefs(registry);
163 content_settings::PolicyProvider::RegisterProfilePrefs(registry); 168 content_settings::PolicyProvider::RegisterProfilePrefs(registry);
169 content_settings::OverrideProvider::RegisterProfilePrefs(registry);
164 } 170 }
165 171
166 ContentSetting HostContentSettingsMap::GetDefaultContentSettingFromProvider( 172 ContentSetting HostContentSettingsMap::GetDefaultContentSettingFromProvider(
167 ContentSettingsType content_type, 173 ContentSettingsType content_type,
168 content_settings::ProviderInterface* provider) const { 174 content_settings::ProviderInterface* provider) const {
169 scoped_ptr<content_settings::RuleIterator> rule_iterator( 175 scoped_ptr<content_settings::RuleIterator> rule_iterator(
170 provider->GetRuleIterator(content_type, std::string(), false)); 176 provider->GetRuleIterator(content_type, std::string(), false));
171 177
172 ContentSettingsPattern wildcard = ContentSettingsPattern::Wildcard(); 178 ContentSettingsPattern wildcard = ContentSettingsPattern::Wildcard();
173 while (rule_iterator->HasNext()) { 179 while (rule_iterator->HasNext()) {
174 content_settings::Rule rule = rule_iterator->Next(); 180 content_settings::Rule rule = rule_iterator->Next();
175 if (rule.primary_pattern == wildcard && 181 if (rule.primary_pattern == wildcard &&
176 rule.secondary_pattern == wildcard) { 182 rule.secondary_pattern == wildcard) {
177 return content_settings::ValueToContentSetting(rule.value.get()); 183 return content_settings::ValueToContentSetting(rule.value.get());
178 } 184 }
179 } 185 }
180 return CONTENT_SETTING_DEFAULT; 186 return CONTENT_SETTING_DEFAULT;
181 } 187 }
182 188
183 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( 189 ContentSetting HostContentSettingsMap::GetDefaultContentSetting(
184 ContentSettingsType content_type, 190 ContentSettingsType content_type,
185 std::string* provider_id) const { 191 std::string* provider_id) const {
186 UsedContentSettingsProviders(); 192 UsedContentSettingsProviders();
187 193
188 // Iterate through the list of providers and return the first non-NULL value 194 // Iterate through the list of providers and return the first non-NULL value
189 // that matches |primary_url| and |secondary_url|. 195 // that matches |primary_url| and |secondary_url|.
190 for (ConstProviderIterator provider = content_settings_providers_.begin(); 196 for (ConstProviderIterator provider = content_settings_providers_.begin();
191 provider != content_settings_providers_.end(); 197 provider != content_settings_providers_.end();
192 ++provider) { 198 ++provider) {
193 if (provider->first == PREF_PROVIDER) 199 if (provider->first == PREF_PROVIDER ||
200 provider->first == OVERRIDE_PROVIDER)
194 continue; 201 continue;
195 ContentSetting default_setting = 202 ContentSetting default_setting =
196 GetDefaultContentSettingFromProvider(content_type, provider->second); 203 GetDefaultContentSettingFromProvider(content_type, provider->second);
197 if (default_setting != CONTENT_SETTING_DEFAULT) { 204 if (default_setting != CONTENT_SETTING_DEFAULT) {
198 if (provider_id) 205 if (provider_id)
199 *provider_id = kProviderNames[provider->first]; 206 *provider_id = kProviderNames[provider->first];
200 return default_setting; 207 return default_setting;
201 } 208 }
202 } 209 }
203 210
204 // The method GetDefaultContentSetting always has to return an explicit 211 // The method GetDefaultContentSetting always has to return an explicit
205 // value that is to be used as default. We here rely on the 212 // value that is to be used as default. We here rely on the
206 // DefaultProvider to always provide a value. 213 // DefaultProvider to always provide a value.
207 NOTREACHED(); 214 NOTREACHED();
208 return CONTENT_SETTING_DEFAULT; 215 return CONTENT_SETTING_DEFAULT;
209 } 216 }
210 217
211 ContentSetting HostContentSettingsMap::GetContentSetting( 218 ContentSetting HostContentSettingsMap::GetContentSetting(
212 const GURL& primary_url, 219 const GURL& primary_url,
213 const GURL& secondary_url, 220 const GURL& secondary_url,
214 ContentSettingsType content_type, 221 ContentSettingsType content_type,
215 const std::string& resource_identifier) const { 222 const std::string& resource_identifier) const {
216 DCHECK(!ContentTypeHasCompoundValue(content_type)); 223 DCHECK(!ContentTypeHasCompoundValue(content_type));
217 scoped_ptr<base::Value> value(GetWebsiteSetting( 224 scoped_ptr<base::Value> value = GetWebsiteSetting(
218 primary_url, secondary_url, content_type, resource_identifier, NULL)); 225 primary_url, secondary_url, content_type, resource_identifier, NULL);
219 return content_settings::ValueToContentSetting(value.get()); 226 return content_settings::ValueToContentSetting(value.get());
220 } 227 }
221 228
222 void HostContentSettingsMap::GetSettingsForOneType( 229 void HostContentSettingsMap::GetSettingsForOneType(
223 ContentSettingsType content_type, 230 ContentSettingsType content_type,
224 const std::string& resource_identifier, 231 const std::string& resource_identifier,
225 ContentSettingsForOneType* settings) const { 232 ContentSettingsForOneType* settings) const {
226 DCHECK(SupportsResourceIdentifier(content_type) || 233 DCHECK(SupportsResourceIdentifier(content_type) ||
227 resource_identifier.empty()); 234 resource_identifier.empty());
228 DCHECK(settings); 235 DCHECK(settings);
229 UsedContentSettingsProviders(); 236 UsedContentSettingsProviders();
230 237
231 settings->clear(); 238 settings->clear();
232 for (ConstProviderIterator provider = content_settings_providers_.begin(); 239 for (ConstProviderIterator provider = content_settings_providers_.begin();
233 provider != content_settings_providers_.end(); 240 provider != content_settings_providers_.end();
234 ++provider) { 241 ++provider) {
242 if (provider->first == OVERRIDE_PROVIDER)
243 continue;
235 // For each provider, iterate first the incognito-specific rules, then the 244 // For each provider, iterate first the incognito-specific rules, then the
236 // normal rules. 245 // normal rules.
237 if (is_off_the_record_) { 246 if (is_off_the_record_) {
238 AddSettingsForOneType(provider->second, 247 AddSettingsForOneType(provider->second,
239 provider->first, 248 provider->first,
240 content_type, 249 content_type,
241 resource_identifier, 250 resource_identifier,
242 settings, 251 settings,
243 true); 252 true);
244 } 253 }
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 base::Time HostContentSettingsMap::GetLastUsageByPattern( 408 base::Time HostContentSettingsMap::GetLastUsageByPattern(
400 const ContentSettingsPattern& primary_pattern, 409 const ContentSettingsPattern& primary_pattern,
401 const ContentSettingsPattern& secondary_pattern, 410 const ContentSettingsPattern& secondary_pattern,
402 ContentSettingsType content_type) { 411 ContentSettingsType content_type) {
403 UsedContentSettingsProviders(); 412 UsedContentSettingsProviders();
404 413
405 return GetPrefProvider()->GetLastUsage( 414 return GetPrefProvider()->GetLastUsage(
406 primary_pattern, secondary_pattern, content_type); 415 primary_pattern, secondary_pattern, content_type);
407 } 416 }
408 417
418 ContentSetting HostContentSettingsMap::GetContentSettingWithoutOverride(
419 const GURL& primary_url,
420 const GURL& secondary_url,
421 ContentSettingsType content_type,
422 const std::string& resource_identifier) {
423 scoped_ptr<base::Value> value(GetWebsiteSettingWithoutOverride(
424 primary_url, secondary_url, content_type, resource_identifier, NULL));
425 return content_settings::ValueToContentSetting(value.get());
426 }
427
428 scoped_ptr<base::Value>
429 HostContentSettingsMap::GetWebsiteSettingWithoutOverride(
430 const GURL& primary_url,
431 const GURL& secondary_url,
432 ContentSettingsType content_type,
433 const std::string& resource_identifier,
434 content_settings::SettingInfo* info) const {
435 return GetWebsiteSettingInternal(primary_url,
436 secondary_url,
437 content_type,
438 resource_identifier,
439 info,
440 false);
441 }
442
443 void HostContentSettingsMap::SetContentSettingOverride(
444 ContentSettingsType content_type,
445 bool is_enabled) {
446 UsedContentSettingsProviders();
447
448 content_settings::OverrideProvider* override =
449 static_cast<content_settings::OverrideProvider*>(
450 content_settings_providers_[OVERRIDE_PROVIDER]);
451 override->SetOverrideSetting(content_type, is_enabled);
452 }
453
454 bool HostContentSettingsMap::GetContentSettingOverride(
455 ContentSettingsType content_type) {
456 UsedContentSettingsProviders();
457
458 content_settings::OverrideProvider* override =
459 static_cast<content_settings::OverrideProvider*>(
460 content_settings_providers_[OVERRIDE_PROVIDER]);
461 return override->IsEnabled(content_type);
462 }
463
409 void HostContentSettingsMap::AddObserver(content_settings::Observer* observer) { 464 void HostContentSettingsMap::AddObserver(content_settings::Observer* observer) {
410 observers_.AddObserver(observer); 465 observers_.AddObserver(observer);
411 } 466 }
412 467
413 void HostContentSettingsMap::RemoveObserver( 468 void HostContentSettingsMap::RemoveObserver(
414 content_settings::Observer* observer) { 469 content_settings::Observer* observer) {
415 observers_.RemoveObserver(observer); 470 observers_.RemoveObserver(observer);
416 } 471 }
417 472
418 void HostContentSettingsMap::SetPrefClockForTesting( 473 void HostContentSettingsMap::SetPrefClockForTesting(
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 return secondary_url.SchemeIs(extensions::kExtensionScheme); 740 return secondary_url.SchemeIs(extensions::kExtensionScheme);
686 default: 741 default:
687 return true; 742 return true;
688 } 743 }
689 } 744 }
690 #endif 745 #endif
691 return primary_url.SchemeIs(content::kChromeDevToolsScheme) || 746 return primary_url.SchemeIs(content::kChromeDevToolsScheme) ||
692 primary_url.SchemeIs(content::kChromeUIScheme); 747 primary_url.SchemeIs(content::kChromeUIScheme);
693 } 748 }
694 749
695 base::Value* HostContentSettingsMap::GetWebsiteSetting( 750 scoped_ptr<base::Value> HostContentSettingsMap::GetWebsiteSetting(
696 const GURL& primary_url, 751 const GURL& primary_url,
697 const GURL& secondary_url, 752 const GURL& secondary_url,
698 ContentSettingsType content_type, 753 ContentSettingsType content_type,
699 const std::string& resource_identifier, 754 const std::string& resource_identifier,
700 content_settings::SettingInfo* info) const { 755 content_settings::SettingInfo* info) const {
701 DCHECK(SupportsResourceIdentifier(content_type) || 756 DCHECK(SupportsResourceIdentifier(content_type) ||
702 resource_identifier.empty()); 757 resource_identifier.empty());
703 758
704 // Check if the scheme of the requesting url is whitelisted. 759 // Check if the scheme of the requesting url is whitelisted.
705 if (ShouldAllowAllContent(primary_url, secondary_url, content_type)) { 760 if (ShouldAllowAllContent(primary_url, secondary_url, content_type)) {
706 if (info) { 761 if (info) {
707 info->source = content_settings::SETTING_SOURCE_WHITELIST; 762 info->source = content_settings::SETTING_SOURCE_WHITELIST;
708 info->primary_pattern = ContentSettingsPattern::Wildcard(); 763 info->primary_pattern = ContentSettingsPattern::Wildcard();
709 info->secondary_pattern = ContentSettingsPattern::Wildcard(); 764 info->secondary_pattern = ContentSettingsPattern::Wildcard();
710 } 765 }
711 return new base::FundamentalValue(CONTENT_SETTING_ALLOW); 766 return scoped_ptr<base::Value>(
767 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
712 } 768 }
713 769
770 return GetWebsiteSettingInternal(primary_url,
771 secondary_url,
772 content_type,
773 resource_identifier,
774 info,
775 true);
776 }
777
778 // static
779 HostContentSettingsMap::ProviderType
780 HostContentSettingsMap::GetProviderTypeFromSource(const std::string& source) {
781 for (size_t i = 0; i < arraysize(kProviderNames); ++i) {
782 if (source == kProviderNames[i])
783 return static_cast<ProviderType>(i);
784 }
785
786 NOTREACHED();
787 return DEFAULT_PROVIDER;
788 }
789
790 content_settings::PrefProvider* HostContentSettingsMap::GetPrefProvider() {
791 return static_cast<content_settings::PrefProvider*>(
792 content_settings_providers_[PREF_PROVIDER]);
793 }
794
795 scoped_ptr<base::Value> HostContentSettingsMap::GetWebsiteSettingInternal(
796 const GURL& primary_url,
797 const GURL& secondary_url,
798 ContentSettingsType content_type,
799 const std::string& resource_identifier,
800 content_settings::SettingInfo* info,
801 bool get_override) const {
802 UsedContentSettingsProviders();
714 ContentSettingsPattern* primary_pattern = NULL; 803 ContentSettingsPattern* primary_pattern = NULL;
715 ContentSettingsPattern* secondary_pattern = NULL; 804 ContentSettingsPattern* secondary_pattern = NULL;
716 if (info) { 805 if (info) {
717 primary_pattern = &info->primary_pattern; 806 primary_pattern = &info->primary_pattern;
718 secondary_pattern = &info->secondary_pattern; 807 secondary_pattern = &info->secondary_pattern;
719 } 808 }
720 809
721 // The list of |content_settings_providers_| is ordered according to their 810 // The list of |content_settings_providers_| is ordered according to their
722 // precedence. 811 // precedence.
723 for (ConstProviderIterator provider = content_settings_providers_.begin(); 812 for (ConstProviderIterator provider = content_settings_providers_.begin();
724 provider != content_settings_providers_.end(); 813 provider != content_settings_providers_.end();
725 ++provider) { 814 ++provider) {
726 base::Value* value = content_settings::GetContentSettingValueAndPatterns( 815 if (!get_override && provider->first == OVERRIDE_PROVIDER)
727 provider->second, primary_url, secondary_url, content_type, 816 continue;
728 resource_identifier, is_off_the_record_, 817
729 primary_pattern, secondary_pattern); 818 scoped_ptr<base::Value> value(
819 content_settings::GetContentSettingValueAndPatterns(provider->second,
820 primary_url,
821 secondary_url,
822 content_type,
823 resource_identifier,
824 is_off_the_record_,
825 primary_pattern,
826 secondary_pattern));
730 if (value) { 827 if (value) {
731 if (info) 828 if (info)
732 info->source = kProviderSourceMap[provider->first]; 829 info->source = kProviderSourceMap[provider->first];
733 return value; 830 return value.Pass();
734 } 831 }
735 } 832 }
736 833
737 if (info) { 834 if (info) {
738 info->source = content_settings::SETTING_SOURCE_NONE; 835 info->source = content_settings::SETTING_SOURCE_NONE;
739 info->primary_pattern = ContentSettingsPattern(); 836 info->primary_pattern = ContentSettingsPattern();
740 info->secondary_pattern = ContentSettingsPattern(); 837 info->secondary_pattern = ContentSettingsPattern();
741 } 838 }
742 return NULL; 839 return scoped_ptr<base::Value>();
743 } 840 }
744
745 // static
746 HostContentSettingsMap::ProviderType
747 HostContentSettingsMap::GetProviderTypeFromSource(
748 const std::string& source) {
749 for (size_t i = 0; i < arraysize(kProviderNames); ++i) {
750 if (source == kProviderNames[i])
751 return static_cast<ProviderType>(i);
752 }
753
754 NOTREACHED();
755 return DEFAULT_PROVIDER;
756 }
757
758 content_settings::PrefProvider* HostContentSettingsMap::GetPrefProvider() {
759 return static_cast<content_settings::PrefProvider*>(
760 content_settings_providers_[PREF_PROVIDER]);
761 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698