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

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

Issue 8383004: Adding CookieSettings for storing cookie content settings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/content_settings/host_content_settings_map.h" 5 #include "chrome/browser/content_settings/host_content_settings_map.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 19 matching lines...) Expand all
30 #include "content/browser/user_metrics.h" 30 #include "content/browser/user_metrics.h"
31 #include "content/public/browser/notification_service.h" 31 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/notification_source.h" 32 #include "content/public/browser/notification_source.h"
33 #include "content/public/common/content_switches.h" 33 #include "content/public/common/content_switches.h"
34 #include "googleurl/src/gurl.h" 34 #include "googleurl/src/gurl.h"
35 #include "net/base/net_errors.h" 35 #include "net/base/net_errors.h"
36 #include "net/base/static_cookie_policy.h" 36 #include "net/base/static_cookie_policy.h"
37 37
38 namespace { 38 namespace {
39 39
40 // Returns true if we should allow all content types for this URL. This is
41 // true for various internal objects like chrome:// URLs, so UI and other
42 // things users think of as "not webpages" don't break.
43 bool ShouldAllowAllContent(const GURL& url, ContentSettingsType content_type) {
44 if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
45 return false;
46 return url.SchemeIs(chrome::kChromeDevToolsScheme) ||
47 url.SchemeIs(chrome::kChromeInternalScheme) ||
48 url.SchemeIs(chrome::kChromeUIScheme) ||
49 url.SchemeIs(chrome::kExtensionScheme);
50 }
51
52 typedef std::vector<content_settings::Rule> Rules; 40 typedef std::vector<content_settings::Rule> Rules;
53 41
54 typedef std::pair<std::string, std::string> StringPair; 42 typedef std::pair<std::string, std::string> StringPair;
55 43
56 const char* kProviderNames[] = { 44 const char* kProviderNames[] = {
57 "policy", 45 "policy",
58 "extension", 46 "extension",
59 "preference", 47 "preference",
60 "default" 48 "default"
61 }; 49 };
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 ContentSettings HostContentSettingsMap::GetDefaultContentSettings() const { 182 ContentSettings HostContentSettingsMap::GetDefaultContentSettings() const {
195 ContentSettings output(CONTENT_SETTING_DEFAULT); 183 ContentSettings output(CONTENT_SETTING_DEFAULT);
196 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { 184 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
197 if (!ContentTypeHasCompoundValue(ContentSettingsType(i))) 185 if (!ContentTypeHasCompoundValue(ContentSettingsType(i)))
198 output.settings[i] = GetDefaultContentSetting(ContentSettingsType(i), 186 output.settings[i] = GetDefaultContentSetting(ContentSettingsType(i),
199 NULL); 187 NULL);
200 } 188 }
201 return output; 189 return output;
202 } 190 }
203 191
204 ContentSetting HostContentSettingsMap::GetCookieContentSetting(
205 const GURL& url,
206 const GURL& first_party_url,
207 bool setting_cookie) const {
208 if (ShouldAllowAllContent(first_party_url, CONTENT_SETTINGS_TYPE_COOKIES))
209 return CONTENT_SETTING_ALLOW;
210
211 // First get any host-specific settings.
212 scoped_ptr<base::Value> value;
213 for (ConstProviderIterator provider = content_settings_providers_.begin();
214 provider != content_settings_providers_.end();
215 ++provider) {
216 if (provider->first == DEFAULT_PROVIDER)
217 continue;
218
219 value.reset(content_settings::GetContentSettingValueAndPatterns(
220 provider->second, url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES,
221 std::string(), is_off_the_record_, NULL, NULL));
222 if (value.get())
223 break;
224 }
225
226 // If no explicit exception has been made and third-party cookies are blocked
227 // by default, apply that rule.
228 if (!value.get() && BlockThirdPartyCookies()) {
229 bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
230 switches::kBlockReadingThirdPartyCookies);
231 net::StaticCookiePolicy policy(strict ?
232 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
233 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
234 int rv;
235 if (setting_cookie)
236 rv = policy.CanSetCookie(url, first_party_url);
237 else
238 rv = policy.CanGetCookies(url, first_party_url);
239 DCHECK_NE(net::ERR_IO_PENDING, rv);
240 if (rv != net::OK)
241 return CONTENT_SETTING_BLOCK;
242 }
243
244 // If no other policy has changed the setting, use the default.
245 if (value.get())
246 return content_settings::ValueToContentSetting(value.get());
247
248 return GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, NULL);
249 }
250
251 ContentSetting HostContentSettingsMap::GetContentSetting( 192 ContentSetting HostContentSettingsMap::GetContentSetting(
252 const GURL& primary_url, 193 const GURL& primary_url,
253 const GURL& secondary_url, 194 const GURL& secondary_url,
254 ContentSettingsType content_type, 195 ContentSettingsType content_type,
255 const std::string& resource_identifier) const { 196 const std::string& resource_identifier) const {
256 scoped_ptr<base::Value> value(GetContentSettingValue( 197 scoped_ptr<base::Value> value(GetContentSettingValue(
257 primary_url, secondary_url, content_type, resource_identifier, 198 primary_url, secondary_url, content_type, resource_identifier,
258 NULL, NULL)); 199 NULL, NULL));
259 return content_settings::ValueToContentSetting(value.get()); 200 return content_settings::ValueToContentSetting(value.get());
260 } 201 }
261 202
262 base::Value* HostContentSettingsMap::GetContentSettingValue( 203 base::Value* HostContentSettingsMap::GetContentSettingValue(
263 const GURL& primary_url, 204 const GURL& primary_url,
264 const GURL& secondary_url, 205 const GURL& secondary_url,
265 ContentSettingsType content_type, 206 ContentSettingsType content_type,
266 const std::string& resource_identifier, 207 const std::string& resource_identifier,
267 ContentSettingsPattern* primary_pattern, 208 ContentSettingsPattern* primary_pattern,
268 ContentSettingsPattern* secondary_pattern) const { 209 ContentSettingsPattern* secondary_pattern) const {
269 DCHECK_NE(CONTENT_SETTINGS_TYPE_COOKIES, content_type);
270 DCHECK(content_settings::SupportsResourceIdentifier(content_type) || 210 DCHECK(content_settings::SupportsResourceIdentifier(content_type) ||
271 resource_identifier.empty()); 211 resource_identifier.empty());
272 212
273 // Check if the scheme of the requesting url is whitelisted. 213 // Check if the scheme of the requesting url is whitelisted.
274 if (ShouldAllowAllContent(secondary_url, content_type)) 214 if (ShouldAllowAllContent(secondary_url, content_type))
275 return Value::CreateIntegerValue(CONTENT_SETTING_ALLOW); 215 return Value::CreateIntegerValue(CONTENT_SETTING_ALLOW);
276 216
277 // The list of |content_settings_providers_| is ordered according to their 217 // The list of |content_settings_providers_| is ordered according to their
278 // precedence. 218 // precedence.
279 for (ConstProviderIterator provider = content_settings_providers_.begin(); 219 for (ConstProviderIterator provider = content_settings_providers_.begin();
280 provider != content_settings_providers_.end(); 220 provider != content_settings_providers_.end();
281 ++provider) { 221 ++provider) {
282 base::Value* value = content_settings::GetContentSettingValueAndPatterns( 222 base::Value* value = content_settings::GetContentSettingValueAndPatterns(
283 provider->second, primary_url, secondary_url, content_type, 223 provider->second, primary_url, secondary_url, content_type,
284 resource_identifier, is_off_the_record_, 224 resource_identifier, is_off_the_record_,
285 primary_pattern, secondary_pattern); 225 primary_pattern, secondary_pattern);
286 if (value) 226 if (value)
287 return value; 227 return value;
288 } 228 }
289 return NULL; 229 return NULL;
290 } 230 }
291 231
292 ContentSettings HostContentSettingsMap::GetContentSettings( 232 ContentSettings HostContentSettingsMap::GetContentSettings(
293 const GURL& primary_url, 233 const GURL& primary_url) const {
294 const GURL& secondary_url) const {
295 ContentSettings output; 234 ContentSettings output;
296 // If we require a resource identifier, set the content settings to default, 235 // If we require a resource identifier, set the content settings to default,
297 // otherwise make the defaults explicit. Values for content type 236 // otherwise make the defaults explicit. Values for content type
298 // CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE can't be mapped to the type 237 // CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE can't be mapped to the type
299 // |ContentSetting|. So we ignore them here. 238 // |ContentSetting|. So we ignore them here.
300 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { 239 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) {
301 ContentSettingsType type = ContentSettingsType(j); 240 ContentSettingsType type = ContentSettingsType(j);
302 if (type == CONTENT_SETTINGS_TYPE_COOKIES) { 241 if (!ContentTypeHasCompoundValue(type)) {
303 output.settings[j] = GetCookieContentSetting(
304 primary_url, secondary_url, false);
305 } else if (!ContentTypeHasCompoundValue(type)) {
306 output.settings[j] = GetContentSetting( 242 output.settings[j] = GetContentSetting(
307 primary_url, 243 primary_url, primary_url, ContentSettingsType(j), std::string());
308 secondary_url,
309 ContentSettingsType(j),
310 std::string());
311 } 244 }
312 } 245 }
313 return output; 246 return output;
314 } 247 }
315 248
316 void HostContentSettingsMap::GetSettingsForOneType( 249 void HostContentSettingsMap::GetSettingsForOneType(
317 ContentSettingsType content_type, 250 ContentSettingsType content_type,
318 const std::string& resource_identifier, 251 const std::string& resource_identifier,
319 SettingsForOneType* settings) const { 252 SettingsForOneType* settings) const {
320 DCHECK(content_settings::SupportsResourceIdentifier(content_type) || 253 DCHECK(content_settings::SupportsResourceIdentifier(content_type) ||
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 ContentSettingsPattern wildcard = ContentSettingsPattern::Wildcard(); 494 ContentSettingsPattern wildcard = ContentSettingsPattern::Wildcard();
562 while (rule_iterator->HasNext()) { 495 while (rule_iterator->HasNext()) {
563 const content_settings::Rule& rule = rule_iterator->Next(); 496 const content_settings::Rule& rule = rule_iterator->Next();
564 settings->push_back(PatternSettingSourceTuple( 497 settings->push_back(PatternSettingSourceTuple(
565 rule.primary_pattern, rule.secondary_pattern, 498 rule.primary_pattern, rule.secondary_pattern,
566 content_settings::ValueToContentSetting(rule.value.get()), 499 content_settings::ValueToContentSetting(rule.value.get()),
567 kProviderNames[provider_type], 500 kProviderNames[provider_type],
568 incognito)); 501 incognito));
569 } 502 }
570 } 503 }
504
505 bool HostContentSettingsMap::ShouldAllowAllContent(
506 const GURL& url,
507 ContentSettingsType content_type) {
508 if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
509 return false;
510 return url.SchemeIs(chrome::kChromeDevToolsScheme) ||
511 url.SchemeIs(chrome::kChromeInternalScheme) ||
512 url.SchemeIs(chrome::kChromeUIScheme) ||
513 url.SchemeIs(chrome::kExtensionScheme);
514 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698