droger | 476922e0 | 2015-03-10 17:17:52 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef IOS_NET_COOKIES_COOKIE_CREATION_TIME_MANAGER_H_ |
| 6 | #define IOS_NET_COOKIES_COOKIE_CREATION_TIME_MANAGER_H_ |
| 7 | |
| 8 | #include <set> |
Takuto Ikuta | adf31eb | 2019-01-05 00:32:48 | [diff] [blame] | 9 | #include <unordered_map> |
droger | 476922e0 | 2015-03-10 17:17:52 | [diff] [blame] | 10 | |
mrefaat | 04349b4 | 2017-12-06 02:18:19 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
droger | 476922e0 | 2015-03-10 17:17:52 | [diff] [blame] | 12 | #include "base/threading/thread_checker.h" |
| 13 | #include "base/time/time.h" |
| 14 | |
| 15 | @class NSHTTPCookie; |
| 16 | |
| 17 | namespace net { |
| 18 | |
| 19 | // The CookieCreationTimeManager allows to get and set the creation time of a |
| 20 | // NSHTTPCookie. |
mrefaat | 04349b4 | 2017-12-06 02:18:19 | [diff] [blame] | 21 | // All the methods of CookieCreationTimeManager must be called on the IO thread, |
| 22 | // except its constructor that can be called from any thread. |
droger | 476922e0 | 2015-03-10 17:17:52 | [diff] [blame] | 23 | // Creation time data is stored either in the cookie properties (when the cookie |
| 24 | // is created by the system) or in |creation_times_| (when the cookie is created |
| 25 | // by CookieStoreIOS). When both are available, |creation_times_| is used, |
| 26 | // because the cookie property only has a one second precision. |
| 27 | class CookieCreationTimeManager { |
| 28 | public: |
| 29 | CookieCreationTimeManager(); |
| 30 | ~CookieCreationTimeManager(); |
| 31 | // Sets the creation time for |cookie|. |
| 32 | // |creation_time| must be unique (not used by another cookie). |
| 33 | void SetCreationTime(NSHTTPCookie* cookie, const base::Time& creation_time); |
| 34 | // Creates a unique creation time (to be used in SetCreationTime()) that is |
| 35 | // as close as possible to |creation_time|. |
| 36 | base::Time MakeUniqueCreationTime(const base::Time& creation_time); |
| 37 | // Gets the creation time for |cookie|. |
| 38 | base::Time GetCreationTime(NSHTTPCookie* cookie); |
| 39 | // Deletes the creation time for |cookie|. |
| 40 | void DeleteCreationTime(NSHTTPCookie* cookie); |
| 41 | // Clears all the creation times. |
| 42 | void Clear(); |
mrefaat | 04349b4 | 2017-12-06 02:18:19 | [diff] [blame] | 43 | // Gets base::WeakPtr to the object to be used in sorting. |
| 44 | base::WeakPtr<CookieCreationTimeManager> GetWeakPtr(); |
droger | 476922e0 | 2015-03-10 17:17:52 | [diff] [blame] | 45 | |
| 46 | private: |
Takuto Ikuta | adf31eb | 2019-01-05 00:32:48 | [diff] [blame] | 47 | std::unordered_map<std::string, base::Time> creation_times_; |
droger | 476922e0 | 2015-03-10 17:17:52 | [diff] [blame] | 48 | std::set<base::Time> unique_times_; |
| 49 | base::ThreadChecker thread_checker_; |
mrefaat | 04349b4 | 2017-12-06 02:18:19 | [diff] [blame] | 50 | base::WeakPtrFactory<CookieCreationTimeManager> weak_factory_; |
droger | 476922e0 | 2015-03-10 17:17:52 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | } // namespace net |
| 54 | |
| 55 | #endif // IOS_NET_COOKIES_COOKIE_CREATION_TIME_MANAGER_H_ |