fake_bluetooth_socket.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2021 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. #include "ash/quick_pair/message_stream/fake_bluetooth_socket.h"
  5. #include "base/strings/string_number_conversions.h"
  6. namespace ash {
  7. namespace quick_pair {
  8. FakeBluetoothSocket::FakeBluetoothSocket() = default;
  9. FakeBluetoothSocket::~FakeBluetoothSocket() = default;
  10. void FakeBluetoothSocket::Receive(
  11. int buffer_size,
  12. ReceiveCompletionCallback success_callback,
  13. ReceiveErrorCompletionCallback error_callback) {
  14. success_callback_ = std::move(success_callback);
  15. error_callback_ = std::move(error_callback);
  16. }
  17. void FakeBluetoothSocket::SetIOBufferFromBytes(std::vector<uint8_t> bytes) {
  18. bytes_ = std::move(bytes);
  19. }
  20. void FakeBluetoothSocket::SetErrorReason(
  21. device::BluetoothSocket::ErrorReason error) {
  22. error_ = error;
  23. }
  24. void FakeBluetoothSocket::SetEmptyBuffer() {
  25. empty_buffer_ = true;
  26. }
  27. void FakeBluetoothSocket::TriggerReceiveCallback() {
  28. if (bytes_.empty() && !empty_buffer_) {
  29. std::move(error_callback_)
  30. .Run(error_,
  31. /*error_message=*/"Error message");
  32. return;
  33. }
  34. std::string buffer_bytes(bytes_.begin(), bytes_.end());
  35. scoped_refptr<net::IOBuffer> io_buffer =
  36. base::MakeRefCounted<net::StringIOBuffer>(buffer_bytes);
  37. if (empty_buffer_) {
  38. io_buffer->data()[0] = '\0';
  39. empty_buffer_ = false;
  40. }
  41. std::move(success_callback_)
  42. .Run(/*buffer_size*/ buffer_bytes.size(),
  43. /*buffer=*/std::move(io_buffer));
  44. }
  45. void FakeBluetoothSocket::Disconnect(base::OnceClosure success_callback) {
  46. std::move(success_callback).Run();
  47. }
  48. } // namespace quick_pair
  49. } // namespace ash