1// Copyright (c) 2012 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 "net/base/static_cookie_policy.h"
6
7#include "base/logging.h"
8#include "net/base/net_errors.h"
9#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
10#include "url/gurl.h"
11
12namespace net {
13
14int StaticCookiePolicy::CanGetCookies(
15    const GURL& url,
16    const GURL& first_party_for_cookies) const {
17  switch (type_) {
18    case StaticCookiePolicy::ALLOW_ALL_COOKIES:
19    case StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES:
20      return OK;
21    case StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES:
22      if (first_party_for_cookies.is_empty())
23        return OK;  // Empty first-party URL indicates a first-party request.
24      return registry_controlled_domains::SameDomainOrHost(
25          url,
26          first_party_for_cookies,
27          registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES) ?
28              OK : ERR_ACCESS_DENIED;
29    case StaticCookiePolicy::BLOCK_ALL_COOKIES:
30      return ERR_ACCESS_DENIED;
31    default:
32      NOTREACHED();
33      return ERR_ACCESS_DENIED;
34  }
35}
36
37int StaticCookiePolicy::CanSetCookie(
38    const GURL& url,
39    const GURL& first_party_for_cookies) const {
40  switch (type_) {
41    case StaticCookiePolicy::ALLOW_ALL_COOKIES:
42      return OK;
43    case StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES:
44    case StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES:
45      if (first_party_for_cookies.is_empty())
46        return OK;  // Empty first-party URL indicates a first-party request.
47      return registry_controlled_domains::SameDomainOrHost(
48          url,
49          first_party_for_cookies,
50          registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES) ?
51              OK : ERR_ACCESS_DENIED;
52    case StaticCookiePolicy::BLOCK_ALL_COOKIES:
53      return ERR_ACCESS_DENIED;
54    default:
55      NOTREACHED();
56      return ERR_ACCESS_DENIED;
57  }
58}
59
60}  // namespace net
61