00df8cfdc212ef9b44e1cf63b1cd16f044e6f0f5
[platform/framework/native/appfw.git] / src / io / FIo_RemoteMessagePortImpl.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_RemoteMessagePortImpl.cpp
20  * @brief       This is the implementation file for the _RemoteMessagePortImpl class.
21  *
22  */
23
24 #include <unique_ptr.h>
25 #include <typeinfo>
26
27 #include <FBaseSysLog.h>
28 #include <FBaseColHashMap.h>
29 #include <FIoLocalMessagePort.h>
30 #include <FIoRemoteMessagePort.h>
31 #include <FIoIMessagePortListener.h>
32
33 #include "FIo_RemoteMessagePortImpl.h"
34 #include "FIo_MessagePortProxy.h"
35
36 using namespace std;
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::App;
41
42 namespace Tizen { namespace Io
43 {
44
45 _RemoteMessagePortImpl::_RemoteMessagePortImpl(void)
46         : __isTrusted(false)
47 {
48
49 }
50
51 _RemoteMessagePortImpl::~_RemoteMessagePortImpl(void)
52 {
53
54 }
55
56 result
57 _RemoteMessagePortImpl::Construct(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
58 {
59         result r = _MessagePortProxy::GetProxy()->RequestRemotePort(remoteAppId, remotePort, isTrusted);
60         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Propagating.");
61
62         __remoteAppId = remoteAppId;
63         __remotePort = remotePort;
64         __isTrusted = isTrusted;
65
66         return E_SUCCESS;
67 }
68
69 AppId
70 _RemoteMessagePortImpl::GetAppId(void) const
71 {
72         return __remoteAppId;
73 }
74
75 String
76 _RemoteMessagePortImpl::GetName(void) const
77 {
78         return __remotePort;
79 }
80
81 bool
82 _RemoteMessagePortImpl::IsTrusted(void) const
83 {
84         return __isTrusted;
85 }
86
87 result
88 _RemoteMessagePortImpl::SendMessage(const IMap* pMessage)
89 {
90         SysTryReturnResult(NID_IO, pMessage != null, E_INVALID_ARG, "The argument is null.");
91         SysTryReturnResult(NID_IO, CheckMessageType(pMessage), E_INVALID_ARG, "The argument is invalid.");
92
93         return _MessagePortProxy::GetProxy()->SendMessage(__remoteAppId, __remotePort, __isTrusted, (HashMap*)pMessage);
94 }
95
96 result
97 _RemoteMessagePortImpl::SendMessage(const LocalMessagePort* pLocalMessagePort, const IMap* pMessage)
98 {
99         SysTryReturnResult(NID_IO, pLocalMessagePort != null, E_INVALID_ARG, "The argument is null.");
100         SysTryReturnResult(NID_IO, pMessage != null, E_INVALID_ARG, "The argument is null.");
101         SysTryReturnResult(NID_IO, CheckMessageType(pMessage), E_INVALID_ARG, "The argument is invalid.");
102
103         return _MessagePortProxy::GetProxy()->SendMessage(pLocalMessagePort->GetName(), pLocalMessagePort->IsTrusted(), __remoteAppId, __remotePort, __isTrusted, (HashMap*)pMessage);
104 }
105
106 bool
107 _RemoteMessagePortImpl::CheckMessageType(const IMap* pMessage)
108 {
109         std::unique_ptr<IMapEnumerator> pEnum (pMessage->GetMapEnumeratorN());
110         while(pEnum->MoveNext() == E_SUCCESS)
111         {
112                 const String* pKey = dynamic_cast<const String*>(pEnum->GetKey());
113                 const Object* pValue = pEnum->GetValue();
114
115                 if (pKey && pValue)
116                 {
117                         if (typeid(*pValue) != typeid(const String))
118                         {
119                                 if (typeid(*pValue) != typeid(const ByteBuffer))
120                                 {
121                                         return false;
122                                 }
123                         }
124                 }
125                 else
126                 {
127                         return false;
128                 }
129         }
130
131         return true;
132 }
133
134 RemoteMessagePort*
135 _RemoteMessagePortImpl::GetMessagePort(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
136 {
137         result r = E_SUCCESS;
138
139         RemoteMessagePort* pRemoteMessagePort = new (std::nothrow) RemoteMessagePort;
140         SysTryReturn(NID_IO, pRemoteMessagePort != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
141
142         unique_ptr<_RemoteMessagePortImpl> pImpl(new (std::nothrow) _RemoteMessagePortImpl);
143         SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
144
145         r = pImpl->Construct(remoteAppId, remotePort, isTrusted);
146         //SysTryCatch(NID_IO, r != E_OBJ_NOT_FOUND, , E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The remote message port is not found.");
147         //SysTryCatch(NID_IO, r != E_CERTIFICATE_VERIFICATION_FAILED, , E_CERTIFICATE_VERIFICATION_FAILED, "[E_CERTIFICATE_VERIFICATION_FAILED] The target application is not signed with the same certificate.");
148         SysTryCatch(NID_IO, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
149
150         pRemoteMessagePort->__pRemoteMessagePortImpl = pImpl.release();
151
152         SetLastResult(r);
153         return pRemoteMessagePort;
154
155 CATCH:
156         delete pRemoteMessagePort;
157
158         return null;
159 }
160
161 RemoteMessagePort*
162 _RemoteMessagePortImpl::GetMessagePortOnly(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
163 {
164         RemoteMessagePort* pRemoteMessagePort = new (std::nothrow) RemoteMessagePort;
165         SysTryReturn(NID_IO, pRemoteMessagePort != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
166
167         unique_ptr<_RemoteMessagePortImpl> pImpl(new (std::nothrow) _RemoteMessagePortImpl);
168         SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
169
170         pImpl->__remoteAppId = remoteAppId;
171         pImpl->__remotePort = remotePort;
172         pImpl->__isTrusted = isTrusted;
173
174         pRemoteMessagePort->__pRemoteMessagePortImpl = pImpl.release();
175
176         SetLastResult(E_SUCCESS);
177         return pRemoteMessagePort;
178
179 CATCH:
180         delete pRemoteMessagePort;
181
182         return null;
183 }
184
185 } } // Tizen::Io