IPC: Support older glib
[platform/core/security/vasum.git] / common / ipc / ipc-gsource.hpp
1 /*
2 *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 *  Contact: Jan Olszak <j.olszak@samsung.com>
5 *
6 *  Licensed under the Apache License, Version 2.0 (the "License");
7 *  you may not use this file except in compliance with the License.
8 *  You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 *  Unless required by applicable law or agreed to in writing, software
13 *  distributed under the License is distributed on an "AS IS" BASIS,
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License
17 */
18
19 /**
20  * @file
21  * @author  Jan Olszak (j.olszak@samsung.com)
22  * @brief    Class for creating a dedicated GSource
23  */
24
25 #ifndef COMMON_IPC_IPC_GSOURCE_HPP
26 #define COMMON_IPC_IPC_GSOURCE_HPP
27
28 #include <glib.h>
29 #include "ipc/types.hpp"
30 #include "utils/callback-guard.hpp"
31
32 #include <memory>
33 #include <mutex>
34 #include <list>
35
36
37 namespace vasum {
38 namespace ipc {
39
40 /**
41  * Class for connecting to the glib's loop.
42  * Creates a dedicated GSource.
43  *
44  * It's supposed to be constructed ONLY with the static create method
45  * and destructed in a glib callback.
46  *
47  * TODO:
48  * - waiting till the managed object (Client or Service) is destroyed
49  *   before IPCGSource stops operating. For now programmer has to ensure this.
50  */
51 struct IPCGSource {
52 public:
53     typedef std::function<void(FileDescriptor fd, short pollEvent)> HandlerCallback;
54     typedef std::shared_ptr<IPCGSource> Pointer;
55
56     ~IPCGSource();
57
58     IPCGSource() = delete;
59     IPCGSource(const IPCGSource&) = delete;
60     IPCGSource& operator=(const IPCGSource&) = delete;
61
62     /**
63      * New file descriptor to listen on.
64      *
65      * @param peerFD file descriptor
66      */
67     void addFD(const FileDescriptor peerFD);
68
69     /**
70      * Removes the file descriptor from the GSource
71      *
72      * @param peerFD file descriptor
73      */
74     void removeFD(const FileDescriptor peerFD);
75
76     /**
77      * Attach to the glib's GMainContext
78      *
79      * @param context where to connect
80      * @return result of the g_source_attach call
81      */
82     guint attach(GMainContext* context = nullptr);
83
84     /**
85      * After this method quits handlerCallback will not be called
86      */
87     void detach();
88
89     /**
90      * Creates the IPCGSource class in the memory allocated by glib.
91      * Calls IPCGSource's constructor
92      *
93      * @param handlerCallback event handling callback
94      *
95      * @return pointer to the IPCGSource
96      */
97     static Pointer create(const HandlerCallback& handlerCallback);
98
99     /**
100      * Callback for the dispatch function
101      */
102     static gboolean onHandlerCall(gpointer userData);
103
104     /**
105      * Locks the internal state mutex and calls the handler callback for each fd
106      */
107     void callHandler();
108
109 private:
110     typedef std::unique_lock<std::recursive_mutex> Lock;
111
112     /**
113      * GSourceFuncs' callback
114      */
115     static gboolean prepare(GSource* source, gint* timeout);
116
117     /**
118      * GSourceFuncs' callback
119      */
120     static gboolean check(GSource* source);
121
122     /**
123      * GSourceFuncs' callback
124      */
125     static gboolean dispatch(GSource* source,
126                              GSourceFunc callbacks,
127                              gpointer userData);
128
129     /**
130      * GSourceFuncs' callback
131      */
132     static void  finalize(GSource* source);
133
134     // Called only from IPCGSource::create
135     IPCGSource(const HandlerCallback& handlerCallback);
136
137     GSource mGSource;
138     HandlerCallback mHandlerCallback;
139     std::list<GPollFD> mGPollFDs;
140     utils::CallbackGuard mGuard;
141     std::recursive_mutex mStateMutex;
142
143 };
144
145 } // namespace ipc
146 } // namespace vasum
147
148 #endif // COMMON_IPC_IPC_GSOURCE_HPP