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

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: Created 3 years, 11 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 // Create an instance of CookieStoreIOS that is generated from the cookies
Eugene But (OOO till 7-30) 2017/01/23 17:37:47 nit: s/Create/Creates I realize that this is just
maksims (do not use this acc) 2017/01/24 10:23:46 Done.
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);
61 60
62 ~CookieStoreIOS() override; 61 ~CookieStoreIOS() override;
63 62
64 enum CookiePolicy { ALLOW, BLOCK }; 63 enum CookiePolicy { ALLOW, BLOCK };
65 64
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(
Eugene But (OOO till 7-30) 2017/01/23 17:37:47 This will break compilation in close source Chrome
maksims (do not use this acc) 2017/01/24 10:23:46 Oh, that what you meant. I though If it could comp
72 NSHTTPCookieStorage* cookie_storage);
73
74 // Must be called when the state of 65 // Must be called when the state of
75 // |NSHTTPCookieStorage sharedHTTPCookieStorage| changes. 66 // |NSHTTPCookieStorage sharedHTTPCookieStorage| changes.
76 // Affects only those CookieStoreIOS instances that are backed by 67 // Affects only those CookieStoreIOS instances that are backed by
77 // |NSHTTPCookieStorage sharedHTTPCookieStorage|. 68 // |NSHTTPCookieStorage sharedHTTPCookieStorage|.
78 static void NotifySystemCookiesChanged(); 69 static void NotifySystemCookiesChanged();
79 70
80 // Only one cookie store may enable metrics. 71 // Only one cookie store may enable metrics.
81 void SetMetricsEnabled(); 72 void SetMetricsEnabled();
82 73
83 // Inherited CookieStore methods. 74 // Inherited CookieStore methods.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 void DeleteSessionCookiesAsync(const DeleteCallback& callback) override; 115 void DeleteSessionCookiesAsync(const DeleteCallback& callback) override;
125 void FlushStore(const base::Closure& callback) override; 116 void FlushStore(const base::Closure& callback) override;
126 117
127 std::unique_ptr<CookieChangedSubscription> AddCallbackForCookie( 118 std::unique_ptr<CookieChangedSubscription> AddCallbackForCookie(
128 const GURL& url, 119 const GURL& url,
129 const std::string& name, 120 const std::string& name,
130 const CookieChangedCallback& callback) override; 121 const CookieChangedCallback& callback) override;
131 122
132 bool IsEphemeral() override; 123 bool IsEphemeral() override;
133 124
134 private: 125 protected:
135 CookieStoreIOS(
136 net::CookieMonster::PersistentCookieStore* persistent_store,
137 NSHTTPCookieStorage* system_store);
138
139 // For tests.
140 friend struct CookieStoreIOSTestTraits;
141
142 enum SynchronizationState { 126 enum SynchronizationState {
143 NOT_SYNCHRONIZED, // Uses CookieMonster as backend. 127 NOT_SYNCHRONIZED, // Uses CookieMonster as backend.
144 SYNCHRONIZED // Uses NSHTTPCookieStorage as backend. 128 SYNCHRONIZED // Uses NSHTTPCookieStorage as backend.
145 }; 129 };
146 130
147 // Cookie fliter for DeleteCookiesWithFilter(). 131 CookieStoreIOS(SynchronizationState state,
148 // Takes a cookie and a creation time and returns true if the cookie must be 132 net::CookieMonster::PersistentCookieStore* persistent_store,
133 NSHTTPCookieStorage* system_store);
134
135 // These three functions are used for wrapping user-supplied callbacks given
136 // to CookieStoreIOS mutator methods. Given a callback, they return a new
137 // callback that invokes UpdateCachesFromCookieMonster() to schedule an
138 // asynchronous synchronization of the cookie cache and then calls the
139 // original callback.
140 SetCookiesCallback WrapSetCallback(const SetCookiesCallback& callback);
141 DeleteCallback WrapDeleteCallback(const DeleteCallback& callback);
142 base::Closure WrapClosure(const base::Closure& callback);
143
144 bool metrics_enabled() { return metrics_enabled_; }
145
146 net::CookieMonster* cookie_monster() { return cookie_monster_.get(); }
147
148 const base::ThreadChecker& thread_checker() { return thread_checker_; }
149
150 private:
151 // For tests.
152 friend struct CookieStoreIOSTestTraits;
153
154 // Cookie filter for DeleteCookiesWithFilter().
155 // Takes a cookie and a creation time and returns true the cookie must be
149 // deleted. 156 // deleted.
150 typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction; 157 typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction;
151 158
152 // Clears the system cookie store. 159 // Clears the system cookie store.
153 void ClearSystemStore(); 160 void ClearSystemStore();
154 // Returns true if the system cookie store policy is 161 // Returns true if the system cookie store policy is
155 // |NSHTTPCookieAcceptPolicyAlways|. 162 // |NSHTTPCookieAcceptPolicyAlways|.
156 bool SystemCookiesAllowed(); 163 bool SystemCookiesAllowed();
157 // Copies the cookies to the backing CookieMonster. If the cookie store is not 164 // Copies the cookies to the backing CookieMonster. If the cookie store is not
158 // synchronized with the system store, this is a no-op. 165 // synchronized with the system store, this is a no-op.
159 void WriteToCookieMonster(NSArray* system_cookies); 166 void WriteToCookieMonster(NSArray* system_cookies);
160 167
161 // Inherited CookieNotificationObserver methods. 168 // Inherited CookieNotificationObserver methods.
162 void OnSystemCookiesChanged() override; 169 void OnSystemCookiesChanged() override;
163 170
164 void DeleteCookiesWithFilter(const CookieFilterFunction& filter, 171 void DeleteCookiesWithFilter(const CookieFilterFunction& filter,
165 const DeleteCallback& callback); 172 const DeleteCallback& callback);
166 173
167 std::unique_ptr<net::CookieMonster> cookie_monster_; 174 std::unique_ptr<net::CookieMonster> cookie_monster_;
168 base::scoped_nsobject<NSHTTPCookieStorage> system_store_; 175 base::scoped_nsobject<NSHTTPCookieStorage> system_store_;
169 std::unique_ptr<CookieCreationTimeManager> creation_time_manager_; 176 std::unique_ptr<CookieCreationTimeManager> creation_time_manager_;
170 bool metrics_enabled_; 177 bool metrics_enabled_;
171 base::CancelableClosure flush_closure_; 178 base::CancelableClosure flush_closure_;
172 179
173 SynchronizationState synchronization_state_; 180 SynchronizationState synchronization_state_;
Eugene But (OOO till 7-30) 2017/01/23 17:37:47 I think we can remove this member as well (see com
maksims (do not use this acc) 2017/01/24 10:23:46 Done.
174 181
175 base::ThreadChecker thread_checker_; 182 base::ThreadChecker thread_checker_;
176 183
177 // Cookie notification methods. 184 // Cookie notification methods.
178 // The cookie cache is updated from both the system store and the 185 // The cookie cache is updated from both the system store and the
179 // CookieStoreIOS' own mutators. Changes when the CookieStoreIOS is 186 // CookieStoreIOS' own mutators. Changes when the CookieStoreIOS is
180 // synchronized are signalled by the system store; changes when the 187 // synchronized are signalled by the system store; changes when the
181 // CookieStoreIOS is not synchronized are signalled by the appropriate 188 // CookieStoreIOS is not synchronized are signalled by the appropriate
182 // mutators on CookieStoreIOS. The cookie cache tracks the system store when 189 // mutators on CookieStoreIOS. The cookie cache tracks the system store when
183 // the CookieStoreIOS is synchronized and the CookieStore when the 190 // the CookieStoreIOS is synchronized and the CookieStore when the
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 265
259 void UpdateCachesAfterSet(const SetCookiesCallback& callback, bool success); 266 void UpdateCachesAfterSet(const SetCookiesCallback& callback, bool success);
260 void UpdateCachesAfterDelete(const DeleteCallback& callback, int num_deleted); 267 void UpdateCachesAfterDelete(const DeleteCallback& callback, int num_deleted);
261 void UpdateCachesAfterClosure(const base::Closure& callback); 268 void UpdateCachesAfterClosure(const base::Closure& callback);
262 269
263 // Takes an NSArray of NSHTTPCookies as returns a net::CookieList. 270 // Takes an NSArray of NSHTTPCookies as returns a net::CookieList.
264 // The returned cookies are ordered by longest path, then earliest 271 // The returned cookies are ordered by longest path, then earliest
265 // creation date. 272 // creation date.
266 net::CookieList CanonicalCookieListFromSystemCookies(NSArray* cookies); 273 net::CookieList CanonicalCookieListFromSystemCookies(NSArray* cookies);
267 274
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 275 // Cached values of system cookies. Only cookies which have an observer added
279 // with AddCallbackForCookie are kept in this cache. 276 // with AddCallbackForCookie are kept in this cache.
280 std::unique_ptr<CookieCache> cookie_cache_; 277 std::unique_ptr<CookieCache> cookie_cache_;
281 278
282 // Callbacks for cookie changes installed by AddCallbackForCookie. 279 // Callbacks for cookie changes installed by AddCallbackForCookie.
283 std::map<std::pair<GURL, std::string>, 280 std::map<std::pair<GURL, std::string>,
284 std::unique_ptr<CookieChangedCallbackList>> 281 std::unique_ptr<CookieChangedCallbackList>>
285 hook_map_; 282 hook_map_;
286 283
287 base::WeakPtrFactory<CookieStoreIOS> weak_factory_; 284 base::WeakPtrFactory<CookieStoreIOS> weak_factory_;
288 285
289 DISALLOW_COPY_AND_ASSIGN(CookieStoreIOS); 286 DISALLOW_COPY_AND_ASSIGN(CookieStoreIOS);
290 }; 287 };
291 288
292 } // namespace net 289 } // namespace net
293 290
294 #endif // IOS_NET_COOKIES_COOKIE_STORE_IOS_H_ 291 #endif // IOS_NET_COOKIES_COOKIE_STORE_IOS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698