Merge "Changing the return value description of IsBitSet(), IsProbablePrimeNumber...
[platform/framework/native/appfw.git] / src / io / FIo_ClientChannelImpl.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_ClientChannelImpl.cpp
19  * @brief       This is the implementation file for the _ClientChannelImpl class.
20  *
21  */
22
23 #include <unique_ptr.h>
24
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"
33
34 using namespace std;
35
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 _ClientChannelImpl::_ClientChannelImpl(void)
45         : __pClientChannel(null)
46         , __pResponseListener(null)
47         , __pChannel(null)
48 {
49
50 }
51
52 _ClientChannelImpl::~_ClientChannelImpl(void)
53 {
54 }
55
56 result
57 _ClientChannelImpl::Construct(ClientChannel* pClientChannel, const Tizen::Base::String& channelName)
58 {
59         String channelId(_AppInfo::GetApplicationId());
60
61         if (!channelName.IsEmpty())
62         {
63                 channelId.Append(L'.');
64                 channelId.Append(channelName);
65         }
66
67         // Create a channel
68         _ChannelManager* pManager = _ChannelManager::GetInstance();
69         SysTryReturnResult(NID_IO, pManager != null, E_SYSTEM, "Failed to create a channel manager");
70
71         _Channel* pChannel = pManager->GetChannel(channelId);
72         SysTryReturnResult(NID_IO, pChannel != null, E_SYSTEM, "Failed to create a channel: [%ls]", channelId.GetPointer());
73
74         pChannel->SetChannelResponseEventListener(*this);
75
76         __pClientChannel = pClientChannel;
77         __pChannel = pChannel;
78
79         return E_SUCCESS;
80 }
81
82 void
83 _ClientChannelImpl::SetChannelResponseEventListener(IChannelResponseEventListener* pResponseEventListener)
84 {
85         __pResponseListener = pResponseEventListener;
86 }
87
88 result
89 _ClientChannelImpl::SendRequest(const String& serverChannelId, const IList* pArgs, RequestId& reqId)
90 {
91         const int APP_ID_LEN = 10;
92
93         String channelId = serverChannelId;
94         String execName;
95
96         if (serverChannelId.GetLength() <= APP_ID_LEN)
97         {
98                 execName = _PackageManagerImpl::GetInstance()->GetDefaultAppExecutableName(serverChannelId);
99                 channelId.Append(L'.');
100                 channelId.Append(execName);
101         }
102
103         if (pArgs == null)
104         {
105                 return  __pChannel->SendNullRequest(channelId, reqId);
106         }
107
108         const ArrayList* pList = dynamic_cast<const ArrayList*>(pArgs);
109         if (pList == null)
110         {
111                 unique_ptr<ArrayList> pArrayList(new (std::nothrow) ArrayList());
112                 pArrayList->Construct(*pArgs);
113
114                 return  __pChannel->SendRequest(channelId, *pArrayList, reqId);
115         }
116
117         return __pChannel->SendRequest(channelId, *pList, reqId);
118 }
119
120 void
121 _ClientChannelImpl::OnChannelResponseReceivedN(RequestId reqId, const String& channelId, IList* pArgs)
122 {
123         if (__pResponseListener && __pClientChannel)
124         {
125                 if (_AppInfo::IsOspCompat() == false)
126                 {
127                         __pResponseListener->OnChannelResponseReceivedN(reqId, *__pClientChannel, channelId, pArgs);
128
129                 }
130                 else
131                 {
132                         String appId;
133                         channelId.SubString(0, 10, appId);
134
135                         __pResponseListener->OnChannelResponseReceivedN(reqId, *__pClientChannel, appId, pArgs);
136                 }
137         }
138         else
139         {
140                 pArgs->RemoveAll(true);
141                 delete pArgs;
142         }
143 }
144
145 _ClientChannelImpl*
146 _ClientChannelImpl::GetInstance(ClientChannel* pChannel)
147 {
148         result r = E_SUCCESS;
149         static _ClientChannelImpl* pImpl = null;
150         if (pImpl == null)
151         {
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.");
154
155                 r = p->Construct(pChannel);
156                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
157
158                 pImpl = p.release();
159         }
160
161         return pImpl;
162 }
163
164 ClientChannel*
165 _ClientChannelImpl::GetClientChannel(const Tizen::Base::String& channelName)
166 {
167         _ChannelManager* pManager = _ChannelManager::GetInstance();
168         SysTryReturn(NID_IO, pManager != null, null, E_SYSTEM, "[E_SYSTEM] Failed to create a _ChannelManager.");
169
170         ClientChannel* pChannel = pManager->GetClientChannel(channelName);
171         if (pChannel == null)
172         {
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.");
176
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.");
179
180                 result r = pImpl->Construct(pChannel, channelName);
181                 SysTryCatch(NID_IO, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] Failed to initialize channel.");
182
183                 pChannel->__pClientChannelImpl = pImpl.release();
184
185                 pManager->AddClientChannel(channelName, pChannel);
186         }
187
188         return pChannel;
189
190 CATCH:
191         delete pChannel;
192
193         return null;
194 }
195
196 } } // Tizen::Io