9dd9b39b1235c54d3b4ea664bb291ff1cbaaddee
[platform/framework/native/channel-service.git] / src / FIo_MessagePortService.cpp
1 // // Open Service Platform
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_MessagePortService.cpp
19  * @brief       This is the implementation file for the _MessagePortService class.
20  *
21  */
22
23 #include <unique_ptr.h>
24
25 #include <bundle.h>
26 #include <message-port.h>
27
28 #include <FBaseStringComparer.h>
29 #include <FBaseSysLog.h>
30
31 #include "FIo_MessagePortStub.h"
32 #include "FIo_MessagePortService.h"
33
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Io;
37 using namespace Tizen::App;
38
39 static const char LOCAL_APPID[] = "LOCAL_APPID";
40 static const char LOCAL_PORT[] = "LOCAL_PORT";
41 static const char TRUSTED_LOCAL[] = "TRUSTED_LOCAL";
42
43 static const char REMOTE_APPID[] = "REMOTE_APPID";
44 static const char REMOTE_PORT[] = "REMOTE_PORT";
45 static const char TRUSTED_REMOTE[] = "TRUSTED_REMOTE";
46 static const char TRUSTED_MESSAGE[] = "TRUSTED_MESSAGE";
47
48 _MessagePortService::_MessagePortService(void)
49         : __pStub(null)
50 {
51
52 }
53
54 _MessagePortService::~_MessagePortService(void)
55 {
56
57 }
58
59 result
60 _MessagePortService::Construct(_MessagePortStub& stub)
61 {
62         static _StringHashProvider hashProvider;
63         static _StringComparer stringComparer;
64         result r = E_SUCCESS;
65
66         __pPorts = new HashMapT <String, int>();
67         __pPorts->Construct(0, 0, hashProvider, stringComparer);
68
69         __pTrustedPorts = new HashMapT <String, int>();
70         __pTrustedPorts->Construct(0, 0, hashProvider, stringComparer);
71
72         __pStub = &stub;
73
74         return E_SUCCESS;
75 }
76
77 int
78 _MessagePortService::RegisterMessagePort(int clientId, const BundleBuffer& buffer)
79 {
80         bundle* b = buffer.b;
81
82         String key = GetKey(buffer);
83         String trusted(bundle_get_val(b, TRUSTED_LOCAL));
84
85         SysLog(NID_IO, "Register a message port: [%ls], client id = %d", key.GetPointer(), clientId);
86
87         bool out = false;
88
89         if (trusted.Equals(L"FALSE", false))
90         {
91                 __pPorts->ContainsKey(key, out);
92                 SysTryReturnResult(NID_IO, !out, MESSAGEPORT_ERROR_IO_ERROR,  "The port (%ls) has already registered", key.GetPointer());
93
94                 __pPorts->Add(key, clientId);
95         }
96         else
97         {
98                 __pTrustedPorts->ContainsKey(key, out);
99                 SysTryReturnResult(NID_IO, !out, MESSAGEPORT_ERROR_IO_ERROR,  "The trusted port (%ls) has already registered", key.GetPointer());
100
101                 __pTrustedPorts->Add(key, clientId);
102         }
103
104         return 0;
105 }
106
107 int
108 _MessagePortService::CheckRemotePort(const BundleBuffer& buffer)
109 {
110         bundle* b = buffer.b;
111
112         String key = GetKey(buffer, false);
113         String trusted(bundle_get_val(b, TRUSTED_REMOTE));
114
115         SysLog(NID_IO, "Check a remote message port: [%ls]", key.GetPointer());
116
117         bool out = false;
118
119         if (trusted.Equals(L"FALSE", false))
120         {
121                 __pPorts->ContainsKey(key, out);
122                 if (!out)
123                 {
124                         SysLogException(NID_IO, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The remote message port [%ls] is not found.", key.GetPointer());
125                         return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
126                 }
127         }
128         else
129         {
130                 __pTrustedPorts->ContainsKey(key, out);
131                 if (!out)
132                 {
133                         SysLogException(NID_IO, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The trusted remote message port [%ls] is not found.", key.GetPointer());
134                         return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
135                 }
136         }
137
138         return 0;
139 }
140
141 result
142 _MessagePortService::UnregisterMessagePort(int clientId)
143 {
144         result r = E_OBJ_NOT_FOUND;
145
146         int value = 0;
147         int count = __pPorts->GetCount();
148
149         String key;
150         IListT<String>* pKeys = __pPorts->GetKeysN();
151         SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
152
153         for (int i = 0; i < count; i++)
154         {
155                 pKeys->GetAt(i, key);
156                 __pPorts->GetValue(key, value);
157                 if (value == clientId)
158                 {
159                         SysLog(NID_IO, "Unregister - key = %ls", key.GetPointer());
160
161                         __pPorts->Remove(key);
162
163                         r = E_SUCCESS;
164                 }
165         }
166
167         delete pKeys;
168
169         count = __pTrustedPorts->GetCount();
170         pKeys = __pTrustedPorts->GetKeysN();
171         SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
172
173         for (int i = 0; i < count; i++)
174         {
175                 pKeys->GetAt(i, key);
176                 __pTrustedPorts->GetValue(key, value);
177                 if (value == clientId)
178                 {
179                         SysLog(NID_IO, "Unregister - key = %ls", key.GetPointer());
180
181                         __pTrustedPorts->Remove(key);
182
183                         r = E_SUCCESS;
184                 }
185         }
186
187         delete pKeys;
188
189         return r;
190 }
191
192 result
193 _MessagePortService::SendMessage(const BundleBuffer& buffer)
194 {
195         result r = E_SUCCESS;
196         int clientId = 0;
197
198         bundle* b = buffer.b;
199
200         String key = GetKey(buffer, false);
201         SysLog(NID_IO, "Sends a message to a remote message port [%ls]", key.GetPointer());
202
203         String trustedMessage(bundle_get_val(b, TRUSTED_MESSAGE));
204         if (trustedMessage.Equals(L"FALSE", false))
205         {
206                 r = __pPorts->GetValue(key, clientId);
207         }
208         else
209         {
210                 r = __pTrustedPorts->GetValue(key, clientId);
211         }
212
213         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OBJ_NOT_FOUND,
214                                 "The destination message port is not found.");
215
216         r = __pStub->SendMessage(clientId, buffer);
217         SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send a message");
218
219         return E_SUCCESS;
220 }
221
222 String 
223 _MessagePortService::GetKey(const BundleBuffer& buffer, bool local) const
224 {
225         const char* pAppId = null; 
226         const char* pPortName = null; 
227
228         if (local)
229         {
230                 pAppId = bundle_get_val(buffer.b, LOCAL_APPID);
231                 pPortName = bundle_get_val(buffer.b, LOCAL_PORT);
232         }
233         else
234         {
235                 pAppId = bundle_get_val(buffer.b, REMOTE_APPID);
236                 pPortName = bundle_get_val(buffer.b, REMOTE_PORT);
237         }
238
239         String key(pAppId);
240         key.Append(L':');
241         key.Append(pPortName);
242
243         return key;
244 }
245