Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 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 NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_COOKIE_STORE_H_ |
| 6 | #define NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_COOKIE_STORE_H_ |
| 7 | |
| 8 | #include <list> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
Francois Doray | 45bdc4c3 | 2018-09-17 19:35:00 | [diff] [blame] | 13 | #include "base/component_export.h" |
Avi Drissman | 41c4a41 | 2023-01-11 22:45:37 | [diff] [blame] | 14 | #include "base/functional/callback_forward.h" |
Lei Zhang | 58a1082 | 2022-11-08 04:23:20 | [diff] [blame] | 15 | #include "base/memory/scoped_refptr.h" |
Etienne Pierre-Doray | 87bb331b | 2018-09-18 17:27:12 | [diff] [blame] | 16 | #include "base/task/task_traits.h" |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 17 | #include "net/cookies/cookie_monster.h" |
Will Harris | 75362d7 | 2024-01-12 19:14:16 | [diff] [blame] | 18 | #include "net/extras/sqlite/cookie_crypto_delegate.h" |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 19 | #include "net/log/net_log_with_source.h" |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 20 | |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 21 | namespace base { |
| 22 | class FilePath; |
| 23 | class SequencedTaskRunner; |
| 24 | } // namespace base |
| 25 | |
| 26 | namespace net { |
| 27 | class CanonicalCookie; |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 28 | |
Etienne Pierre-Doray | 87bb331b | 2018-09-18 17:27:12 | [diff] [blame] | 29 | // Returns recommended task priority for |background_task_runner|. |
| 30 | base::TaskPriority COMPONENT_EXPORT(NET_EXTRAS) |
| 31 | GetCookieStoreBackgroundSequencePriority(); |
| 32 | |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 33 | // Implements the PersistentCookieStore interface in terms of a SQLite database. |
| 34 | // For documentation about the actual member functions consult the documentation |
| 35 | // of the parent class |CookieMonster::PersistentCookieStore|. |
Francois Doray | 45bdc4c3 | 2018-09-17 19:35:00 | [diff] [blame] | 36 | class COMPONENT_EXPORT(NET_EXTRAS) SQLitePersistentCookieStore |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 37 | : public CookieMonster::PersistentCookieStore { |
| 38 | public: |
| 39 | // Contains the origin and a bool indicating whether or not the |
| 40 | // origin is secure. |
| 41 | typedef std::pair<std::string, bool> CookieOrigin; |
| 42 | |
Dylan Cutler | c880aeb | 2021-09-15 20:35:27 | [diff] [blame] | 43 | // Port number to use for cookies whose source port is unknown at the time of |
| 44 | // database migration to V13. The value -1 comes from url::PORT_UNSPECIFIED. |
Peter Kasting | a622c5f | 2023-10-17 00:00:55 | [diff] [blame] | 45 | static constexpr int kDefaultUnknownPort = -1; |
Dylan Cutler | c880aeb | 2021-09-15 20:35:27 | [diff] [blame] | 46 | |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 47 | // All blocking database accesses will be performed on |
| 48 | // |background_task_runner|, while |client_task_runner| is used to invoke |
Will Harris | 711a5ec | 2023-04-04 21:43:37 | [diff] [blame] | 49 | // callbacks. If |enable_exclusive_access| is set to true then sqlite will |
| 50 | // be asked to open the database with flag `exclusive=1`. In practice, this is |
| 51 | // only respected on Windows. |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 52 | SQLitePersistentCookieStore( |
| 53 | const base::FilePath& path, |
| 54 | const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
| 55 | const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
| 56 | bool restore_old_session_cookies, |
Will Harris | 75362d7 | 2024-01-12 19:14:16 | [diff] [blame] | 57 | std::unique_ptr<CookieCryptoDelegate> crypto_delegate, |
Will Harris | 711a5ec | 2023-04-04 21:43:37 | [diff] [blame] | 58 | bool enable_exclusive_access); |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 59 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 60 | SQLitePersistentCookieStore(const SQLitePersistentCookieStore&) = delete; |
| 61 | SQLitePersistentCookieStore& operator=(const SQLitePersistentCookieStore&) = |
| 62 | delete; |
| 63 | |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 64 | // Deletes the cookies whose origins match those given in |cookies|. |
| 65 | void DeleteAllInList(const std::list<CookieOrigin>& cookies); |
| 66 | |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 67 | // CookieMonster::PersistentCookieStore: |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 68 | void Load(LoadedCallback loaded_callback, |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 69 | const NetLogWithSource& net_log) override; |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 70 | void LoadCookiesForKey(const std::string& key, |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 71 | LoadedCallback callback) override; |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 72 | void AddCookie(const CanonicalCookie& cc) override; |
| 73 | void UpdateCookieAccessTime(const CanonicalCookie& cc) override; |
| 74 | void DeleteCookie(const CanonicalCookie& cc) override; |
| 75 | void SetForceKeepSessionState() override; |
Lily Chen | 9934f7e | 2019-03-13 19:16:55 | [diff] [blame] | 76 | void SetBeforeCommitCallback(base::RepeatingClosure callback) override; |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 77 | void Flush(base::OnceClosure callback) override; |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 78 | |
Maks Orlovich | 1a9ffad8 | 2018-09-14 23:05:53 | [diff] [blame] | 79 | // Returns how many operations are currently queued. For test use only; |
| 80 | // and the background thread needs to be wedged for accessing this to be |
| 81 | // non-racey. Also requires the client thread to be current. |
| 82 | size_t GetQueueLengthForTesting(); |
| 83 | |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 84 | private: |
| 85 | ~SQLitePersistentCookieStore() override; |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 86 | void CompleteLoad(LoadedCallback callback, |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 87 | std::vector<std::unique_ptr<CanonicalCookie>> cookie_list); |
| 88 | void CompleteKeyedLoad( |
| 89 | const std::string& key, |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 90 | LoadedCallback callback, |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 91 | std::vector<std::unique_ptr<CanonicalCookie>> cookie_list); |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 92 | |
| 93 | class Backend; |
| 94 | |
Helen Li | edd838a | 2018-08-14 23:27:29 | [diff] [blame] | 95 | const scoped_refptr<Backend> backend_; |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 96 | NetLogWithSource net_log_; |
rohitrao | 1860223c | 2015-05-16 01:07:14 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace net |
| 100 | |
| 101 | #endif // NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_COOKIE_STORE_H_ |