Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_cookie_helper.h
blobb17639930c3a41111423674a58262ef182a21706
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_
8 #include <map>
9 #include <string>
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"
17 class GURL;
19 namespace net {
20 class CanonicalCookie;
21 class URLRequestContextGetter;
24 // This class fetches cookie information on behalf of a caller
25 // on the UI thread.
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> {
31 public:
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
37 // callback.
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);
45 protected:
46 friend class base::RefCountedThreadSafe<BrowsingDataCookieHelper>;
47 virtual ~BrowsingDataCookieHelper();
49 net::URLRequestContextGetter* request_context_getter() {
50 return request_context_getter_.get();
53 private:
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
70 // AddChangedCookie.
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
76 // cookies.
77 class CannedBrowsingDataCookieHelper : public BrowsingDataCookieHelper {
78 public:
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.
108 void Reset();
110 // True if no cookie are currently stored.
111 bool empty() const;
113 // BrowsingDataCookieHelper methods.
114 void StartFetching(
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_;
126 private:
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
129 // if does.
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_