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_ClientChannelImpl.cpp
19 * @brief This is the implementation file for the _ClientChannelImpl class.
23 #include <unique_ptr.h>
25 #include <FApp_AppInfo.h>
26 #include <FBaseSysLog.h>
27 #include <FIoClientChannel.h>
28 #include <FIoChannelTypes.h>
29 #include <FAppPkg_PackageManagerImpl.h>
30 #include "FIo_ClientChannelImpl.h"
31 #include "FIo_Channel.h"
32 #include "FIo_ChannelManager.h"
36 using namespace Tizen::App;
37 using namespace Tizen::App::Package;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
41 namespace Tizen { namespace Io
44 _ClientChannelImpl::_ClientChannelImpl(void)
45 : __pClientChannel(null)
46 , __pResponseListener(null)
52 _ClientChannelImpl::~_ClientChannelImpl(void)
57 _ClientChannelImpl::Construct(ClientChannel* pClientChannel, const Tizen::Base::String& channelName)
59 String channelId(_AppInfo::GetApplicationId());
61 if (!channelName.IsEmpty())
63 channelId.Append(L'.');
64 channelId.Append(channelName);
68 _ChannelManager* pManager = _ChannelManager::GetInstance();
69 SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager");
71 _Channel* pChannel = pManager->GetChannel(channelId);
72 SysTryReturnResult(NID_IO, pChannel != null, E_SYSTEM, "Failed to create a channel: [%ls]", channelId.GetPointer());
74 pChannel->SetChannelResponseEventListener(*this);
76 __pClientChannel = pClientChannel;
77 __pChannel = pChannel;
83 _ClientChannelImpl::SetChannelResponseEventListener(IChannelResponseEventListener* pResponseEventListener)
85 __pResponseListener = pResponseEventListener;
89 _ClientChannelImpl::SendRequest(const String& serverChannelId, const IList* pArgs, RequestId& reqId)
91 const int APP_ID_LEN = 10;
93 String channelId = serverChannelId;
96 if (serverChannelId.GetLength() <= APP_ID_LEN)
98 execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(serverChannelId);
99 channelId.Append(L'.');
100 channelId.Append(execName);
105 return __pChannel->SendNullRequest(channelId, reqId);
108 const ArrayList* pList = dynamic_cast<const ArrayList*>(pArgs);
111 unique_ptr<ArrayList> pArrayList(new (std::nothrow) ArrayList());
112 pArrayList->Construct(*pArgs);
114 return __pChannel->SendRequest(channelId, *pArrayList, reqId);
117 return __pChannel->SendRequest(channelId, *pList, reqId);
121 _ClientChannelImpl::OnChannelResponseReceivedN(RequestId reqId, const String& channelId, IList* pArgs)
123 if (__pResponseListener && __pClientChannel)
125 if (_AppInfo::IsOspCompat() == false)
127 __pResponseListener->OnChannelResponseReceivedN(reqId, *__pClientChannel, channelId, pArgs);
133 channelId.SubString(0, 10, appId);
135 __pResponseListener->OnChannelResponseReceivedN(reqId, *__pClientChannel, appId, pArgs);
140 pArgs->RemoveAll(true);
146 _ClientChannelImpl::GetInstance(ClientChannel* pChannel)
148 result r = E_SUCCESS;
149 static _ClientChannelImpl* pImpl = null;
152 unique_ptr<_ClientChannelImpl> p(new (std::nothrow) _ClientChannelImpl);
153 SysTryReturn(NID_IO, p != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
155 r = p->Construct(pChannel);
156 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
165 _ClientChannelImpl::GetClientChannel(const Tizen::Base::String& channelName)
167 _ChannelManager* pManager = _ChannelManager::GetInstance();
168 SysTryReturn(NID_IO, pManager != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a _ChannelManager.");
170 ClientChannel* pChannel = pManager->GetClientChannel(channelName);
171 if (pChannel == null)
173 // unique_ptr cannot be used because Dtor is private
174 pChannel = new (std::nothrow) ClientChannel;
175 SysTryReturn(NID_IO, pChannel != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
177 unique_ptr<_ClientChannelImpl> pImpl(new (std::nothrow) _ClientChannelImpl);
178 SysTryCatch(NID_IO, pImpl != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
180 result r = pImpl->Construct(pChannel, channelName);
181 SysTryCatch(NID_IO, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
183 pChannel->__pClientChannelImpl = pImpl.release();
185 pManager->AddClientChannel(channelName, pChannel);