server_socket.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2020 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. #ifndef DEVICE_BLUETOOTH_SERVER_SOCKET_H_
  5. #define DEVICE_BLUETOOTH_SERVER_SOCKET_H_
  6. #include "base/memory/ref_counted.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "device/bluetooth/public/mojom/adapter.mojom.h"
  9. namespace device {
  10. class BluetoothDevice;
  11. class BluetoothSocket;
  12. } // namespace device
  13. namespace bluetooth {
  14. // Implementation of Mojo ServerSocket in
  15. // device/bluetooth/public/mojom/adapter.mojom.
  16. // It handles requests to accept incoming connections from remote devices,
  17. // returning a Socket.
  18. // Uses the platform abstraction of //device/bluetooth.
  19. // An instance of this class is constructed by Adapter. When the instance is
  20. // destroyed, the underlying BluetoothSocket is destroyed.
  21. class ServerSocket : public mojom::ServerSocket {
  22. public:
  23. explicit ServerSocket(
  24. scoped_refptr<device::BluetoothSocket> bluetooth_socket);
  25. ~ServerSocket() override;
  26. ServerSocket(const ServerSocket&) = delete;
  27. ServerSocket& operator=(const ServerSocket&) = delete;
  28. // mojom::ServerSocket:
  29. void Accept(AcceptCallback callback) override;
  30. void Disconnect(DisconnectCallback callback) override;
  31. private:
  32. void OnAccept(AcceptCallback callback,
  33. const device::BluetoothDevice* device,
  34. scoped_refptr<device::BluetoothSocket> socket);
  35. void OnAcceptError(AcceptCallback callback, const std::string& error_message);
  36. scoped_refptr<device::BluetoothSocket> server_socket_;
  37. base::WeakPtrFactory<ServerSocket> weak_ptr_factory_{this};
  38. };
  39. } // namespace bluetooth
  40. #endif // DEVICE_BLUETOOTH_SERVER_SOCKET_H_