Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #ifndef DEVICE_BLUETOOTH_DBUS_BLUEZ_DBUS_MANAGER_H_ | |
| 6 #define DEVICE_BLUETOOTH_DBUS_BLUEZ_DBUS_MANAGER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "chromeos/chromeos_export.h" | |
|
stevenjb
2015/09/17 21:48:01
Use DEVICE_BLUETOOTH_EXPORT
rkc
2015/09/21 18:54:23
Done.
| |
| 14 #include "device/bluetooth/dbus/bluetooth_dbus_client_bundle.h" | |
| 15 | |
| 16 namespace base { | |
| 17 class Thread; | |
| 18 } // namespace base | |
| 19 | |
| 20 namespace dbus { | |
| 21 class Bus; | |
| 22 class ObjectPath; | |
| 23 } // namespace dbus | |
| 24 | |
| 25 namespace bluez { | |
| 26 | |
| 27 // Style Note: Clients are sorted by names. | |
| 28 class BluetoothAdapterClient; | |
| 29 class BluetoothAgentManagerClient; | |
| 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 | |
| 42 // BluezDBusManagerSetter manages the D-Bus thread, the thread dedicated to | |
| 43 // handling asynchronous D-Bus operations. | |
| 44 // | |
| 45 // This class also manages D-Bus connections and D-Bus clients, which | |
| 46 // depend on the D-Bus thread to ensure the right order of shutdowns for | |
| 47 // the D-Bus thread, the D-Bus connections, and the D-Bus clients. | |
| 48 // | |
| 49 // CALLBACKS IN D-BUS CLIENTS: | |
| 50 // | |
| 51 // D-Bus clients managed by BluezDBusManagerSetter are guaranteed to be deleted | |
| 52 // after the D-Bus thread so the clients don't need to worry if new | |
| 53 // incoming messages arrive from the D-Bus thread during shutdown of the | |
| 54 // clients. The UI message loop is not running during the shutdown hence | |
| 55 // the UI message loop won't post tasks to D-BUS clients during the | |
| 56 // shutdown. However, to be extra cautious, clients should use | |
| 57 // WeakPtrFactory when creating callbacks that run on UI thread. See | |
| 58 // session_manager_client.cc for examples. | |
| 59 // | |
| 60 class CHROMEOS_EXPORT BluezDBusManager { | |
| 61 public: | |
| 62 // Sets the global instance. Must be called before any calls to Get(). | |
| 63 // We explicitly initialize and shut down the global object, rather than | |
| 64 // making it a Singleton, to ensure clean startup and shutdown. | |
| 65 // This will initialize real or stub DBusClients depending on command-line | |
| 66 // arguments and whether this process runs in a ChromeOS environment. | |
| 67 static void Initialize(); | |
| 68 | |
| 69 // Returns a DBusThreadManagerSetter instance that allows tests to | |
| 70 // replace individual D-Bus clients with their own implementations. | |
| 71 // Also initializes the main BluezDBusManager for testing if necessary. | |
| 72 static scoped_ptr<BluezDBusManagerSetter> GetSetterForTesting(); | |
| 73 | |
| 74 // Returns true if BluezDBusManager has been initialized. Call this to | |
| 75 // avoid initializing + shutting down BluezDBusManager more than once. | |
| 76 static bool IsInitialized(); | |
| 77 | |
| 78 // Destroys the global instance. | |
| 79 static void Shutdown(); | |
| 80 | |
| 81 // Gets the global instance. Initialize() must be called first. | |
| 82 static BluezDBusManager* Get(); | |
| 83 | |
| 84 // Returns true if |client| is stubbed. | |
| 85 bool IsUsingStub() { return client_bundle_->IsUsingStub(); } | |
| 86 | |
| 87 // Returns various D-Bus bus instances, owned by BluezDBusManager. | |
| 88 dbus::Bus* GetSystemBus(); | |
| 89 | |
| 90 // All returned objects are owned by BluezDBusManager. Do not use these | |
| 91 // pointers after BluezDBusManager has been shut down. | |
| 92 BluetoothAdapterClient* GetBluetoothAdapterClient(); | |
| 93 BluetoothLEAdvertisingManagerClient* GetBluetoothLEAdvertisingManagerClient(); | |
| 94 BluetoothAgentManagerClient* GetBluetoothAgentManagerClient(); | |
| 95 BluetoothDeviceClient* GetBluetoothDeviceClient(); | |
| 96 BluetoothGattCharacteristicClient* GetBluetoothGattCharacteristicClient(); | |
| 97 BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient(); | |
| 98 BluetoothGattManagerClient* GetBluetoothGattManagerClient(); | |
| 99 BluetoothGattServiceClient* GetBluetoothGattServiceClient(); | |
| 100 BluetoothInputClient* GetBluetoothInputClient(); | |
| 101 BluetoothMediaClient* GetBluetoothMediaClient(); | |
| 102 BluetoothMediaTransportClient* GetBluetoothMediaTransportClient(); | |
| 103 BluetoothProfileManagerClient* GetBluetoothProfileManagerClient(); | |
| 104 | |
| 105 private: | |
| 106 friend class BluezDBusManagerSetter; | |
| 107 | |
| 108 // Creates a new BluezDBusManager using the DBusClients set in | |
| 109 // |client_bundle|. | |
| 110 explicit BluezDBusManager( | |
| 111 scoped_ptr<BluetoothDBusClientBundle> client_bundle); | |
| 112 ~BluezDBusManager(); | |
| 113 | |
| 114 // Creates a global instance of BluezDBusManager with the real | |
| 115 // implementations for all clients that are listed in |unstub_client_mask| and | |
| 116 // stub implementations for all clients that are not included. Cannot be | |
| 117 // called more than once. | |
| 118 static void CreateGlobalInstance(bool use_stubs); | |
| 119 | |
| 120 // Initialize global thread manager instance with all real dbus client | |
| 121 // implementations. | |
| 122 static void InitializeWithRealClients(); | |
| 123 | |
| 124 // Initialize global thread manager instance with stubbed-out dbus clients | |
| 125 // implementation. | |
| 126 static void InitializeWithStubs(); | |
| 127 | |
| 128 // Initialize with stub implementations for only certain clients that are | |
| 129 // not included in the comma-separated |unstub_clients| list. | |
| 130 static void InitializeWithPartialStub(const std::string& unstub_clients); | |
| 131 | |
| 132 // Initializes all currently stored DBusClients with the system bus and | |
| 133 // performs additional setup. | |
| 134 void InitializeClients(); | |
| 135 | |
| 136 scoped_ptr<BluetoothDBusClientBundle> client_bundle_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(BluezDBusManager); | |
| 139 }; | |
| 140 | |
| 141 class CHROMEOS_EXPORT BluezDBusManagerSetter { | |
| 142 public: | |
| 143 ~BluezDBusManagerSetter(); | |
| 144 | |
| 145 void SetBluetoothAdapterClient(scoped_ptr<BluetoothAdapterClient> client); | |
| 146 void SetBluetoothLEAdvertisingManagerClient( | |
| 147 scoped_ptr<BluetoothLEAdvertisingManagerClient> client); | |
| 148 void SetBluetoothAgentManagerClient( | |
| 149 scoped_ptr<BluetoothAgentManagerClient> client); | |
| 150 void SetBluetoothDeviceClient(scoped_ptr<BluetoothDeviceClient> client); | |
| 151 void SetBluetoothGattCharacteristicClient( | |
| 152 scoped_ptr<BluetoothGattCharacteristicClient> client); | |
| 153 void SetBluetoothGattDescriptorClient( | |
| 154 scoped_ptr<BluetoothGattDescriptorClient> client); | |
| 155 void SetBluetoothGattManagerClient( | |
| 156 scoped_ptr<BluetoothGattManagerClient> client); | |
| 157 void SetBluetoothGattServiceClient( | |
| 158 scoped_ptr<BluetoothGattServiceClient> client); | |
| 159 void SetBluetoothInputClient(scoped_ptr<BluetoothInputClient> client); | |
| 160 void SetBluetoothMediaClient(scoped_ptr<BluetoothMediaClient> client); | |
| 161 void SetBluetoothMediaTransportClient( | |
| 162 scoped_ptr<BluetoothMediaTransportClient> client); | |
| 163 void SetBluetoothProfileManagerClient( | |
| 164 scoped_ptr<BluetoothProfileManagerClient> client); | |
| 165 | |
| 166 private: | |
| 167 friend class BluezDBusManager; | |
| 168 | |
| 169 BluezDBusManagerSetter(); | |
| 170 | |
| 171 DISALLOW_COPY_AND_ASSIGN(BluezDBusManagerSetter); | |
| 172 }; | |
| 173 | |
| 174 } // namespace bluez | |
| 175 | |
| 176 #endif // DEVICE_BLUETOOTH_DBUS_BLUEZ_DBUS_MANAGER_H_ | |
| OLD | NEW |