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