Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: ios/net/cookies/cookie_store_ios.h

Issue 2649083002: Divide CookieStoreIOS into two different classes with different backends (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef IOS_NET_COOKIES_COOKIE_STORE_IOS_H_ 5 #ifndef IOS_NET_COOKIES_COOKIE_STORE_IOS_H_
6 #define IOS_NET_COOKIES_COOKIE_STORE_IOS_H_ 6 #define IOS_NET_COOKIES_COOKIE_STORE_IOS_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
(...skipping 23 matching lines...) Expand all
34 // Observer for changes on |NSHTTPCookieStorge sharedHTTPCookieStorage|. 34 // Observer for changes on |NSHTTPCookieStorge sharedHTTPCookieStorage|.
35 class CookieNotificationObserver { 35 class CookieNotificationObserver {
36 public: 36 public:
37 // Called when any cookie is added, deleted or changed in 37 // Called when any cookie is added, deleted or changed in
38 // |NSHTTPCookieStorge sharedHTTPCookieStorage|. 38 // |NSHTTPCookieStorge sharedHTTPCookieStorage|.
39 virtual void OnSystemCookiesChanged() = 0; 39 virtual void OnSystemCookiesChanged() = 0;
40 }; 40 };
41 41
42 // The CookieStoreIOS is an implementation of CookieStore relying on 42 // The CookieStoreIOS is an implementation of CookieStore relying on
43 // NSHTTPCookieStorage, ensuring that the cookies are consistent between the 43 // NSHTTPCookieStorage, ensuring that the cookies are consistent between the
44 // network stack and NSHTTPCookieStorage. 44 // network stack and NSHTTPCookieStorage. CookieStoreIOS is not thread safe.
45 // CookieStoreIOS is not thread safe.
46 // 45 //
47 // CookieStoreIOS can be created synchronized with the system cookie store (via 46 // CookieStoreIOS is created synchronized with the system cookie store -
48 // CreateCookieStore) or not (other constructors). If a CookieStoreIOS is not
49 // synchronized with the system store, changes are written back to the backing
50 // CookieStore. If a CookieStoreIOS is synchronized with the system store,
51 // changes are written directly to the system cookie store, then propagated to 47 // changes are written directly to the system cookie store, then propagated to
52 // the backing store by OnSystemCookiesChanged, which is called by the system 48 // the backing store by OnSystemCookiesChanged, which is called by the system
53 // store once the change to the system store is written back. 49 // store once the change to the system store is written back.
50 // For not synchronized CookieStore, please see CookieStoreIOSPersistent.
54 class CookieStoreIOS : public net::CookieStore, 51 class CookieStoreIOS : public net::CookieStore,
55 public CookieNotificationObserver { 52 public CookieNotificationObserver {
56 public: 53 public:
57 // Creates a CookieStoreIOS with a default value of 54 // Creates an instance of CookieStoreIOS that is generated from the cookies
58 // |NSHTTPCookieStorage sharedCookieStorage| as the system's cookie store. 55 // stored in |cookie_storage|. The CookieStoreIOS uses the |cookie_storage|
59 explicit CookieStoreIOS( 56 // as its default backend and is initially synchronized with it.
60 net::CookieMonster::PersistentCookieStore* persistent_store); 57 // Apple does not persist the cookies' creation dates in NSHTTPCookieStorage,
58 // so callers should not expect these values to be populated.
59 explicit CookieStoreIOS(NSHTTPCookieStorage* cookie_storage);
60
61 // Creates a CookieStoreIOS with NSHTTPCookieStorage backend.
62 // TODO(crbug.com/683964): Remove this method.
63 static std::unique_ptr<CookieStoreIOS> CreateCookieStore(
64 NSHTTPCookieStorage* cookie_storage);
61 65
62 ~CookieStoreIOS() override; 66 ~CookieStoreIOS() override;
63 67
64 enum CookiePolicy { ALLOW, BLOCK }; 68 enum CookiePolicy { ALLOW, BLOCK };
65 69
66 // Create an instance of CookieStoreIOS that is generated from the cookies
67 // stored in |cookie_storage|. The CookieStoreIOS uses the |cookie_storage|
68 // as its default backend and is initially synchronized with it.
69 // Apple does not persist the cookies' creation dates in NSHTTPCookieStorage,
70 // so callers should not expect these values to be populated.
71 static std::unique_ptr<CookieStoreIOS> CreateCookieStore(
72 NSHTTPCookieStorage* cookie_storage);
73
74 // Must be called when the state of 70 // Must be called when the state of
75 // |NSHTTPCookieStorage sharedHTTPCookieStorage| changes. 71 // |NSHTTPCookieStorage sharedHTTPCookieStorage| changes.
76 // Affects only those CookieStoreIOS instances that are backed by 72 // Affects only those CookieStoreIOS instances that are backed by
77 // |NSHTTPCookieStorage sharedHTTPCookieStorage|. 73 // |NSHTTPCookieStorage sharedHTTPCookieStorage|.
78 static void NotifySystemCookiesChanged(); 74 static void NotifySystemCookiesChanged();
79 75
80 // Only one cookie store may enable metrics. 76 // Only one cookie store may enable metrics.
81 void SetMetricsEnabled(); 77 void SetMetricsEnabled();
82 78
83 // Inherited CookieStore methods. 79 // Inherited CookieStore methods.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 void DeleteSessionCookiesAsync(const DeleteCallback& callback) override; 120 void DeleteSessionCookiesAsync(const DeleteCallback& callback) override;
125 void FlushStore(const base::Closure& callback) override; 121 void FlushStore(const base::Closure& callback) override;
126 122
127 std::unique_ptr<CookieChangedSubscription> AddCallbackForCookie( 123 std::unique_ptr<CookieChangedSubscription> AddCallbackForCookie(
128 const GURL& url, 124 const GURL& url,
129 const std::string& name, 125 const std::string& name,
130 const CookieChangedCallback& callback) override; 126 const CookieChangedCallback& callback) override;
131 127
132 bool IsEphemeral() override; 128 bool IsEphemeral() override;
133 129
130 protected:
131 CookieStoreIOS(net::CookieMonster::PersistentCookieStore* persistent_store,
132 NSHTTPCookieStorage* system_store);
133
134 // These three functions are used for wrapping user-supplied callbacks given
135 // to CookieStoreIOS mutator methods. Given a callback, they return a new
136 // callback that invokes UpdateCachesFromCookieMonster() to schedule an
137 // asynchronous synchronization of the cookie cache and then calls the
138 // original callback.
139 SetCookiesCallback WrapSetCallback(const SetCookiesCallback& callback);
140 DeleteCallback WrapDeleteCallback(const DeleteCallback& callback);
141 base::Closure WrapClosure(const base::Closure& callback);
142
143 bool metrics_enabled() { return metrics_enabled_; }
144
145 net::CookieMonster* cookie_monster() { return cookie_monster_.get(); }
146
147 const base::ThreadChecker& thread_checker() { return thread_checker_; }
148
134 private: 149 private:
135 CookieStoreIOS( 150 // Cookie filter for DeleteCookiesWithFilter().
136 net::CookieMonster::PersistentCookieStore* persistent_store, 151 // Takes a cookie and a creation time and returns true the cookie must be
137 NSHTTPCookieStorage* system_store);
138
139 // For tests.
140 friend struct CookieStoreIOSTestTraits;
141
142 enum SynchronizationState {
143 NOT_SYNCHRONIZED, // Uses CookieMonster as backend.
144 SYNCHRONIZED // Uses NSHTTPCookieStorage as backend.
145 };
146
147 // Cookie fliter for DeleteCookiesWithFilter().
148 // Takes a cookie and a creation time and returns true if the cookie must be
149 // deleted. 152 // deleted.
150 typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction; 153 typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction;
151 154
152 // Clears the system cookie store. 155 // Clears the system cookie store.
153 void ClearSystemStore(); 156 void ClearSystemStore();
154 // Returns true if the system cookie store policy is 157 // Returns true if the system cookie store policy is
155 // |NSHTTPCookieAcceptPolicyAlways|. 158 // |NSHTTPCookieAcceptPolicyAlways|.
156 bool SystemCookiesAllowed(); 159 bool SystemCookiesAllowed();
157 // Copies the cookies to the backing CookieMonster. If the cookie store is not 160 // Copies the cookies to the backing CookieMonster.
158 // synchronized with the system store, this is a no-op. 161 virtual void WriteToCookieMonster(NSArray* system_cookies);
159 void WriteToCookieMonster(NSArray* system_cookies);
160 162
161 // Inherited CookieNotificationObserver methods. 163 // Inherited CookieNotificationObserver methods.
162 void OnSystemCookiesChanged() override; 164 void OnSystemCookiesChanged() override;
163 165
164 void DeleteCookiesWithFilter(const CookieFilterFunction& filter, 166 void DeleteCookiesWithFilter(const CookieFilterFunction& filter,
165 const DeleteCallback& callback); 167 const DeleteCallback& callback);
166 168
167 std::unique_ptr<net::CookieMonster> cookie_monster_; 169 std::unique_ptr<net::CookieMonster> cookie_monster_;
168 base::scoped_nsobject<NSHTTPCookieStorage> system_store_; 170 base::scoped_nsobject<NSHTTPCookieStorage> system_store_;
169 std::unique_ptr<CookieCreationTimeManager> creation_time_manager_; 171 std::unique_ptr<CookieCreationTimeManager> creation_time_manager_;
170 bool metrics_enabled_; 172 bool metrics_enabled_;
171 base::CancelableClosure flush_closure_; 173 base::CancelableClosure flush_closure_;
172 174
173 SynchronizationState synchronization_state_;
174
175 base::ThreadChecker thread_checker_; 175 base::ThreadChecker thread_checker_;
176 176
177 // Cookie notification methods. 177 // Cookie notification methods.
178 // The cookie cache is updated from both the system store and the 178 // The cookie cache is updated from both the system store and the
179 // CookieStoreIOS' own mutators. Changes when the CookieStoreIOS is 179 // CookieStoreIOS' own mutators. Changes when the CookieStoreIOS is
180 // synchronized are signalled by the system store; changes when the 180 // synchronized are signalled by the system store; changes when the
181 // CookieStoreIOS is not synchronized are signalled by the appropriate 181 // CookieStoreIOS is not synchronized are signalled by the appropriate
182 // mutators on CookieStoreIOS. The cookie cache tracks the system store when 182 // mutators on CookieStoreIOS. The cookie cache tracks the system store when
183 // the CookieStoreIOS is synchronized and the CookieStore when the 183 // the CookieStoreIOS is synchronized and the CookieStore when the
184 // CookieStoreIOS is not synchronized. 184 // CookieStoreIOS is not synchronized.
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 258
259 void UpdateCachesAfterSet(const SetCookiesCallback& callback, bool success); 259 void UpdateCachesAfterSet(const SetCookiesCallback& callback, bool success);
260 void UpdateCachesAfterDelete(const DeleteCallback& callback, int num_deleted); 260 void UpdateCachesAfterDelete(const DeleteCallback& callback, int num_deleted);
261 void UpdateCachesAfterClosure(const base::Closure& callback); 261 void UpdateCachesAfterClosure(const base::Closure& callback);
262 262
263 // Takes an NSArray of NSHTTPCookies as returns a net::CookieList. 263 // Takes an NSArray of NSHTTPCookies as returns a net::CookieList.
264 // The returned cookies are ordered by longest path, then earliest 264 // The returned cookies are ordered by longest path, then earliest
265 // creation date. 265 // creation date.
266 net::CookieList CanonicalCookieListFromSystemCookies(NSArray* cookies); 266 net::CookieList CanonicalCookieListFromSystemCookies(NSArray* cookies);
267 267
268 // These three functions are used for wrapping user-supplied callbacks given
269 // to CookieStoreIOS mutator methods. Given a callback, they return a new
270 // callback that invokes UpdateCachesFromCookieMonster() to schedule an
271 // asynchronous synchronization of the cookie cache and then calls the
272 // original callback.
273
274 SetCookiesCallback WrapSetCallback(const SetCookiesCallback& callback);
275 DeleteCallback WrapDeleteCallback(const DeleteCallback& callback);
276 base::Closure WrapClosure(const base::Closure& callback);
277
278 // Cached values of system cookies. Only cookies which have an observer added 268 // Cached values of system cookies. Only cookies which have an observer added
279 // with AddCallbackForCookie are kept in this cache. 269 // with AddCallbackForCookie are kept in this cache.
280 std::unique_ptr<CookieCache> cookie_cache_; 270 std::unique_ptr<CookieCache> cookie_cache_;
281 271
282 // Callbacks for cookie changes installed by AddCallbackForCookie. 272 // Callbacks for cookie changes installed by AddCallbackForCookie.
283 std::map<std::pair<GURL, std::string>, 273 std::map<std::pair<GURL, std::string>,
284 std::unique_ptr<CookieChangedCallbackList>> 274 std::unique_ptr<CookieChangedCallbackList>>
285 hook_map_; 275 hook_map_;
286 276
287 base::WeakPtrFactory<CookieStoreIOS> weak_factory_; 277 base::WeakPtrFactory<CookieStoreIOS> weak_factory_;
288 278
289 DISALLOW_COPY_AND_ASSIGN(CookieStoreIOS); 279 DISALLOW_COPY_AND_ASSIGN(CookieStoreIOS);
290 }; 280 };
291 281
292 } // namespace net 282 } // namespace net
293 283
294 #endif // IOS_NET_COOKIES_COOKIE_STORE_IOS_H_ 284 #endif // IOS_NET_COOKIES_COOKIE_STORE_IOS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698