bluez_dbus_manager.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright (c) 2012 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_BLUEZ_DBUS_MANAGER_H_
  5. #define DEVICE_BLUETOOTH_DBUS_BLUEZ_DBUS_MANAGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "device/bluetooth/bluetooth_export.h"
  13. #include "device/bluetooth/dbus/bluetooth_dbus_client_bundle.h"
  14. namespace dbus {
  15. class Bus;
  16. class Response;
  17. class ErrorResponse;
  18. } // namespace dbus
  19. namespace floss {
  20. class FlossManagerClient;
  21. }
  22. namespace bluez {
  23. // Style Note: Clients are sorted by names.
  24. class BluetoothAdapterClient;
  25. class BluetoothAdminPolicyClient;
  26. class BluetoothAdvertisementMonitorManagerClient;
  27. class BluetoothAgentManagerClient;
  28. class BluetoothBatteryClient;
  29. class BluetoothDebugManagerClient;
  30. class BluetoothDeviceClient;
  31. class BluetoothGattCharacteristicClient;
  32. class BluetoothGattDescriptorClient;
  33. class BluetoothGattManagerClient;
  34. class BluetoothGattServiceClient;
  35. class BluetoothInputClient;
  36. class BluetoothLEAdvertisingManagerClient;
  37. class BluetoothMediaClient;
  38. class BluetoothMediaTransportClient;
  39. class BluetoothProfileManagerClient;
  40. class BluezDBusManagerSetter;
  41. // BluezDBusManager manages manages D-Bus connections and D-Bus clients, which
  42. // depend on the D-Bus thread to ensure the right order of shutdowns for
  43. // the D-Bus thread, the D-Bus connections, and the D-Bus clients.
  44. //
  45. // CALLBACKS IN D-BUS CLIENTS:
  46. //
  47. // D-Bus clients managed by BluezDBusManagerSetter are guaranteed to be deleted
  48. // after the D-Bus thread so the clients don't need to worry if new
  49. // incoming messages arrive from the D-Bus thread during shutdown of the
  50. // clients. The UI message loop is not running during the shutdown hence
  51. // the UI message loop won't post tasks to D-BUS clients during the
  52. // shutdown. However, to be extra cautious, clients should use
  53. // WeakPtrFactory when creating callbacks that run on UI thread. See
  54. // session_manager_client.cc for examples.
  55. //
  56. // Alternate D-Bus Client:
  57. //
  58. // BluezDBusManager is used by two separate clients. If both clients used the
  59. // same DBus connection to talk to BlueZ, then they could override each others'
  60. // state. For example, clients can start a scan with a set of filters; if
  61. // client #1 sets filter A, and then client #2 sets filter B, BlueZ would only
  62. // scan with filter B. BlueZ distinguishes between clients based on their D-Bus
  63. // connection, so if two clients with different connections try to start a scan
  64. // with two filters, BlueZ will merge these filters.
  65. //
  66. // For this reason, BluezDBusManager keeps two sets of the same client and uses
  67. // two separate D-Bus connections: "Bluetooth*Client" and
  68. // "AlternateBluetooth*Client".
  69. class DEVICE_BLUETOOTH_EXPORT BluezDBusManager {
  70. public:
  71. // Initializes the global instance with a real client. Must be called before
  72. // any calls to Get(). We explicitly initialize and shutdown the global object
  73. // rather than making it a Singleton to ensure clean startup and shutdown.
  74. // * On Chrome OS, |system_bus| must not be null. It will be used as the
  75. // primary bus and a secondary bus will be created.
  76. // * On Linux, |system_bus| is ignored and a primary bus only will be created
  77. // and used.
  78. static void Initialize(dbus::Bus* system_bus);
  79. // Initializes the global instance with a fake client.
  80. static void InitializeFake();
  81. // Returns a BluezDBusManagerSetter instance that allows tests to
  82. // replace individual D-Bus clients with their own implementations.
  83. // Also initializes the main BluezDBusManager for testing if necessary.
  84. static std::unique_ptr<BluezDBusManagerSetter> GetSetterForTesting();
  85. // Returns true if BluezDBusManager has been initialized. Call this to
  86. // avoid initializing + shutting down BluezDBusManager more than once.
  87. static bool IsInitialized();
  88. // Destroys the global instance.
  89. static void Shutdown();
  90. // Gets the global instance. Initialize() must be called first.
  91. static BluezDBusManager* Get();
  92. BluezDBusManager(const BluezDBusManager&) = delete;
  93. BluezDBusManager& operator=(const BluezDBusManager&) = delete;
  94. // Returns various D-Bus bus instances, owned by BluezDBusManager.
  95. dbus::Bus* GetSystemBus();
  96. // Returns true once we know whether Object Manager is supported or not.
  97. // Until this method returns true, no classes should try to use the
  98. // D-Bus Clients.
  99. bool IsObjectManagerSupportKnown() { return object_manager_support_known_; }
  100. // Calls |callback| once we know whether Object Manager is supported or not.
  101. void CallWhenObjectManagerSupportIsKnown(base::OnceClosure callback);
  102. // Returns true if Object Manager is supported.
  103. bool IsObjectManagerSupported() { return object_manager_supported_; }
  104. // Returns true if |client| is fake.
  105. bool IsUsingFakes() { return client_bundle_->IsUsingFakes(); }
  106. // All returned objects are owned by BluezDBusManager. Do not use these
  107. // pointers after BluezDBusManager has been shut down.
  108. BluetoothAdapterClient* GetBluetoothAdapterClient();
  109. BluetoothAdminPolicyClient* GetBluetoothAdminPolicyClient();
  110. BluetoothAdvertisementMonitorManagerClient*
  111. GetBluetoothAdvertisementMonitorManagerClient();
  112. BluetoothLEAdvertisingManagerClient* GetBluetoothLEAdvertisingManagerClient();
  113. BluetoothAgentManagerClient* GetBluetoothAgentManagerClient();
  114. BluetoothBatteryClient* GetBluetoothBatteryClient();
  115. BluetoothDebugManagerClient* GetBluetoothDebugManagerClient();
  116. BluetoothDeviceClient* GetBluetoothDeviceClient();
  117. BluetoothGattCharacteristicClient* GetBluetoothGattCharacteristicClient();
  118. BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient();
  119. BluetoothGattManagerClient* GetBluetoothGattManagerClient();
  120. BluetoothGattServiceClient* GetBluetoothGattServiceClient();
  121. BluetoothInputClient* GetBluetoothInputClient();
  122. BluetoothMediaClient* GetBluetoothMediaClient();
  123. BluetoothMediaTransportClient* GetBluetoothMediaTransportClient();
  124. BluetoothProfileManagerClient* GetBluetoothProfileManagerClient();
  125. // See "Alternate D-Bus Client" note above.
  126. BluetoothAdapterClient* GetAlternateBluetoothAdapterClient();
  127. BluetoothAdminPolicyClient* GetAlternateBluetoothAdminPolicyClient();
  128. BluetoothDeviceClient* GetAlternateBluetoothDeviceClient();
  129. private:
  130. friend class BluezDBusManagerSetter;
  131. // Creates a new BluezDBusManager using the DBusClients set in
  132. // |client_bundle|. |alternate_bus| is used by a separate set of D-Bus
  133. // clients; see "Alternate D-Bus Client" note above.
  134. explicit BluezDBusManager(dbus::Bus* bus,
  135. dbus::Bus* alternate_bus,
  136. bool use_stubs);
  137. ~BluezDBusManager();
  138. // Creates a global instance of BluezDBusManager. Cannot be called more than
  139. // once.
  140. static void CreateGlobalInstance(dbus::Bus* bus,
  141. dbus::Bus* alternate_bus,
  142. bool use_stubs);
  143. void OnObjectManagerSupported(dbus::Response* response);
  144. void OnObjectManagerNotSupported(dbus::ErrorResponse* response);
  145. void OnFlossObjectManagerSupported(dbus::Response* response);
  146. void OnFlossObjectManagerNotSupported(dbus::ErrorResponse* response);
  147. // Initializes all currently stored DBusClients with the system bus and
  148. // performs additional setup.
  149. void InitializeClients();
  150. raw_ptr<dbus::Bus> bus_;
  151. // Separate D-Bus connection used by the "Alternate" set of D-Bus clients. See
  152. // "Alternate D-Bus Client" note above.
  153. raw_ptr<dbus::Bus> alternate_bus_;
  154. std::unique_ptr<BluetoothDBusClientBundle> client_bundle_;
  155. // Needed to enable/disable Floss at D-Bus initialization. We treat this
  156. // D-Bus client specially and not include it in client bundle since we only
  157. // need to Init() it and nothing else.
  158. std::unique_ptr<floss::FlossManagerClient> floss_manager_client_;
  159. base::OnceClosure object_manager_support_known_callback_;
  160. bool object_manager_support_known_;
  161. bool object_manager_supported_;
  162. // Note: This should remain the last member so it'll be destroyed and
  163. // invalidate its weak pointers before any other members are destroyed.
  164. base::WeakPtrFactory<BluezDBusManager> weak_ptr_factory_{this};
  165. };
  166. class DEVICE_BLUETOOTH_EXPORT BluezDBusManagerSetter {
  167. public:
  168. BluezDBusManagerSetter(const BluezDBusManagerSetter&) = delete;
  169. BluezDBusManagerSetter& operator=(const BluezDBusManagerSetter&) = delete;
  170. ~BluezDBusManagerSetter();
  171. void SetBluetoothAdapterClient(
  172. std::unique_ptr<BluetoothAdapterClient> client);
  173. void SetBluetoothAdminPolicyClient(
  174. std::unique_ptr<BluetoothAdminPolicyClient> client);
  175. void SetBluetoothAdvertisementMonitorManagerClient(
  176. std::unique_ptr<BluetoothAdvertisementMonitorManagerClient> client);
  177. void SetBluetoothLEAdvertisingManagerClient(
  178. std::unique_ptr<BluetoothLEAdvertisingManagerClient> client);
  179. void SetBluetoothAgentManagerClient(
  180. std::unique_ptr<BluetoothAgentManagerClient> client);
  181. void SetBluetoothBatteryClient(
  182. std::unique_ptr<BluetoothBatteryClient> client);
  183. void SetBluetoothDebugManagerClient(
  184. std::unique_ptr<BluetoothDebugManagerClient> client);
  185. void SetBluetoothDeviceClient(std::unique_ptr<BluetoothDeviceClient> client);
  186. void SetBluetoothGattCharacteristicClient(
  187. std::unique_ptr<BluetoothGattCharacteristicClient> client);
  188. void SetBluetoothGattDescriptorClient(
  189. std::unique_ptr<BluetoothGattDescriptorClient> client);
  190. void SetBluetoothGattManagerClient(
  191. std::unique_ptr<BluetoothGattManagerClient> client);
  192. void SetBluetoothGattServiceClient(
  193. std::unique_ptr<BluetoothGattServiceClient> client);
  194. void SetBluetoothInputClient(std::unique_ptr<BluetoothInputClient> client);
  195. void SetBluetoothMediaClient(std::unique_ptr<BluetoothMediaClient> client);
  196. void SetBluetoothMediaTransportClient(
  197. std::unique_ptr<BluetoothMediaTransportClient> client);
  198. void SetBluetoothProfileManagerClient(
  199. std::unique_ptr<BluetoothProfileManagerClient> client);
  200. void SetAlternateBluetoothAdapterClient(
  201. std::unique_ptr<BluetoothAdapterClient> client);
  202. void SetAlternateBluetoothAdminPolicyClient(
  203. std::unique_ptr<BluetoothAdminPolicyClient> client);
  204. void SetAlternateBluetoothDeviceClient(
  205. std::unique_ptr<BluetoothDeviceClient> client);
  206. private:
  207. friend class BluezDBusManager;
  208. BluezDBusManagerSetter();
  209. };
  210. } // namespace bluez
  211. #endif // DEVICE_BLUETOOTH_DBUS_BLUEZ_DBUS_MANAGER_H_