Tizen 2.0 Release
[framework/web/wrt-commons.git] / modules / dbus / include / dpl / dbus / connection.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    connection.h
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version 1.0
20  * @brief
21  */
22
23 #ifndef DPL_DBUS_CONNECTION_H
24 #define DPL_DBUS_CONNECTION_H
25
26 #include <memory>
27 #include <vector>
28 #include <string>
29 #include <gio/gio.h>
30 #include <dpl/generic_event.h>
31 #include <dpl/event/event_support.h>
32 #include <dpl/dbus/object.h>
33 #include <dpl/dbus/object_proxy.h>
34
35 namespace DPL {
36 namespace DBus {
37
38 namespace ConnectionEvents
39 {
40 /**
41  * Emitted when service name is acquired.
42  *
43  * Arg0 Acquired name.
44  */
45 DECLARE_GENERIC_EVENT_1(ServiceNameAcquiredEvent, std::string)
46
47 /**
48  * Emitted when service name is lost.
49  *
50  * Arg0 Lost name.
51  */
52 DECLARE_GENERIC_EVENT_1(ServiceNameLostEvent, std::string)
53
54 /**
55  * Emitted when remote host closes connection.
56  *
57  * Arg0 Low-level error message.
58  */
59 DECLARE_GENERIC_EVENT_1(ConnectionBrokenEvent, std::string)
60
61 /**
62  * Emitted when invalid or malformed data appear on connection.
63  *
64  * Arg0 Low-level error message.
65  */
66 DECLARE_GENERIC_EVENT_1(ConnectionInvalidEvent, std::string)
67 }
68
69 class Server;
70
71 class Connection;
72 typedef std::shared_ptr<Connection> ConnectionPtr;
73
74 typedef std::shared_ptr<ObjectProxy> ObjectProxyPtr;
75
76 class Connection :
77         public DPL::Event::EventSupport<ConnectionEvents::ServiceNameAcquiredEvent>,
78         public DPL::Event::EventSupport<ConnectionEvents::ServiceNameLostEvent>,
79         public DPL::Event::EventSupport<ConnectionEvents::ConnectionBrokenEvent>,
80         public DPL::Event::EventSupport<ConnectionEvents::ConnectionInvalidEvent>
81 {
82 public:
83     /**
84      * Acquires connection to session bus.
85      *
86      * @return Session bus connection.
87      * @throw DBus::Exception If unable to connect to session bus.
88      */
89     static ConnectionPtr sessionBus();
90
91     /**
92      * Acquires connection to system bus.
93      *
94      * @return System bus connection.
95      * @throw DBus::Exception If unable to connect to system bus.
96      */
97     static ConnectionPtr systemBus();
98
99     /**
100      * Acquires connection to specified bus.
101      *
102      * @return Bus connection.
103      * @throw DBus::Exception If unable to connect to a bus.
104      */
105     static ConnectionPtr connectTo(GBusType busType);
106
107     /**
108      * Acquires connection to for specified address.
109      *
110      * @return Connection.
111      * @throw DBus::Exception If unable to connect.
112      *
113      * @remarks Address should be in DBus format (@see DBus documentation).
114      */
115     static ConnectionPtr connectTo(const std::string& address);
116
117     ~Connection();
118
119     /**
120      * Sets up a service on the connection.
121      *
122      * @param serviceName Service to register.
123      * @throw DBus::Exception If registration failed.
124      *
125      * @remarks Add objects before services to prevent notifications about new
126      *          interfaces being added.
127      */
128     void registerService(const std::string& serviceName);
129
130     /**
131      * Unregisters a service from the connection.
132      *
133      * @param serviceName Service to unregister.
134      * @throw DBus::Exception If service not registered.
135      */
136     void unregisterService(const std::string& serviceName);
137
138     /**
139      * Adds object to the connection.
140      *
141      * @param object Object to register.
142      * @throw DBus::Exception If registration failed.
143      *
144      * @remarks Add objects before services to prevent notifications about new
145      *          interfaces being added.
146      */
147     void registerObject(const ObjectPtr& object);
148
149     /**
150      * Removed object from the connection.
151      *
152      * @param objectPath Path of the object to unregister.
153      * @throw DBus::Exception If object not registered.
154      */
155     void unregisterObject(const std::string& objectPath);
156
157     /**
158      * Creates proxy to remote objects.
159      *
160      * @param serviceName Name of the DBus service.
161      * @param objectPath DBus path to the object.
162      * @return Object proxy.
163      * @throw DBus::ConnectionClosedException If connection is closed.
164      */
165     ObjectProxyPtr createObjectProxy(const std::string& serviceName,
166                                      const std::string& objectPath);
167
168 private:
169     friend class Server;
170
171     typedef std::map<std::string, guint> RegisteredServices;
172
173     struct ObjectRegistration
174     {
175         ObjectRegistration(guint _registrationId, const ObjectPtr& _object)
176             : registrationId(_registrationId),
177               object(_object)
178         {
179         }
180
181         guint registrationId;
182         ObjectPtr object;
183     };
184     typedef std::map<std::string, ObjectRegistration> RegisteredObjects;
185
186     static void onServiceNameAcquired(GDBusConnection* connection,
187                                       const gchar* serviceName,
188                                       gpointer data);
189
190     static void onServiceNameLost(GDBusConnection* connection,
191                                   const gchar* serviceName,
192                                   gpointer data);
193
194     static void onConnectionClosed(GDBusConnection* connection,
195                                    gboolean peerVanished,
196                                    GError* error,
197                                    gpointer data);
198
199     explicit Connection(GDBusConnection* connection);
200
201     GDBusConnection* m_connection;
202
203     RegisteredServices m_registeredServices;
204
205     RegisteredObjects m_registeredObjects;
206 };
207
208 }
209 }
210
211 #endif // DPL_DBUS_CONNECTION_H