Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / content_settings / host_content_settings_map.cc
blob9c85abff35d0d10ea819b7f8e8bbb9e6776bfa7d
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
3 // found in the LICENSE file.
5 #include "chrome/browser/content_settings/host_content_settings_map.h"
7 #include <utility>
9 #include "base/basictypes.h"
10 #include "base/command_line.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/clock.h"
16 #include "chrome/browser/content_settings/content_settings_default_provider.h"
17 #include "chrome/browser/content_settings/content_settings_observable_provider.h"
18 #include "chrome/browser/content_settings/content_settings_policy_provider.h"
19 #include "chrome/browser/content_settings/content_settings_pref_provider.h"
20 #include "chrome/browser/content_settings/content_settings_utils.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "components/content_settings/core/browser/content_settings_details.h"
25 #include "components/content_settings/core/browser/content_settings_provider.h"
26 #include "components/content_settings/core/browser/content_settings_rule.h"
27 #include "components/content_settings/core/common/content_settings_pattern.h"
28 #include "components/pref_registry/pref_registry_syncable.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/common/content_switches.h"
31 #include "net/base/net_errors.h"
32 #include "net/base/static_cookie_policy.h"
33 #include "url/gurl.h"
35 #if defined(ENABLE_EXTENSIONS)
36 #include "extensions/common/constants.h"
37 #endif
39 using content::BrowserThread;
41 namespace {
43 typedef std::vector<content_settings::Rule> Rules;
45 typedef std::pair<std::string, std::string> StringPair;
47 // TODO(bauerb): Expose constants.
48 const char* kProviderNames[] = {
49 "platform_app",
50 "policy",
51 "extension",
52 "preference",
53 "default"
56 content_settings::SettingSource kProviderSourceMap[] = {
57 content_settings::SETTING_SOURCE_EXTENSION,
58 content_settings::SETTING_SOURCE_POLICY,
59 content_settings::SETTING_SOURCE_EXTENSION,
60 content_settings::SETTING_SOURCE_USER,
61 content_settings::SETTING_SOURCE_USER,
63 COMPILE_ASSERT(arraysize(kProviderSourceMap) ==
64 HostContentSettingsMap::NUM_PROVIDER_TYPES,
65 kProviderSourceMap_has_incorrect_size);
67 // Returns true if the |content_type| supports a resource identifier.
68 // Resource identifiers are supported (but not required) for plug-ins.
69 bool SupportsResourceIdentifier(ContentSettingsType content_type) {
70 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS;
73 } // namespace
75 HostContentSettingsMap::HostContentSettingsMap(
76 PrefService* prefs,
77 bool incognito) :
78 #ifndef NDEBUG
79 used_from_thread_id_(base::PlatformThread::CurrentId()),
80 #endif
81 prefs_(prefs),
82 is_off_the_record_(incognito) {
83 content_settings::ObservableProvider* policy_provider =
84 new content_settings::PolicyProvider(prefs_);
85 policy_provider->AddObserver(this);
86 content_settings_providers_[POLICY_PROVIDER] = policy_provider;
88 content_settings::ObservableProvider* pref_provider =
89 new content_settings::PrefProvider(prefs_, is_off_the_record_);
90 pref_provider->AddObserver(this);
91 content_settings_providers_[PREF_PROVIDER] = pref_provider;
93 content_settings::ObservableProvider* default_provider =
94 new content_settings::DefaultProvider(prefs_, is_off_the_record_);
95 default_provider->AddObserver(this);
96 content_settings_providers_[DEFAULT_PROVIDER] = default_provider;
98 if (!is_off_the_record_) {
99 // Migrate obsolete preferences.
100 MigrateObsoleteClearOnExitPref();
104 // static
105 void HostContentSettingsMap::RegisterProfilePrefs(
106 user_prefs::PrefRegistrySyncable* registry) {
107 registry->RegisterIntegerPref(
108 prefs::kContentSettingsWindowLastTabIndex,
110 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
111 registry->RegisterBooleanPref(
112 prefs::kContentSettingsClearOnExitMigrated,
113 false,
114 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
116 // Register the prefs for the content settings providers.
117 content_settings::DefaultProvider::RegisterProfilePrefs(registry);
118 content_settings::PrefProvider::RegisterProfilePrefs(registry);
119 content_settings::PolicyProvider::RegisterProfilePrefs(registry);
122 void HostContentSettingsMap::RegisterProvider(
123 ProviderType type,
124 scoped_ptr<content_settings::ObservableProvider> provider) {
125 DCHECK(!content_settings_providers_[type]);
126 provider->AddObserver(this);
127 content_settings_providers_[type] = provider.release();
129 #ifndef NDEBUG
130 DCHECK_NE(used_from_thread_id_, base::kInvalidThreadId)
131 << "Used from multiple threads before initialization complete.";
132 #endif
134 OnContentSettingChanged(ContentSettingsPattern(),
135 ContentSettingsPattern(),
136 CONTENT_SETTINGS_TYPE_DEFAULT,
137 std::string());
140 ContentSetting HostContentSettingsMap::GetDefaultContentSettingFromProvider(
141 ContentSettingsType content_type,
142 content_settings::ProviderInterface* provider) const {
143 scoped_ptr<content_settings::RuleIterator> rule_iterator(
144 provider->GetRuleIterator(content_type, std::string(), false));
146 ContentSettingsPattern wildcard = ContentSettingsPattern::Wildcard();
147 while (rule_iterator->HasNext()) {
148 content_settings::Rule rule = rule_iterator->Next();
149 if (rule.primary_pattern == wildcard &&
150 rule.secondary_pattern == wildcard) {
151 return content_settings::ValueToContentSetting(rule.value.get());
154 return CONTENT_SETTING_DEFAULT;
157 ContentSetting HostContentSettingsMap::GetDefaultContentSetting(
158 ContentSettingsType content_type,
159 std::string* provider_id) const {
160 UsedContentSettingsProviders();
162 // Iterate through the list of providers and return the first non-NULL value
163 // that matches |primary_url| and |secondary_url|.
164 for (ConstProviderIterator provider = content_settings_providers_.begin();
165 provider != content_settings_providers_.end();
166 ++provider) {
167 if (provider->first == PREF_PROVIDER)
168 continue;
169 ContentSetting default_setting =
170 GetDefaultContentSettingFromProvider(content_type, provider->second);
171 if (default_setting != CONTENT_SETTING_DEFAULT) {
172 if (provider_id)
173 *provider_id = kProviderNames[provider->first];
174 return default_setting;
178 return CONTENT_SETTING_DEFAULT;
181 ContentSetting HostContentSettingsMap::GetContentSetting(
182 const GURL& primary_url,
183 const GURL& secondary_url,
184 ContentSettingsType content_type,
185 const std::string& resource_identifier) const {
186 DCHECK(!ContentTypeHasCompoundValue(content_type));
187 scoped_ptr<base::Value> value(GetWebsiteSetting(
188 primary_url, secondary_url, content_type, resource_identifier, NULL));
189 return content_settings::ValueToContentSetting(value.get());
192 void HostContentSettingsMap::GetSettingsForOneType(
193 ContentSettingsType content_type,
194 const std::string& resource_identifier,
195 ContentSettingsForOneType* settings) const {
196 DCHECK(SupportsResourceIdentifier(content_type) ||
197 resource_identifier.empty());
198 DCHECK(settings);
199 UsedContentSettingsProviders();
201 settings->clear();
202 for (ConstProviderIterator provider = content_settings_providers_.begin();
203 provider != content_settings_providers_.end();
204 ++provider) {
205 // For each provider, iterate first the incognito-specific rules, then the
206 // normal rules.
207 if (is_off_the_record_) {
208 AddSettingsForOneType(provider->second,
209 provider->first,
210 content_type,
211 resource_identifier,
212 settings,
213 true);
215 AddSettingsForOneType(provider->second,
216 provider->first,
217 content_type,
218 resource_identifier,
219 settings,
220 false);
224 void HostContentSettingsMap::SetDefaultContentSetting(
225 ContentSettingsType content_type,
226 ContentSetting setting) {
227 DCHECK(IsSettingAllowedForType(prefs_, setting, content_type));
229 base::Value* value = NULL;
230 if (setting != CONTENT_SETTING_DEFAULT)
231 value = new base::FundamentalValue(setting);
232 SetWebsiteSetting(
233 ContentSettingsPattern::Wildcard(),
234 ContentSettingsPattern::Wildcard(),
235 content_type,
236 std::string(),
237 value);
240 void HostContentSettingsMap::SetWebsiteSetting(
241 const ContentSettingsPattern& primary_pattern,
242 const ContentSettingsPattern& secondary_pattern,
243 ContentSettingsType content_type,
244 const std::string& resource_identifier,
245 base::Value* value) {
246 DCHECK(IsValueAllowedForType(prefs_, value, content_type));
247 DCHECK(SupportsResourceIdentifier(content_type) ||
248 resource_identifier.empty());
249 UsedContentSettingsProviders();
251 for (ProviderIterator provider = content_settings_providers_.begin();
252 provider != content_settings_providers_.end();
253 ++provider) {
254 if (provider->second->SetWebsiteSetting(primary_pattern,
255 secondary_pattern,
256 content_type,
257 resource_identifier,
258 value)) {
259 return;
262 NOTREACHED();
265 void HostContentSettingsMap::SetNarrowestWebsiteSetting(
266 const ContentSettingsPattern& primary_pattern,
267 const ContentSettingsPattern& secondary_pattern,
268 ContentSettingsType content_type,
269 const std::string& resource_identifier,
270 ContentSetting setting,
271 content_settings::SettingInfo existing_info) {
272 ContentSettingsPattern narrow_primary = primary_pattern;
273 ContentSettingsPattern narrow_secondary = secondary_pattern;
275 DCHECK_EQ(content_settings::SETTING_SOURCE_USER, existing_info.source);
276 ContentSettingsPattern::Relation r1 =
277 existing_info.primary_pattern.Compare(primary_pattern);
278 if (r1 == ContentSettingsPattern::PREDECESSOR) {
279 narrow_primary = existing_info.primary_pattern;
280 } else if (r1 == ContentSettingsPattern::IDENTITY) {
281 ContentSettingsPattern::Relation r2 =
282 existing_info.secondary_pattern.Compare(secondary_pattern);
283 DCHECK(r2 != ContentSettingsPattern::DISJOINT_ORDER_POST &&
284 r2 != ContentSettingsPattern::DISJOINT_ORDER_PRE);
285 if (r2 == ContentSettingsPattern::PREDECESSOR)
286 narrow_secondary = existing_info.secondary_pattern;
289 SetContentSetting(
290 narrow_primary, narrow_secondary, content_type, std::string(), setting);
293 void HostContentSettingsMap::SetContentSetting(
294 const ContentSettingsPattern& primary_pattern,
295 const ContentSettingsPattern& secondary_pattern,
296 ContentSettingsType content_type,
297 const std::string& resource_identifier,
298 ContentSetting setting) {
299 DCHECK(!ContentTypeHasCompoundValue(content_type));
301 if (setting == CONTENT_SETTING_ALLOW &&
302 (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION ||
303 content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS)) {
304 UpdateLastUsageByPattern(primary_pattern, secondary_pattern, content_type);
307 base::Value* value = NULL;
308 if (setting != CONTENT_SETTING_DEFAULT)
309 value = new base::FundamentalValue(setting);
310 SetWebsiteSetting(primary_pattern,
311 secondary_pattern,
312 content_type,
313 resource_identifier,
314 value);
317 ContentSetting HostContentSettingsMap::GetContentSettingAndMaybeUpdateLastUsage(
318 const GURL& primary_url,
319 const GURL& secondary_url,
320 ContentSettingsType content_type,
321 const std::string& resource_identifier) {
322 DCHECK_CURRENTLY_ON(BrowserThread::UI);
324 ContentSetting setting = GetContentSetting(
325 primary_url, secondary_url, content_type, resource_identifier);
326 if (setting == CONTENT_SETTING_ALLOW) {
327 UpdateLastUsageByPattern(
328 ContentSettingsPattern::FromURLNoWildcard(primary_url),
329 ContentSettingsPattern::FromURLNoWildcard(secondary_url),
330 content_type);
332 return setting;
335 void HostContentSettingsMap::UpdateLastUsage(const GURL& primary_url,
336 const GURL& secondary_url,
337 ContentSettingsType content_type) {
338 UpdateLastUsageByPattern(
339 ContentSettingsPattern::FromURLNoWildcard(primary_url),
340 ContentSettingsPattern::FromURLNoWildcard(secondary_url),
341 content_type);
344 void HostContentSettingsMap::UpdateLastUsageByPattern(
345 const ContentSettingsPattern& primary_pattern,
346 const ContentSettingsPattern& secondary_pattern,
347 ContentSettingsType content_type) {
348 UsedContentSettingsProviders();
350 GetPrefProvider()->UpdateLastUsage(
351 primary_pattern, secondary_pattern, content_type);
353 FOR_EACH_OBSERVER(
354 content_settings::Observer,
355 observers_,
356 OnContentSettingUsed(primary_pattern, secondary_pattern, content_type));
359 base::Time HostContentSettingsMap::GetLastUsage(
360 const GURL& primary_url,
361 const GURL& secondary_url,
362 ContentSettingsType content_type) {
363 return GetLastUsageByPattern(
364 ContentSettingsPattern::FromURLNoWildcard(primary_url),
365 ContentSettingsPattern::FromURLNoWildcard(secondary_url),
366 content_type);
369 base::Time HostContentSettingsMap::GetLastUsageByPattern(
370 const ContentSettingsPattern& primary_pattern,
371 const ContentSettingsPattern& secondary_pattern,
372 ContentSettingsType content_type) {
373 UsedContentSettingsProviders();
375 return GetPrefProvider()->GetLastUsage(
376 primary_pattern, secondary_pattern, content_type);
379 void HostContentSettingsMap::AddObserver(content_settings::Observer* observer) {
380 observers_.AddObserver(observer);
383 void HostContentSettingsMap::RemoveObserver(
384 content_settings::Observer* observer) {
385 observers_.RemoveObserver(observer);
388 void HostContentSettingsMap::SetPrefClockForTesting(
389 scoped_ptr<base::Clock> clock) {
390 UsedContentSettingsProviders();
392 GetPrefProvider()->SetClockForTesting(clock.Pass());
395 void HostContentSettingsMap::AddExceptionForURL(
396 const GURL& primary_url,
397 const GURL& secondary_url,
398 ContentSettingsType content_type,
399 ContentSetting setting) {
400 // TODO(markusheintz): Until the UI supports pattern pairs, both urls must
401 // match.
402 DCHECK(primary_url == secondary_url);
403 DCHECK(!ContentTypeHasCompoundValue(content_type));
405 // Make sure there is no entry that would override the pattern we are about
406 // to insert for exactly this URL.
407 SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(primary_url),
408 ContentSettingsPattern::Wildcard(),
409 content_type,
410 std::string(),
411 CONTENT_SETTING_DEFAULT);
413 SetContentSetting(ContentSettingsPattern::FromURL(primary_url),
414 ContentSettingsPattern::Wildcard(),
415 content_type,
416 std::string(),
417 setting);
420 void HostContentSettingsMap::ClearSettingsForOneType(
421 ContentSettingsType content_type) {
422 UsedContentSettingsProviders();
423 for (ProviderIterator provider = content_settings_providers_.begin();
424 provider != content_settings_providers_.end();
425 ++provider) {
426 provider->second->ClearAllContentSettingsRules(content_type);
430 bool HostContentSettingsMap::IsValueAllowedForType(
431 PrefService* prefs, const base::Value* value, ContentSettingsType type) {
432 return ContentTypeHasCompoundValue(type) || IsSettingAllowedForType(
433 prefs, content_settings::ValueToContentSetting(value), type);
436 // static
437 bool HostContentSettingsMap::IsSettingAllowedForType(
438 PrefService* prefs,
439 ContentSetting setting,
440 ContentSettingsType content_type) {
441 // We don't yet support stored content settings for mixed scripting.
442 if (content_type == CONTENT_SETTINGS_TYPE_MIXEDSCRIPT)
443 return false;
445 // BLOCK semantics are not implemented for fullscreen.
446 if (content_type == CONTENT_SETTINGS_TYPE_FULLSCREEN &&
447 setting == CONTENT_SETTING_BLOCK) {
448 return false;
451 // We don't support ALLOW for media default setting.
452 if (content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM &&
453 setting == CONTENT_SETTING_ALLOW) {
454 return false;
457 #if defined(OS_ANDROID)
458 // App banners store a dictionary.
459 if (content_type == CONTENT_SETTINGS_TYPE_APP_BANNER)
460 return false;
461 #endif
463 // DEFAULT, ALLOW and BLOCK are always allowed.
464 if (setting == CONTENT_SETTING_DEFAULT ||
465 setting == CONTENT_SETTING_ALLOW ||
466 setting == CONTENT_SETTING_BLOCK) {
467 return true;
469 switch (content_type) {
470 case CONTENT_SETTINGS_TYPE_COOKIES:
471 return setting == CONTENT_SETTING_SESSION_ONLY;
472 case CONTENT_SETTINGS_TYPE_PLUGINS:
473 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
474 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
475 case CONTENT_SETTINGS_TYPE_MOUSELOCK:
476 case CONTENT_SETTINGS_TYPE_MEDIASTREAM:
477 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC:
478 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA:
479 case CONTENT_SETTINGS_TYPE_PPAPI_BROKER:
480 case CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS:
481 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
482 return setting == CONTENT_SETTING_ASK;
483 default:
484 return false;
488 // static
489 bool HostContentSettingsMap::ContentTypeHasCompoundValue(
490 ContentSettingsType type) {
491 // Values for content type CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
492 // CONTENT_SETTINGS_TYPE_MEDIASTREAM, and
493 // CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS are of type dictionary/map.
494 // Compound types like dictionaries can't be mapped to the type
495 // |ContentSetting|.
496 #if defined(OS_ANDROID)
497 if (type == CONTENT_SETTINGS_TYPE_APP_BANNER)
498 return true;
499 #endif
501 return (type == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE ||
502 type == CONTENT_SETTINGS_TYPE_MEDIASTREAM ||
503 type == CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS);
506 void HostContentSettingsMap::OnContentSettingChanged(
507 const ContentSettingsPattern& primary_pattern,
508 const ContentSettingsPattern& secondary_pattern,
509 ContentSettingsType content_type,
510 std::string resource_identifier) {
511 FOR_EACH_OBSERVER(content_settings::Observer,
512 observers_,
513 OnContentSettingChanged(primary_pattern,
514 secondary_pattern,
515 content_type,
516 resource_identifier));
519 HostContentSettingsMap::~HostContentSettingsMap() {
520 DCHECK(!prefs_);
521 STLDeleteValues(&content_settings_providers_);
524 void HostContentSettingsMap::ShutdownOnUIThread() {
525 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
526 DCHECK(prefs_);
527 prefs_ = NULL;
528 for (ProviderIterator it = content_settings_providers_.begin();
529 it != content_settings_providers_.end();
530 ++it) {
531 it->second->ShutdownOnUIThread();
535 void HostContentSettingsMap::MigrateObsoleteClearOnExitPref() {
536 // Don't migrate more than once.
537 if (prefs_->HasPrefPath(prefs::kContentSettingsClearOnExitMigrated) &&
538 prefs_->GetBoolean(prefs::kContentSettingsClearOnExitMigrated)) {
539 return;
542 if (!prefs_->GetBoolean(prefs::kClearSiteDataOnExit)) {
543 // Nothing to be done
544 prefs_->SetBoolean(prefs::kContentSettingsClearOnExitMigrated, true);
545 return;
548 // Change the default cookie settings:
549 // old new
550 // ---------------- ----------------
551 // ALLOW SESSION_ONLY
552 // SESSION_ONLY SESSION_ONLY
553 // BLOCK BLOCK
554 ContentSetting default_setting = GetDefaultContentSettingFromProvider(
555 CONTENT_SETTINGS_TYPE_COOKIES,
556 content_settings_providers_[DEFAULT_PROVIDER]);
557 if (default_setting == CONTENT_SETTING_ALLOW) {
558 SetDefaultContentSetting(
559 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_SESSION_ONLY);
562 // Change the exceptions using the same rules.
563 ContentSettingsForOneType exceptions;
564 AddSettingsForOneType(content_settings_providers_[PREF_PROVIDER],
565 PREF_PROVIDER,
566 CONTENT_SETTINGS_TYPE_COOKIES,
567 std::string(),
568 &exceptions,
569 false);
570 for (ContentSettingsForOneType::iterator it = exceptions.begin();
571 it != exceptions.end(); ++it) {
572 if (it->setting != CONTENT_SETTING_ALLOW)
573 continue;
574 SetWebsiteSetting(it->primary_pattern,
575 it->secondary_pattern,
576 CONTENT_SETTINGS_TYPE_COOKIES,
577 std::string(),
578 new base::FundamentalValue(CONTENT_SETTING_SESSION_ONLY));
581 prefs_->SetBoolean(prefs::kContentSettingsClearOnExitMigrated, true);
584 void HostContentSettingsMap::AddSettingsForOneType(
585 const content_settings::ProviderInterface* provider,
586 ProviderType provider_type,
587 ContentSettingsType content_type,
588 const std::string& resource_identifier,
589 ContentSettingsForOneType* settings,
590 bool incognito) const {
591 scoped_ptr<content_settings::RuleIterator> rule_iterator(
592 provider->GetRuleIterator(content_type,
593 resource_identifier,
594 incognito));
595 while (rule_iterator->HasNext()) {
596 const content_settings::Rule& rule = rule_iterator->Next();
597 ContentSetting setting_value = CONTENT_SETTING_DEFAULT;
598 // TODO(bauerb): Return rules as a list of values, not content settings.
599 // Handle the case using compound values for its exceptions and arbitrary
600 // values for its default setting. Here we assume all the exceptions
601 // are granted as |CONTENT_SETTING_ALLOW|.
602 if (ContentTypeHasCompoundValue(content_type) &&
603 rule.value.get() &&
604 rule.primary_pattern != ContentSettingsPattern::Wildcard()) {
605 setting_value = CONTENT_SETTING_ALLOW;
606 } else {
607 setting_value = content_settings::ValueToContentSetting(rule.value.get());
609 settings->push_back(ContentSettingPatternSource(
610 rule.primary_pattern, rule.secondary_pattern,
611 setting_value,
612 kProviderNames[provider_type],
613 incognito));
617 void HostContentSettingsMap::UsedContentSettingsProviders() const {
618 #ifndef NDEBUG
619 if (used_from_thread_id_ == base::kInvalidThreadId)
620 return;
622 if (base::PlatformThread::CurrentId() != used_from_thread_id_)
623 used_from_thread_id_ = base::kInvalidThreadId;
624 #endif
627 bool HostContentSettingsMap::ShouldAllowAllContent(
628 const GURL& primary_url,
629 const GURL& secondary_url,
630 ContentSettingsType content_type) {
631 if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS ||
632 content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION ||
633 content_type == CONTENT_SETTINGS_TYPE_MIDI_SYSEX) {
634 return false;
636 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
637 if (content_type == CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER) {
638 return false;
640 #endif
641 if (secondary_url.SchemeIs(content::kChromeUIScheme) &&
642 content_type == CONTENT_SETTINGS_TYPE_COOKIES &&
643 primary_url.SchemeIsSecure()) {
644 return true;
646 #if defined(ENABLE_EXTENSIONS)
647 if (primary_url.SchemeIs(extensions::kExtensionScheme)) {
648 switch (content_type) {
649 case CONTENT_SETTINGS_TYPE_PLUGINS:
650 case CONTENT_SETTINGS_TYPE_MEDIASTREAM:
651 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC:
652 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA:
653 return false;
654 case CONTENT_SETTINGS_TYPE_COOKIES:
655 return secondary_url.SchemeIs(extensions::kExtensionScheme);
656 default:
657 return true;
660 #endif
661 return primary_url.SchemeIs(content::kChromeDevToolsScheme) ||
662 primary_url.SchemeIs(content::kChromeUIScheme);
665 base::Value* HostContentSettingsMap::GetWebsiteSetting(
666 const GURL& primary_url,
667 const GURL& secondary_url,
668 ContentSettingsType content_type,
669 const std::string& resource_identifier,
670 content_settings::SettingInfo* info) const {
671 DCHECK(SupportsResourceIdentifier(content_type) ||
672 resource_identifier.empty());
674 // Check if the scheme of the requesting url is whitelisted.
675 if (ShouldAllowAllContent(primary_url, secondary_url, content_type)) {
676 if (info) {
677 info->source = content_settings::SETTING_SOURCE_WHITELIST;
678 info->primary_pattern = ContentSettingsPattern::Wildcard();
679 info->secondary_pattern = ContentSettingsPattern::Wildcard();
681 return new base::FundamentalValue(CONTENT_SETTING_ALLOW);
684 ContentSettingsPattern* primary_pattern = NULL;
685 ContentSettingsPattern* secondary_pattern = NULL;
686 if (info) {
687 primary_pattern = &info->primary_pattern;
688 secondary_pattern = &info->secondary_pattern;
691 // The list of |content_settings_providers_| is ordered according to their
692 // precedence.
693 for (ConstProviderIterator provider = content_settings_providers_.begin();
694 provider != content_settings_providers_.end();
695 ++provider) {
696 base::Value* value = content_settings::GetContentSettingValueAndPatterns(
697 provider->second, primary_url, secondary_url, content_type,
698 resource_identifier, is_off_the_record_,
699 primary_pattern, secondary_pattern);
700 if (value) {
701 if (info)
702 info->source = kProviderSourceMap[provider->first];
703 return value;
707 if (info) {
708 info->source = content_settings::SETTING_SOURCE_NONE;
709 info->primary_pattern = ContentSettingsPattern();
710 info->secondary_pattern = ContentSettingsPattern();
712 return NULL;
715 // static
716 HostContentSettingsMap::ProviderType
717 HostContentSettingsMap::GetProviderTypeFromSource(
718 const std::string& source) {
719 for (size_t i = 0; i < arraysize(kProviderNames); ++i) {
720 if (source == kProviderNames[i])
721 return static_cast<ProviderType>(i);
724 NOTREACHED();
725 return DEFAULT_PROVIDER;
728 content_settings::PrefProvider* HostContentSettingsMap::GetPrefProvider() {
729 return static_cast<content_settings::PrefProvider*>(
730 content_settings_providers_[PREF_PROVIDER]);