• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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_DEVICE_BOUND_SESSIONS_SESSION_SERVICE_IMPL_H_
6 #define NET_DEVICE_BOUND_SESSIONS_SESSION_SERVICE_IMPL_H_
7 
8 #include <map>
9 #include <memory>
10 #include <optional>
11 #include <string>
12 
13 #include "base/memory/weak_ptr.h"
14 #include "net/base/net_export.h"
15 #include "net/device_bound_sessions/registration_fetcher.h"
16 #include "net/device_bound_sessions/registration_fetcher_param.h"
17 #include "net/device_bound_sessions/session.h"
18 #include "net/device_bound_sessions/session_key.h"
19 #include "net/device_bound_sessions/session_service.h"
20 
21 namespace net {
22 class URLRequest;
23 class URLRequestContext;
24 class SchemefulSite;
25 }  // namespace net
26 
27 namespace unexportable_keys {
28 class UnexportableKeyService;
29 }
30 
31 namespace net::device_bound_sessions {
32 
33 class SessionStore;
34 
35 class NET_EXPORT SessionServiceImpl : public SessionService {
36  public:
37   SessionServiceImpl(unexportable_keys::UnexportableKeyService& key_service,
38                      const URLRequestContext* request_context,
39                      SessionStore* store);
40   ~SessionServiceImpl() override;
41 
42   // Loads saved session data from disk if a `SessionStore` object is provided
43   // during construction. Otherwise, it is a no-op.
44   void LoadSessionsAsync();
45 
46   void RegisterBoundSession(OnAccessCallback on_access_callback,
47                             RegistrationFetcherParam registration_params,
48                             const IsolationInfo& isolation_info) override;
49 
50   std::optional<Session::Id> GetAnySessionRequiringDeferral(
51       URLRequest* request) override;
52 
53   void DeferRequestForRefresh(
54       URLRequest* request,
55       Session::Id session_id,
56       RefreshCompleteCallback restart_callback,
57       RefreshCompleteCallback continue_callback) override;
58 
59   void SetChallengeForBoundSession(OnAccessCallback on_access_callback,
60                                    const GURL& request_url,
61                                    const SessionChallengeParam& param) override;
62 
63   void GetAllSessionsAsync(
64       base::OnceCallback<void(const std::vector<SessionKey>&)> callback)
65       override;
66   void DeleteSession(const SchemefulSite& site, const Session::Id& id) override;
67 
68   Session* GetSessionForTesting(const SchemefulSite& site,
69                                 const std::string& session_id) const;
70 
71  private:
72   friend class SessionServiceImplWithStoreTest;
73 
74   // The key is the site (eTLD+1) of the session's origin.
75   using SessionsMap = std::multimap<SchemefulSite, std::unique_ptr<Session>>;
76 
77   void OnLoadSessionsComplete(SessionsMap sessions);
78 
79   void OnRegistrationComplete(
80       OnAccessCallback on_access_callback,
81       std::optional<RegistrationFetcher::RegistrationCompleteParams> params);
82 
83   void StartSessionRefresh(
84       const Session& session,
85       const IsolationInfo& isolation_info,
86       // TODO(crbug.com/353764893): Replace this callback placeholder.
87       OnAccessCallback on_access_callback);
88   void AddSession(const SchemefulSite& site, std::unique_ptr<Session> session);
89 
90   // Get all the unexpired sessions for a given site. This also removes
91   // expired sessions for the site and extends the TTL of used sessions.
92   std::pair<SessionsMap::iterator, SessionsMap::iterator> GetSessionsForSite(
93       const SchemefulSite& site);
94 
95   // Remove a session from the session map. It also clears the session from
96   // `session_store_` and any BFCache entries.
97   // Return the iterator to the next session in the map.
98   [[nodiscard]] SessionsMap::iterator DeleteSessionInternal(
99       const SchemefulSite& site,
100       SessionsMap::iterator it);
101 
102   // Whether we are waiting on the initial load of saved sessions to complete.
103   bool pending_initialization_ = false;
104   // Functions to call once initialization completes.
105   std::vector<base::OnceClosure> queued_operations_;
106 
107   const raw_ref<unexportable_keys::UnexportableKeyService> key_service_;
108   raw_ptr<const URLRequestContext> context_;
109   raw_ptr<SessionStore> session_store_ = nullptr;
110 
111   // Storage is similar to how CookieMonster stores its cookies.
112   SessionsMap unpartitioned_sessions_;
113 
114   base::WeakPtrFactory<SessionServiceImpl> weak_factory_{this};
115 };
116 
117 }  // namespace net::device_bound_sessions
118 
119 #endif  // NET_DEVICE_BOUND_SESSIONS_SESSION_SERVICE_IMPL_H_
120