Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 |
| 134 private: | 130 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 { | 131 enum SynchronizationState { |
|
Eugene But (OOO till 7-30)
2017/01/24 22:31:30
Do we still need this enum?
maksims (do not use this acc)
2017/01/27 12:46:36
Done.
| |
| 143 NOT_SYNCHRONIZED, // Uses CookieMonster as backend. | 132 NOT_SYNCHRONIZED, // Uses CookieMonster as backend. |
| 144 SYNCHRONIZED // Uses NSHTTPCookieStorage as backend. | 133 SYNCHRONIZED // Uses NSHTTPCookieStorage as backend. |
| 145 }; | 134 }; |
| 146 | 135 |
| 147 // Cookie fliter for DeleteCookiesWithFilter(). | 136 CookieStoreIOS(net::CookieMonster::PersistentCookieStore* persistent_store, |
| 148 // Takes a cookie and a creation time and returns true if the cookie must be | 137 NSHTTPCookieStorage* system_store); |
| 138 | |
| 139 // These three functions are used for wrapping user-supplied callbacks given | |
| 140 // to CookieStoreIOS mutator methods. Given a callback, they return a new | |
| 141 // callback that invokes UpdateCachesFromCookieMonster() to schedule an | |
| 142 // asynchronous synchronization of the cookie cache and then calls the | |
| 143 // original callback. | |
| 144 SetCookiesCallback WrapSetCallback(const SetCookiesCallback& callback); | |
| 145 DeleteCallback WrapDeleteCallback(const DeleteCallback& callback); | |
| 146 base::Closure WrapClosure(const base::Closure& callback); | |
| 147 | |
| 148 bool metrics_enabled() { return metrics_enabled_; } | |
| 149 | |
| 150 net::CookieMonster* cookie_monster() { return cookie_monster_.get(); } | |
| 151 | |
| 152 const base::ThreadChecker& thread_checker() { return thread_checker_; } | |
| 153 | |
| 154 private: | |
| 155 // For tests. | |
| 156 friend struct CookieStoreIOSTestTraits; | |
|
Eugene But (OOO till 7-30)
2017/01/24 22:31:30
Do we still need this friend? Looks like you remov
maksims (do not use this acc)
2017/01/27 12:46:36
Done.
| |
| 157 | |
| 158 // Cookie filter for DeleteCookiesWithFilter(). | |
| 159 // Takes a cookie and a creation time and returns true the cookie must be | |
| 149 // deleted. | 160 // deleted. |
| 150 typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction; | 161 typedef base::Callback<bool(NSHTTPCookie*, base::Time)> CookieFilterFunction; |
| 151 | 162 |
| 152 // Clears the system cookie store. | 163 // Clears the system cookie store. |
| 153 void ClearSystemStore(); | 164 void ClearSystemStore(); |
| 154 // Returns true if the system cookie store policy is | 165 // Returns true if the system cookie store policy is |
| 155 // |NSHTTPCookieAcceptPolicyAlways|. | 166 // |NSHTTPCookieAcceptPolicyAlways|. |
| 156 bool SystemCookiesAllowed(); | 167 bool SystemCookiesAllowed(); |
| 157 // Copies the cookies to the backing CookieMonster. If the cookie store is not | 168 // Copies the cookies to the backing CookieMonster. If the cookie store is not |
| 158 // synchronized with the system store, this is a no-op. | 169 // synchronized with the system store, this is a no-op. |
| 159 void WriteToCookieMonster(NSArray* system_cookies); | 170 void WriteToCookieMonster(NSArray* system_cookies); |
| 160 | 171 |
| 161 // Inherited CookieNotificationObserver methods. | 172 // Inherited CookieNotificationObserver methods. |
| 162 void OnSystemCookiesChanged() override; | 173 void OnSystemCookiesChanged() override; |
| 163 | 174 |
| 164 void DeleteCookiesWithFilter(const CookieFilterFunction& filter, | 175 void DeleteCookiesWithFilter(const CookieFilterFunction& filter, |
| 165 const DeleteCallback& callback); | 176 const DeleteCallback& callback); |
| 166 | 177 |
| 167 std::unique_ptr<net::CookieMonster> cookie_monster_; | 178 std::unique_ptr<net::CookieMonster> cookie_monster_; |
| 168 base::scoped_nsobject<NSHTTPCookieStorage> system_store_; | 179 base::scoped_nsobject<NSHTTPCookieStorage> system_store_; |
| 169 std::unique_ptr<CookieCreationTimeManager> creation_time_manager_; | 180 std::unique_ptr<CookieCreationTimeManager> creation_time_manager_; |
| 170 bool metrics_enabled_; | 181 bool metrics_enabled_; |
| 171 base::CancelableClosure flush_closure_; | 182 base::CancelableClosure flush_closure_; |
| 172 | 183 |
| 173 SynchronizationState synchronization_state_; | |
| 174 | |
| 175 base::ThreadChecker thread_checker_; | 184 base::ThreadChecker thread_checker_; |
| 176 | 185 |
| 177 // Cookie notification methods. | 186 // Cookie notification methods. |
| 178 // The cookie cache is updated from both the system store and the | 187 // The cookie cache is updated from both the system store and the |
| 179 // CookieStoreIOS' own mutators. Changes when the CookieStoreIOS is | 188 // CookieStoreIOS' own mutators. Changes when the CookieStoreIOS is |
| 180 // synchronized are signalled by the system store; changes when the | 189 // synchronized are signalled by the system store; changes when the |
| 181 // CookieStoreIOS is not synchronized are signalled by the appropriate | 190 // CookieStoreIOS is not synchronized are signalled by the appropriate |
| 182 // mutators on CookieStoreIOS. The cookie cache tracks the system store when | 191 // mutators on CookieStoreIOS. The cookie cache tracks the system store when |
| 183 // the CookieStoreIOS is synchronized and the CookieStore when the | 192 // the CookieStoreIOS is synchronized and the CookieStore when the |
| 184 // CookieStoreIOS is not synchronized. | 193 // CookieStoreIOS is not synchronized. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 | 267 |
| 259 void UpdateCachesAfterSet(const SetCookiesCallback& callback, bool success); | 268 void UpdateCachesAfterSet(const SetCookiesCallback& callback, bool success); |
| 260 void UpdateCachesAfterDelete(const DeleteCallback& callback, int num_deleted); | 269 void UpdateCachesAfterDelete(const DeleteCallback& callback, int num_deleted); |
| 261 void UpdateCachesAfterClosure(const base::Closure& callback); | 270 void UpdateCachesAfterClosure(const base::Closure& callback); |
| 262 | 271 |
| 263 // Takes an NSArray of NSHTTPCookies as returns a net::CookieList. | 272 // Takes an NSArray of NSHTTPCookies as returns a net::CookieList. |
| 264 // The returned cookies are ordered by longest path, then earliest | 273 // The returned cookies are ordered by longest path, then earliest |
| 265 // creation date. | 274 // creation date. |
| 266 net::CookieList CanonicalCookieListFromSystemCookies(NSArray* cookies); | 275 net::CookieList CanonicalCookieListFromSystemCookies(NSArray* cookies); |
| 267 | 276 |
| 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 | 277 // Cached values of system cookies. Only cookies which have an observer added |
| 279 // with AddCallbackForCookie are kept in this cache. | 278 // with AddCallbackForCookie are kept in this cache. |
| 280 std::unique_ptr<CookieCache> cookie_cache_; | 279 std::unique_ptr<CookieCache> cookie_cache_; |
| 281 | 280 |
| 282 // Callbacks for cookie changes installed by AddCallbackForCookie. | 281 // Callbacks for cookie changes installed by AddCallbackForCookie. |
| 283 std::map<std::pair<GURL, std::string>, | 282 std::map<std::pair<GURL, std::string>, |
| 284 std::unique_ptr<CookieChangedCallbackList>> | 283 std::unique_ptr<CookieChangedCallbackList>> |
| 285 hook_map_; | 284 hook_map_; |
| 286 | 285 |
| 287 base::WeakPtrFactory<CookieStoreIOS> weak_factory_; | 286 base::WeakPtrFactory<CookieStoreIOS> weak_factory_; |
| 288 | 287 |
| 289 DISALLOW_COPY_AND_ASSIGN(CookieStoreIOS); | 288 DISALLOW_COPY_AND_ASSIGN(CookieStoreIOS); |
| 290 }; | 289 }; |
| 291 | 290 |
| 292 } // namespace net | 291 } // namespace net |
| 293 | 292 |
| 294 #endif // IOS_NET_COOKIES_COOKIE_STORE_IOS_H_ | 293 #endif // IOS_NET_COOKIES_COOKIE_STORE_IOS_H_ |
| OLD | NEW |