Add privilege check code.
[platform/framework/native/appfw.git] / src / server / appfw / inc / FIo_IpcServer.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 /**
18  * @file        FIo_IpcServer.h
19  * @brief       This is the header file for the _IpcServer class.
20  *
21  * This file contains the declarations of _IpcServer.
22  */
23
24 #ifndef _FIO_INTERNAL_IPC_SERVER_H_
25 #define _FIO_INTERNAL_IPC_SERVER_H_
26
27 #include <string>
28 #include <pthread.h>
29 #include <map>
30 #include <glib.h>
31
32 #include <ipc/ipc_message_macros.h>
33 #include <ipc/ipc_message_utils.h>
34
35 #include <FBaseResult.h>
36 #include <FBaseObject.h>
37 #include <FBaseString.h>
38 #include <FAppTypes.h>
39
40 namespace Tizen { namespace Base { namespace Runtime
41 {
42 class _EventDispatcher;
43 }}}
44
45 namespace Tizen { namespace Io
46 {
47
48 class _IIpcServerEventListener;
49
50 /**
51  * @class _IpcServer
52  * @brief This class provides methods to handle IPC request messages.
53  *
54  */
55 class _OSP_EXPORT_ _IpcServer
56         : public Tizen::Base::Object
57 {
58 public:
59         _IpcServer(void);
60
61         virtual ~_IpcServer(void);
62
63         /**
64          * Constructs the instance of this class and starts the IPC server.
65          *
66          * @return An error code
67          * @param[in] name                      The name of IPC server
68          * @param[in] listener          The listener for IPC server
69          * @param[in] runOnCallerThread Set to @c true, if the server runs on the caller thread
70          *                                      @c false, if the server runs on its own thread.
71          * @exception E_SUCCESS         The method was successful.
72          * @exception E_OUT_OF_MEMORY   Insufficient memory.
73          * @exception E_SYSTEM          Occurs when runOnCallerThread is set to true where the caller thread is worker thread.
74          */
75         result Construct(const Tizen::Base::String& name, const _IIpcServerEventListener& listener, bool runOnCallerThread = true);
76
77         /**
78          * Returns the name of the IPC server.
79          *
80          * @return The name of the IPC server.
81          */
82         Tizen::Base::String GetName(void) const;
83
84         /**
85          * Returns the id the of the client which sent a request message.
86          *
87          * @return The id of the IPC client.
88          * @remark This can be called only in a message handler.
89          */
90         int GetClientId(void) const;
91
92         /**
93          * Returns the package id of the client which sent a request message.
94          *
95          * @return The package id of the IPC client.
96          * @remark This can be called only in a message handler.
97          */
98         Tizen::App::PackageId GetClientPackageId(void) const;
99
100         /**
101          * Returns the application id of the client which sent a request message.
102          *
103          * @return The application id of the IPC client.
104          * @remark This can be called only in a message handler.
105          */
106         Tizen::App::AppId GetClientApplicationId(void) const;
107
108         /**
109          * Stops the IPC server.
110          *
111          * @return An error code
112          * @exception E_SUCCESS         The method was successful.
113          * @exception E_INVALID_STATE   The IPC server has not been started.
114          */
115         result Stop(void);
116
117         /**
118          * Sends a message to an IPC client.
119          *
120          * @return An error code
121          * @param[in] clientId      The id of the IPC client
122          * @param[in] message   The message to send
123          * @exception E_SUCCESS The method was successful.
124          * @exception E_INVALID_ARG             The message is synchronous.
125          * @exception E_INVALID_OPERATION       The client didn't set a listener.
126          * @exception E_RESOURCE_UNAVAILABLE    The socket buffer is full.
127          * @exception E_OUT_OF_MEMORY   Insufficient memory.
128          * @exception E_SYSTEM          A system error occurred.
129          *
130          * @remark Only an asychronous message can be sent to an IPC client.
131          */
132         result SendResponse(int clientId, const IPC::Message& message);
133
134         result SendResponse(int clientId, IPC::Message* pMessage);
135
136         result Start(void);
137
138         bool Send(IPC::Message* msg);
139
140 private:
141         _IpcServer(const _IpcServer& value);
142
143         _IpcServer& operator =(const _IpcServer& value);
144
145         static void* ThreadProc(void* pParam);
146
147         void Run(void* pParam);
148
149         static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
150
151         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
152
153         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
154
155         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
156
157         struct  _ClientInfo;
158
159         /**
160          *      @struct __ChannelInfo
161          *      @brief  This struct represent a channel.
162          */
163         struct  _ChannelInfo
164         {
165                 _ChannelInfo(void);
166                 ~_ChannelInfo(void);
167
168                 struct _ClientInfo* pClientInfo;
169                 GIOChannel* pGIOChannel;
170                 GSource* pGSource;
171                 bool destroySource;
172         };
173
174         /**
175          *      @struct __ClientInfo
176          *      @brief  This struct represent a client connected to this server.
177          */
178         struct  _ClientInfo
179         {
180                 _ClientInfo(void);
181                 ~_ClientInfo(void);
182
183                 int clientId;                              /**< the client id */
184                 _IpcServer* pIpcServer;                    /**< the pointer to an _ IpcServer */
185                 GIOChannel* pReverseChannel;               /**< the channel for sending reverse message */
186                 std::vector <struct _ChannelInfo*> channels;   /**< the set of channels associated with a client */
187                 Tizen::Base::String appId;
188         };
189
190         Tizen::Base::String __name;
191         bool __runOnCallerThread;
192         Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
193         _IIpcServerEventListener* __pListener;
194
195         pthread_t __handlerThread;
196         GMainContext* __pHandlerGMainContext;
197         GMainLoop* __pHandlerGMainLoop;
198
199         // handling connection
200         GSource* __pConnectGSource;
201
202         // handling received message
203         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
204         std::string __pending;
205
206         // current message handling context
207         GIOChannel* __pCurrentChannel;
208         _ClientInfo* __pCurrentClientInfo;
209
210         std::map <int, _ClientInfo*> __clients;   // pid of client is used for key
211 }; // _IpcServer
212
213 } } // Tizen::Io
214
215 #endif // _FIO_INTERNAL_IPC_SERVER_H_