Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_cookie_helper.h
bloba703ea5218c63585151804b4e8f781e2c72c5c1c
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 explicit BrowsingDataCookieHelper(
33 net::URLRequestContextGetter* request_context_getter);
35 // Starts the fetching process, which will notify its completion via
36 // callback.
37 // This must be called only in the UI thread.
38 virtual void StartFetching(
39 const base::Callback<void(const net::CookieList& cookies)>& 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();
57 // Callback function for get cookie. This must be called in the IO thread.
58 void OnFetchComplete(const net::CookieList& cookies);
60 // Notifies the completion callback. This must be called in the UI thread.
61 void NotifyInUIThread(const net::CookieList& cookies);
63 // Delete a single cookie. This must be called in IO thread.
64 void DeleteCookieOnIOThread(const net::CanonicalCookie& cookie);
66 // Indicates whether or not we're currently fetching information:
67 // it's true when StartFetching() is called in the UI thread, and it's reset
68 // after we notify the callback in the UI thread.
69 // This member is only mutated on the UI thread.
70 bool is_fetching_;
72 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
74 // This member is only mutated on the UI thread.
75 base::Callback<void(const net::CookieList& cookies)> completion_callback_;
77 DISALLOW_COPY_AND_ASSIGN(BrowsingDataCookieHelper);
80 // This class is a thin wrapper around BrowsingDataCookieHelper that does not
81 // fetch its information from the persistent cookie store. It is a simple
82 // container for CanonicalCookies. Clients that use this container can add
83 // cookies that are sent to a server via the AddReadCookies method and cookies
84 // that are received from a server or set via JavaScript using the method
85 // AddChangedCookie.
86 // Cookies are distinguished by the tuple cookie name (called cookie-name in
87 // RFC 6265), cookie domain (called cookie-domain in RFC 6265), cookie path
88 // (called cookie-path in RFC 6265) and host-only-flag (see RFC 6265 section
89 // 5.3). Cookies with same tuple (cookie-name, cookie-domain, cookie-path,
90 // host-only-flag) as cookie that are already stored, will replace the stored
91 // cookies.
92 class CannedBrowsingDataCookieHelper : public BrowsingDataCookieHelper {
93 public:
94 typedef std::map<GURL, canonical_cookie::CookieHashSet*> OriginCookieSetMap;
96 explicit CannedBrowsingDataCookieHelper(
97 net::URLRequestContextGetter* request_context);
99 // Adds the cookies from |cookie_list|. Current cookies that have the same
100 // cookie name, cookie domain, cookie path, host-only-flag tuple as passed
101 // cookies are replaced by the passed cookies.
102 void AddReadCookies(const GURL& frame_url,
103 const GURL& request_url,
104 const net::CookieList& cookie_list);
106 // Adds a CanonicalCookie that is created from the passed |cookie_line|
107 // (called set-cookie-string in RFC 6225). The |cookie_line| is parsed,
108 // normalized and validated. Invalid |cookie_line|s are ignored. The logic
109 // for parsing, normalizing an validating the |cookie_line| mirrors the logic
110 // of CookieMonster's method SetCookieWithOptions. If the |cookie_line| does
111 // not include a cookie domain attribute (called domain-av in RFC 6265) or a
112 // cookie path (called path-av in RFC 6265), then the host and the
113 // default-path of the request-uri are used as domain-value and path-value
114 // for the cookie. CanonicalCookies created from a |cookie_line| with no
115 // cookie domain attribute are host only cookies.
116 // TODO(markusheintz): Remove the dublicated logic.
117 void AddChangedCookie(const GURL& frame_url,
118 const GURL& request_url,
119 const std::string& cookie_line,
120 const net::CookieOptions& options);
122 // Clears the list of canned cookies.
123 void Reset();
125 // True if no cookie are currently stored.
126 bool empty() const;
128 // BrowsingDataCookieHelper methods.
129 void StartFetching(
130 const net::CookieMonster::GetCookieListCallback& callback) override;
131 void DeleteCookie(const net::CanonicalCookie& cookie) override;
133 // Returns the number of stored cookies.
134 size_t GetCookieCount() const;
136 // Returns the map that contains the cookie lists for all frame urls.
137 const OriginCookieSetMap& origin_cookie_set_map() {
138 return origin_cookie_set_map_;
141 private:
142 // Check if the cookie set contains a cookie with the same name,
143 // domain, and path as the newly created cookie. Delete the old cookie
144 // if does.
145 bool DeleteMatchingCookie(const net::CanonicalCookie& add_cookie,
146 canonical_cookie::CookieHashSet* cookie_set);
148 ~CannedBrowsingDataCookieHelper() override;
150 // Returns the |CookieSet| for the given |origin|.
151 canonical_cookie::CookieHashSet* GetCookiesFor(const GURL& origin);
153 // Adds the |cookie| to the cookie set for the given |frame_url|.
154 void AddCookie(const GURL& frame_url,
155 const net::CanonicalCookie& cookie);
157 // Map that contains the cookie sets for all frame origins.
158 OriginCookieSetMap origin_cookie_set_map_;
160 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataCookieHelper);
163 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_