bluetooth_gatt_application_service_provider_impl.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2016 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_DBUS_BLUETOOTH_GATT_APPLICATION_SERVICE_PROVIDER_IMPL_H_
  5. #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_APPLICATION_SERVICE_PROVIDER_IMPL_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/gtest_prod_util.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/threading/platform_thread.h"
  14. #include "dbus/bus.h"
  15. #include "dbus/exported_object.h"
  16. #include "dbus/message.h"
  17. #include "dbus/object_path.h"
  18. #include "device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h"
  19. #include "device/bluetooth/dbus/bluetooth_gatt_application_service_provider.h"
  20. #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider.h"
  21. #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_service_provider.h"
  22. #include "device/bluetooth/dbus/bluetooth_gatt_service_service_provider.h"
  23. namespace bluez {
  24. class BluetoothLocalGattServiceBlueZ;
  25. // The BluetoothGattApplicationServiceProvider implementation used in
  26. // production.
  27. class DEVICE_BLUETOOTH_EXPORT BluetoothGattApplicationServiceProviderImpl
  28. : public BluetoothGattApplicationServiceProvider {
  29. public:
  30. // Use nullptr for |bus| to create for testing.
  31. BluetoothGattApplicationServiceProviderImpl(
  32. dbus::Bus* bus,
  33. const dbus::ObjectPath& object_path,
  34. const std::map<dbus::ObjectPath, BluetoothLocalGattServiceBlueZ*>&
  35. services);
  36. BluetoothGattApplicationServiceProviderImpl(
  37. const BluetoothGattApplicationServiceProviderImpl&) = delete;
  38. BluetoothGattApplicationServiceProviderImpl& operator=(
  39. const BluetoothGattApplicationServiceProviderImpl&) = delete;
  40. ~BluetoothGattApplicationServiceProviderImpl() override;
  41. private:
  42. friend class BluetoothGattApplicationServiceProviderTest;
  43. FRIEND_TEST_ALL_PREFIXES(BluetoothGattApplicationServiceProviderTest,
  44. GetManagedObjects);
  45. // Returns true if the current thread is on the origin thread.
  46. bool OnOriginThread();
  47. template <typename Attribute>
  48. void WriteObjectDict(dbus::MessageWriter* writer,
  49. const std::string& attribute_interface,
  50. Attribute* attribute);
  51. template <typename Attribute>
  52. void WriteInterfaceDict(dbus::MessageWriter* writer,
  53. const std::string& attribute_interface,
  54. Attribute* attribute);
  55. void WriteAttributeProperties(
  56. dbus::MessageWriter* writer,
  57. BluetoothGattServiceServiceProvider* service_provider);
  58. void WriteAttributeProperties(
  59. dbus::MessageWriter* writer,
  60. BluetoothGattCharacteristicServiceProvider* characteristic_provider);
  61. void WriteAttributeProperties(
  62. dbus::MessageWriter* writer,
  63. BluetoothGattDescriptorServiceProvider* descriptor_provider);
  64. // Called by dbus:: when the Bluetooth daemon wants to fetch all the objects
  65. // managed by this object manager.
  66. void GetManagedObjects(dbus::MethodCall* method_call,
  67. dbus::ExportedObject::ResponseSender response_sender);
  68. // Called by dbus:: when a method is exported.
  69. void OnExported(const std::string& interface_name,
  70. const std::string& method_name,
  71. bool success);
  72. // Origin thread (i.e. the UI thread in production).
  73. base::PlatformThreadId origin_thread_id_;
  74. // D-Bus bus object is exported on, not owned by this object and must
  75. // outlive it.
  76. raw_ptr<dbus::Bus> bus_;
  77. // D-Bus object path of object we are exporting, kept so we can unregister
  78. // again in our destructor.
  79. dbus::ObjectPath object_path_;
  80. // D-Bus object we are exporting, owned by this object.
  81. scoped_refptr<dbus::ExportedObject> exported_object_;
  82. // Weak pointer factory for generating 'this' pointers that might live longer
  83. // than we do.
  84. // Note: This should remain the last member so it'll be destroyed and
  85. // invalidate its weak pointers before any other members are destroyed.
  86. base::WeakPtrFactory<BluetoothGattApplicationServiceProviderImpl>
  87. weak_ptr_factory_{this};
  88. };
  89. } // namespace bluez
  90. #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_APPLICATION_SERVICE_PROVIDER_IMPL_H_