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// Brought to you by number 42.
6
7#ifndef NET_COOKIES_COOKIE_STORE_H_
8#define NET_COOKIES_COOKIE_STORE_H_
9
10#include <string>
11#include <vector>
12
13#include "base/basictypes.h"
14#include "base/callback.h"
15#include "base/memory/ref_counted.h"
16#include "base/time/time.h"
17#include "net/base/net_export.h"
18#include "net/cookies/canonical_cookie.h"
19#include "net/cookies/cookie_options.h"
20
21class GURL;
22
23namespace net {
24
25class CookieMonster;
26
27// An interface for storing and retrieving cookies. Implementations need to
28// be thread safe as its methods can be accessed from IO as well as UI threads.
29class NET_EXPORT CookieStore : public base::RefCountedThreadSafe<CookieStore> {
30 public:
31  // Callback definitions.
32  typedef base::Callback<void(const CookieList& cookies)> GetCookieListCallback;
33  typedef base::Callback<void(const std::string& cookie)> GetCookiesCallback;
34  typedef base::Callback<void(bool success)> SetCookiesCallback;
35  typedef base::Callback<void(int num_deleted)> DeleteCallback;
36
37  // Sets a single cookie.  Expects a cookie line, like "a=1; domain=b.com".
38  //
39  // Fails either if the cookie is invalid or if this is a non-HTTPONLY cookie
40  // and it would overwrite an existing HTTPONLY cookie.
41  // Returns true if the cookie is successfully set.
42  virtual void SetCookieWithOptionsAsync(
43      const GURL& url,
44      const std::string& cookie_line,
45      const CookieOptions& options,
46      const SetCookiesCallback& callback) = 0;
47
48  // TODO(???): what if the total size of all the cookies >4k, can we have a
49  // header that big or do we need multiple Cookie: headers?
50  // Note: Some sites, such as Facebook, occasionally use Cookie headers >4k.
51  //
52  // Simple interface, gets a cookie string "a=b; c=d" for the given URL.
53  // Use options to access httponly cookies.
54  virtual void GetCookiesWithOptionsAsync(
55      const GURL& url,
56      const CookieOptions& options,
57      const GetCookiesCallback& callback) = 0;
58
59  // Returns all matching cookies without marking them as accessed,
60  // including HTTP only cookies.
61  virtual void GetAllCookiesForURLAsync(
62      const GURL& url,
63      const GetCookieListCallback& callback) = 0;
64
65  // Deletes the passed in cookie for the specified URL.
66  virtual void DeleteCookieAsync(const GURL& url,
67                                 const std::string& cookie_name,
68                                 const base::Closure& callback) = 0;
69
70  // Deletes all of the cookies that have a creation_date greater than or equal
71  // to |delete_begin| and less than |delete_end|
72  // Returns the number of cookies that have been deleted.
73  virtual void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin,
74                                            const base::Time& delete_end,
75                                            const DeleteCallback& callback) = 0;
76
77  // Deletes all of the cookies that match the host of the given URL
78  // regardless of path and that have a creation_date greater than or
79  // equal to |delete_begin| and less then |delete_end|. This includes
80  // all http_only and secure cookies, but does not include any domain
81  // cookies that may apply to this host.
82  // Returns the number of cookies deleted.
83  virtual void DeleteAllCreatedBetweenForHostAsync(
84      const base::Time delete_begin,
85      const base::Time delete_end,
86      const GURL& url,
87      const DeleteCallback& callback) = 0;
88
89  virtual void DeleteSessionCookiesAsync(const DeleteCallback&) = 0;
90
91  // Returns the underlying CookieMonster.
92  virtual CookieMonster* GetCookieMonster() = 0;
93
94 protected:
95  friend class base::RefCountedThreadSafe<CookieStore>;
96  CookieStore();
97  virtual ~CookieStore();
98};
99
100}  // namespace net
101
102#endif  // NET_COOKIES_COOKIE_STORE_H_
103