OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cookie_settings.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "chrome/browser/content_settings/content_settings_pattern.h" | |
9 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
10 #include "chrome/browser/prefs/pref_service.h" | |
11 #include "chrome/common/chrome_notification_types.h" | |
12 #include "chrome/common/chrome_switches.h" | |
13 #include "chrome/common/pref_names.h" | |
14 #include "content/browser/browser_thread.h" | |
15 #include "content/browser/user_metrics.h" | |
16 #include "content/common/notification_service.h" | |
17 #include "content/common/notification_source.h" | |
18 #include "googleurl/src/gurl.h" | |
19 #include "net/base/net_errors.h" | |
20 #include "net/base/static_cookie_policy.h" | |
21 | |
22 CookieSettings::CookieSettings( | |
23 HostContentSettingsMap* host_content_settings_map, | |
24 PrefService* prefs, | |
25 bool incognito) | |
26 : host_content_settings_map_(host_content_settings_map), | |
27 prefs_(prefs), | |
28 is_off_the_record_(incognito), | |
29 block_third_party_cookies_(false), | |
30 is_block_third_party_cookies_managed_(false) { | |
31 block_third_party_cookies_ = | |
32 prefs_->GetBoolean(prefs::kBlockThirdPartyCookies); | |
33 if (block_third_party_cookies_) { | |
34 UserMetrics::RecordAction( | |
35 UserMetricsAction("ThirdPartyCookieBlockingEnabled")); | |
36 } else { | |
37 UserMetrics::RecordAction( | |
38 UserMetricsAction("ThirdPartyCookieBlockingDisabled")); | |
39 } | |
40 is_block_third_party_cookies_managed_ = | |
41 prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies); | |
42 | |
43 // TODO(markusheintz): Discuss whether it is sensible to move migration code | |
markusheintz_
2011/08/29 08:47:09
I think this TODO is not needed anymore so if you
marja
2011/09/01 11:03:19
Migration code removed.
| |
44 // to PrefContentSettingsProvider. | |
45 MigrateObsoleteCookiePref(); | |
46 | |
47 pref_change_registrar_.Init(prefs_); | |
48 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); | |
49 } | |
50 | |
51 ContentSetting CookieSettings::GetDefaultSetting() const { | |
52 ContentSetting allow_or_block = | |
53 host_content_settings_map_->GetDefaultContentSetting( | |
54 CONTENT_SETTINGS_TYPE_COOKIES); | |
55 DCHECK(allow_or_block == CONTENT_SETTING_ALLOW || | |
56 allow_or_block == CONTENT_SETTING_BLOCK); | |
57 if (allow_or_block == CONTENT_SETTING_BLOCK) | |
58 return allow_or_block; | |
59 ContentSetting session_only = | |
60 host_content_settings_map_->GetDefaultContentSetting( | |
61 CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY); | |
62 DCHECK(allow_or_block == CONTENT_SETTING_ALLOW || | |
63 allow_or_block == CONTENT_SETTING_SESSION_ONLY); | |
64 return session_only; | |
65 } | |
66 | |
67 bool CookieSettings::IsCookieAllowed(const GURL& url, | |
68 const GURL& first_party_url, | |
69 bool setting_cookie) const { | |
70 ContentSetting setting = GetCookieContentSetting( | |
71 url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, setting_cookie); | |
72 DCHECK(setting == CONTENT_SETTING_ALLOW || | |
73 setting == CONTENT_SETTING_BLOCK); | |
74 return setting == CONTENT_SETTING_ALLOW; | |
75 } | |
76 | |
77 bool CookieSettings::IsCookieSessionOnly(const GURL& origin) const { | |
78 // FIXME(marja): Add another content type here | |
79 ContentSetting setting = GetCookieContentSetting( | |
80 origin, origin, CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY, true); | |
81 DCHECK(setting == CONTENT_SETTING_ALLOW || | |
82 setting == CONTENT_SETTING_SESSION_ONLY); | |
83 return (setting == CONTENT_SETTING_SESSION_ONLY); | |
84 } | |
85 | |
86 void CookieSettings::SetDefaultSetting(ContentSetting setting) { | |
87 DCHECK(setting == CONTENT_SETTING_ALLOW || | |
88 setting == CONTENT_SETTING_SESSION_ONLY || | |
89 setting == CONTENT_SETTING_BLOCK); | |
90 ContentSetting allowed = CONTENT_SETTING_BLOCK; | |
91 ContentSetting session_only = CONTENT_SETTING_ALLOW; | |
92 if (setting == CONTENT_SETTING_ALLOW || | |
93 setting == CONTENT_SETTING_SESSION_ONLY) | |
94 allowed = CONTENT_SETTING_ALLOW; | |
95 if (setting == CONTENT_SETTING_SESSION_ONLY) | |
96 session_only = CONTENT_SETTING_SESSION_ONLY; | |
97 host_content_settings_map_->SetDefaultContentSetting( | |
98 CONTENT_SETTINGS_TYPE_COOKIES, allowed); | |
99 host_content_settings_map_->SetDefaultContentSetting( | |
100 CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY, session_only); | |
101 } | |
102 | |
103 void CookieSettings::SetCookieAllowed( | |
104 const ContentSettingsPattern& primary_pattern, | |
105 const ContentSettingsPattern& secondary_pattern, | |
106 bool allowed) { | |
107 host_content_settings_map_->SetContentSetting( | |
108 primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_COOKIES, "", | |
109 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); | |
110 } | |
111 | |
112 void CookieSettings::SetCookieSessionOnly( | |
113 const ContentSettingsPattern& primary_pattern, | |
114 bool session_only) { | |
115 host_content_settings_map_->SetContentSetting( | |
116 primary_pattern, ContentSettingsPattern::Wildcard(), | |
117 CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY, "", | |
118 session_only ? CONTENT_SETTING_SESSION_ONLY : CONTENT_SETTING_ALLOW); | |
119 } | |
120 | |
121 void CookieSettings::ResetCookieAllowed( | |
122 const ContentSettingsPattern& primary_pattern, | |
123 const ContentSettingsPattern& secondary_pattern) { | |
124 host_content_settings_map_->SetContentSetting( | |
125 primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_COOKIES, "", | |
126 CONTENT_SETTING_DEFAULT); | |
127 } | |
128 | |
129 void CookieSettings::ResetCookieSessionOnly( | |
130 const ContentSettingsPattern& primary_pattern) { | |
131 host_content_settings_map_->SetContentSetting( | |
132 primary_pattern, ContentSettingsPattern::Wildcard(), | |
133 CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY, "", CONTENT_SETTING_DEFAULT); | |
134 } | |
135 | |
136 void CookieSettings::SetBlockThirdPartyCookies(bool block) { | |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
138 DCHECK(prefs_); | |
139 | |
140 // This setting may not be directly modified for OTR sessions. Instead, it | |
141 // is synced to the main profile's setting. | |
142 if (is_off_the_record_) { | |
143 NOTREACHED(); | |
144 return; | |
145 } | |
146 | |
147 // If the preference block-third-party-cookies is managed then do not allow to | |
148 // change it. | |
149 if (prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies)) { | |
150 NOTREACHED(); | |
151 return; | |
152 } | |
153 | |
154 { | |
155 base::AutoLock auto_lock(lock_); | |
156 block_third_party_cookies_ = block; | |
157 } | |
158 | |
159 prefs_->SetBoolean(prefs::kBlockThirdPartyCookies, block); | |
160 } | |
161 | |
162 void CookieSettings::Observe(int type, | |
163 const NotificationSource& source, | |
164 const NotificationDetails& details) { | |
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
166 | |
167 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
168 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); | |
169 | |
170 std::string* name = Details<std::string>(details).ptr(); | |
171 if (*name == prefs::kBlockThirdPartyCookies) { | |
172 base::AutoLock auto_lock(lock_); | |
173 block_third_party_cookies_ = prefs_->GetBoolean( | |
174 prefs::kBlockThirdPartyCookies); | |
175 is_block_third_party_cookies_managed_ = | |
176 prefs_->IsManagedPreference( | |
177 prefs::kBlockThirdPartyCookies); | |
178 } else { | |
179 NOTREACHED() << "Unexpected preference observed"; | |
180 return; | |
181 } | |
182 } else { | |
183 NOTREACHED() << "Unexpected notification"; | |
184 } | |
185 } | |
186 | |
187 void CookieSettings::ShutdownOnUIThread() { | |
188 pref_change_registrar_.RemoveAll(); | |
189 } | |
190 | |
191 ContentSetting CookieSettings::GetCookieContentSetting( | |
192 const GURL& url, | |
193 const GURL& first_party_url, | |
194 ContentSettingsType content_type, | |
195 bool setting_cookie) const { | |
196 if (HostContentSettingsMap::ShouldAllowAllContent(first_party_url)) | |
197 return CONTENT_SETTING_ALLOW; | |
198 | |
199 // First get any host-specific settings. | |
200 ContentSetting setting = | |
201 host_content_settings_map_->GetNonDefaultContentSetting( | |
202 url, first_party_url, content_type, ""); | |
203 | |
204 // Check if third-party cookie settings deny the cookie. This check makes | |
205 // sense only for the CONTENT_SETTINGS_TYPE_COOKIES, since the session only | |
206 // check is based on one url only. | |
207 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES && | |
208 setting == CONTENT_SETTING_DEFAULT && | |
209 BlockThirdPartyCookies()) { | |
210 bool strict = CommandLine::ForCurrentProcess()->HasSwitch( | |
211 switches::kBlockReadingThirdPartyCookies); | |
212 net::StaticCookiePolicy policy(strict ? | |
213 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES : | |
214 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); | |
215 int rv; | |
216 if (setting_cookie) | |
217 rv = policy.CanSetCookie(url, first_party_url); | |
218 else | |
219 rv = policy.CanGetCookies(url, first_party_url); | |
220 DCHECK_NE(net::ERR_IO_PENDING, rv); | |
221 if (rv != net::OK) | |
222 setting = CONTENT_SETTING_BLOCK; | |
223 } | |
224 | |
225 // If no other policy has changed the setting, use the default. | |
226 if (setting == CONTENT_SETTING_DEFAULT) { | |
227 setting = | |
228 host_content_settings_map_->GetDefaultContentSetting(content_type); | |
229 } | |
230 return setting; | |
231 } | |
232 | |
233 void CookieSettings::MigrateObsoleteCookiePref() { | |
234 if (prefs_->HasPrefPath(prefs::kCookieBehavior)) { | |
235 int cookie_behavior = prefs_->GetInteger(prefs::kCookieBehavior); | |
236 prefs_->ClearPref(prefs::kCookieBehavior); | |
237 if (!prefs_->HasPrefPath(prefs::kDefaultContentSettings)) { | |
238 host_content_settings_map_->SetDefaultContentSetting( | |
239 CONTENT_SETTINGS_TYPE_COOKIES, | |
240 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? | |
241 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); | |
242 } | |
243 if (!prefs_->HasPrefPath(prefs::kBlockThirdPartyCookies)) { | |
244 SetBlockThirdPartyCookies(cookie_behavior == | |
245 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); | |
246 } | |
247 } | |
248 } | |
OLD | NEW |