Remove the web channel
[platform/framework/native/channel-service.git] / src / FIo_ChannelService.cpp
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_ChannelService.cpp
20  * @brief       This is the implementation file for the _ChannelService class.
21  *
22  */
23
24 #include <unique_ptr.h>
25
26 #include <FBaseStringComparer.h>
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include <FBaseRt_EventDispatcher.h>
30 #include "FIo_ChannelService.h"
31 #include "FIo_ChannelMessages.h"
32 #include "FIo_IChannelServiceStub.h"
33 #include "FIo_ChannelCAppStub.h"
34
35 using namespace std;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Io;
40
41
42 namespace Tizen { namespace Io
43 {
44
45 _ChannelService::_ChannelService(void)
46         : __pIChannelServiceStub(null)
47 {
48
49 }
50
51 _ChannelService::~_ChannelService(void)
52 {
53
54 }
55
56 result
57 _ChannelService::Construct(_IChannelServiceStub& stub)
58 {
59         static _StringHashProvider hashProvider;
60         static _StringComparer stringComparer;
61         result r = E_SUCCESS;
62
63         __channels.Construct(0, 0, hashProvider, stringComparer);
64
65         __pIChannelServiceStub = &stub;
66
67         __pIChannelServiceStub->SetChannelService(*this);
68
69         // Create a CAppStub
70         unique_ptr<_ChannelCAppStub> pCStub(new (std::nothrow) _ChannelCAppStub());
71         SysTryReturnResult(NID_IO, pCStub != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
72
73         r = pCStub->Construct();
74         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to create a CAppStub.");
75
76         pCStub->SetChannelService(*this);
77
78         pCStub.release();
79
80         return E_SUCCESS;
81 }
82
83 result
84 _ChannelService::RegisterChannel(const String& channelId, const _IChannelServiceEventListener& listener)
85 {
86         return E_SUCCESS;
87 }
88
89 result
90 _ChannelService::RegisterChannel(const String& channelId, int clientId, unsigned int type)
91 {
92         result r = E_SUCCESS;
93         _ChannelInfo* pChannelInfo = null;
94
95         SysLog(NID_IO, "Register a channel : %ls.", channelId.GetPointer());
96
97         r = __channels.GetValue(channelId, pChannelInfo);
98         SysTryReturnResult(NID_IO, pChannelInfo == null, E_SYSTEM,  "Channel has already been registered.");
99
100         pChannelInfo = new (std::nothrow) _ChannelInfo;
101         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
102
103         pChannelInfo->channelId = channelId;
104         pChannelInfo->clientId = clientId;
105         pChannelInfo->type = type;
106         pChannelInfo->pGIOChannel = null;
107
108         __channels.Add(channelId, pChannelInfo);
109
110         return E_SUCCESS;
111 }
112
113 result
114 _ChannelService::RegisterChannel(const String& channelId, int clientId, void* pGIOChannel)
115 {
116         result r = E_SUCCESS;
117         _ChannelInfo* pChannelInfo = null;
118
119         SysLog(NID_IO, "Register a channel : [%ls]", channelId.GetPointer());
120
121         r = __channels.GetValue(channelId, pChannelInfo);
122         SysTryReturnResult(NID_IO, pChannelInfo == null, E_SYSTEM, "Channel has already been registered.");
123
124         pChannelInfo = new (std::nothrow) _ChannelInfo;
125         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
126
127         pChannelInfo->channelId = channelId;
128         pChannelInfo->clientId = clientId;
129         pChannelInfo->type = CAPP_CHANNEL;
130         pChannelInfo->pGIOChannel = pGIOChannel;
131
132         __channels.Add(channelId, pChannelInfo);
133
134         return E_SUCCESS;
135 }
136
137 result
138 _ChannelService::UnregisterChannel(const String& channelId)
139 {
140         return E_SUCCESS;
141 }
142
143 result
144 _ChannelService::UnregisterChannel(int clientId)
145 {
146         SysLog(NID_IO, "Unregister - clientId =  %d", clientId);
147
148         result r = E_OBJ_NOT_FOUND;
149
150         String key;
151         _ChannelInfo* pValue = null;
152         IListT<String>* pKeys = __channels.GetKeysN();
153         SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
154
155         int count = __channels.GetCount();
156
157         for (int i = 0; i < count; i++)
158         {
159                 pKeys->GetAt(i, key);
160                 __channels.GetValue(key, pValue);
161                 if (pValue != null && pValue->clientId == clientId)
162                 {
163                         SysLog(NID_IO, "Unregister - ChannelId = %ls", key.GetPointer());
164                         __channels.Remove(key);
165                         delete pValue;
166
167                         r = E_SUCCESS;
168                 }
169         }
170
171         delete pKeys;
172
173         return r;
174 }
175
176 result
177 _ChannelService::SendRequest(const String& src,
178                                                          const String& dest,
179                                                          const ArrayList& args,
180                                                          int requestId)
181 {
182         SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
183
184         _ChannelInfo* pChannelInfo = null;
185
186         __channels.GetValue(dest, pChannelInfo);
187         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
188                                 "Destination channel is not found.");
189
190         result r = __pIChannelServiceStub->SendRequest(pChannelInfo->clientId, src, dest, args, requestId);
191         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the request data.");
192
193         return E_SUCCESS;
194 }
195
196 result
197 _ChannelService::SendNullRequest(const String& src,
198                                                          const String& dest,
199                                                          int requestId)
200 {
201         SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
202
203         _ChannelInfo* pChannelInfo = null;
204
205         __channels.GetValue(dest, pChannelInfo);
206         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
207                                 "Destination channel is not found.");
208
209         __pIChannelServiceStub->SendNullRequest(pChannelInfo->clientId, src, dest, requestId);
210
211         return E_SUCCESS;
212 }
213
214 result
215 _ChannelService::SendResponse(const String& src,
216                                                           const String& dest,
217                                                           const ArrayList& args,
218                                                           int requestId)
219 {
220         SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
221
222         _ChannelInfo* pChannelInfo = null;
223
224         __channels.GetValue(dest, pChannelInfo);
225         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
226                                 "Destination channel not found.");
227
228         // Channel for CApp
229         if (pChannelInfo->type == CAPP_CHANNEL)
230         {
231                 bool ret = _ChannelCAppStub::SendResponse(requestId, pChannelInfo->pGIOChannel, args);
232                 SysTryReturnResult(NID_IO, ret, E_SYSTEM, "Failed to send the data to a CApp.");
233         }
234         // Channel for OspApp
235         else
236         {
237                 result r = __pIChannelServiceStub->SendResponse(pChannelInfo->clientId, src, dest, args, requestId);
238                 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send the response data");
239         }
240
241         return E_SUCCESS;
242 }
243
244 result
245 _ChannelService::SendNullResponse(const String& src,
246                                                           const String& dest,
247                                                           int requestId)
248 {
249         SysLog(NID_IO, "[%ls] ---> [%ls], Request ID = %d", src.GetPointer(), dest.GetPointer(), requestId);
250
251         _ChannelInfo* pChannelInfo = null;
252
253         __channels.GetValue(dest, pChannelInfo);
254         SysTryReturnResult(NID_IO, pChannelInfo != null, E_OBJ_NOT_FOUND,
255                                 "Destination channel not found.");
256
257         __pIChannelServiceStub->SendNullResponse(pChannelInfo->clientId, src, dest, requestId);
258
259         return E_SUCCESS;
260 }
261
262 bool
263 _ChannelService::IsChannelRegistered(const String& channelId)
264 {
265         bool out = false;
266
267         __channels.ContainsKey(channelId, out);
268
269         return out;
270 }
271
272 }}