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 <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"
14
15namespace base {
16class Thread;
17};
18
19namespace dbus {
20class Bus;
21class ObjectPath;
22};
23
24namespace chromeos {
25
26class DBusThreadManagerObserver;
27
28// Style Note: Clients are sorted by names.
29class BluetoothAdapterClient;
30class BluetoothAgentManagerClient;
31class BluetoothDeviceClient;
32class BluetoothGattCharacteristicClient;
33class BluetoothGattDescriptorClient;
34class BluetoothGattManagerClient;
35class BluetoothGattServiceClient;
36class BluetoothInputClient;
37class BluetoothProfileManagerClient;
38class CrasAudioClient;
39class CrosDisksClient;
40class CryptohomeClient;
41class DBusClient;
42class DebugDaemonClient;
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  static void Initialize();
90
91  // Sets an alternative DBusThreadManager such as MockDBusThreadManager
92  // to be used in |Initialize()| for testing. Tests that call
93  // DBusThreadManager::Initialize() (such as browser_tests and
94  // interactive_ui_tests) should use this instead of calling
95  // |InitiailzeForTesting|.  The injected object will be owned by the
96  // internal pointer and deleted by Shutdown().
97  static void SetInstanceForTesting(DBusThreadManager* dbus_thread_manager);
98
99  // Similar to Initialize(), but injects an alternative
100  // DBusThreadManager using SetInstanceForTest first.  The injected
101  // object will be owned by the internal pointer and deleted by
102  // Shutdown(). Does not create any Fake client implementations.
103  static void InitializeForTesting(DBusThreadManager* dbus_thread_manager);
104
105  // Initialize with stub implementations for tests, creating a complete set
106  // of fake/stub client implementations. Also initializes a default set of
107  // fake Shill devices and services, customizable with switches::kShillStub.
108  static void InitializeWithStub();
109
110  // Returns true if DBusThreadManager has been initialized. Call this to
111  // avoid initializing + shutting down DBusThreadManager more than once.
112  static bool IsInitialized();
113
114  // Destroys the global instance.
115  static void Shutdown();
116
117  // Gets the global instance. Initialize() must be called first.
118  static DBusThreadManager* Get();
119
120  // Adds or removes an observer.
121  virtual void AddObserver(DBusThreadManagerObserver* observer) = 0;
122  virtual void RemoveObserver(DBusThreadManagerObserver* observer) = 0;
123
124  // Returns various D-Bus bus instances, owned by DBusThreadManager.
125  virtual dbus::Bus* GetSystemBus() = 0;
126
127  // All returned objects are owned by DBusThreadManager.  Do not cache these
128  // pointers and use them after DBusThreadManager has been shut down.
129  virtual BluetoothAdapterClient* GetBluetoothAdapterClient() = 0;
130  virtual BluetoothAgentManagerClient* GetBluetoothAgentManagerClient() = 0;
131  virtual BluetoothDeviceClient* GetBluetoothDeviceClient() = 0;
132  virtual BluetoothGattCharacteristicClient*
133      GetBluetoothGattCharacteristicClient() = 0;
134  virtual BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient() = 0;
135  virtual BluetoothGattManagerClient* GetBluetoothGattManagerClient() = 0;
136  virtual BluetoothGattServiceClient* GetBluetoothGattServiceClient() = 0;
137  virtual BluetoothInputClient* GetBluetoothInputClient() = 0;
138  virtual BluetoothProfileManagerClient* GetBluetoothProfileManagerClient() = 0;
139  virtual CrasAudioClient* GetCrasAudioClient() = 0;
140  virtual CrosDisksClient* GetCrosDisksClient() = 0;
141  virtual CryptohomeClient* GetCryptohomeClient() = 0;
142  virtual DebugDaemonClient* GetDebugDaemonClient() = 0;
143  virtual GsmSMSClient* GetGsmSMSClient() = 0;
144  virtual ImageBurnerClient* GetImageBurnerClient() = 0;
145  virtual IntrospectableClient* GetIntrospectableClient() = 0;
146  virtual LorgnetteManagerClient* GetLorgnetteManagerClient() = 0;
147  virtual ModemMessagingClient* GetModemMessagingClient() = 0;
148  virtual NfcAdapterClient* GetNfcAdapterClient() = 0;
149  virtual NfcDeviceClient* GetNfcDeviceClient() = 0;
150  virtual NfcManagerClient* GetNfcManagerClient() = 0;
151  virtual NfcRecordClient* GetNfcRecordClient() = 0;
152  virtual NfcTagClient* GetNfcTagClient() = 0;
153  virtual PermissionBrokerClient* GetPermissionBrokerClient() = 0;
154  virtual PowerManagerClient* GetPowerManagerClient() = 0;
155  virtual PowerPolicyController* GetPowerPolicyController() = 0;
156  virtual SessionManagerClient* GetSessionManagerClient() = 0;
157  virtual ShillDeviceClient* GetShillDeviceClient() = 0;
158  virtual ShillIPConfigClient* GetShillIPConfigClient() = 0;
159  virtual ShillManagerClient* GetShillManagerClient() = 0;
160  virtual ShillServiceClient* GetShillServiceClient() = 0;
161  virtual ShillProfileClient* GetShillProfileClient() = 0;
162  virtual SMSClient* GetSMSClient() = 0;
163  virtual SystemClockClient* GetSystemClockClient() = 0;
164  virtual UpdateEngineClient* GetUpdateEngineClient() = 0;
165
166  virtual ~DBusThreadManager();
167
168 protected:
169  DBusThreadManager();
170
171 private:
172  // InitializeClients is called after g_dbus_thread_manager is set.
173  // NOTE: Clients that access other clients in their Init() must be
174  // initialized in the correct order.
175  static void InitializeClients();
176
177  // Initializes |client| with the |system_bus_|.
178  static void InitClient(DBusClient* client);
179
180  DISALLOW_COPY_AND_ASSIGN(DBusThreadManager);
181};
182
183}  // namespace chromeos
184
185#endif  // CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_
186