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 CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_ 6 #define CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_ 7 8 #include "base/callback.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "chromeos/chromeos_export.h" 12 #include "chromeos/dbus/dbus_client_bundle.h" 13 14 namespace base { 15 class Thread; 16 } // namespace base 17 18 namespace dbus { 19 class Bus; 20 class ObjectPath; 21 } // namespace dbus 22 23 namespace chromeos { 24 25 // Style Note: Clients are sorted by names. 26 class BluetoothAdapterClient; 27 class BluetoothAgentManagerClient; 28 class BluetoothDeviceClient; 29 class BluetoothGattCharacteristicClient; 30 class BluetoothGattDescriptorClient; 31 class BluetoothGattManagerClient; 32 class BluetoothGattServiceClient; 33 class BluetoothInputClient; 34 class BluetoothProfileManagerClient; 35 class CrasAudioClient; 36 class CrosDisksClient; 37 class CryptohomeClient; 38 class DBusClient; 39 class DBusThreadManager; 40 class DBusThreadManagerSetter; 41 class DebugDaemonClient; 42 class EasyUnlockClient; 43 class GsmSMSClient; 44 class ImageBurnerClient; 45 class IntrospectableClient; 46 class LorgnetteManagerClient; 47 class ModemMessagingClient; 48 class NfcAdapterClient; 49 class NfcDeviceClient; 50 class NfcManagerClient; 51 class NfcRecordClient; 52 class NfcTagClient; 53 class PermissionBrokerClient; 54 class PowerManagerClient; 55 class PowerPolicyController; 56 class SessionManagerClient; 57 class ShillDeviceClient; 58 class ShillIPConfigClient; 59 class ShillManagerClient; 60 class ShillProfileClient; 61 class ShillServiceClient; 62 class SMSClient; 63 class SystemClockClient; 64 class UpdateEngineClient; 65 66 // DBusThreadManager manages the D-Bus thread, the thread dedicated to 67 // handling asynchronous D-Bus operations. 68 // 69 // This class also manages D-Bus connections and D-Bus clients, which 70 // depend on the D-Bus thread to ensure the right order of shutdowns for 71 // the D-Bus thread, the D-Bus connections, and the D-Bus clients. 72 // 73 // CALLBACKS IN D-BUS CLIENTS: 74 // 75 // D-Bus clients managed by DBusThreadManager are guaranteed to be deleted 76 // after the D-Bus thread so the clients don't need to worry if new 77 // incoming messages arrive from the D-Bus thread during shutdown of the 78 // clients. The UI message loop is not running during the shutdown hence 79 // the UI message loop won't post tasks to D-BUS clients during the 80 // shutdown. However, to be extra cautious, clients should use 81 // WeakPtrFactory when creating callbacks that run on UI thread. See 82 // session_manager_client.cc for examples. 83 // 84 class CHROMEOS_EXPORT DBusThreadManager { 85 public: 86 // Sets the global instance. Must be called before any calls to Get(). 87 // We explicitly initialize and shut down the global object, rather than 88 // making it a Singleton, to ensure clean startup and shutdown. 89 // This will initialize real or stub DBusClients depending on command-line 90 // arguments and whether this process runs in a ChromeOS environment. 91 static void Initialize(); 92 93 // Returns a DBusThreadManagerSetter instance that allows tests to 94 // replace individual D-Bus clients with their own implementations. 95 // Also initializes the main DBusThreadManager for testing if necessary. 96 static scoped_ptr<DBusThreadManagerSetter> GetSetterForTesting(); 97 98 // Returns true if DBusThreadManager has been initialized. Call this to 99 // avoid initializing + shutting down DBusThreadManager more than once. 100 static bool IsInitialized(); 101 102 // Destroys the global instance. 103 static void Shutdown(); 104 105 // Gets the global instance. Initialize() must be called first. 106 static DBusThreadManager* Get(); 107 108 // Returns true if |client| is stubbed. 109 bool IsUsingStub(DBusClientBundle::DBusClientType client); 110 111 // Returns various D-Bus bus instances, owned by DBusThreadManager. 112 dbus::Bus* GetSystemBus(); 113 114 // All returned objects are owned by DBusThreadManager. Do not cache these 115 // pointers and use them after DBusThreadManager has been shut down. 116 BluetoothAdapterClient* GetBluetoothAdapterClient(); 117 BluetoothAgentManagerClient* GetBluetoothAgentManagerClient(); 118 BluetoothDeviceClient* GetBluetoothDeviceClient(); 119 BluetoothGattCharacteristicClient* GetBluetoothGattCharacteristicClient(); 120 BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient(); 121 BluetoothGattManagerClient* GetBluetoothGattManagerClient(); 122 BluetoothGattServiceClient* GetBluetoothGattServiceClient(); 123 BluetoothInputClient* GetBluetoothInputClient(); 124 BluetoothProfileManagerClient* GetBluetoothProfileManagerClient(); 125 CrasAudioClient* GetCrasAudioClient(); 126 CrosDisksClient* GetCrosDisksClient(); 127 CryptohomeClient* GetCryptohomeClient(); 128 DebugDaemonClient* GetDebugDaemonClient(); 129 EasyUnlockClient* GetEasyUnlockClient(); 130 GsmSMSClient* GetGsmSMSClient(); 131 ImageBurnerClient* GetImageBurnerClient(); 132 IntrospectableClient* GetIntrospectableClient(); 133 LorgnetteManagerClient* GetLorgnetteManagerClient(); 134 ModemMessagingClient* GetModemMessagingClient(); 135 NfcAdapterClient* GetNfcAdapterClient(); 136 NfcDeviceClient* GetNfcDeviceClient(); 137 NfcManagerClient* GetNfcManagerClient(); 138 NfcRecordClient* GetNfcRecordClient(); 139 NfcTagClient* GetNfcTagClient(); 140 PermissionBrokerClient* GetPermissionBrokerClient(); 141 PowerManagerClient* GetPowerManagerClient(); 142 PowerPolicyController* GetPowerPolicyController(); 143 SessionManagerClient* GetSessionManagerClient(); 144 ShillDeviceClient* GetShillDeviceClient(); 145 ShillIPConfigClient* GetShillIPConfigClient(); 146 ShillManagerClient* GetShillManagerClient(); 147 ShillServiceClient* GetShillServiceClient(); 148 ShillProfileClient* GetShillProfileClient(); 149 SMSClient* GetSMSClient(); 150 SystemClockClient* GetSystemClockClient(); 151 UpdateEngineClient* GetUpdateEngineClient(); 152 153 private: 154 friend class DBusThreadManagerSetter; 155 156 // Creates a new DBusThreadManager using the DBusClients set in 157 // |client_bundle|. 158 explicit DBusThreadManager(scoped_ptr<DBusClientBundle> client_bundle); 159 ~DBusThreadManager(); 160 161 // Creates a global instance of DBusThreadManager with the real 162 // implementations for all clients that are listed in |unstub_client_mask| and 163 // stub implementations for all clients that are not included. Cannot be 164 // called more than once. 165 static void CreateGlobalInstance( 166 DBusClientBundle::DBusClientTypeMask unstub_client_mask); 167 168 // Initialize global thread manager instance with all real dbus client 169 // implementations. 170 static void InitializeWithRealClients(); 171 172 // Initialize global thread manager instance with stubbed-out dbus clients 173 // implementation. 174 static void InitializeWithStubs(); 175 176 // Initialize with stub implementations for only certain clients that are 177 // not included in the comma-separated |unstub_clients| list. 178 static void InitializeWithPartialStub(const std::string& unstub_clients); 179 180 // Initializes all currently stored DBusClients with the system bus and 181 // performs additional setup. 182 void InitializeClients(); 183 184 scoped_ptr<base::Thread> dbus_thread_; 185 scoped_refptr<dbus::Bus> system_bus_; 186 scoped_ptr<DBusClientBundle> client_bundle_; 187 scoped_ptr<PowerPolicyController> power_policy_controller_; 188 189 DISALLOW_COPY_AND_ASSIGN(DBusThreadManager); 190 }; 191 192 class CHROMEOS_EXPORT DBusThreadManagerSetter { 193 public: 194 ~DBusThreadManagerSetter(); 195 196 void SetBluetoothAdapterClient(scoped_ptr<BluetoothAdapterClient> client); 197 void SetBluetoothAgentManagerClient( 198 scoped_ptr<BluetoothAgentManagerClient> client); 199 void SetBluetoothDeviceClient(scoped_ptr<BluetoothDeviceClient> client); 200 void SetBluetoothGattCharacteristicClient( 201 scoped_ptr<BluetoothGattCharacteristicClient> client); 202 void SetBluetoothGattDescriptorClient( 203 scoped_ptr<BluetoothGattDescriptorClient> client); 204 void SetBluetoothGattManagerClient( 205 scoped_ptr<BluetoothGattManagerClient> client); 206 void SetBluetoothGattServiceClient( 207 scoped_ptr<BluetoothGattServiceClient> client); 208 void SetBluetoothInputClient(scoped_ptr<BluetoothInputClient> client); 209 void SetBluetoothProfileManagerClient( 210 scoped_ptr<BluetoothProfileManagerClient> client); 211 void SetCrasAudioClient(scoped_ptr<CrasAudioClient> client); 212 void SetCrosDisksClient(scoped_ptr<CrosDisksClient> client); 213 void SetCryptohomeClient(scoped_ptr<CryptohomeClient> client); 214 void SetDebugDaemonClient(scoped_ptr<DebugDaemonClient> client); 215 void SetEasyUnlockClient(scoped_ptr<EasyUnlockClient> client); 216 void SetLorgnetteManagerClient(scoped_ptr<LorgnetteManagerClient> client); 217 void SetShillDeviceClient(scoped_ptr<ShillDeviceClient> client); 218 void SetShillIPConfigClient(scoped_ptr<ShillIPConfigClient> client); 219 void SetShillManagerClient(scoped_ptr<ShillManagerClient> client); 220 void SetShillServiceClient(scoped_ptr<ShillServiceClient> client); 221 void SetShillProfileClient(scoped_ptr<ShillProfileClient> client); 222 void SetGsmSMSClient(scoped_ptr<GsmSMSClient> client); 223 void SetImageBurnerClient(scoped_ptr<ImageBurnerClient> client); 224 void SetIntrospectableClient(scoped_ptr<IntrospectableClient> client); 225 void SetModemMessagingClient(scoped_ptr<ModemMessagingClient> client); 226 void SetNfcAdapterClient(scoped_ptr<NfcAdapterClient> client); 227 void SetNfcDeviceClient(scoped_ptr<NfcDeviceClient> client); 228 void SetNfcManagerClient(scoped_ptr<NfcManagerClient> client); 229 void SetNfcRecordClient(scoped_ptr<NfcRecordClient> client); 230 void SetNfcTagClient(scoped_ptr<NfcTagClient> client); 231 void SetPermissionBrokerClient(scoped_ptr<PermissionBrokerClient> client); 232 void SetPowerManagerClient(scoped_ptr<PowerManagerClient> client); 233 void SetSessionManagerClient(scoped_ptr<SessionManagerClient> client); 234 void SetSMSClient(scoped_ptr<SMSClient> client); 235 void SetSystemClockClient(scoped_ptr<SystemClockClient> client); 236 void SetUpdateEngineClient(scoped_ptr<UpdateEngineClient> client); 237 238 private: 239 friend class DBusThreadManager; 240 241 DBusThreadManagerSetter(); 242 243 DISALLOW_COPY_AND_ASSIGN(DBusThreadManagerSetter); 244 }; 245 246 } // namespace chromeos 247 248 #endif // CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_ 249