c8345e978158222e538d865770b6ddd9dd82d8de
[platform/framework/native/appfw.git] / src / io / FIo_RemoteMessagePortImpl.cpp
1 //
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_RemoteMessagePortImpl.cpp
19  * @brief       This is the implementation file for the _RemoteMessagePortImpl class.
20  *
21  */
22
23 #include <unique_ptr.h>
24 #include <typeinfo>
25
26 #include <FBaseSysLog.h>
27 #include <FBaseColHashMap.h>
28 #include <FIoLocalMessagePort.h>
29 #include <FIoRemoteMessagePort.h>
30 #include <FIoIMessagePortListener.h>
31
32 #include "FIo_RemoteMessagePortImpl.h"
33 #include "FIo_MessagePortProxy.h"
34
35 using namespace std;
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::App;
40
41 namespace Tizen { namespace Io
42 {
43
44 _RemoteMessagePortImpl::_RemoteMessagePortImpl(void)
45         : __isTrusted(false)
46 {
47
48 }
49
50 _RemoteMessagePortImpl::~_RemoteMessagePortImpl(void)
51 {
52
53 }
54
55 result
56 _RemoteMessagePortImpl::Construct(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
57 {
58         result r = _MessagePortProxy::GetProxy()->RequestRemotePort(remoteAppId, remotePort, isTrusted);
59         SysTryReturnResult(NID_IO, r == E_SUCCESS, r, "Propagating.");
60
61         __remoteAppId = remoteAppId;
62         __remotePort = remotePort;
63         __isTrusted = isTrusted;
64
65         return E_SUCCESS;
66 }
67
68 AppId
69 _RemoteMessagePortImpl::GetAppId(void) const
70 {
71         return __remoteAppId;
72 }
73
74 String
75 _RemoteMessagePortImpl::GetName(void) const
76 {
77         return __remotePort;
78 }
79
80 bool
81 _RemoteMessagePortImpl::IsTrusted(void) const
82 {
83         return __isTrusted;
84 }
85
86 result
87 _RemoteMessagePortImpl::SendMessage(const IMap* pMessage)
88 {
89         SysTryReturnResult(NID_IO, pMessage != null, E_INVALID_ARG, "The argument is null.");
90
91         return _MessagePortProxy::GetProxy()->SendMessage(__remoteAppId, __remotePort, __isTrusted, (HashMap*)pMessage);
92 }
93
94 result
95 _RemoteMessagePortImpl::SendMessage(const LocalMessagePort* pLocalMessagePort, const IMap* pMessage)
96 {
97         SysTryReturnResult(NID_IO, pLocalMessagePort != null, E_INVALID_ARG, "The argument is null.");
98         SysTryReturnResult(NID_IO, pMessage != null, E_INVALID_ARG, "The argument is null.");
99
100         return _MessagePortProxy::GetProxy()->SendMessage(pLocalMessagePort->GetName(), pLocalMessagePort->IsTrusted(), __remoteAppId, __remotePort, __isTrusted, (HashMap*)pMessage);
101 }
102
103 RemoteMessagePort*
104 _RemoteMessagePortImpl::GetMessagePort(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
105 {
106         result r = E_SUCCESS;
107
108         RemoteMessagePort* pRemoteMessagePort = new (std::nothrow) RemoteMessagePort;
109         SysTryReturn(NID_IO, pRemoteMessagePort != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
110
111         unique_ptr<_RemoteMessagePortImpl> pImpl(new (std::nothrow) _RemoteMessagePortImpl);
112         SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
113
114         r = pImpl->Construct(remoteAppId, remotePort, isTrusted);
115         SysTryCatch(NID_IO, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
116
117         pRemoteMessagePort->__pRemoteMessagePortImpl = pImpl.release();
118
119         SetLastResult(r);
120         return pRemoteMessagePort;
121
122 CATCH:
123         delete pRemoteMessagePort;
124
125         return null;
126 }
127
128 RemoteMessagePort*
129 _RemoteMessagePortImpl::GetMessagePortOnly(const AppId& remoteAppId, const String& remotePort, bool isTrusted)
130 {
131         RemoteMessagePort* pRemoteMessagePort = new (std::nothrow) RemoteMessagePort;
132         SysTryReturn(NID_IO, pRemoteMessagePort != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
133
134         unique_ptr<_RemoteMessagePortImpl> pImpl(new (std::nothrow) _RemoteMessagePortImpl);
135         SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
136
137         pImpl->__remoteAppId = remoteAppId;
138         pImpl->__remotePort = remotePort;
139         pImpl->__isTrusted = isTrusted;
140
141         pRemoteMessagePort->__pRemoteMessagePortImpl = pImpl.release();
142
143         SetLastResult(E_SUCCESS);
144         return pRemoteMessagePort;
145
146 CATCH:
147         delete pRemoteMessagePort;
148
149         return null;
150 }
151
152 } } // Tizen::Io