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

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

Issue 7713034: HostContentSettingsMap refactoring. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Code review comments. Created 9 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 | Annotate | Revision Log
OLDNEW
(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 block_third_party_cookies_ =
31 prefs_->GetBoolean(prefs::kBlockThirdPartyCookies);
32 if (block_third_party_cookies_) {
33 UserMetrics::RecordAction(
34 UserMetricsAction("ThirdPartyCookieBlockingEnabled"));
35 } else {
36 UserMetrics::RecordAction(
37 UserMetricsAction("ThirdPartyCookieBlockingDisabled"));
38 }
39
40 pref_change_registrar_.Init(prefs_);
41 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this);
42 }
43
44 ContentSetting CookieSettings::GetDefaultSetting() const {
45 ContentSetting allow_or_block =
46 host_content_settings_map_->GetDefaultContentSetting(
47 CONTENT_SETTINGS_TYPE_COOKIES);
48 DCHECK(allow_or_block == CONTENT_SETTING_ALLOW ||
49 allow_or_block == CONTENT_SETTING_BLOCK);
50 if (allow_or_block == CONTENT_SETTING_BLOCK)
51 return allow_or_block;
52 ContentSetting session_only =
53 host_content_settings_map_->GetDefaultContentSetting(
54 CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY);
55 DCHECK(allow_or_block == CONTENT_SETTING_ALLOW ||
56 allow_or_block == CONTENT_SETTING_SESSION_ONLY);
57 return session_only;
58 }
59
60 bool CookieSettings::IsReadingCookieAllowed(const GURL& url,
61 const GURL& first_party_url) const {
62 ContentSetting setting = GetCookieContentSetting(
63 url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, false);
64 DCHECK(setting == CONTENT_SETTING_ALLOW ||
65 setting == CONTENT_SETTING_BLOCK);
66 return setting == CONTENT_SETTING_ALLOW;
67 }
68
69 bool CookieSettings::IsSettingCookieAllowed(const GURL& url,
70 const GURL& first_party_url) const {
71 ContentSetting setting = GetCookieContentSetting(
72 url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, true);
73 DCHECK(setting == CONTENT_SETTING_ALLOW ||
74 setting == CONTENT_SETTING_BLOCK);
75 return setting == CONTENT_SETTING_ALLOW;
76 }
77
78 bool CookieSettings::IsCookieSessionOnly(const GURL& origin) const {
79 // FIXME(marja): Add another content type here
Bernhard Bauer 2011/09/01 11:41:54 Is this comment still up to date?
marja 2011/09/01 13:34:48 Removed.
80 ContentSetting setting = GetCookieContentSetting(
Bernhard Bauer 2011/09/01 11:41:54 I think you could directly consult the HCSM here.
marja 2011/09/01 13:34:48 Hmm, not sure if I understand. Do you mean duplica
Bernhard Bauer 2011/09/01 13:58:49 No, HostContentSettingsMap has a GetContentSetting
81 origin, origin, CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY, true);
82 DCHECK(setting == CONTENT_SETTING_ALLOW ||
83 setting == CONTENT_SETTING_SESSION_ONLY);
84 return (setting == CONTENT_SETTING_SESSION_ONLY);
85 }
86
87 void CookieSettings::SetDefaultSetting(ContentSetting setting) {
88 DCHECK(setting == CONTENT_SETTING_ALLOW ||
89 setting == CONTENT_SETTING_SESSION_ONLY ||
90 setting == CONTENT_SETTING_BLOCK);
91 ContentSetting allowed = CONTENT_SETTING_BLOCK;
92 ContentSetting session_only = CONTENT_SETTING_ALLOW;
93 if (setting == CONTENT_SETTING_ALLOW ||
94 setting == CONTENT_SETTING_SESSION_ONLY)
95 allowed = CONTENT_SETTING_ALLOW;
96 if (setting == CONTENT_SETTING_SESSION_ONLY)
97 session_only = CONTENT_SETTING_SESSION_ONLY;
98 host_content_settings_map_->SetDefaultContentSetting(
99 CONTENT_SETTINGS_TYPE_COOKIES, allowed);
100 host_content_settings_map_->SetDefaultContentSetting(
101 CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY, session_only);
102 }
103
104 void CookieSettings::SetCookieSetting(
105 const ContentSettingsPattern& primary_pattern,
106 ContentSetting content_setting) {
107 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
108 content_setting == CONTENT_SETTING_SESSION_ONLY ||
109 content_setting == CONTENT_SETTING_BLOCK);
110 bool allowed =
111 content_setting == CONTENT_SETTING_ALLOW ||
112 content_setting == CONTENT_SETTING_SESSION_ONLY;
113 bool session_only = (content_setting == CONTENT_SETTING_SESSION_ONLY);
114 host_content_settings_map_->SetContentSetting(
115 primary_pattern, ContentSettingsPattern::Wildcard(),
116 CONTENT_SETTINGS_TYPE_COOKIES, "",
117 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
118 host_content_settings_map_->SetContentSetting(
119 primary_pattern, ContentSettingsPattern::Wildcard(),
120 CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY, "",
121 session_only ? CONTENT_SETTING_SESSION_ONLY : CONTENT_SETTING_ALLOW);
122 }
123
124 void CookieSettings::ResetCookieSetting(
125 const ContentSettingsPattern& primary_pattern) {
126 host_content_settings_map_->SetContentSetting(
127 primary_pattern, ContentSettingsPattern::Wildcard(),
128 CONTENT_SETTINGS_TYPE_COOKIES, "",
129 CONTENT_SETTING_DEFAULT);
130 host_content_settings_map_->SetContentSetting(
131 primary_pattern, ContentSettingsPattern::Wildcard(),
132 CONTENT_SETTINGS_TYPE_COOKIES_SESSION_ONLY, "",
133 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 } else {
176 NOTREACHED() << "Unexpected preference observed";
177 return;
178 }
179 } else {
180 NOTREACHED() << "Unexpected notification";
181 }
182 }
183
184 void CookieSettings::Shutdown() {
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
186 pref_change_registrar_.RemoveAll();
187 }
188
189 ContentSetting CookieSettings::GetCookieContentSetting(
190 const GURL& url,
191 const GURL& first_party_url,
192 ContentSettingsType content_type,
193 bool setting_cookie) const {
194 if (HostContentSettingsMap::ShouldAllowAllContent(
195 first_party_url, CONTENT_SETTINGS_TYPE_COOKIES))
196 return CONTENT_SETTING_ALLOW;
197
198 // First get any host-specific settings.
199 ContentSetting setting =
200 host_content_settings_map_->GetNonDefaultContentSetting(
201 url, first_party_url, content_type, "");
202
203 // Check if third-party cookie settings deny the cookie. This check makes
204 // sense only for the CONTENT_SETTINGS_TYPE_COOKIES, since the session only
205 // check is based on one url only.
206 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES &&
207 setting == CONTENT_SETTING_DEFAULT &&
208 BlockThirdPartyCookies()) {
209 bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
210 switches::kBlockReadingThirdPartyCookies);
211 net::StaticCookiePolicy policy(strict ?
212 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
213 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
214 int rv;
215 if (setting_cookie)
216 rv = policy.CanSetCookie(url, first_party_url);
217 else
218 rv = policy.CanGetCookies(url, first_party_url);
219 DCHECK_NE(net::ERR_IO_PENDING, rv);
220 if (rv != net::OK)
221 setting = CONTENT_SETTING_BLOCK;
222 }
223
224 // If no other policy has changed the setting, use the default.
225 if (setting == CONTENT_SETTING_DEFAULT) {
226 setting =
227 host_content_settings_map_->GetDefaultContentSetting(content_type);
228 }
229 return setting;
230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698