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 #ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_ 6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/callback.h" 13 #include "base/memory/ref_counted.h" 14 #include "net/cookies/cookie_monster.h" 15 16 class GURL; 17 18 namespace net { 19 class CanonicalCookie; 20 class URLRequestContextGetter; 21 } 22 23 // This class fetches cookie information on behalf of a caller 24 // on the UI thread. 25 // A client of this class need to call StartFetching from the UI thread to 26 // initiate the flow, and it'll be notified by the callback in its UI 27 // thread at some later point. 28 class BrowsingDataCookieHelper 29 : public base::RefCountedThreadSafe<BrowsingDataCookieHelper> { 30 public: 31 explicit BrowsingDataCookieHelper( 32 net::URLRequestContextGetter* request_context_getter); 33 34 // Starts the fetching process, which will notify its completion via 35 // callback. 36 // This must be called only in the UI thread. 37 virtual void StartFetching( 38 const base::Callback<void(const net::CookieList& cookies)>& callback); 39 40 // Requests a single cookie to be deleted in the IO thread. This must be 41 // called in the UI thread. 42 virtual void DeleteCookie(const net::CanonicalCookie& cookie); 43 44 protected: 45 friend class base::RefCountedThreadSafe<BrowsingDataCookieHelper>; 46 virtual ~BrowsingDataCookieHelper(); 47 48 net::URLRequestContextGetter* request_context_getter() { 49 return request_context_getter_.get(); 50 } 51 52 private: 53 // Fetch the cookies. This must be called in the IO thread. 54 void FetchCookiesOnIOThread(); 55 56 // Callback function for get cookie. This must be called in the IO thread. 57 void OnFetchComplete(const net::CookieList& cookies); 58 59 // Notifies the completion callback. This must be called in the UI thread. 60 void NotifyInUIThread(const net::CookieList& cookies); 61 62 // Delete a single cookie. This must be called in IO thread. 63 void DeleteCookieOnIOThread(const net::CanonicalCookie& cookie); 64 65 // Indicates whether or not we're currently fetching information: 66 // it's true when StartFetching() is called in the UI thread, and it's reset 67 // after we notify the callback in the UI thread. 68 // This only mutates on the UI thread. 69 bool is_fetching_; 70 71 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 72 73 // This only mutates on the UI thread. 74 base::Callback<void(const net::CookieList& cookies)> completion_callback_; 75 76 DISALLOW_COPY_AND_ASSIGN(BrowsingDataCookieHelper); 77 }; 78 79 // This class is a thin wrapper around BrowsingDataCookieHelper that does not 80 // fetch its information from the persistent cookie store. It is a simple 81 // container for CanonicalCookies. Clients that use this container can add 82 // cookies that are sent to a server via the AddReadCookies method and cookies 83 // that are received from a server or set via JavaScript using the method 84 // AddChangedCookie. 85 // Cookies are distinguished by the tuple cookie name (called cookie-name in 86 // RFC 6265), cookie domain (called cookie-domain in RFC 6265), cookie path 87 // (called cookie-path in RFC 6265) and host-only-flag (see RFC 6265 section 88 // 5.3). Cookies with same tuple (cookie-name, cookie-domain, cookie-path, 89 // host-only-flag) as cookie that are already stored, will replace the stored 90 // cookies. 91 class CannedBrowsingDataCookieHelper : public BrowsingDataCookieHelper { 92 public: 93 typedef std::map<GURL, net::CookieList*> OriginCookieListMap; 94 95 explicit CannedBrowsingDataCookieHelper( 96 net::URLRequestContextGetter* request_context); 97 98 // Return a copy of the cookie helper. Only one consumer can use the 99 // StartFetching method at a time, so we need to create a copy of the helper 100 // everytime we instantiate a cookies tree model for it. 101 CannedBrowsingDataCookieHelper* Clone(); 102 103 // Adds the cookies from |cookie_list|. Current cookies that have the same 104 // cookie name, cookie domain, cookie path, host-only-flag tuple as passed 105 // cookies are replaced by the passed cookies. 106 void AddReadCookies(const GURL& frame_url, 107 const GURL& request_url, 108 const net::CookieList& cookie_list); 109 110 // Adds a CanonicalCookie that is created from the passed |cookie_line| 111 // (called set-cookie-string in RFC 6225). The |cookie_line| is parsed, 112 // normalized and validated. Invalid |cookie_line|s are ignored. The logic 113 // for parsing, normalizing an validating the |cookie_line| mirrors the logic 114 // of CookieMonster's method SetCookieWithOptions. If the |cookie_line| does 115 // not include a cookie domain attribute (called domain-av in RFC 6265) or a 116 // cookie path (called path-av in RFC 6265), then the host and the 117 // default-path of the request-uri are used as domain-value and path-value 118 // for the cookie. CanonicalCookies created from a |cookie_line| with no 119 // cookie domain attribute are host only cookies. 120 // TODO(markusheintz): Remove the dublicated logic. 121 void AddChangedCookie(const GURL& frame_url, 122 const GURL& request_url, 123 const std::string& cookie_line, 124 const net::CookieOptions& options); 125 126 // Clears the list of canned cookies. 127 void Reset(); 128 129 // True if no cookie are currently stored. 130 bool empty() const; 131 132 // BrowsingDataCookieHelper methods. 133 virtual void StartFetching( 134 const net::CookieMonster::GetCookieListCallback& callback) OVERRIDE; 135 136 // Returns the number of stored cookies. 137 size_t GetCookieCount() const; 138 139 // Returns the map that contains the cookie lists for all frame urls. 140 const OriginCookieListMap& origin_cookie_list_map() { 141 return origin_cookie_list_map_; 142 } 143 144 private: 145 // Check if the cookie list contains a cookie with the same name, 146 // domain, and path as the newly created cookie. Delete the old cookie 147 // if does. 148 bool DeleteMatchingCookie(const net::CanonicalCookie& add_cookie, 149 net::CookieList* cookie_list); 150 151 virtual ~CannedBrowsingDataCookieHelper(); 152 153 // Returns the |CookieList| for the given |origin|. 154 net::CookieList* GetCookiesFor(const GURL& origin); 155 156 // Adds the |cookie| to the cookie list for the given |frame_url|. 157 void AddCookie(const GURL& frame_url, 158 const net::CanonicalCookie& cookie); 159 160 // Map that contains the cookie lists for all frame origins. 161 OriginCookieListMap origin_cookie_list_map_; 162 163 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataCookieHelper); 164 }; 165 166 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_COOKIE_HELPER_H_ 167