sync with master
[platform/framework/native/appfw.git] / src / io / FIo_ServerChannelImpl.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_ServerChannelImpl.cpp
20  * @brief       This is the implementation file for the _ServerChannelImpl class.
21  *
22  */
23
24 #include <unique_ptr.h>
25
26 #include <FApp_AppInfo.h>
27 #include <FBaseSysLog.h>
28 #include <FIoChannelTypes.h>
29 #include <FIoServerChannel.h>
30 #include <FAppPkg_PackageManagerImpl.h>
31 #include "FIo_ServerChannelImpl.h"
32 #include "FIo_Channel.h"
33 #include "FIo_ChannelManager.h"
34
35 using namespace std;
36 using namespace Tizen::App;
37 using namespace Tizen::App::Package;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40
41 namespace Tizen { namespace Io
42 {
43
44 _ServerChannelImpl::_ServerChannelImpl(void)
45         : __pServerChannel(null)
46         , __pRequestListener(null)
47         , __pChannel(null)
48 {
49 }
50
51 _ServerChannelImpl::~_ServerChannelImpl(void)
52 {
53 }
54
55 result
56 _ServerChannelImpl::Construct(ServerChannel* pServerChannel, const Tizen::Base::String& channelName)
57 {
58         String channelId(_AppInfo::GetApplicationId());
59
60         if (!channelName.IsEmpty())
61         {
62                 channelId.Append(L'.');
63                 channelId.Append(channelName);
64         }
65
66         // Create a channel
67         _ChannelManager* pManager = _ChannelManager::GetInstance();
68         SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager");
69
70         _Channel* pChannel = pManager->GetChannel(channelId);
71         SysTryReturnResult(NID_IO, pChannel != null, E_SYSTEM, "Failed to create a channel: [%ls]", channelId.GetPointer());
72
73         pChannel->SetChannelRequestEventListener(*this);
74
75         __pServerChannel = pServerChannel;
76         __pChannel = pChannel;
77
78         return E_SUCCESS;
79 }
80
81 void
82 _ServerChannelImpl::SetChannelRequestEventListener(IChannelRequestEventListener* pRequestListener)
83 {
84         __pRequestListener = (IChannelRequestEventListener*) pRequestListener;
85 }
86
87 result
88 _ServerChannelImpl::SendResponse(const String& clientChannelId, const IList* pArgs, RequestId reqId)
89 {
90         const int APP_ID_LEN = 10;
91
92         String channelId = clientChannelId;
93         String execName;
94
95         if (clientChannelId.GetLength() <= APP_ID_LEN)
96         {
97                 execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(clientChannelId);
98                 channelId.Append(L'.');
99                 channelId.Append(execName);
100         }
101
102         if (pArgs == null)
103         {
104                 return __pChannel->SendNullResponse(channelId, reqId);
105         }
106
107         const ArrayList* pList = dynamic_cast<const ArrayList*>(pArgs);
108         if (pList == null)
109         {
110                 unique_ptr<ArrayList> pArrayList(new (std::nothrow) ArrayList());
111                 pArrayList->Construct(*pArgs);
112
113                 return __pChannel->SendResponse(channelId, *pArrayList, reqId);
114         }
115
116         return __pChannel->SendResponse(channelId, *pList, reqId);
117 }
118
119 void
120 _ServerChannelImpl::OnChannelRequestReceivedN(RequestId reqId, const String& channelId, IList* pArgs)
121 {
122         SysLog(NID_IO, "Received from [%ls]", channelId.GetPointer());
123
124         if (__pRequestListener != null)
125         {
126                 if (_AppInfo::IsOspCompat() == false)
127                 {
128                         __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, channelId, pArgs);
129                 }
130                 else
131                 {
132                         String appId;
133                         channelId.SubString(0, 10, appId);
134
135                         __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, appId, pArgs);
136                 }
137         }
138         else
139         {
140                 pArgs->RemoveAll(true);
141                 delete pArgs;
142         }
143 }
144
145
146 _ServerChannelImpl* 
147  _ServerChannelImpl::GetInstance(ServerChannel* pChannel)
148 {
149
150         result r = E_SUCCESS;
151         static _ServerChannelImpl* pImpl = null;
152         if (pImpl == null)
153         {
154                 unique_ptr<_ServerChannelImpl> p(new (std::nothrow) _ServerChannelImpl);
155                 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
156
157                 r = p->Construct(pChannel);
158                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
159
160                 pImpl = p.release();
161         }
162
163         return pImpl;
164 }
165
166 ServerChannel*
167 _ServerChannelImpl::GetServerChannel(const Tizen::Base::String& channelName)
168 {
169         _ChannelManager* pManager = _ChannelManager::GetInstance();
170         SysTryReturn(NID_IO, pManager != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a _ChannelManager.");
171
172         ServerChannel* pChannel = pManager->GetServerChannel(channelName);
173         if (pChannel == null)
174         {
175                 // unique_ptr cannot be used because Dtor is private
176                 pChannel = new (std::nothrow) ServerChannel;
177                 SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
178
179                 unique_ptr<_ServerChannelImpl> pImpl(new (std::nothrow) _ServerChannelImpl);
180                 SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
181
182                 result r = pImpl->Construct(pChannel, channelName);
183                 SysTryCatch(NID_IO, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
184
185                 pChannel->__pServerChannelImpl = pImpl.release();
186
187                 pManager->AddServerChannel(channelName, pChannel);
188         }
189
190         return pChannel;
191
192 CATCH:
193         delete pChannel;
194
195         return null;
196 }
197
198 } } // Tizen::Io