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/net/chrome_cookie_policy.h"
6
7 #include "base/command_line.h"
8 #include "base/string_util.h"
9 #include "chrome/browser/content_settings/host_content_settings_map.h"
10 #include "chrome/browser/ui/browser_list.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "content/browser/browser_thread.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/static_cookie_policy.h"
15
16 // ----------------------------------------------------------------------------
17
ChromeCookiePolicy(HostContentSettingsMap * map)18 ChromeCookiePolicy::ChromeCookiePolicy(HostContentSettingsMap* map)
19 : host_content_settings_map_(map),
20 strict_third_party_blocking_(
21 CommandLine::ForCurrentProcess()->HasSwitch(
22 switches::kBlockReadingThirdPartyCookies)) {}
23
~ChromeCookiePolicy()24 ChromeCookiePolicy::~ChromeCookiePolicy() {}
25
CanGetCookies(const GURL & url,const GURL & first_party) const26 int ChromeCookiePolicy::CanGetCookies(const GURL& url,
27 const GURL& first_party) const {
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
29
30 if (host_content_settings_map_->BlockThirdPartyCookies()) {
31 net::StaticCookiePolicy policy(strict_third_party_blocking_ ?
32 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
33 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
34 int rv = policy.CanGetCookies(url, first_party);
35 DCHECK_NE(net::ERR_IO_PENDING, rv);
36 if (rv != net::OK)
37 return rv;
38 }
39
40 int policy = CheckPolicy(url);
41 if (policy == net::OK_FOR_SESSION_ONLY)
42 policy = net::OK;
43 DCHECK_NE(net::ERR_IO_PENDING, policy);
44 return policy;
45 }
46
CanSetCookie(const GURL & url,const GURL & first_party,const std::string & cookie_line) const47 int ChromeCookiePolicy::CanSetCookie(const GURL& url,
48 const GURL& first_party,
49 const std::string& cookie_line) const {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
51
52 if (host_content_settings_map_->BlockThirdPartyCookies()) {
53 net::StaticCookiePolicy policy(strict_third_party_blocking_ ?
54 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
55 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
56 int rv = policy.CanSetCookie(url, first_party, cookie_line);
57 if (rv != net::OK)
58 return rv;
59 }
60
61 int policy = CheckPolicy(url);
62 DCHECK_NE(net::ERR_IO_PENDING, policy);
63 return policy;
64 }
65
CheckPolicy(const GURL & url) const66 int ChromeCookiePolicy::CheckPolicy(const GURL& url) const {
67 ContentSetting setting = host_content_settings_map_->GetContentSetting(
68 url, CONTENT_SETTINGS_TYPE_COOKIES, "");
69 if (setting == CONTENT_SETTING_BLOCK)
70 return net::ERR_ACCESS_DENIED;
71 if (setting == CONTENT_SETTING_ALLOW)
72 return net::OK;
73 if (setting == CONTENT_SETTING_SESSION_ONLY)
74 return net::OK_FOR_SESSION_ONLY;
75 NOTREACHED();
76 return net::ERR_ACCESS_DENIED;
77 }
78