blob: 28006ce3d676d48bd683540c2fa35dc43e10b6a3 [file] [log] [blame]
droger476922e02015-03-10 17:17:521// 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 Ikutaadf31eb2019-01-05 00:32:489#include <unordered_map>
droger476922e02015-03-10 17:17:5210
mrefaat04349b42017-12-06 02:18:1911#include "base/memory/weak_ptr.h"
droger476922e02015-03-10 17:17:5212#include "base/threading/thread_checker.h"
13#include "base/time/time.h"
14
15@class NSHTTPCookie;
16
17namespace net {
18
19// The CookieCreationTimeManager allows to get and set the creation time of a
20// NSHTTPCookie.
mrefaat04349b42017-12-06 02:18:1921// All the methods of CookieCreationTimeManager must be called on the IO thread,
22// except its constructor that can be called from any thread.
droger476922e02015-03-10 17:17:5223// 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.
27class 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();
mrefaat04349b42017-12-06 02:18:1943 // Gets base::WeakPtr to the object to be used in sorting.
44 base::WeakPtr<CookieCreationTimeManager> GetWeakPtr();
droger476922e02015-03-10 17:17:5245
46 private:
Takuto Ikutaadf31eb2019-01-05 00:32:4847 std::unordered_map<std::string, base::Time> creation_times_;
droger476922e02015-03-10 17:17:5248 std::set<base::Time> unique_times_;
49 base::ThreadChecker thread_checker_;
mrefaat04349b42017-12-06 02:18:1950 base::WeakPtrFactory<CookieCreationTimeManager> weak_factory_;
droger476922e02015-03-10 17:17:5251};
52
53} // namespace net
54
55#endif // IOS_NET_COOKIES_COOKIE_CREATION_TIME_MANAGER_H_