Clear webapp storage when site data is cleared
[chromium-blink-merge.git] / components / content_settings / core / browser / cookie_settings.cc
blob73ba6f64bac559b7121b84d7ea608e2c7964a841
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 "components/content_settings/core/browser/cookie_settings.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "components/content_settings/core/browser/content_settings_utils.h"
11 #include "components/content_settings/core/browser/host_content_settings_map.h"
12 #include "components/content_settings/core/common/content_settings_pattern.h"
13 #include "components/content_settings/core/common/pref_names.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/static_cookie_policy.h"
17 #include "url/gurl.h"
19 namespace {
21 bool IsValidSetting(ContentSetting setting) {
22 return (setting == CONTENT_SETTING_ALLOW ||
23 setting == CONTENT_SETTING_SESSION_ONLY ||
24 setting == CONTENT_SETTING_BLOCK);
27 bool IsAllowed(ContentSetting setting) {
28 DCHECK(IsValidSetting(setting));
29 return (setting == CONTENT_SETTING_ALLOW ||
30 setting == CONTENT_SETTING_SESSION_ONLY);
33 } // namespace
35 namespace content_settings {
37 CookieSettings::CookieSettings(
38 HostContentSettingsMap* host_content_settings_map,
39 PrefService* prefs,
40 const char* extension_scheme)
41 : host_content_settings_map_(host_content_settings_map),
42 extension_scheme_(extension_scheme),
43 block_third_party_cookies_(
44 prefs->GetBoolean(prefs::kBlockThirdPartyCookies)) {
45 pref_change_registrar_.Init(prefs);
46 pref_change_registrar_.Add(
47 prefs::kBlockThirdPartyCookies,
48 base::Bind(&CookieSettings::OnBlockThirdPartyCookiesChanged,
49 base::Unretained(this)));
52 ContentSetting CookieSettings::GetDefaultCookieSetting(
53 std::string* provider_id) const {
54 return host_content_settings_map_->GetDefaultContentSetting(
55 CONTENT_SETTINGS_TYPE_COOKIES, provider_id);
58 bool CookieSettings::IsReadingCookieAllowed(const GURL& url,
59 const GURL& first_party_url) const {
60 ContentSetting setting = GetCookieSetting(url, first_party_url, false, NULL);
61 return IsAllowed(setting);
64 bool CookieSettings::IsSettingCookieAllowed(const GURL& url,
65 const GURL& first_party_url) const {
66 ContentSetting setting = GetCookieSetting(url, first_party_url, true, NULL);
67 return IsAllowed(setting);
70 bool CookieSettings::IsCookieSessionOnly(const GURL& origin) const {
71 ContentSetting setting = GetCookieSetting(origin, origin, true, NULL);
72 DCHECK(IsValidSetting(setting));
73 return (setting == CONTENT_SETTING_SESSION_ONLY);
76 void CookieSettings::GetCookieSettings(
77 ContentSettingsForOneType* settings) const {
78 host_content_settings_map_->GetSettingsForOneType(
79 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), settings);
82 void CookieSettings::RegisterProfilePrefs(
83 user_prefs::PrefRegistrySyncable* registry) {
84 registry->RegisterBooleanPref(
85 prefs::kBlockThirdPartyCookies, false,
86 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
89 void CookieSettings::SetDefaultCookieSetting(ContentSetting setting) {
90 DCHECK(IsValidSetting(setting));
91 host_content_settings_map_->SetDefaultContentSetting(
92 CONTENT_SETTINGS_TYPE_COOKIES, setting);
95 void CookieSettings::SetCookieSetting(
96 const ContentSettingsPattern& primary_pattern,
97 const ContentSettingsPattern& secondary_pattern,
98 ContentSetting setting) {
99 DCHECK(IsValidSetting(setting));
100 if (setting == CONTENT_SETTING_SESSION_ONLY) {
101 DCHECK(secondary_pattern == ContentSettingsPattern::Wildcard());
103 host_content_settings_map_->SetContentSetting(
104 primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_COOKIES,
105 std::string(), setting);
108 void CookieSettings::ResetCookieSetting(
109 const ContentSettingsPattern& primary_pattern,
110 const ContentSettingsPattern& secondary_pattern) {
111 host_content_settings_map_->SetContentSetting(
112 primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_COOKIES,
113 std::string(), CONTENT_SETTING_DEFAULT);
116 bool CookieSettings::IsStorageDurable(const GURL& origin) const {
117 // TODO(dgrogan): DCHECK somewhere that secondary doesn't get set for DURABLE.
118 // TODO(dgrogan): Should "resource_identifier" be something other than
119 // std::string()?
120 ContentSetting setting = host_content_settings_map_->GetContentSetting(
121 origin /*primary*/, origin /*secondary*/,
122 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE,
123 std::string() /*resource_identifier*/);
124 return setting == CONTENT_SETTING_ALLOW;
127 void CookieSettings::ShutdownOnUIThread() {
128 DCHECK(thread_checker_.CalledOnValidThread());
129 pref_change_registrar_.RemoveAll();
132 ContentSetting CookieSettings::GetCookieSetting(const GURL& url,
133 const GURL& first_party_url,
134 bool setting_cookie,
135 SettingSource* source) const {
136 // Auto-allow in extensions or for WebUI embedded in a secure origin.
137 if (url.SchemeIsCryptographic() && first_party_url.SchemeIs(kChromeUIScheme))
138 return CONTENT_SETTING_ALLOW;
140 #if defined(ENABLE_EXTENSIONS)
141 if (url.SchemeIs(kExtensionScheme) &&
142 first_party_url.SchemeIs(kExtensionScheme)) {
143 return CONTENT_SETTING_ALLOW;
145 #endif
147 // First get any host-specific settings.
148 SettingInfo info;
149 scoped_ptr<base::Value> value = host_content_settings_map_->GetWebsiteSetting(
150 url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, std::string(),
151 &info);
152 if (source)
153 *source = info.source;
155 // If no explicit exception has been made and third-party cookies are blocked
156 // by default, apply that rule.
157 if (info.primary_pattern.MatchesAllHosts() &&
158 info.secondary_pattern.MatchesAllHosts() &&
159 ShouldBlockThirdPartyCookies() &&
160 !first_party_url.SchemeIs(extension_scheme_)) {
161 net::StaticCookiePolicy policy(
162 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES);
163 int rv;
164 if (setting_cookie)
165 rv = policy.CanSetCookie(url, first_party_url);
166 else
167 rv = policy.CanGetCookies(url, first_party_url);
168 DCHECK_NE(net::ERR_IO_PENDING, rv);
169 if (rv != net::OK)
170 return CONTENT_SETTING_BLOCK;
173 // We should always have a value, at least from the default provider.
174 DCHECK(value.get());
175 return ValueToContentSetting(value.get());
178 CookieSettings::~CookieSettings() {
181 void CookieSettings::OnBlockThirdPartyCookiesChanged() {
182 DCHECK(thread_checker_.CalledOnValidThread());
184 base::AutoLock auto_lock(lock_);
185 block_third_party_cookies_ = pref_change_registrar_.prefs()->GetBoolean(
186 prefs::kBlockThirdPartyCookies);
189 bool CookieSettings::ShouldBlockThirdPartyCookies() const {
190 base::AutoLock auto_lock(lock_);
191 return block_third_party_cookies_;
194 } // namespace content_settings