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

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

Issue 8383004: Adding CookieSettings for storing cookie content settings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Code review. Created 9 years, 1 month 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 "chrome/browser/content_settings/cookie_settings.h"
6
7 #include "base/command_line.h"
8 #include "chrome/browser/content_settings/content_settings_utils.h"
9 #include "chrome/browser/content_settings/host_content_settings_map.h"
10 #include "chrome/browser/prefs/pref_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_dependency_manager.h"
13 #include "chrome/browser/profiles/profile_keyed_service.h"
14 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/content_settings_pattern.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/browser/browser_thread.h"
19 #include "content/browser/user_metrics.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h"
22 #include "googleurl/src/gurl.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/static_cookie_policy.h"
25
26 namespace {
27
28 bool IsAllowed(ContentSetting setting) {
29 DCHECK(setting == CONTENT_SETTING_ALLOW ||
30 setting == CONTENT_SETTING_SESSION_ONLY ||
31 setting == CONTENT_SETTING_BLOCK);
32 return (setting == CONTENT_SETTING_ALLOW ||
33 setting == CONTENT_SETTING_SESSION_ONLY);
34 }
35
36 } // namespace
37
38 // |ProfileKeyedFactory| is the owner of the |ProfileKeyedService|s. This
39 // wrapper class allows others to hold shared pointers to CookieSettings.
40 class CookieSettingsWrapper : public ProfileKeyedService {
41 public:
42 explicit CookieSettingsWrapper(scoped_refptr<CookieSettings> cookie_settings)
43 : cookie_settings_(cookie_settings) {}
44 virtual ~CookieSettingsWrapper() {}
45
46 CookieSettings* cookie_settings() { return cookie_settings_.get(); }
47
48 private:
49 // ProfileKeyedService methods:
50 virtual void Shutdown() OVERRIDE {
51 cookie_settings_->ShutdownOnUIThread();
52 }
53
54 scoped_refptr<CookieSettings> cookie_settings_;
55 };
56
57 // static
58 CookieSettings::Factory* CookieSettings::Factory::GetInstance() {
59 return Singleton<CookieSettings::Factory>::get();
60 }
61
62 CookieSettingsWrapper* CookieSettings::Factory::GetWrapperForProfile(
63 Profile* profile) {
64 return static_cast<CookieSettingsWrapper*>(
65 GetServiceForProfile(profile,true));
66 }
67
68 CookieSettings::Factory::Factory()
69 : ProfileKeyedServiceFactory(ProfileDependencyManager::GetInstance()) {
70 }
71
72 ProfileKeyedService* CookieSettings::Factory::BuildServiceInstanceFor(
73 Profile* profile) const {
74 scoped_refptr<CookieSettings> cookie_settings = new CookieSettings(
75 profile->GetHostContentSettingsMap(),
76 profile->GetPrefs());
77 return new CookieSettingsWrapper(cookie_settings);
78 }
79
80 CookieSettings::CookieSettings(
81 HostContentSettingsMap* host_content_settings_map,
82 PrefService* prefs)
83 : host_content_settings_map_(host_content_settings_map),
84 block_third_party_cookies_(
85 prefs->GetBoolean(prefs::kBlockThirdPartyCookies)) {
86 if (block_third_party_cookies_) {
87 UserMetrics::RecordAction(
88 UserMetricsAction("ThirdPartyCookieBlockingEnabled"));
89 } else {
90 UserMetrics::RecordAction(
91 UserMetricsAction("ThirdPartyCookieBlockingDisabled"));
92 }
93
94 pref_change_registrar_.Init(prefs);
95 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this);
96 }
97
98 CookieSettings::~CookieSettings() {
99 }
100
101 // static
102 CookieSettings* CookieSettings::GetForProfile(Profile* profile) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 CookieSettings* cookie_settings =
105 Factory::GetInstance()->GetWrapperForProfile(profile)->cookie_settings();
106 DCHECK(cookie_settings);
107 return cookie_settings;
108 }
109
110 ContentSetting
111 CookieSettings::GetDefaultCookieSetting(std::string* provider_id) const {
112 return host_content_settings_map_->GetDefaultContentSetting(
113 CONTENT_SETTINGS_TYPE_COOKIES, provider_id);
114 }
115
116 bool CookieSettings::IsReadingCookieAllowed(const GURL& url,
117 const GURL& first_party_url) const {
118 ContentSetting setting = GetCookieSetting(url, first_party_url, false);
119 return IsAllowed(setting);
120 }
121
122 bool CookieSettings::IsSettingCookieAllowed(const GURL& url,
123 const GURL& first_party_url) const {
124 ContentSetting setting = GetCookieSetting(url, first_party_url, true);
125 return IsAllowed(setting);
126 }
127
128 bool CookieSettings::IsCookieSessionOnly(const GURL& origin) const {
129 ContentSetting setting = GetCookieSetting(origin, origin, true);
130 DCHECK(setting == CONTENT_SETTING_ALLOW ||
131 setting == CONTENT_SETTING_SESSION_ONLY ||
132 setting == CONTENT_SETTING_BLOCK);
133 return (setting == CONTENT_SETTING_SESSION_ONLY);
134 }
135
136 void CookieSettings::GetCookieSettings(
137 HostContentSettingsMap::SettingsForOneType* settings) const {
138 return host_content_settings_map_->GetSettingsForOneType(
139 CONTENT_SETTINGS_TYPE_COOKIES, "", settings);
140 }
141
142 void CookieSettings::SetDefaultCookieSetting(ContentSetting setting) {
143 DCHECK(setting == CONTENT_SETTING_ALLOW ||
Bernhard Bauer 2011/10/26 13:24:09 Now you could also pull this out into a method |Is
marja 2011/10/27 08:48:51 Done.
144 setting == CONTENT_SETTING_SESSION_ONLY ||
145 setting == CONTENT_SETTING_BLOCK);
146 host_content_settings_map_->SetDefaultContentSetting(
147 CONTENT_SETTINGS_TYPE_COOKIES, setting);
148 }
149
150 void CookieSettings::SetCookieSetting(
151 const ContentSettingsPattern& primary_pattern,
152 const ContentSettingsPattern& secondary_pattern,
153 ContentSetting setting) {
154 DCHECK(setting == CONTENT_SETTING_ALLOW ||
155 setting == CONTENT_SETTING_SESSION_ONLY ||
156 setting == CONTENT_SETTING_BLOCK);
157 if (setting == CONTENT_SETTING_SESSION_ONLY) {
158 DCHECK(secondary_pattern == ContentSettingsPattern::Wildcard());
159 }
160 host_content_settings_map_->SetContentSetting(
161 primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_COOKIES, "",
162 setting);
163 }
164
165 void CookieSettings::ResetCookieSetting(
166 const ContentSettingsPattern& primary_pattern,
167 const ContentSettingsPattern& secondary_pattern) {
168 host_content_settings_map_->SetContentSetting(
169 primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_COOKIES, "",
170 CONTENT_SETTING_DEFAULT);
171 }
172
173 void CookieSettings::Observe(int type,
174 const content::NotificationSource& source,
175 const content::NotificationDetails& details) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
177
178 if (type == chrome::NOTIFICATION_PREF_CHANGED) {
179 PrefService* prefs = content::Source<PrefService>(source).ptr();
180 std::string* name = content::Details<std::string>(details).ptr();
181 if (*name == prefs::kBlockThirdPartyCookies) {
182 base::AutoLock auto_lock(lock_);
183 block_third_party_cookies_ = prefs->GetBoolean(
184 prefs::kBlockThirdPartyCookies);
185 } else {
186 NOTREACHED() << "Unexpected preference observed";
187 return;
188 }
189 } else {
190 NOTREACHED() << "Unexpected notification";
191 }
192 }
193
194 void CookieSettings::ShutdownOnUIThread() {
195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
196 pref_change_registrar_.RemoveAll();
197 }
198
199 ContentSetting CookieSettings::GetCookieSetting(
200 const GURL& url,
201 const GURL& first_party_url,
202 bool setting_cookie) const {
203 if (HostContentSettingsMap::ShouldAllowAllContent(
204 first_party_url,
205 CONTENT_SETTINGS_TYPE_COOKIES)) {
206 return CONTENT_SETTING_ALLOW;
207 }
208
209 ContentSettingsPattern primary_pattern;
210 ContentSettingsPattern secondary_pattern;
211 // First get any host-specific settings.
212 scoped_ptr<base::Value> value(
213 host_content_settings_map_->GetContentSettingValue(
214 url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, "",
215 &primary_pattern, &secondary_pattern));
216
217 // If no explicit exception has been made and third-party cookies are blocked
218 // by default, apply that rule.
219 if (primary_pattern == ContentSettingsPattern::Wildcard() &&
220 secondary_pattern == ContentSettingsPattern::Wildcard() &&
221 ShouldBlockThirdPartyCookies()) {
222 bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
223 switches::kBlockReadingThirdPartyCookies);
224 net::StaticCookiePolicy policy(strict ?
225 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
226 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
227 int rv;
228 if (setting_cookie)
229 rv = policy.CanSetCookie(url, first_party_url);
230 else
231 rv = policy.CanGetCookies(url, first_party_url);
232 DCHECK_NE(net::ERR_IO_PENDING, rv);
233 if (rv != net::OK)
234 return CONTENT_SETTING_BLOCK;
235 }
236
237 // We should always have a value, at least from the default provider.
238 DCHECK(value.get());
239 return content_settings::ValueToContentSetting(value.get());
240 }
241
242 // static
243 void CookieSettings::RegisterUserPrefs(PrefService* prefs) {
244 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies,
245 false,
246 PrefService::SYNCABLE_PREF);
247 }
248
249 bool CookieSettings::ShouldBlockThirdPartyCookies() const {
250 base::AutoLock auto_lock(lock_);
251 return block_third_party_cookies_;
252 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698