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>
28 #include <FBaseStringComparer.h>
29 #include <FBaseSysLog.h>
31 #include "FIo_MessagePortStub.h"
32 #include "FIo_MessagePortService.h"
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Io;
37 using namespace Tizen::App;
39 static const char LOCAL_APPID[] = "LOCAL_APPID";
40 static const char LOCAL_PORT[] = "LOCAL_PORT";
41 static const char TRUSTED_LOCAL[] = "TRUSTED_LOCAL";
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";
48 _MessagePortService::_MessagePortService(void)
54 _MessagePortService::~_MessagePortService(void)
60 _MessagePortService::Construct(_MessagePortStub& stub)
62 static _StringHashProvider hashProvider;
63 static _StringComparer stringComparer;
66 __pPorts = new HashMapT <String, int>();
67 __pPorts->Construct(0, 0, hashProvider, stringComparer);
69 __pTrustedPorts = new HashMapT <String, int>();
70 __pTrustedPorts->Construct(0, 0, hashProvider, stringComparer);
78 _MessagePortService::RegisterMessagePort(int clientId, const BundleBuffer& buffer)
82 String key = GetKey(buffer);
83 String trusted(bundle_get_val(b, TRUSTED_LOCAL));
85 SysLog(NID_IO, "Register a message port: [%ls], client id = %d", key.GetPointer(), clientId);
89 if (trusted.Equals(L"FALSE", false))
91 __pPorts->ContainsKey(key, out);
92 SysTryReturnResult(NID_IO, !out, MESSAGEPORT_ERROR_IO_ERROR, "The port (%ls) has already registered", key.GetPointer());
94 __pPorts->Add(key, clientId);
98 __pTrustedPorts->ContainsKey(key, out);
99 SysTryReturnResult(NID_IO, !out, MESSAGEPORT_ERROR_IO_ERROR, "The trusted port (%ls) has already registered", key.GetPointer());
101 __pTrustedPorts->Add(key, clientId);
108 _MessagePortService::CheckRemotePort(const BundleBuffer& buffer)
110 bundle* b = buffer.b;
112 String key = GetKey(buffer, false);
113 String trusted(bundle_get_val(b, TRUSTED_REMOTE));
115 SysLog(NID_IO, "Check a remote message port: [%ls]", key.GetPointer());
119 if (trusted.Equals(L"FALSE", false))
121 __pPorts->ContainsKey(key, out);
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;
130 __pTrustedPorts->ContainsKey(key, out);
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;
142 _MessagePortService::UnregisterMessagePort(int clientId)
144 result r = E_OBJ_NOT_FOUND;
147 int count = __pPorts->GetCount();
150 IListT<String>* pKeys = __pPorts->GetKeysN();
151 SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
153 for (int i = 0; i < count; i++)
155 pKeys->GetAt(i, key);
156 __pPorts->GetValue(key, value);
157 if (value == clientId)
159 SysLog(NID_IO, "Unregister - key = %ls", key.GetPointer());
161 __pPorts->Remove(key);
169 count = __pTrustedPorts->GetCount();
170 pKeys = __pTrustedPorts->GetKeysN();
171 SysTryReturnResult(NID_IO, pKeys != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
173 for (int i = 0; i < count; i++)
175 pKeys->GetAt(i, key);
176 __pTrustedPorts->GetValue(key, value);
177 if (value == clientId)
179 SysLog(NID_IO, "Unregister - key = %ls", key.GetPointer());
181 __pTrustedPorts->Remove(key);
193 _MessagePortService::SendMessage(const BundleBuffer& buffer)
195 result r = E_SUCCESS;
198 bundle* b = buffer.b;
200 String key = GetKey(buffer, false);
201 SysLog(NID_IO, "Sends a message to a remote message port [%ls]", key.GetPointer());
203 String trustedMessage(bundle_get_val(b, TRUSTED_MESSAGE));
204 if (trustedMessage.Equals(L"FALSE", false))
206 r = __pPorts->GetValue(key, clientId);
210 r = __pTrustedPorts->GetValue(key, clientId);
213 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OBJ_NOT_FOUND,
214 "The destination message port is not found.");
216 r = __pStub->SendMessage(clientId, buffer);
217 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send a message");
223 _MessagePortService::GetKey(const BundleBuffer& buffer, bool local) const
225 const char* pAppId = null;
226 const char* pPortName = null;
230 pAppId = bundle_get_val(buffer.b, LOCAL_APPID);
231 pPortName = bundle_get_val(buffer.b, LOCAL_PORT);
235 pAppId = bundle_get_val(buffer.b, REMOTE_APPID);
236 pPortName = bundle_get_val(buffer.b, REMOTE_PORT);
241 key.Append(pPortName);