Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / dbus_thread_manager.h
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
15 namespace base {
16 class Thread;
17 };
18
19 namespace dbus {
20 class Bus;
21 class ObjectPath;
22 };
23
24 namespace chromeos {
25
26 class DBusThreadManagerObserver;
27
28 // Style Note: Clients are sorted by names.
29 class BluetoothAdapterClient;
30 class BluetoothAgentManagerClient;
31 class BluetoothDeviceClient;
32 class BluetoothGattCharacteristicClient;
33 class BluetoothGattDescriptorClient;
34 class BluetoothGattServiceClient;
35 class BluetoothInputClient;
36 class BluetoothProfileManagerClient;
37 class CrasAudioClient;
38 class CrosDisksClient;
39 class CryptohomeClient;
40 class DBusClient;
41 class DebugDaemonClient;
42 class GsmSMSClient;
43 class ImageBurnerClient;
44 class IntrospectableClient;
45 class ModemMessagingClient;
46 class NfcAdapterClient;
47 class NfcDeviceClient;
48 class NfcManagerClient;
49 class NfcRecordClient;
50 class NfcTagClient;
51 class PermissionBrokerClient;
52 class PowerManagerClient;
53 class PowerPolicyController;
54 class SessionManagerClient;
55 class ShillDeviceClient;
56 class ShillIPConfigClient;
57 class ShillManagerClient;
58 class ShillProfileClient;
59 class ShillServiceClient;
60 class SMSClient;
61 class SystemClockClient;
62 class UpdateEngineClient;
63
64 // DBusThreadManager manages the D-Bus thread, the thread dedicated to
65 // handling asynchronous D-Bus operations.
66 //
67 // This class also manages D-Bus connections and D-Bus clients, which
68 // depend on the D-Bus thread to ensure the right order of shutdowns for
69 // the D-Bus thread, the D-Bus connections, and the D-Bus clients.
70 //
71 // CALLBACKS IN D-BUS CLIENTS:
72 //
73 // D-Bus clients managed by DBusThreadManager are guaranteed to be deleted
74 // after the D-Bus thread so the clients don't need to worry if new
75 // incoming messages arrive from the D-Bus thread during shutdown of the
76 // clients. The UI message loop is not running during the shutdown hence
77 // the UI message loop won't post tasks to D-BUS clients during the
78 // shutdown. However, to be extra cautious, clients should use
79 // WeakPtrFactory when creating callbacks that run on UI thread. See
80 // session_manager_client.cc for examples.
81 //
82 class CHROMEOS_EXPORT DBusThreadManager {
83  public:
84   // Sets the global instance. Must be called before any calls to Get().
85   // We explicitly initialize and shut down the global object, rather than
86   // making it a Singleton, to ensure clean startup and shutdown.
87   static void Initialize();
88
89   // Sets an alternative DBusThreadManager such as MockDBusThreadManager
90   // to be used in |Initialize()| for testing. Tests that call
91   // DBusThreadManager::Initialize() (such as browser_tests and
92   // interactive_ui_tests) should use this instead of calling
93   // |InitiailzeForTesting|.  The injected object will be owned by the
94   // internal pointer and deleted by Shutdown().
95   static void SetInstanceForTesting(DBusThreadManager* dbus_thread_manager);
96
97   // Similar to Initialize(), but injects an alternative
98   // DBusThreadManager using SetInstanceForTest first.  The injected
99   // object will be owned by the internal pointer and deleted by
100   // Shutdown(). Does not create any Fake client implementations.
101   static void InitializeForTesting(DBusThreadManager* dbus_thread_manager);
102
103   // Initialize with stub implementations for tests, creating a complete set
104   // of fake/stub client implementations. Also initializes a default set of
105   // fake Shill devices and services, customizable with switches::kShillStub.
106   static void InitializeWithStub();
107
108   // Returns true if DBusThreadManager has been initialized. Call this to
109   // avoid initializing + shutting down DBusThreadManager more than once.
110   static bool IsInitialized();
111
112   // Destroys the global instance.
113   static void Shutdown();
114
115   // Gets the global instance. Initialize() must be called first.
116   static DBusThreadManager* Get();
117
118   // Adds or removes an observer.
119   virtual void AddObserver(DBusThreadManagerObserver* observer) = 0;
120   virtual void RemoveObserver(DBusThreadManagerObserver* observer) = 0;
121
122   // Returns various D-Bus bus instances, owned by DBusThreadManager.
123   virtual dbus::Bus* GetSystemBus() = 0;
124
125   // All returned objects are owned by DBusThreadManager.  Do not cache these
126   // pointers and use them after DBusThreadManager has been shut down.
127   virtual BluetoothAdapterClient* GetBluetoothAdapterClient() = 0;
128   virtual BluetoothAgentManagerClient* GetBluetoothAgentManagerClient() = 0;
129   virtual BluetoothDeviceClient* GetBluetoothDeviceClient() = 0;
130   virtual BluetoothGattCharacteristicClient*
131       GetBluetoothGattCharacteristicClient() = 0;
132   virtual BluetoothGattDescriptorClient* GetBluetoothGattDescriptorClient() = 0;
133   virtual BluetoothGattServiceClient* GetBluetoothGattServiceClient() = 0;
134   virtual BluetoothInputClient* GetBluetoothInputClient() = 0;
135   virtual BluetoothProfileManagerClient* GetBluetoothProfileManagerClient() = 0;
136   virtual CrasAudioClient* GetCrasAudioClient() = 0;
137   virtual CrosDisksClient* GetCrosDisksClient() = 0;
138   virtual CryptohomeClient* GetCryptohomeClient() = 0;
139   virtual DebugDaemonClient* GetDebugDaemonClient() = 0;
140   virtual GsmSMSClient* GetGsmSMSClient() = 0;
141   virtual ImageBurnerClient* GetImageBurnerClient() = 0;
142   virtual IntrospectableClient* GetIntrospectableClient() = 0;
143   virtual ModemMessagingClient* GetModemMessagingClient() = 0;
144   virtual NfcAdapterClient* GetNfcAdapterClient() = 0;
145   virtual NfcDeviceClient* GetNfcDeviceClient() = 0;
146   virtual NfcManagerClient* GetNfcManagerClient() = 0;
147   virtual NfcRecordClient* GetNfcRecordClient() = 0;
148   virtual NfcTagClient* GetNfcTagClient() = 0;
149   virtual PermissionBrokerClient* GetPermissionBrokerClient() = 0;
150   virtual PowerManagerClient* GetPowerManagerClient() = 0;
151   virtual PowerPolicyController* GetPowerPolicyController() = 0;
152   virtual SessionManagerClient* GetSessionManagerClient() = 0;
153   virtual ShillDeviceClient* GetShillDeviceClient() = 0;
154   virtual ShillIPConfigClient* GetShillIPConfigClient() = 0;
155   virtual ShillManagerClient* GetShillManagerClient() = 0;
156   virtual ShillServiceClient* GetShillServiceClient() = 0;
157   virtual ShillProfileClient* GetShillProfileClient() = 0;
158   virtual SMSClient* GetSMSClient() = 0;
159   virtual SystemClockClient* GetSystemClockClient() = 0;
160   virtual UpdateEngineClient* GetUpdateEngineClient() = 0;
161
162   virtual ~DBusThreadManager();
163
164  protected:
165   DBusThreadManager();
166
167  private:
168   // InitializeClients is called after g_dbus_thread_manager is set.
169   // NOTE: Clients that access other clients in their Init() must be
170   // initialized in the correct order.
171   static void InitializeClients();
172
173   // Initializes |client| with the |system_bus_|.
174   static void InitClient(DBusClient* client);
175
176   DISALLOW_COPY_AND_ASSIGN(DBusThreadManager);
177 };
178
179 }  // namespace chromeos
180
181 #endif  // CHROMEOS_DBUS_DBUS_THREAD_MANAGER_H_