xref: /aosp_15_r20/external/cronet/net/extras/sqlite/sqlite_persistent_cookie_store.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 
13 #include "base/component_export.h"
14 #include "base/functional/callback_forward.h"
15 #include "base/memory/scoped_refptr.h"
16 #include "base/task/task_traits.h"
17 #include "net/cookies/cookie_monster.h"
18 #include "net/extras/sqlite/cookie_crypto_delegate.h"
19 #include "net/log/net_log_with_source.h"
20 
21 namespace base {
22 class FilePath;
23 class SequencedTaskRunner;
24 }  // namespace base
25 
26 namespace net {
27 class CanonicalCookie;
28 
29 // Returns recommended task priority for |background_task_runner|.
30 base::TaskPriority COMPONENT_EXPORT(NET_EXTRAS)
31     GetCookieStoreBackgroundSequencePriority();
32 
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|.
COMPONENT_EXPORT(NET_EXTRAS)36 class COMPONENT_EXPORT(NET_EXTRAS) SQLitePersistentCookieStore
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 
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.
45   static constexpr int kDefaultUnknownPort = -1;
46 
47   // All blocking database accesses will be performed on
48   // |background_task_runner|, while |client_task_runner| is used to invoke
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.
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,
57       std::unique_ptr<CookieCryptoDelegate> crypto_delegate,
58       bool enable_exclusive_access);
59 
60   SQLitePersistentCookieStore(const SQLitePersistentCookieStore&) = delete;
61   SQLitePersistentCookieStore& operator=(const SQLitePersistentCookieStore&) =
62       delete;
63 
64   // Deletes the cookies whose origins match those given in |cookies|.
65   void DeleteAllInList(const std::list<CookieOrigin>& cookies);
66 
67   // CookieMonster::PersistentCookieStore:
68   void Load(LoadedCallback loaded_callback,
69             const NetLogWithSource& net_log) override;
70   void LoadCookiesForKey(const std::string& key,
71                          LoadedCallback callback) override;
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;
76   void SetBeforeCommitCallback(base::RepeatingClosure callback) override;
77   void Flush(base::OnceClosure callback) override;
78 
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 
84  private:
85   ~SQLitePersistentCookieStore() override;
86   void CompleteLoad(LoadedCallback callback,
87                     std::vector<std::unique_ptr<CanonicalCookie>> cookie_list);
88   void CompleteKeyedLoad(
89       const std::string& key,
90       LoadedCallback callback,
91       std::vector<std::unique_ptr<CanonicalCookie>> cookie_list);
92 
93   class Backend;
94 
95   const scoped_refptr<Backend> backend_;
96   NetLogWithSource net_log_;
97 };
98 
99 }  // namespace net
100 
101 #endif  // NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_COOKIE_STORE_H_
102