OLD | NEW |
| (Empty) |
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_COOKIE_HELPER_H_ | |
6 #define CHROME_BROWSER_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_; | |
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, but gets them passed | |
81 // as a parameter during construction. | |
82 class CannedBrowsingDataCookieHelper : public BrowsingDataCookieHelper { | |
83 public: | |
84 typedef std::map<GURL, net::CookieList*> OriginCookieListMap; | |
85 | |
86 explicit CannedBrowsingDataCookieHelper( | |
87 net::URLRequestContextGetter* request_context); | |
88 | |
89 // Return a copy of the cookie helper. Only one consumer can use the | |
90 // StartFetching method at a time, so we need to create a copy of the helper | |
91 // everytime we instantiate a cookies tree model for it. | |
92 CannedBrowsingDataCookieHelper* Clone(); | |
93 | |
94 // Adds cookies and delete the current cookies with the same Name, Domain, | |
95 // and Path as the newly created ones. | |
96 void AddReadCookies(const GURL& frame_url, | |
97 const GURL& request_url, | |
98 const net::CookieList& cookie_list); | |
99 | |
100 // Adds cookies that will be stored by the CookieMonster. Designed to mirror | |
101 // the logic of SetCookieWithOptions. | |
102 void AddChangedCookie(const GURL& frame_url, | |
103 const GURL& request_url, | |
104 const std::string& cookie_line, | |
105 const net::CookieOptions& options); | |
106 | |
107 // Clears the list of canned cookies. | |
108 void Reset(); | |
109 | |
110 // True if no cookie are currently stored. | |
111 bool empty() const; | |
112 | |
113 // BrowsingDataCookieHelper methods. | |
114 virtual void StartFetching( | |
115 const net::CookieMonster::GetCookieListCallback& callback) OVERRIDE; | |
116 | |
117 // Returns the number of stored cookies. | |
118 size_t GetCookieCount() const; | |
119 | |
120 // Returns the map that contains the cookie lists for all frame urls. | |
121 const OriginCookieListMap& origin_cookie_list_map() { | |
122 return origin_cookie_list_map_; | |
123 } | |
124 | |
125 private: | |
126 // Check if the cookie list contains a cookie with the same name, | |
127 // domain, and path as the newly created cookie. Delete the old cookie | |
128 // if does. | |
129 bool DeleteMatchingCookie(const net::CanonicalCookie& add_cookie, | |
130 net::CookieList* cookie_list); | |
131 | |
132 virtual ~CannedBrowsingDataCookieHelper(); | |
133 | |
134 // Returns the |CookieList| for the given |origin|. | |
135 net::CookieList* GetCookiesFor(const GURL& origin); | |
136 | |
137 // Adds the |cookie| to the cookie list for the given |frame_url|. | |
138 void AddCookie(const GURL& frame_url, | |
139 const net::CanonicalCookie& cookie); | |
140 | |
141 // Map that contains the cookie lists for all frame origins. | |
142 OriginCookieListMap origin_cookie_list_map_; | |
143 | |
144 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataCookieHelper); | |
145 }; | |
146 | |
147 #endif // CHROME_BROWSER_BROWSING_DATA_COOKIE_HELPER_H_ | |
OLD | NEW |