export _AppControlImpl::FindAndStart()
[platform/framework/native/appfw.git] / src / app / FApp_RequestManagerT.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        FApp_RequestManagerT.cpp
19  * @brief       This is the implementationfor the _RequestManagerT class.
20  */
21
22 #include <limits.h>
23
24 #include "FApp_RequestManagerT.h"
25 #include "FApp_LaunchInfo.h"
26 #include "FApp_AppArg.h"
27 #include "FApp_IAppControlPluginProvider.h"
28
29
30 using namespace Tizen::Base::Runtime;
31
32
33 namespace Tizen { namespace App
34 {
35
36 _ResultInfo::~_ResultInfo(void)
37 {
38         _AppArg* pArg = &arg;
39         delete pArg;
40 }
41
42 _InProcessInfo::~_InProcessInfo(void)
43 {
44         if (pProvider)
45         {
46                 pProvider->Release();
47         }
48 }
49
50 _DataControlInfo::~_DataControlInfo(void)
51 {
52         delete pArg;
53 }
54
55
56 template<typename T>
57 _RequestManagerT<T>::_RequestManagerT(void)
58 : __reqId(-1)
59 {
60         __requestList.Construct();
61 }
62
63 template<typename T>
64 _RequestManagerT<T>::~_RequestManagerT(void)
65 {
66         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
67         if (pIter == null)
68         {
69                 return;
70         }
71
72         while (pIter->MoveNext() == E_SUCCESS)
73         {
74                 T* pInfo = null;
75                 result r = pIter->GetValue(pInfo);
76
77                 if (IsFailed(r))
78                 {
79                         continue;
80                 }
81
82                 delete pInfo;
83         }
84
85         delete pIter;
86 }
87
88 template<typename T> int
89 _RequestManagerT<T>::InsertItem(T* pItem)
90 {
91         SysTryReturn(NID_APP, pItem != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Empty argument.");
92
93 #if 0
94         // [INFO] temporary disable due to OBS build system
95         __sync_add_and_fetch(&__reqId, 1);
96
97         // [TODO] awkward implementation after overflow
98         // overflow detection
99         __sync_bool_compare_and_swap(&__reqId, INT_MAX, 1);
100 #else
101         __reqId++;
102         if (__reqId >= INT_MAX)
103         {
104                 __reqId = 1;
105         }
106 #endif
107         SysLog(NID_APP, "Current request ID : %d.", __reqId);
108
109         pItem->reqId = __reqId;
110         __requestList.Add(__reqId, pItem);
111
112         return __reqId;
113 }
114
115 template<typename T> void
116 _RequestManagerT<T>::RemoveItem(int reqId)
117 {
118         T* pItem = FindItem(reqId);
119         if (pItem)
120         {
121                 __requestList.Remove(reqId);
122                 delete pItem;
123         }
124 }
125
126 template<typename T> void
127 _RequestManagerT<T>::RemoveItem(T* pItem)
128 {
129         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
130         if (pItem == null || pIter == null)
131         {
132                 return;
133         }
134
135         while (pIter->MoveNext() == E_SUCCESS)
136         {
137                 T* pInfo = null;
138                 result r = pIter->GetValue(pInfo);
139
140                 if (pInfo == pItem)
141                 {
142                         int id = -1;
143                         r = pIter->GetKey(id);
144                         if (r == E_SUCCESS)
145                         {
146                                 __requestList.Remove(id);
147                         }
148
149                         delete pItem;
150                         delete pIter;
151
152                         return;
153                 }
154         }
155
156         delete pIter;
157 }
158
159 template<typename T> T*
160 _RequestManagerT<T>::FindItem(int reqId) const
161 {
162         T* pItem = null;
163         result r = __requestList.GetValue(reqId, pItem);
164
165         return (r == E_SUCCESS) ? pItem : null;
166 }
167
168 template<typename T> T*
169 _RequestManagerT<T>::FindItemWithListener(Pred pred, IEventListener* pListener) const
170 {
171         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
172         if (pIter == null)
173         {
174                 return null;
175         }
176
177         while (pIter->MoveNext() == E_SUCCESS)
178         {
179                 T* pInfo = null;
180                 result r = pIter->GetValue(pInfo);
181
182                 if (pInfo)
183                 {
184                         if (pred(*pInfo, pListener))
185                         {
186                                 return pInfo;
187                         }
188                 }
189         }
190
191         return null;
192 }
193
194
195 // specialization
196 template<>
197 _RequestManagerT<_ResultInfo>::~_RequestManagerT(void)
198 {
199         // clear 1st request
200         _ResultInfo* pItem = FindItem(0);
201         if (pItem)
202         {
203                 __requestList.Remove(0);
204                 delete pItem;
205         }
206
207
208         RequestListEnumType* pIter = __requestList.GetMapEnumeratorN();
209         if (pIter == null)
210         {
211                 return;
212         }
213
214         while (pIter->MoveNext() == E_SUCCESS)
215         {
216                 _ResultInfo* pInfo = null;
217                 result r = pIter->GetValue(pInfo);
218
219                 if (IsFailed(r))
220                 {
221                         continue;
222                 }
223
224                 delete pInfo;
225         }
226
227         delete pIter;
228 }
229
230
231 template<> void
232 _RequestManagerT<_ResultInfo>::RemoveItem(int reqId)
233 {
234         // 1st request is launch request itself and do not remove it ever.
235         if (reqId == 0)
236         {
237                 return;
238         }
239
240         _ResultInfo* pItem = FindItem(reqId);
241         if (pItem)
242         {
243                 __requestList.Remove(reqId);
244                 delete pItem;
245         }
246 }
247
248
249 // explicit template initialization
250 template class _RequestManagerT<_DataControlInfo>;
251 template class _RequestManagerT<_InProcessInfo>;
252 template class _RequestManagerT<_ResultInfo>;
253
254 } } // Tizen::App
255