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 #ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "chrome/browser/browsing_data/canonical_cookie_hash.h"
15 #include "net/cookies/cookie_monster.h"
20 class CanonicalCookie
;
21 class URLRequestContextGetter
;
24 // This class fetches cookie information on behalf of a caller
26 // A client of this class need to call StartFetching from the UI thread to
27 // initiate the flow, and it'll be notified by the callback in its UI
28 // thread at some later point.
29 class BrowsingDataCookieHelper
30 : public base::RefCountedThreadSafe
<BrowsingDataCookieHelper
> {
32 using FetchCallback
= base::Callback
<void(const net::CookieList
&)>;
33 explicit BrowsingDataCookieHelper(
34 net::URLRequestContextGetter
* request_context_getter
);
36 // Starts the fetching process, which will notify its completion via
38 // This must be called only in the UI thread.
39 virtual void StartFetching(const FetchCallback
& callback
);
41 // Requests a single cookie to be deleted in the IO thread. This must be
42 // called in the UI thread.
43 virtual void DeleteCookie(const net::CanonicalCookie
& cookie
);
46 friend class base::RefCountedThreadSafe
<BrowsingDataCookieHelper
>;
47 virtual ~BrowsingDataCookieHelper();
49 net::URLRequestContextGetter
* request_context_getter() {
50 return request_context_getter_
.get();
54 // Fetch the cookies. This must be called in the IO thread.
55 void FetchCookiesOnIOThread(const FetchCallback
& callback
);
57 // Delete a single cookie. This must be called in IO thread.
58 void DeleteCookieOnIOThread(const net::CanonicalCookie
& cookie
);
60 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
62 DISALLOW_COPY_AND_ASSIGN(BrowsingDataCookieHelper
);
65 // This class is a thin wrapper around BrowsingDataCookieHelper that does not
66 // fetch its information from the persistent cookie store. It is a simple
67 // container for CanonicalCookies. Clients that use this container can add
68 // cookies that are sent to a server via the AddReadCookies method and cookies
69 // that are received from a server or set via JavaScript using the method
71 // Cookies are distinguished by the tuple cookie name (called cookie-name in
72 // RFC 6265), cookie domain (called cookie-domain in RFC 6265), cookie path
73 // (called cookie-path in RFC 6265) and host-only-flag (see RFC 6265 section
74 // 5.3). Cookies with same tuple (cookie-name, cookie-domain, cookie-path,
75 // host-only-flag) as cookie that are already stored, will replace the stored
77 class CannedBrowsingDataCookieHelper
: public BrowsingDataCookieHelper
{
79 typedef std::map
<GURL
, canonical_cookie::CookieHashSet
*> OriginCookieSetMap
;
81 explicit CannedBrowsingDataCookieHelper(
82 net::URLRequestContextGetter
* request_context
);
84 // Adds the cookies from |cookie_list|. Current cookies that have the same
85 // cookie name, cookie domain, cookie path, host-only-flag tuple as passed
86 // cookies are replaced by the passed cookies.
87 void AddReadCookies(const GURL
& frame_url
,
88 const GURL
& request_url
,
89 const net::CookieList
& cookie_list
);
91 // Adds a CanonicalCookie that is created from the passed |cookie_line|
92 // (called set-cookie-string in RFC 6225). The |cookie_line| is parsed,
93 // normalized and validated. Invalid |cookie_line|s are ignored. The logic
94 // for parsing, normalizing an validating the |cookie_line| mirrors the logic
95 // of CookieMonster's method SetCookieWithOptions. If the |cookie_line| does
96 // not include a cookie domain attribute (called domain-av in RFC 6265) or a
97 // cookie path (called path-av in RFC 6265), then the host and the
98 // default-path of the request-uri are used as domain-value and path-value
99 // for the cookie. CanonicalCookies created from a |cookie_line| with no
100 // cookie domain attribute are host only cookies.
101 // TODO(markusheintz): Remove the dublicated logic.
102 void AddChangedCookie(const GURL
& frame_url
,
103 const GURL
& request_url
,
104 const std::string
& cookie_line
,
105 const net::CookieOptions
& options
);
107 // Clears the list of canned cookies.
110 // True if no cookie are currently stored.
113 // BrowsingDataCookieHelper methods.
115 const net::CookieMonster::GetCookieListCallback
& callback
) override
;
116 void DeleteCookie(const net::CanonicalCookie
& cookie
) override
;
118 // Returns the number of stored cookies.
119 size_t GetCookieCount() const;
121 // Returns the map that contains the cookie lists for all frame urls.
122 const OriginCookieSetMap
& origin_cookie_set_map() {
123 return origin_cookie_set_map_
;
127 // Check if the cookie set contains a cookie with the same name,
128 // domain, and path as the newly created cookie. Delete the old cookie
130 bool DeleteMatchingCookie(const net::CanonicalCookie
& add_cookie
,
131 canonical_cookie::CookieHashSet
* cookie_set
);
133 ~CannedBrowsingDataCookieHelper() override
;
135 // Returns the |CookieSet| for the given |origin|.
136 canonical_cookie::CookieHashSet
* GetCookiesFor(const GURL
& origin
);
138 // Adds the |cookie| to the cookie set for the given |frame_url|.
139 void AddCookie(const GURL
& frame_url
,
140 const net::CanonicalCookie
& cookie
);
142 // Map that contains the cookie sets for all frame origins.
143 OriginCookieSetMap origin_cookie_set_map_
;
145 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataCookieHelper
);
148 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_