1 // // Open Service Platform
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_MessagePortService.cpp
19 * @brief This is the implementation file for the _MessagePortService class.
23 #include <unique_ptr.h>
26 #include <message-port.h>
27 #include <package_manager.h>
29 #include <FBaseStringComparer.h>
30 #include <FBaseSysLog.h>
31 #include <FBase_StringConverter.h>
33 #include "FIo_MessagePortStub.h"
34 #include "FIo_MessagePortService.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Io;
39 using namespace Tizen::App;
43 static const char LOCAL_APPID[] = "LOCAL_APPID";
44 static const char LOCAL_PORT[] = "LOCAL_PORT";
45 static const char TRUSTED_LOCAL[] = "TRUSTED_LOCAL";
47 static const char REMOTE_APPID[] = "REMOTE_APPID";
48 static const char REMOTE_PORT[] = "REMOTE_PORT";
49 static const char TRUSTED_REMOTE[] = "TRUSTED_REMOTE";
50 static const char TRUSTED_MESSAGE[] = "TRUSTED_MESSAGE";
52 _MessagePortService::_MessagePortService(void)
58 _MessagePortService::~_MessagePortService(void)
64 _MessagePortService::Construct(_MessagePortStub& stub)
66 static _StringHashProvider hashProvider;
67 static _StringComparer stringComparer;
69 __pPorts = new HashMapT <String, int>();
70 __pPorts->Construct(0, 0, hashProvider, stringComparer);
72 __pTrustedPorts = new HashMapT <String, int>();
73 __pTrustedPorts->Construct(0, 0, hashProvider, stringComparer);
81 _MessagePortService::RegisterMessagePort(int clientId, const BundleBuffer& buffer)
85 String key = GetKey(buffer);
86 String trusted(bundle_get_val(b, TRUSTED_LOCAL));
88 SysLog(NID_IO, "Register a message port: [%ls], client id = %d", key.GetPointer(), clientId);
92 if (trusted.Equals(L"TRUE", false))
94 r = __pTrustedPorts->GetValue(key, value);
97 SysTryReturn(NID_IO, value != clientId, MESSAGEPORT_ERROR_IO_ERROR, E_SYSTEM,
98 "[E_SYSTEM] The local message port (%ls) has already registered", key.GetPointer());
100 SysLog(NID_IO, "Remove garbage values : %ls", key.GetPointer());
101 __pTrustedPorts->Remove(key);
104 __pTrustedPorts->Add(key, clientId);
108 r = __pPorts->GetValue(key, value);
111 SysTryReturn(NID_IO, value != clientId, MESSAGEPORT_ERROR_IO_ERROR, E_SYSTEM,
112 "[E_SYSTEM] The local message port (%ls) has already registered", key.GetPointer());
114 SysLog(NID_IO, "Remove garbage values : %ls", key.GetPointer());
115 __pPorts->Remove(key);
118 __pPorts->Add(key, clientId);
125 _MessagePortService::CheckRemotePort(const BundleBuffer& buffer)
127 bundle* b = buffer.b;
129 String key = GetKey(buffer, false);
130 String trusted(bundle_get_val(b, TRUSTED_REMOTE));
132 SysLog(NID_IO, "Check a remote message port: [%ls]", key.GetPointer());
136 if (trusted.Equals(L"TRUE", false))
138 __pTrustedPorts->ContainsKey(key, out);
142 __pPorts->ContainsKey(key, out);
145 SysTryReturn(NID_IO, out == true, MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND, E_OBJ_NOT_FOUND,
146 "[E_OBJ_NOT_FOUND] The remote message port (%ls) is not found.", key.GetPointer());
148 if (trusted.Equals(L"TRUE", false))
150 String localAppId = bundle_get_val(b, LOCAL_APPID);
151 String remoteAppId = bundle_get_val(b, REMOTE_APPID);
153 // Check the preloaded
154 if (!IsPreloaded(localAppId, remoteAppId))
156 // Check the certificate
157 return CheckCertificate(localAppId, remoteAppId);
165 _MessagePortService::UnregisterMessagePort(int clientId)
167 result r = E_OBJ_NOT_FOUND;
170 int count = __pPorts->GetCount();
173 IListT<String>* pKeys = __pPorts->GetKeysN();
174 SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
176 for (int i = 0; i < count; i++)
178 pKeys->GetAt(i, key);
179 __pPorts->GetValue(key, value);
180 if (value == clientId)
182 SysLog(NID_IO, "Unregister - key = %ls", key.GetPointer());
184 __pPorts->Remove(key);
192 count = __pTrustedPorts->GetCount();
193 pKeys = __pTrustedPorts->GetKeysN();
194 SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
196 for (int i = 0; i < count; i++)
198 pKeys->GetAt(i, key);
199 __pTrustedPorts->GetValue(key, value);
200 if (value == clientId)
202 SysLog(NID_IO, "Unregister - key = %ls", key.GetPointer());
204 __pTrustedPorts->Remove(key);
216 _MessagePortService::SendMessage(const BundleBuffer& metadata, const BundleBuffer& buffer)
218 result r = E_SUCCESS;
221 bundle* b = metadata.b;
223 String key = GetKey(metadata, false);
224 SysLog(NID_IO, "Sends a message to a remote message port [%ls]", key.GetPointer());
226 String trustedMessage(bundle_get_val(b, TRUSTED_MESSAGE));
227 if (trustedMessage.Equals(L"TRUE", false))
229 r = __pTrustedPorts->GetValue(key, clientId);
233 r = __pPorts->GetValue(key, clientId);
236 SysTryReturn(NID_IO, r == E_SUCCESS, MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND, E_OBJ_NOT_FOUND,
237 "[E_OBJ_NOT_FOUND] The destination message port (%ls) is not found.", key.GetPointer());
239 if (trustedMessage.Equals(L"TRUE", false))
241 String localAppId = bundle_get_val(b, LOCAL_APPID);
242 String remoteAppId = bundle_get_val(b, REMOTE_APPID);
244 // Check the preloaded
245 if (!IsPreloaded(localAppId, remoteAppId))
247 // Check the certificate
248 int ret = CheckCertificate(localAppId, remoteAppId);
256 r = __pStub->SendMessage(clientId, metadata, buffer);
257 SysTryReturn(NID_IO, r == E_SUCCESS, MESSAGEPORT_ERROR_IO_ERROR, E_SYSTEM,
258 "[E_SYSTEM] Failed to send a message.");
264 _MessagePortService::GetKey(const BundleBuffer& buffer, bool local) const
266 const char* pAppId = null;
267 const char* pPortName = null;
269 bundle* b = buffer.b;
273 pAppId = bundle_get_val(b, LOCAL_APPID);
274 pPortName = bundle_get_val(b, LOCAL_PORT);
278 pAppId = bundle_get_val(b, REMOTE_APPID);
279 pPortName = bundle_get_val(b, REMOTE_PORT);
284 key.Append(pPortName);
290 _MessagePortService::IsPreloaded(const String& localAppId, const String& remoteAppId) const
292 bool preload_local = false;
293 bool preload_remote = false;
295 unique_ptr<char[]> pLocalAppId(_StringConverter::CopyToCharArrayN(localAppId));
296 unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
298 int ret = package_manager_is_preload_package_by_app_id(pLocalAppId.get(), &preload_local);
301 ret = package_manager_is_preload_package_by_app_id(pRemoteAppId.get(), &preload_remote);
304 if (preload_local && preload_remote)
311 SysLog(NID_IO, "Failed to check the preloaded application. %d", ret);
316 SysLog(NID_IO, "Failed to check the preloaded application. %d", ret);
323 _MessagePortService::CheckCertificate(const String& localAppId, const String& remoteAppId) const
325 package_manager_compare_result_type_e res;
327 unique_ptr<char[]> pLocalAppId(_StringConverter::CopyToCharArrayN(localAppId));
328 unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
330 int ret = package_manager_compare_app_cert_info(pLocalAppId.get(), pRemoteAppId.get(), &res);
331 SysTryReturn(NID_IO, ret == 0, MESSAGEPORT_ERROR_IO_ERROR, E_SYSTEM,
332 "[E_SYSTEM] Failed to check the certificate: %d", ret);
334 SysTryReturn(NID_IO, res == PACAKGE_MANAGER_COMPARE_MATCH, MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH, E_CERTIFICATE_VERIFICATION_FAILED,
335 "[E_CERTIFICATE_VERIFICATION_FAILED] Both applications are not signed with the same certificate: %d", res);