Give names to all utility processes.
[chromium-blink-merge.git] / ios / net / cookies / cookie_store_ios.h
blobd44123dc6e880d87d1c3e40cd03f64d8c9926fe8
1 // Copyright 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 IOS_NET_COOKIES_COOKIE_STORE_IOS_H_
6 #define IOS_NET_COOKIES_COOKIE_STORE_IOS_H_
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
13 #include "base/callback.h"
14 #include "base/cancelable_callback.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/time/time.h"
18 #include "ios/net/cookies/cookie_cache.h"
19 #include "net/cookies/cookie_monster.h"
20 #include "net/cookies/cookie_store.h"
21 #include "url/gurl.h"
23 #if defined(__OBJC__)
24 @class NSHTTPCookie;
25 @class NSArray;
26 #else
27 class NSHTTPCookie;
28 class NSArray;
29 #endif
31 namespace net {
33 class CookieCreationTimeManager;
35 // Observer for system cookie notifications.
36 class CookieNotificationObserver {
37 public:
38 // Called when any cookie is added, deleted or changed in the system store.
39 virtual void OnSystemCookiesChanged() = 0;
40 // Called when the cookie policy changes.
41 virtual void OnSystemCookiePolicyChanged() = 0;
44 // The CookieStoreIOS is an implementation of CookieStore relying on
45 // NSHTTPCookieStorage, ensuring that the cookies are consistent between the
46 // network stack and the UIWebViews.
47 // On iOS, the Chrome CookieMonster is not used in conjunction with UIWebView,
48 // because UIWebView expects the cookies to be in the shared
49 // NSHTTPCookieStorage. In particular, javascript may read and write cookies
50 // there.
51 // CookieStoreIOS is not thread safe.
53 // At any given time, a CookieStoreIOS can either be synchronized with the
54 // system cookie store or not. If a CookieStoreIOS is not synchronized with the
55 // system store, changes are written back to the backing CookieStore. If a
56 // CookieStoreIOS is synchronized with the system store, changes are written
57 // directly to the system cookie store, then propagated to the backing store by
58 // OnSystemCookiesChanged, which is called by the system store once the change
59 // to the system store is written back.
61 // To unsynchronize, CookieStoreIOS copies the system cookie store into its
62 // backing CookieStore. To synchronize, CookieStoreIOS clears the system cookie
63 // store, copies its backing CookieStore into the system cookie store.
64 class CookieStoreIOS : public net::CookieStore,
65 public CookieNotificationObserver {
66 public:
67 explicit CookieStoreIOS(
68 net::CookieMonster::PersistentCookieStore* persistent_store);
70 enum CookiePolicy { ALLOW, BLOCK };
72 // Must be called on the thread where CookieStoreIOS instances live.
73 static void SetCookiePolicy(CookiePolicy setting);
75 // Create an instance of CookieStoreIOS that is generated from the cookies
76 // stored in the system NSHTTPCookieStorage. The caller is responsible for
77 // deleting the cookie store.
78 // Apple does not persist the cookies' creation dates in the system store,
79 // so callers should not expect these values to be populated.
80 static CookieStoreIOS* CreateCookieStoreFromNSHTTPCookieStorage();
82 // As there is only one system store, only one CookieStoreIOS at a time may
83 // be synchronized with it.
84 static void SwitchSynchronizedStore(CookieStoreIOS* old_store,
85 CookieStoreIOS* new_store);
87 // Must be called when the state of the system cookie store changes.
88 static void NotifySystemCookiesChanged();
90 // Saves the cookies to the cookie monster.
91 // Note: ignores the write cookie operation if |write_on_flush_| is false.
92 void Flush(const base::Closure& callback);
94 // Unsynchronizes the cookie store if it is currently synchronized.
95 void UnSynchronize();
97 // Only one cookie store may enable metrics.
98 void SetMetricsEnabled();
100 // Sets the delay between flushes. Only used in tests.
101 void set_flush_delay_for_testing(base::TimeDelta delay) {
102 flush_delay_ = delay;
105 // Inherited CookieStore methods.
106 void SetCookieWithOptionsAsync(const GURL& url,
107 const std::string& cookie_line,
108 const net::CookieOptions& options,
109 const SetCookiesCallback& callback) override;
110 void GetCookiesWithOptionsAsync(const GURL& url,
111 const net::CookieOptions& options,
112 const GetCookiesCallback& callback) override;
113 void GetAllCookiesForURLAsync(const GURL& url,
114 const GetCookieListCallback& callback) override;
115 void DeleteCookieAsync(const GURL& url,
116 const std::string& cookie_name,
117 const base::Closure& callback) override;
118 net::CookieMonster* GetCookieMonster() override;
119 void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin,
120 const base::Time& delete_end,
121 const DeleteCallback& callback) override;
122 void DeleteAllCreatedBetweenForHostAsync(
123 const base::Time delete_begin,
124 const base::Time delete_end,
125 const GURL& url,
126 const DeleteCallback& callback) override;
127 void DeleteSessionCookiesAsync(const DeleteCallback& callback) override;
129 scoped_ptr<CookieChangedSubscription> AddCallbackForCookie(
130 const GURL& url,
131 const std::string& name,
132 const CookieChangedCallback& callback) override;
134 protected:
135 ~CookieStoreIOS() override;
137 private:
138 // For tests.
139 friend struct CookieStoreIOSTestTraits;
141 enum SynchronizationState {
142 NOT_SYNCHRONIZED, // Uses CookieMonster as backend.
143 SYNCHRONIZING, // Moves from NSHTTPCookieStorage to CookieMonster.
144 SYNCHRONIZED // Uses NSHTTPCookieStorage as backend.
147 // Cookie fliter for DeleteCookiesWithFilter().
148 // Takes a cookie and a creation time and returns true if the cookie must be
149 // deleted.
150 typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction;
152 // Clears the system cookie store.
153 void ClearSystemStore();
154 // Changes the synchronization of the store.
155 // If |synchronized| is true, then the system cookie store is used as a
156 // backend, else |cookie_monster_| is used. Cookies are moved from one to
157 // the other accordingly.
158 void SetSynchronizedWithSystemStore(bool synchronized);
159 // Returns true if the system cookie store policy is
160 // |NSHTTPCookieAcceptPolicyAlways|.
161 bool SystemCookiesAllowed();
162 // Converts |cookies| to NSHTTPCookie and add them to the system store.
163 void AddCookiesToSystemStore(const net::CookieList& cookies);
164 // Copies the cookies to the backing CookieMonster. If the cookie store is not
165 // synchronized with the system store, this is a no-op.
166 void WriteToCookieMonster(NSArray* system_cookies);
168 // Inherited CookieNotificationObserver methods.
169 void OnSystemCookiesChanged() override;
170 void OnSystemCookiePolicyChanged() override;
172 void DeleteCookiesWithFilter(const CookieFilterFunction& filter,
173 const DeleteCallback& callback);
175 scoped_refptr<net::CookieMonster> cookie_monster_;
176 scoped_ptr<CookieCreationTimeManager> creation_time_manager_;
177 bool metrics_enabled_;
178 base::TimeDelta flush_delay_;
179 base::CancelableClosure flush_closure_;
181 SynchronizationState synchronization_state_;
182 // Tasks received when SYNCHRONIZING are queued and run when SYNCHRONIZED.
183 std::vector<base::Closure> tasks_pending_synchronization_;
185 base::ThreadChecker thread_checker_;
187 // Cookie notification methods.
188 // The cookie cache is updated from both the system store and the
189 // CookieStoreIOS' own mutators. Changes when the CookieStoreIOS is
190 // synchronized are signalled by the system store; changes when the
191 // CookieStoreIOS is not synchronized are signalled by the appropriate
192 // mutators on CookieStoreIOS. The cookie cache tracks the system store when
193 // the CookieStoreIOS is synchronized and the CookieStore when the
194 // CookieStoreIOS is not synchronized.
196 // Fetches any cookies named |name| that would be sent with a request for
197 // |url| from the system cookie store and pushes them onto the back of the
198 // vector pointed to by |cookies|. Returns true if any cookies were pushed
199 // onto the vector, and false otherwise.
200 bool GetSystemCookies(const GURL& url,
201 const std::string& name,
202 std::vector<net::CanonicalCookie>* cookies);
204 // Updates the cookie cache with the current set of system cookies named
205 // |name| that would be sent with a request for |url|. Returns whether the
206 // cache changed.
207 // |out_removed_cookies|, if not null, will be populated with the cookies that
208 // were removed.
209 // |out_changed_cookies|, if not null, will be populated with the cookies that
210 // were added.
211 bool UpdateCacheForCookieFromSystem(
212 const GURL& gurl,
213 const std::string& name,
214 std::vector<net::CanonicalCookie>* out_removed_cookies,
215 std::vector<net::CanonicalCookie>* out_added_cookies);
217 // Runs all callbacks registered for cookies named |name| that would be sent
218 // with a request for |url|.
219 // All cookies in |cookies| must have the name equal to |name|.
220 void RunCallbacksForCookies(const GURL& url,
221 const std::string& name,
222 const std::vector<net::CanonicalCookie>& cookies,
223 bool removed);
225 // Called by this CookieStoreIOS' internal CookieMonster instance when
226 // GetAllCookiesForURLAsync() completes. Updates the cookie cache and runs
227 // callbacks if the cache changed.
228 void GotCookieListFor(const std::pair<GURL, std::string> key,
229 const net::CookieList& cookies);
231 // Fetches new values for all (url, name) pairs that have hooks registered,
232 // asynchronously invoking callbacks if necessary.
233 void UpdateCachesFromCookieMonster();
235 // Called after cookies are cleared from NSHTTPCookieStorage so that cookies
236 // can be cleared from .binarycookies file. |callback| is called after all the
237 // cookies are deleted (with the total number of cookies deleted).
238 // |num_deleted| contains the number of cookies deleted from
239 // NSHTTPCookieStorage.
240 void DidClearNSHTTPCookieStorageCookies(const DeleteCallback& callback,
241 int num_deleted);
242 // Called after cookies are cleared from .binarycookies files. |callback| is
243 // called after all the cookies are deleted with the total number of cookies
244 // deleted.
245 // |num_deleted_from_nshttp_cookie_storage| contains the number of cookies
246 // deleted from NSHTTPCookieStorage.
247 void DidClearBinaryCookiesFileCookies(
248 const DeleteCallback& callback,
249 int num_deleted_from_nshttp_cookie_storage);
251 // Callback-wrapping:
252 // When this CookieStoreIOS object is synchronized with the system store,
253 // OnSystemCookiesChanged is responsible for updating the cookie cache (and
254 // hence running callbacks).
256 // When this CookieStoreIOS object is not synchronized (or is synchronizing),
257 // the various mutator methods (SetCookieWithOptionsAsync &c) instead store
258 // their state in a CookieMonster object to be written back when the system
259 // store synchronizes. To deliver notifications in a timely manner, the
260 // mutators have to ensure that hooks get run, but only after the changes have
261 // been written back to CookieMonster. To do this, the mutators wrap the
262 // user-supplied callback in a callback which schedules an asynchronous task
263 // to synchronize the cache and run callbacks, then calls through to the
264 // user-specified callback.
266 // These three UpdateCachesAfter functions are responsible for scheduling an
267 // asynchronous cache update (using UpdateCachesFromCookieMonster()) and
268 // calling the provided callback.
270 void UpdateCachesAfterSet(const SetCookiesCallback& callback, bool success);
271 void UpdateCachesAfterDelete(const DeleteCallback& callback, int num_deleted);
272 void UpdateCachesAfterClosure(const base::Closure& callback);
274 // These three functions are used for wrapping user-supplied callbacks given
275 // to CookieStoreIOS mutator methods. Given a callback, they return a new
276 // callback that invokes UpdateCachesFromCookieMonster() to schedule an
277 // asynchronous synchronization of the cookie cache and then calls the
278 // original callback.
280 SetCookiesCallback WrapSetCallback(const SetCookiesCallback& callback);
281 DeleteCallback WrapDeleteCallback(const DeleteCallback& callback);
282 base::Closure WrapClosure(const base::Closure& callback);
284 // Cached values of system cookies. Only cookies which have an observer added
285 // with AddCallbackForCookie are kept in this cache.
286 scoped_ptr<CookieCache> cookie_cache_;
288 // Callbacks for cookie changes installed by AddCallbackForCookie.
289 typedef std::map<std::pair<GURL, std::string>, CookieChangedCallbackList*>
290 CookieChangedHookMap;
291 CookieChangedHookMap hook_map_;
293 DISALLOW_COPY_AND_ASSIGN(CookieStoreIOS);
296 } // namespace net
298 #endif // IOS_NET_COOKIES_COOKIE_STORE_IOS_H_