22afad7d5ba5231c2891342aa2203c03a43eadc2
[framework/web/wrt-plugins-common.git] / src / modules / tizen / 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  * @author          Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
18  */
19
20 #ifndef WRTDEVICEAPIS_DBUS_CONNECTION_H_
21 #define WRTDEVICEAPIS_DBUS_CONNECTION_H_
22
23 #include <string>
24 #include <map>
25 #include <set>
26 #include <dbus/dbus.h>
27 #include <dpl/shared_ptr.h>
28 #include <dpl/generic_event.h>
29 #include <dpl/event/event_support.h>
30 #include <dpl/event/event_listener.h>
31 #include <dpl/waitable_event.h>
32 #include <dpl/thread.h>
33 #include <dpl/noncopyable.h>
34 #include <dpl/event/controller.h>
35 #include <dpl/type_list.h>
36 #include <dpl/mutex.h>
37 #include "MessageEvent.h"
38
39 namespace WrtDeviceApis {
40 namespace DBus {
41 class Connection : public DPL::Event::EventSupport<MessageEvent>
42 {
43   public:
44     typedef DPL::Event::EventListener<MessageEvent> EventListener;
45
46   public:
47     Connection();
48     ~Connection();
49
50     void open(DBusBusType bus);
51     void close();
52     void addFilter(const std::string& rule);
53     void removeFilter(const std::string& rule);
54     void setWorkerThread(DPL::Thread* thread);
55
56   private:
57     DECLARE_GENERIC_EVENT_2(AddDescriptorEvent,
58                             DPL::WaitableHandle,
59                             DPL::WaitMode::Type)
60
61     DECLARE_GENERIC_EVENT_2(RemoveDescriptorEvent,
62                             DPL::WaitableHandle,
63                             DPL::WaitMode::Type)
64
65     class MessageDispatcher :
66         public DPL::Event::Controller<DPL::TypeListDecl<
67                 AddDescriptorEvent,
68                 RemoveDescriptorEvent>::Type>,
69         private DPL::WaitableHandleWatchSupport::WaitableHandleListener
70     {
71       public:
72         explicit MessageDispatcher(Connection* connection);
73         ~MessageDispatcher();
74         void addDescriptor(DPL::WaitableHandle handle,
75                            DPL::WaitMode::Type mode);
76         void removeDescriptor(DPL::WaitableHandle handle,
77                               DPL::WaitMode::Type mode);
78
79       protected:
80         void OnEventReceived(const AddDescriptorEvent& event);
81         void OnEventReceived(const RemoveDescriptorEvent& event);
82         void OnWaitableHandleEvent(DPL::WaitableHandle waitableHandle,
83                                    DPL::WaitMode::Type mode);
84
85       private:
86         Connection* m_connection;
87     };
88
89     class Watch : private DPL::Noncopyable
90     {
91       public:
92         explicit Watch(DBusWatch* watch);
93
94         inline DPL::WaitableHandle getHandle() const;
95         inline DPL::WaitMode::Type getType() const;
96         inline bool isEnabled() const;
97         inline DBusWatch* get() const;
98
99       private:
100         DBusWatch* m_watch;
101     };
102     typedef DPL::SharedPtr<Watch> WatchPtr;
103
104     typedef std::map<DPL::WaitableHandle, WatchPtr> WatchSet;
105     typedef WatchSet::iterator WatchSetIterator;
106
107     typedef std::set<std::string> FilterSet;
108     typedef FilterSet::iterator FilterSetIterator;
109
110   private:
111     static dbus_bool_t addWatch(DBusWatch* watch,
112                                 void* data);
113     static void removeWatch(DBusWatch* watch,
114                             void* data);
115     static void toggleWatch(DBusWatch* watch,
116                             void* data);
117     static DBusHandlerResult filterMessage(DBusConnection* connection,
118                                            DBusMessage* message,
119                                            void* data);
120
121   private:
122     void addFilterInternal(const std::string& rule);
123     void removeFilterInternal(const std::string& rule);
124
125   private:
126     DBusConnection* m_connection;
127     DBusError m_error;
128
129     MessageDispatcher* m_messageDispatcher;
130     bool m_connected;
131     FilterSet m_filters;
132     WatchSet m_watches;
133     DPL::Mutex m_watchesMutex;
134 };
135
136 typedef DPL::SharedPtr<Connection> ConnectionPtr;
137 } // DBus
138 } // WrtDeviceApis
139
140 #endif // WRTDEVICEAPIS_DBUS_CONNECTION_H_