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#ifndef NET_BASE_STATIC_COOKIE_POLICY_H_
6#define NET_BASE_STATIC_COOKIE_POLICY_H_
7#pragma once
8
9#include <string>
10
11#include "base/basictypes.h"
12#include "net/base/cookie_policy.h"
13
14class GURL;
15
16namespace net {
17
18// The StaticCookiePolicy class implements a static cookie policy that supports
19// three modes: allow all, deny all, or block third-party cookies.
20//
21// NOTE: This CookiePolicy implementation always completes synchronously.  The
22// callback parameter will be ignored if specified.  Just pass NULL.
23//
24class StaticCookiePolicy : public CookiePolicy {
25 public:
26  // Do not change the order of these types as they are persisted in
27  // preferences.
28  enum Type {
29    // Do not perform any cookie blocking.
30    ALLOW_ALL_COOKIES = 0,
31    // Prevent only third-party cookies from being set.
32    BLOCK_SETTING_THIRD_PARTY_COOKIES,
33    // Block all cookies (third-party or not) from begin set or read.
34    BLOCK_ALL_COOKIES,
35    // Prevent only third-party cookies from being set or read.
36    BLOCK_ALL_THIRD_PARTY_COOKIES
37  };
38
39  StaticCookiePolicy()
40      : type_(StaticCookiePolicy::ALLOW_ALL_COOKIES) {
41  }
42
43  explicit StaticCookiePolicy(Type type)
44      : type_(type) {
45  }
46
47  // Sets the current policy to enforce. This should be called when the user's
48  // preferences change.
49  void set_type(Type type) { type_ = type; }
50  Type type() const { return type_; }
51
52  // CookiePolicy methods:
53
54  // Consults the user's third-party cookie blocking preferences to determine
55  // whether the URL's cookies can be read.
56  virtual int CanGetCookies(const GURL& url,
57                            const GURL& first_party_for_cookies) const;
58
59  // Consults the user's third-party cookie blocking preferences to determine
60  // whether the URL's cookies can be set.
61  virtual int CanSetCookie(const GURL& url,
62                           const GURL& first_party_for_cookies,
63                           const std::string& cookie_line) const;
64
65 private:
66  Type type_;
67
68  DISALLOW_COPY_AND_ASSIGN(StaticCookiePolicy);
69};
70
71}  // namespace net
72
73#endif  // NET_BASE_STATIC_COOKIE_POLICY_H_
74