chrome_cookie_policy.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file. 4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/net/chrome_cookie_policy.h" 6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "base/command_line.h" 8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/string_util.h" 921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen#include "chrome/browser/content_settings/host_content_settings_map.h" 10ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/ui/browser_list.h" 1172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "chrome/common/chrome_switches.h" 12dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/browser_thread.h" 13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "net/base/net_errors.h" 14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "net/base/static_cookie_policy.h" 15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// ---------------------------------------------------------------------------- 17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 18c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochChromeCookiePolicy::ChromeCookiePolicy(HostContentSettingsMap* map) 19ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen : host_content_settings_map_(map), 20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen strict_third_party_blocking_( 21ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen CommandLine::ForCurrentProcess()->HasSwitch( 22ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen switches::kBlockReadingThirdPartyCookies)) {} 23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 24ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenChromeCookiePolicy::~ChromeCookiePolicy() {} 25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochint ChromeCookiePolicy::CanGetCookies(const GURL& url, 27ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const GURL& first_party) const { 28731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (host_content_settings_map_->BlockThirdPartyCookies()) { 3172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::StaticCookiePolicy policy(strict_third_party_blocking_ ? 3272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES : 3372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); 34ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int rv = policy.CanGetCookies(url, first_party); 35ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen DCHECK_NE(net::ERR_IO_PENDING, rv); 36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (rv != net::OK) 37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return rv; 38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch int policy = CheckPolicy(url); 41731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick if (policy == net::OK_FOR_SESSION_ONLY) 42731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick policy = net::OK; 43ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen DCHECK_NE(net::ERR_IO_PENDING, policy); 44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return policy; 45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochint ChromeCookiePolicy::CanSetCookie(const GURL& url, 48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const GURL& first_party, 49ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen const std::string& cookie_line) const { 50731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (host_content_settings_map_->BlockThirdPartyCookies()) { 5372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::StaticCookiePolicy policy(strict_third_party_blocking_ ? 5472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES : 5572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); 56ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen int rv = policy.CanSetCookie(url, first_party, cookie_line); 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (rv != net::OK) 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return rv; 59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch int policy = CheckPolicy(url); 62ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen DCHECK_NE(net::ERR_IO_PENDING, policy); 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return policy; 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochint ChromeCookiePolicy::CheckPolicy(const GURL& url) const { 67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch ContentSetting setting = host_content_settings_map_->GetContentSetting( 683345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick url, CONTENT_SETTINGS_TYPE_COOKIES, ""); 69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (setting == CONTENT_SETTING_BLOCK) 70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return net::ERR_ACCESS_DENIED; 71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (setting == CONTENT_SETTING_ALLOW) 72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return net::OK; 73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch if (setting == CONTENT_SETTING_SESSION_ONLY) 74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch return net::OK_FOR_SESSION_ONLY; 75ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen NOTREACHED(); 76ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen return net::ERR_ACCESS_DENIED; 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch} 78