socket.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_SOCKET_H_
  5. #define DEVICE_BLUETOOTH_SOCKET_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "device/bluetooth/bluetooth_socket.h"
  11. #include "device/bluetooth/public/mojom/adapter.mojom.h"
  12. #include "mojo/public/cpp/system/data_pipe.h"
  13. #include "mojo/public/cpp/system/simple_watcher.h"
  14. namespace net {
  15. class IOBuffer;
  16. } // namespace net
  17. namespace bluetooth {
  18. // Implementation of Mojo Socket in
  19. // device/bluetooth/public/mojom/adapter.mojom.
  20. // It handles requests to interact with a Socket.
  21. // Uses the platform abstraction of //device/bluetooth.
  22. // An instance of this class is constructed by Adapter and strongly bound to its
  23. // MessagePipe. When the instance is destroyed, the underlying BluetoothSocket
  24. // is destroyed.
  25. class Socket : public mojom::Socket {
  26. public:
  27. Socket(scoped_refptr<device::BluetoothSocket> bluetooth_socket,
  28. mojo::ScopedDataPipeProducerHandle receive_stream,
  29. mojo::ScopedDataPipeConsumerHandle send_stream);
  30. ~Socket() override;
  31. Socket(const Socket&) = delete;
  32. Socket& operator=(const Socket&) = delete;
  33. // mojom::Socket:
  34. void Disconnect(DisconnectCallback callback) override;
  35. private:
  36. // "Receiving" in this context means receiving data from |bluetooth_socket_|
  37. // via BluetoothSocket::Receive() and *writing* it to |receive_stream_|.
  38. void OnReceiveStreamWritable(MojoResult result);
  39. void ShutdownReceive();
  40. void ReceiveMore();
  41. void OnBluetoothSocketReceive(void* pending_write_buffer,
  42. int num_bytes_received,
  43. scoped_refptr<net::IOBuffer> io_buffer);
  44. void OnBluetoothSocketReceiveError(
  45. device::BluetoothSocket::ErrorReason error_reason,
  46. const std::string& error_message);
  47. // "Sending" in this context means *reading* data from |send_stream_| and
  48. // sending it over the |bluetooth_socket_| via BluetoothSocket::Send().
  49. void OnSendStreamReadable(MojoResult result);
  50. void ShutdownSend();
  51. void SendMore();
  52. void OnBluetoothSocketSend(int num_bytes_sent);
  53. void OnBluetoothSocketSendError(const std::string& error_message);
  54. scoped_refptr<device::BluetoothSocket> bluetooth_socket_;
  55. mojo::ScopedDataPipeProducerHandle receive_stream_;
  56. mojo::ScopedDataPipeConsumerHandle send_stream_;
  57. mojo::SimpleWatcher receive_stream_watcher_;
  58. mojo::SimpleWatcher send_stream_watcher_;
  59. base::WeakPtrFactory<Socket> weak_ptr_factory_{this};
  60. };
  61. } // namespace bluetooth
  62. #endif // DEVICE_BLUETOOTH_SOCKET_H_