Update the spec file
[platform/framework/native/channel-service.git] / inc / FIo_ChannelWebAppStub.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FIo_ChannelWebAppStub.h
20  * @brief       This is the header file for the _ChannelWebAppStub class.
21  *
22  * This file contains the declarations of _ChannelWebAppStub.
23  */
24
25 #ifndef _FIO_INTERNAL_CHANNEL_WEBAPP_STUB_H_
26 #define _FIO_INTERNAL_CHANNEL_WEBAPP_STUB_H_
27
28 #include <string>
29 #include <pthread.h>
30 #include <map>
31 #include <glib.h>
32 #include <libsoup/soup.h>
33
34 #include <FOspConfig.h>
35 #include <FBaseObject.h>
36 #include <FBaseString.h>
37 #include <FBaseResult.h>
38 #include <FBaseColArrayList.h>
39 #include <FBaseColHashMap.h>
40 #include <FAppTypes.h>
41 #include <FIo_ChannelService.h>
42
43 namespace Tizen { namespace Base { namespace Runtime
44 {
45 class _EventDispatcher;
46 }}}
47
48 namespace Tizen { namespace Io
49 {
50
51 enum WebSocketType
52 {
53         WS_NONE,
54         WS_HYBI00,
55         WS_HYBI10
56 };
57
58 class _IIpcServerEventListener;
59 class _ChannelServiceStub;
60
61 /**
62  * @class _ChannelWebAppStub
63  * @brief This class provides methods to handle web-application request messages.
64  * @since 2.1
65  *
66  */
67 class _OSP_EXPORT_ _ChannelWebAppStub
68         : public Tizen::Base::Object
69 {
70 public:
71         /**
72          * This is the default constructor for this class.
73          * @since 2.1
74          */
75         _ChannelWebAppStub(void);
76
77         /**
78          * This is the destructor for this class.
79          * @since 2.1
80          */
81         virtual ~_ChannelWebAppStub(void);
82
83         result Construct(void);
84
85         void SetChannelService(_ChannelService& service);
86
87         static bool SendResponse(int clientId, const Tizen::Base::Collection::ArrayList& args);
88
89 private:
90         _ChannelWebAppStub(const _ChannelWebAppStub& rhs);
91
92         _ChannelWebAppStub& operator =(const _ChannelWebAppStub& rhs);
93
94         static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
95
96         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
97
98         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
99
100         static std::string CreateResponseHeader(int appSocket, SoupMessageHeaders *header, const std::string& response);
101
102         static WebSocketType VerifyHttpHeaderType(std::string header, int headerLength);
103         static std::string MakeHybi00Response(uint32_t first, uint32_t second, unsigned char* third);
104         static std::string MakeHybi10Response(std::string& secureAccept);
105
106         static bool AppendChallengeNumber(uint32_t number, unsigned char *response);
107         static unsigned int ExtractChallengeNumber(const char* keyString);
108         static char* EncodeBase64(unsigned char *string, int len);
109
110
111 //      struct  _ClientInfo;
112 //
113 //      struct  _ChannelInfo
114 //      {
115 //              _ChannelInfo(void);
116 //              ~_ChannelInfo(void);
117 //
118 //              struct _ClientInfo* pClientInfo;
119 //              GIOChannel* pGIOChannel;
120 //              GSource* pGSource;
121 //              bool destroySource;
122 //      };
123 //
124 //      struct  _ClientInfo
125 //      {
126 //              _ClientInfo(void);
127 //              ~_ClientInfo(void);
128 //
129 //              int clientId;                              // the client id
130 //              _ChannelWebAppStub* pChannelStub;                   // the pointer to an _ChannelWebAppStub
131 //              //GIOChannel* pReverseChannel;               // the channel for sending reverse message
132 //              std::vector <struct _ChannelInfo*> channels;   // the set of channels associated with a client
133 //              //Tizen::Base::String appId;
134 //              //Tizen::Base::String appExecutableName;
135 //      };
136
137         class _ClientInfo;
138
139         class _ChannelInfo
140         {
141         public:
142                 _ChannelInfo(void);
143                 ~_ChannelInfo(void);
144
145                 _ClientInfo* pClientInfo;
146
147                 Tizen::App::AppId destAppId;
148
149                 GIOChannel* pGIOChannel;
150                 GSource* pGSource;
151                 bool destroySource;
152         };
153
154         class _ClientInfo
155                 : public Tizen::Base::Object
156         {
157         public:
158                 _ClientInfo(void);
159                 ~_ClientInfo(void);
160
161                 Tizen::App::AppId srcAppId;
162
163                 //int clientId;
164                 Tizen::Base::String key; // key = srcAppId + srcAppExecName
165                 _ChannelWebAppStub* pChannelStub;
166                 std::vector <struct _ChannelInfo*> channels;   /**< the set of channels associated with a client */
167         };
168
169         class _WebSocketMessage
170         {
171                 public:
172                         _WebSocketMessage(void);
173                         ~_WebSocketMessage(void);
174
175
176                         typedef union
177                         {
178                                 struct
179                                 {
180                                         unsigned int opcode : 4;
181                                         unsigned int rsv3 : 1;
182                                         unsigned int rsv2 : 1;
183                                         unsigned int rsv1 : 1;
184                                         unsigned int fin : 1;
185                                         unsigned int payloadLen : 7;
186                                         unsigned int masked : 1;
187                                 } factors;
188                                 uint16_t header;
189                         } MiniHeader;
190
191                         int ParseHeader(uint8_t* header, int messageLen);
192                         int ParsePayload(uint8_t* payload, int messageLen);
193                         void ConsoleMessage() const;
194                         static size_t CreateMessage(std::string& payload, std::string& message);
195
196                         MiniHeader m_header;
197                         uint8_t m_payloadOffset;
198                         uint8_t m_maskingKey[4];
199                         uint64_t m_payloadLen;
200                         bool m_headerParsed;
201                         uint8_t* m_payload;
202
203                 private:
204                         _WebSocketMessage(_WebSocketMessage& webSocketMessage);
205
206                         _WebSocketMessage& operator =(const _WebSocketMessage& webSocketMessage);
207
208         };
209
210         _ChannelService* __pChannelService;
211
212         GMainContext* __pGMainContext;
213         GSource* __pServerGSource;
214         GSource* __pClientGSource;
215
216         Tizen::Base::Collection::HashMap* __pClients;
217
218 }; // _ChannelWebAppStub
219
220 }} // Tizen::Io
221
222 #endif // _FIO_INTERNAL_CHANNEL_WEBAPP_STUB_H_
223