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
14namespace base {
15class Thread;
16}  // namespace base
17
18namespace dbus {
19class Bus;
20class ObjectPath;
21}  // namespace dbus
22
23namespace chromeos {
24
25// Style Note: Clients are sorted by names.
26class BluetoothAdapterClient;
27class BluetoothAgentManagerClient;
28class BluetoothDeviceClient;
29class BluetoothGattCharacteristicClient;
30class BluetoothGattDescriptorClient;
31class BluetoothGattManagerClient;
32class BluetoothGattServiceClient;
33class BluetoothInputClient;
34class BluetoothProfileManagerClient;
35class CrasAudioClient;
36class CrosDisksClient;
37class CryptohomeClient;
38class DBusClient;
39class DBusThreadManager;
40class DBusThreadManagerSetter;
41class DebugDaemonClient;
42class EasyUnlockClient;
43class GsmSMSClient;
44class ImageBurnerClient;
45class IntrospectableClient;
46class LorgnetteManagerClient;
47class ModemMessagingClient;
48class NfcAdapterClient;
49class NfcDeviceClient;
50class NfcManagerClient;
51class NfcRecordClient;
52class NfcTagClient;
53class PermissionBrokerClient;
54class PowerManagerClient;
55class PowerPolicyController;
56class SessionManagerClient;
57class ShillDeviceClient;
58class ShillIPConfigClient;
59class ShillManagerClient;
60class ShillProfileClient;
61class ShillServiceClient;
62class SMSClient;
63class SystemClockClient;
64class 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//
84class 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
192class 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