sync with tizen_2.0
[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         const String& appId = _AppInfo::GetAppId();
59         const String& appExecutableName = _AppInfo::GetAppExecutableName();
60
61         String channelId(appId);
62         channelId.Append(L'.');
63         channelId.Append(appExecutableName);
64
65         if (!channelName.IsEmpty())
66         {
67                 channelId.Append(L'.');
68                 channelId.Append(channelName);
69         }
70
71         // Create a channel
72         _ChannelManager* pManager = _ChannelManager::GetInstance();
73         SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager");
74
75         _Channel* pChannel = pManager->GetChannel(channelId);
76         SysTryReturnResult(NID_IO, pChannel != null, E_SYSTEM, "Failed to create a channel: [%ls]", channelId.GetPointer());
77
78         pChannel->SetChannelRequestEventListener(*this);
79
80         __pServerChannel = pServerChannel;
81         __pChannel = pChannel;
82
83         return E_SUCCESS;
84 }
85
86 void
87 _ServerChannelImpl::SetChannelRequestEventListener(IChannelRequestEventListener* pRequestListener)
88 {
89         __pRequestListener = (IChannelRequestEventListener*) pRequestListener;
90 }
91
92 result
93 _ServerChannelImpl::SendResponse(const String& clientChannelId, const IList* pArgs, RequestId reqId)
94 {
95         const int APP_ID_LEN = 10;
96
97         String channelId = clientChannelId;
98         String execName;
99
100         if (clientChannelId.GetLength() <= APP_ID_LEN)
101         {
102                 execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(clientChannelId);
103                 channelId.Append(L'.');
104                 channelId.Append(execName);
105         }
106
107         if (pArgs == null)
108         {
109                 return __pChannel->SendNullResponse(channelId, reqId);
110         }
111
112         const ArrayList* pList = dynamic_cast<const ArrayList*>(pArgs);
113         if (pList == null)
114         {
115                 unique_ptr<ArrayList> pArrayList(new (std::nothrow) ArrayList());
116                 pArrayList->Construct(*pArgs);
117
118                 return __pChannel->SendResponse(channelId, *pArrayList, reqId);
119         }
120
121         return __pChannel->SendResponse(channelId, *pList, reqId);
122 }
123
124 void
125 _ServerChannelImpl::OnChannelRequestReceivedN(RequestId reqId, const String& channelId, IList* pArgs)
126 {
127         SysLog(NID_IO, "Received from [%ls]", channelId.GetPointer());
128
129         if (__pRequestListener != null)
130         {
131                 if (_AppInfo::IsOspCompat() == false)
132                 {
133                         __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, channelId, pArgs);
134                 }
135                 else
136                 {
137                         String appId;
138                         channelId.SubString(0, 10, appId);
139
140                         __pRequestListener->OnChannelRequestReceivedN(reqId, *__pServerChannel, appId, pArgs);
141                 }
142         }
143         else
144         {
145                 pArgs->RemoveAll(true);
146                 delete pArgs;
147         }
148 }
149
150
151 _ServerChannelImpl* 
152  _ServerChannelImpl::GetInstance(ServerChannel* pChannel)
153 {
154
155         result r = E_SUCCESS;
156         static _ServerChannelImpl* pImpl = null;
157         if (pImpl == null)
158         {
159                 unique_ptr<_ServerChannelImpl> p(new (std::nothrow) _ServerChannelImpl);
160                 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
161
162                 r = p->Construct(pChannel);
163                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
164
165                 pImpl = p.release();
166         }
167
168         return pImpl;
169 }
170
171 ServerChannel*
172 _ServerChannelImpl::GetServerChannel(const Tizen::Base::String& channelName)
173 {
174         _ChannelManager* pManager = _ChannelManager::GetInstance();
175         SysTryReturn(NID_IO, pManager != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a _ChannelManager.");
176
177         ServerChannel* pChannel = pManager->GetServerChannel(channelName);
178         if (pChannel == null)
179         {
180                 // unique_ptr cannot be used because Dtor is private
181                 pChannel = new (std::nothrow) ServerChannel;
182                 SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
183
184                 unique_ptr<_ServerChannelImpl> pImpl(new (std::nothrow) _ServerChannelImpl);
185                 SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
186
187                 result r = pImpl->Construct(pChannel, channelName);
188                 SysTryCatch(NID_IO, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
189
190                 pChannel->__pServerChannelImpl = pImpl.release();
191
192                 pManager->AddServerChannel(channelName, pChannel);
193         }
194
195         return pChannel;
196
197 CATCH:
198         delete pChannel;
199
200         return null;
201 }
202
203 } } // Tizen::Io