fake_bluetooth_input_client.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2013 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 "device/bluetooth/dbus/fake_bluetooth_input_client.h"
  5. #include <map>
  6. #include "base/bind.h"
  7. #include "base/logging.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "base/observer_list.h"
  10. #include "dbus/bus.h"
  11. #include "dbus/message.h"
  12. #include "dbus/object_manager.h"
  13. #include "dbus/object_proxy.h"
  14. #include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
  15. #include "third_party/cros_system_api/dbus/service_constants.h"
  16. namespace bluez {
  17. FakeBluetoothInputClient::Properties::Properties(
  18. const PropertyChangedCallback& callback)
  19. : BluetoothInputClient::Properties(
  20. nullptr,
  21. bluetooth_input::kBluetoothInputInterface,
  22. callback) {}
  23. FakeBluetoothInputClient::Properties::~Properties() = default;
  24. void FakeBluetoothInputClient::Properties::Get(
  25. dbus::PropertyBase* property,
  26. dbus::PropertySet::GetCallback callback) {
  27. DVLOG(1) << "Get " << property->name();
  28. std::move(callback).Run(false);
  29. }
  30. void FakeBluetoothInputClient::Properties::GetAll() {
  31. DVLOG(1) << "GetAll";
  32. }
  33. void FakeBluetoothInputClient::Properties::Set(
  34. dbus::PropertyBase* property,
  35. dbus::PropertySet::SetCallback callback) {
  36. DVLOG(1) << "Set " << property->name();
  37. std::move(callback).Run(false);
  38. }
  39. FakeBluetoothInputClient::FakeBluetoothInputClient() = default;
  40. FakeBluetoothInputClient::~FakeBluetoothInputClient() = default;
  41. void FakeBluetoothInputClient::Init(dbus::Bus* bus,
  42. const std::string& bluetooth_service_name) {
  43. }
  44. void FakeBluetoothInputClient::AddObserver(Observer* observer) {
  45. observers_.AddObserver(observer);
  46. }
  47. void FakeBluetoothInputClient::RemoveObserver(Observer* observer) {
  48. observers_.RemoveObserver(observer);
  49. }
  50. FakeBluetoothInputClient::Properties* FakeBluetoothInputClient::GetProperties(
  51. const dbus::ObjectPath& object_path) {
  52. auto iter = properties_map_.find(object_path);
  53. if (iter != properties_map_.end())
  54. return iter->second.get();
  55. return nullptr;
  56. }
  57. void FakeBluetoothInputClient::AddInputDevice(
  58. const dbus::ObjectPath& object_path) {
  59. if (properties_map_.find(object_path) != properties_map_.end())
  60. return;
  61. std::unique_ptr<Properties> properties = std::make_unique<Properties>(
  62. base::BindRepeating(&FakeBluetoothInputClient::OnPropertyChanged,
  63. base::Unretained(this), object_path));
  64. // The LegacyAutopair and DisplayPinCode devices represent a typical mouse
  65. // and keyboard respectively, so mark them as ReconnectMode "any". The
  66. // DisplayPasskey device represents a Bluetooth 2.1+ keyboard and the
  67. // ConnectUnpairable device represents a pre-standardization mouse, so mark
  68. // them as ReconnectMode "device".
  69. if (object_path.value() == FakeBluetoothDeviceClient::kDisplayPasskeyPath ||
  70. object_path.value() ==
  71. FakeBluetoothDeviceClient::kConnectUnpairablePath) {
  72. properties->reconnect_mode.ReplaceValue(
  73. bluetooth_input::kDeviceReconnectModeProperty);
  74. } else {
  75. properties->reconnect_mode.ReplaceValue(
  76. bluetooth_input::kAnyReconnectModeProperty);
  77. }
  78. properties_map_[object_path] = std::move(properties);
  79. for (auto& observer : observers_)
  80. observer.InputAdded(object_path);
  81. }
  82. void FakeBluetoothInputClient::RemoveInputDevice(
  83. const dbus::ObjectPath& object_path) {
  84. auto it = properties_map_.find(object_path);
  85. if (it == properties_map_.end())
  86. return;
  87. for (auto& observer : observers_)
  88. observer.InputRemoved(object_path);
  89. properties_map_.erase(it);
  90. }
  91. void FakeBluetoothInputClient::OnPropertyChanged(
  92. const dbus::ObjectPath& object_path,
  93. const std::string& property_name) {
  94. for (auto& observer : observers_)
  95. observer.InputPropertyChanged(object_path, property_name);
  96. }
  97. } // namespace bluez