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.
5 // Brought to you by number 42.
7 #ifndef NET_COOKIES_COOKIE_STORE_H_
8 #define NET_COOKIES_COOKIE_STORE_H_
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/callback_list.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/time/time.h"
18 #include "net/base/net_export.h"
19 #include "net/cookies/canonical_cookie.h"
20 #include "net/cookies/cookie_options.h"
28 // An interface for storing and retrieving cookies. Implementations need to
29 // be thread safe as its methods can be accessed from IO as well as UI threads.
30 class NET_EXPORT CookieStore
: public base::RefCountedThreadSafe
<CookieStore
> {
32 // Callback definitions.
33 typedef base::Callback
<void(const CookieList
& cookies
)> GetCookieListCallback
;
34 typedef base::Callback
<void(const std::string
& cookie
)> GetCookiesCallback
;
35 typedef base::Callback
<void(bool success
)> SetCookiesCallback
;
36 typedef base::Callback
<void(int num_deleted
)> DeleteCallback
;
37 typedef base::Callback
<void(const CanonicalCookie
& cookie
, bool removed
)>
38 CookieChangedCallback
;
39 typedef base::CallbackList
<void(const CanonicalCookie
& cookie
, bool removed
)>
40 CookieChangedCallbackList
;
41 typedef CookieChangedCallbackList::Subscription CookieChangedSubscription
;
43 // Sets a single cookie. Expects a cookie line, like "a=1; domain=b.com".
45 // Fails either if the cookie is invalid or if this is a non-HTTPONLY cookie
46 // and it would overwrite an existing HTTPONLY cookie.
47 // Returns true if the cookie is successfully set.
48 virtual void SetCookieWithOptionsAsync(
50 const std::string
& cookie_line
,
51 const CookieOptions
& options
,
52 const SetCookiesCallback
& callback
) = 0;
54 // TODO(???): what if the total size of all the cookies >4k, can we have a
55 // header that big or do we need multiple Cookie: headers?
56 // Note: Some sites, such as Facebook, occasionally use Cookie headers >4k.
58 // Simple interface, gets a cookie string "a=b; c=d" for the given URL.
59 // Use options to access httponly cookies.
60 virtual void GetCookiesWithOptionsAsync(
62 const CookieOptions
& options
,
63 const GetCookiesCallback
& callback
) = 0;
65 // Returns all matching cookies without marking them as accessed,
66 // including HTTP only cookies.
67 virtual void GetAllCookiesForURLAsync(
69 const GetCookieListCallback
& callback
) = 0;
71 // Deletes the passed in cookie for the specified URL.
72 virtual void DeleteCookieAsync(const GURL
& url
,
73 const std::string
& cookie_name
,
74 const base::Closure
& callback
) = 0;
76 // Deletes all of the cookies that have a creation_date greater than or equal
77 // to |delete_begin| and less than |delete_end|
78 // Returns the number of cookies that have been deleted.
79 virtual void DeleteAllCreatedBetweenAsync(const base::Time
& delete_begin
,
80 const base::Time
& delete_end
,
81 const DeleteCallback
& callback
) = 0;
83 // Deletes all of the cookies that match the host of the given URL
84 // regardless of path and that have a creation_date greater than or
85 // equal to |delete_begin| and less then |delete_end|. This includes
86 // all http_only and secure cookies, but does not include any domain
87 // cookies that may apply to this host.
88 // Returns the number of cookies deleted.
89 virtual void DeleteAllCreatedBetweenForHostAsync(
90 const base::Time delete_begin
,
91 const base::Time delete_end
,
93 const DeleteCallback
& callback
) = 0;
95 virtual void DeleteSessionCookiesAsync(const DeleteCallback
&) = 0;
97 // Returns the underlying CookieMonster.
98 virtual CookieMonster
* GetCookieMonster() = 0;
100 // Add a callback to be notified when the set of cookies named |name| that
101 // would be sent for a request to |url| changes. The returned handle is
102 // guaranteed not to hold a hard reference to the CookieStore object.
104 // |callback| will be called when a cookie is added or removed. |callback| is
105 // passed the respective |cookie| which was added to or removed from the
106 // cookies and a boolean indicating if the cookies was removed or not.
108 // Note that |callback| is called twice when a cookie is updated: once for
109 // the removal of the existing cookie and once for the adding the new cookie.
111 // Note that this method consumes memory and CPU per (url, name) pair ever
112 // registered that are still consumed even after all subscriptions for that
113 // (url, name) pair are removed. If this method ever needs to support an
114 // unbounded amount of such pairs, this contract needs to change and
115 // implementors need to be improved to not behave this way.
116 virtual scoped_ptr
<CookieChangedSubscription
> AddCallbackForCookie(
118 const std::string
& name
,
119 const CookieChangedCallback
& callback
) = 0;
122 friend class base::RefCountedThreadSafe
<CookieStore
>;
124 virtual ~CookieStore();
129 #endif // NET_COOKIES_COOKIE_STORE_H_