|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* vim:expandtab:shiftwidth=4:tabstop=4: |
|
3 */ |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #ifndef NSDBUSSERVICE_H_ |
|
9 #define NSDBUSSERVICE_H_ |
|
10 |
|
11 #define DBUS_API_SUBJECT_TO_CHANGE |
|
12 #include <dbus/dbus.h> |
|
13 |
|
14 #include "nsITimer.h" |
|
15 #include "nsCOMPtr.h" |
|
16 |
|
17 class DBusClient { |
|
18 public: |
|
19 virtual void RegisterWithConnection(DBusConnection* connection) = 0; |
|
20 virtual void UnregisterWithConnection(DBusConnection* connection) = 0; |
|
21 virtual bool HandleMessage(DBusMessage* msg) = 0; |
|
22 }; |
|
23 |
|
24 #define NS_DBUS_IID \ |
|
25 { 0xba4f79b7, 0x0d4c, 0x4b3a, { 0x8a, 0x85, 0x6f, 0xb3, 0x0d, 0xce, 0xf5, 0x11 } } |
|
26 |
|
27 /** |
|
28 * The nsDBusService component interfaces with DBUS to communicate with daemons |
|
29 * in systems supporting DBUS. It links dynamically to the DBUS libraries so |
|
30 * will not load on systems without those libraries ... but that's harmless. |
|
31 * |
|
32 * Currently the only daemon we communicate with is NetworkManager. We listen |
|
33 * for NetworkManager state changes; we set nsIOService's offline status to |
|
34 * FALSE when NetworkManager reports NM_STATE_CONNECTED, and to TRUE otherwise. |
|
35 * We also solicit the current status from NetworkManager when this component |
|
36 * gets loaded. |
|
37 * |
|
38 * In the future we could extend this class to talk to other daemons. |
|
39 * |
|
40 * Currently all communication is asynchronous. This isn't hard to implement |
|
41 * and avoids blocking our main thread. |
|
42 */ |
|
43 class nsDBusService : public nsISupports |
|
44 { |
|
45 public: |
|
46 nsDBusService(); |
|
47 virtual ~nsDBusService(); |
|
48 |
|
49 NS_DECL_ISUPPORTS |
|
50 NS_DECLARE_STATIC_IID_ACCESSOR(NS_DBUS_IID) |
|
51 |
|
52 static already_AddRefed<nsDBusService> Get(); |
|
53 |
|
54 nsresult AddClient(DBusClient* client); |
|
55 void RemoveClient(DBusClient* client); |
|
56 |
|
57 DBusPendingCall* SendWithReply(DBusClient* client, DBusMessage* message); |
|
58 |
|
59 // these should be private but we need to call them from static functions |
|
60 bool HandleMessage(DBusMessage* message); |
|
61 void DoTimerCallback(nsITimer* aTimer); |
|
62 |
|
63 private: |
|
64 nsresult CreateConnection(); |
|
65 void DropConnection(); |
|
66 void HandleDBusDisconnect(); |
|
67 |
|
68 static nsDBusService* gSingleton; |
|
69 |
|
70 DBusConnection* mConnection; |
|
71 nsCOMPtr<nsITimer> mReconnectTimer; |
|
72 DBusClient* mSingleClient; |
|
73 }; |
|
74 |
|
75 NS_DEFINE_STATIC_IID_ACCESSOR(nsDBusService, NS_DBUS_IID) |
|
76 |
|
77 #endif /*NSDBUSSERVICE_H_*/ |