blob: dc55a1a81e8aaa5557d85988b15d7173414c6906 [file] [log] [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_NETWORK_DEVICE_BOUND_SESSION_MANAGER_H_
#define SERVICES_NETWORK_DEVICE_BOUND_SESSION_MANAGER_H_
#include <vector>
#include "base/callback_list.h"
#include "base/functional/callback_helpers.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "net/device_bound_sessions/session_display.h"
#include "net/device_bound_sessions/session_error.h"
#include "services/network/public/mojom/device_bound_sessions.mojom.h"
namespace net::device_bound_sessions {
class SessionService;
struct SessionKey;
} // namespace net::device_bound_sessions
namespace network {
class CookieManager;
class COMPONENT_EXPORT(NETWORK_SERVICE) DeviceBoundSessionManager
: public mojom::DeviceBoundSessionManager {
public:
static std::unique_ptr<DeviceBoundSessionManager> Create(
net::device_bound_sessions::SessionService* service,
CookieManager* cookie_manager);
~DeviceBoundSessionManager() override;
void AddReceiver(
mojo::PendingReceiver<mojom::DeviceBoundSessionManager> receiver);
// network::mojom::DeviceBoundSessionManager
void GetAllSessions(GetAllSessionsCallback callback) override;
void DeleteSession(
net::device_bound_sessions::DeletionReason reason,
const net::device_bound_sessions::SessionKey& session_key) override;
void DeleteAllSessions(net::device_bound_sessions::DeletionReason reason,
std::optional<base::Time> created_after_time,
std::optional<base::Time> created_before_time,
network::mojom::ClearDataFilterPtr filter,
base::OnceClosure completion_callback) override;
void AddObserver(
const GURL& url,
mojo::PendingRemote<network::mojom::DeviceBoundSessionAccessObserver>
observer) override;
void AddEventObserver(
mojo::PendingRemote<network::mojom::DeviceBoundSessionEventObserver>
observer) override;
void CreateBoundSessions(
std::vector<net::device_bound_sessions::SessionParams> params,
const std::vector<uint8_t>& wrapped_key,
const std::vector<net::CanonicalCookie>& cookies_to_set,
const net::CookieOptions& cookie_options,
CreateBoundSessionsCallback callback) override;
private:
// State associated with a DeviceBoundSessionAccessObserver.
struct AccessObserverRegistration {
AccessObserverRegistration();
~AccessObserverRegistration();
// Mojo interface
mojo::Remote<network::mojom::DeviceBoundSessionAccessObserver> remote;
// Subscription for inclusion in the SessionService's CallbackList.
base::ScopedClosureRunner subscription;
};
// State associated with a DeviceBoundSessionEventObserver.
struct EventObserverRegistration {
EventObserverRegistration();
~EventObserverRegistration();
// Mojo interface
mojo::Remote<network::mojom::DeviceBoundSessionEventObserver> remote;
// Subscription for inclusion in the SessionService's CallbackList.
base::CallbackListSubscription subscription;
base::WeakPtrFactory<EventObserverRegistration> weak_factory{this};
};
explicit DeviceBoundSessionManager(
net::device_bound_sessions::SessionService* service,
CookieManager* cookie_manager);
// Remove an observer by its registration.
void RemoveAccessObserver(AccessObserverRegistration* registration);
void RemoveEventObserver(EventObserverRegistration* registration);
void PopulateSessionDisplays(
base::WeakPtr<EventObserverRegistration> registration,
const std::vector<net::device_bound_sessions::SessionDisplay>&
session_displays);
void OnCreateBoundSessionsAdded(
const std::vector<net::CanonicalCookie>& cookies_to_set,
const net::CookieOptions& cookie_options,
CreateBoundSessionsCallback callback,
std::vector<net::device_bound_sessions::SessionError::ErrorType>
session_errors);
raw_ptr<net::device_bound_sessions::SessionService> service_;
// `raw_ptr` is safe because both `this` and `cookie_manager_` are
// owned by the `NetworkContext`.
raw_ptr<CookieManager> cookie_manager_;
mojo::ReceiverSet<network::mojom::DeviceBoundSessionManager> receivers_;
std::vector<std::unique_ptr<AccessObserverRegistration>>
access_observer_registrations_;
std::vector<std::unique_ptr<EventObserverRegistration>>
event_observer_registrations_;
base::WeakPtrFactory<DeviceBoundSessionManager> weak_factory_{this};
};
} // namespace network
#endif // SERVICES_NETWORK_DEVICE_BOUND_SESSION_MANAGER_H_