bluetooth_gatt_service_service_provider_impl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_SERVICE_SERVICE_PROVIDER_IMPL_H_
  5. #define DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_SERVICE_SERVICE_PROVIDER_IMPL_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/threading/platform_thread.h"
  12. #include "dbus/bus.h"
  13. #include "dbus/exported_object.h"
  14. #include "dbus/message.h"
  15. #include "dbus/object_path.h"
  16. #include "device/bluetooth/bluetooth_export.h"
  17. #include "device/bluetooth/dbus/bluetooth_gatt_service_service_provider.h"
  18. namespace bluez {
  19. // The BluetoothGattServiceServiceProvider implementation used in production.
  20. class DEVICE_BLUETOOTH_EXPORT BluetoothGattServiceServiceProviderImpl
  21. : public BluetoothGattServiceServiceProvider {
  22. public:
  23. // Use nullptr for |bus| to create for testing.
  24. BluetoothGattServiceServiceProviderImpl(
  25. dbus::Bus* bus,
  26. const dbus::ObjectPath& object_path,
  27. const std::string& uuid,
  28. bool is_primary,
  29. const std::vector<dbus::ObjectPath>& includes);
  30. BluetoothGattServiceServiceProviderImpl(
  31. const BluetoothGattServiceServiceProviderImpl&) = delete;
  32. BluetoothGattServiceServiceProviderImpl& operator=(
  33. const BluetoothGattServiceServiceProviderImpl&) = delete;
  34. ~BluetoothGattServiceServiceProviderImpl() override;
  35. private:
  36. // Returns true if the current thread is on the origin thread.
  37. bool OnOriginThread();
  38. // Called by dbus:: when the Bluetooth daemon fetches a single property of
  39. // the service.
  40. void Get(dbus::MethodCall* method_call,
  41. dbus::ExportedObject::ResponseSender response_sender);
  42. // Called by dbus:: when the Bluetooth daemon sets a single property of the
  43. // service.
  44. void Set(dbus::MethodCall* method_call,
  45. dbus::ExportedObject::ResponseSender response_sender);
  46. // Called by dbus:: when the Bluetooth daemon fetches all properties of the
  47. // service.
  48. void GetAll(dbus::MethodCall* method_call,
  49. dbus::ExportedObject::ResponseSender response_sender);
  50. void WriteProperties(dbus::MessageWriter* writer) override;
  51. // Called by dbus:: when a method is exported.
  52. void OnExported(const std::string& interface_name,
  53. const std::string& method_name,
  54. bool success);
  55. const dbus::ObjectPath& object_path() const override;
  56. // Origin thread (i.e. the UI thread in production).
  57. base::PlatformThreadId origin_thread_id_;
  58. // 128-bit service UUID of this object.
  59. std::string uuid_;
  60. // Flag indicating that this is a primary service.
  61. bool is_primary_;
  62. // List of object paths that represent other exported GATT services that are
  63. // included from this service.
  64. std::vector<dbus::ObjectPath> includes_;
  65. // D-Bus bus object is exported on, not owned by this object and must
  66. // outlive it.
  67. raw_ptr<dbus::Bus> bus_;
  68. // D-Bus object path of object we are exporting, kept so we can unregister
  69. // again in our destructor.
  70. dbus::ObjectPath object_path_;
  71. // D-Bus object we are exporting, owned by this object.
  72. scoped_refptr<dbus::ExportedObject> exported_object_;
  73. // Weak pointer factory for generating 'this' pointers that might live longer
  74. // than we do.
  75. // Note: This should remain the last member so it'll be destroyed and
  76. // invalidate its weak pointers before any other members are destroyed.
  77. base::WeakPtrFactory<BluetoothGattServiceServiceProviderImpl>
  78. weak_ptr_factory_{this};
  79. };
  80. } // namespace bluez
  81. #endif // DEVICE_BLUETOOTH_DBUS_BLUETOOTH_GATT_SERVICE_SERVICE_PROVIDER_IMPL_H_