fix memory leak(merged from tizen_2.2)
[platform/framework/native/appfw.git] / src / app / FApp_ServiceAppImpl.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_ServiceAppImpl.cpp
19  * @brief       This is the implementation for the _ServiceAppImpl class.
20  */
21
22 #include <cstdio>
23 #include <linux/limits.h>
24
25 #include <FAppAppRegistry.h>
26 #include <FBaseErrors.h>
27 #include <FBaseSysLog.h>
28 #include <FAppPkgPackageInfo.h>
29
30 #include <FBaseRt_LibraryImpl.h>
31
32 #include "FApp_AppInfo.h"
33 #include "FApp_AppImpl.h"
34 #include "FApp_ServiceAppImpl.h"
35 #include "FAppPkg_PackageManagerImpl.h"
36 #include "FAppPkg_PackageInfoImpl.h"
37 #include "FApp_AppManagerImpl.h"
38 #include "FApp_TemplateUtil.h"
39 #include "FApp_IAppEventListener.h"
40
41 using namespace Tizen::App::Package;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Base::Runtime;
45 using namespace Tizen::System;
46
47 const wchar_t USE_UI_KEY[] = L"UseUi";
48 const wchar_t USE_UI_VAL_TRUE[] = L"True";
49 const wchar_t LIFE_DURATION_KEY[] = L"LifeDuration";
50 const int LIFE_DURATION_MSEC_MAX = 30000;
51 const int LIFE_DURATION_MSEC_MAX_PLATFORM = 60000*3;
52
53 static const RequestId  HANDLER_REQUEST_ALARMID = 2;
54
55 namespace Tizen { namespace App
56 {
57
58 static const wchar_t* ALARM_PLUGIN_LIBRARY_PATH = L"/opt/apps/aospd00043/lib/libosp-cond-alarm.so";
59 typedef void (*OnAlarmForLaunch)(int alarmId);
60
61 _ServiceAppImpl* _ServiceAppImpl::__pServiceAppImpl = null;
62
63
64 _ServiceAppImpl::_ServiceAppImpl(ServiceApp* pServiceApp)
65         : __pAppImpl(_AppImpl::GetInstance())
66         , __pServiceApp(pServiceApp)
67         , __pAppTerminatingInternalEventListener(null)
68         , __pLifeDurationTimer(null)
69         , __lifeDuration(0)
70         , __pauseLifeDurationTimer(false)
71 {
72         __pServiceAppImpl = this;
73         SysTryReturnVoidResult(NID_APP, __pAppImpl, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
74 }
75
76
77 _ServiceAppImpl::~_ServiceAppImpl(void)
78 {
79         __pServiceAppImpl = null;
80         if( __pLifeDurationTimer )
81         {
82                 __pLifeDurationTimer->Cancel();
83                 delete __pLifeDurationTimer;
84         }
85 }
86
87
88 bool
89 _ServiceAppImpl::OnCreate(void)
90 {
91         SysLog(NID_APP, "Platform creation event.");
92
93         _AppInfo::SetAppState(INITIALIZING);
94
95         return true;
96 }
97
98
99 void
100 _ServiceAppImpl::OnService(service_s* service, bool initial)
101 {
102         SysLog(NID_APP, "Service requested.");
103         char* pOperation = NULL;
104         int errVal = service_get_operation(service, &pOperation);
105
106         if ( (errVal == SERVICE_ERROR_NONE) && (!strcmp(pOperation, "osp.appsvc.operation.ALARM")) )
107         {
108                 OnAlarmOperation(service);
109         }
110
111         if (pOperation)
112         {
113                 free(pOperation);
114         }
115
116         if( __lifeDuration > 0 && __pauseLifeDurationTimer == false)
117         {
118                 SetLifeDurationTimer(__lifeDuration);
119         }
120 }
121
122 void
123 _ServiceAppImpl::OnAlarmOperation(service_s* service)
124 {
125         char* pAlarmId = NULL;
126
127         int errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pAlarmId);
128         if (errVal == SERVICE_ERROR_NONE)
129         {
130                 int alarmId = atoi(pAlarmId);
131
132                 SysLog(NID_SYS, "Start to load external lib");
133                 Library lib;
134                 OnAlarmForLaunch pOnAlarmForLaunch = null;
135                 result r = lib.Construct(ALARM_PLUGIN_LIBRARY_PATH);
136
137                 if(r == E_SUCCESS)
138                 {
139                         SysLog(NID_SYS, "Open alarm condition library");
140                         pOnAlarmForLaunch = (OnAlarmForLaunch)lib.GetProcAddress(L"OnAlarmForLaunch");
141                         if(pOnAlarmForLaunch != null)
142                         {
143                                 SysLog(NID_SYS, "Function is found");
144                                 pOnAlarmForLaunch(alarmId);
145                                 SysLog(NID_SYS, "Requested to check current alarm id to AlarmConditionHandler %d", alarmId);
146                         }
147                         else
148                         {
149                                 SysLog(NID_SYS, "Fail to find alarm function");
150                         }
151                 }
152                 else
153                 {
154                         SysLog(NID_SYS, "Fail to open alarm condition library");
155                 }
156         }
157
158         if (pAlarmId)
159         {
160                 free(pAlarmId);
161         }
162 }
163
164 void
165 _ServiceAppImpl::OnTerminate(void)
166 {
167         SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
168
169         if (_AppInfo::GetAppState() == TERMINATED)
170         {
171                 return;
172         }
173
174         _AppInfo::SetAppState(TERMINATING);
175
176         if (OnServiceAppImplTerminating(__pAppImpl->IsForcedTermination()) != true)
177         {
178                 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
179         }
180
181         _AppInfo::SetAppState(TERMINATED);
182 }
183
184
185 void
186 _ServiceAppImpl::OnResume(void)
187 {
188         SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
189 }
190
191
192 void
193 _ServiceAppImpl::OnPause(void)
194 {
195         SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
196 }
197
198
199 long
200 _ServiceAppImpl::OnWindowHandleRequest(void)
201 {
202         return -1;
203 }
204
205
206 result
207 _ServiceAppImpl::OnFrameRaiseRequested(void)
208 {
209         return E_SUCCESS;
210 }
211
212
213 _ServiceAppImpl*
214 _ServiceAppImpl::GetInstance(void)
215 {
216         return __pServiceAppImpl;
217 }
218
219
220 ServiceApp*
221 _ServiceAppImpl::GetServiceAppInstance(void)
222 {
223         return __pServiceApp;
224 }
225
226
227 bool
228 _ServiceAppImpl::OnAppInitializing(void)
229 {
230         const String& packageId = _AppInfo::GetPackageId();
231         const String& exeName = _AppInfo::GetAppExecutableName();
232
233         HashMapT<String, _AppFeatureInfoImpl*>* pInfo = _PackageManagerImpl::GetInstance()->GetPackageAppFeatureMapN(packageId, exeName);
234
235         if (pInfo)
236         {
237                 _AppFeatureInfoImpl* pFeature = null;
238
239                 result r = pInfo->GetValue(USE_UI_KEY, pFeature);
240
241                 if (r == E_SUCCESS)
242                 {
243                         const String& val = pFeature->GetValue();
244                         if (val == USE_UI_VAL_TRUE)
245                         {
246                                 SysLog(NID_APP, "Using remote ui on service application.");
247
248                                 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
249                                 result (*pInit)(void) = null;
250
251                                 pInit = reinterpret_cast<result (*)()>(lib.GetProcAddress(L"InitializeUiFramework"));
252                                 if (pInit)
253                                 {
254                                         r = (*pInit)();
255                                         SysLog(NID_APP, "[%s] UI initialized.", GetErrorMessage(r));
256                                 }
257                         }
258                 }
259
260                 r = pInfo->GetValue(LIFE_DURATION_KEY, pFeature);
261                 if (r == E_SUCCESS)
262                 {
263                         const String& val = pFeature->GetValue();
264                         r = Integer::Parse(val, __lifeDuration);
265                         if( r == E_SUCCESS )
266                         {
267                                 SysLog(NID_APP, "LifeDuration is (%d) millis.", __lifeDuration);
268                         }
269                 }
270
271                 _DeleteCollectionMapValue<String, _AppFeatureInfoImpl>(*pInfo);
272                 delete pInfo;
273         }
274
275         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
276         return __pServiceApp->OnAppInitializing(*(AppRegistry::GetInstance()));
277 }
278
279 bool
280 _ServiceAppImpl::OnAppInitialized(void)
281 {
282         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
283         return __pServiceApp->OnAppInitialized();
284 }
285
286 bool
287 _ServiceAppImpl::OnServiceAppImplTerminating(bool forcedTermination)
288 {
289         SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
290
291         if( __pAppTerminatingInternalEventListener)
292         {
293                 __pAppTerminatingInternalEventListener->OnApplicationTerminated(L"", 0);
294         }
295         return __pServiceApp->OnAppTerminating(*(AppRegistry::GetInstance()), forcedTermination);
296 }
297
298 void
299 _ServiceAppImpl::SetLifeDurationTimer(int lifeDuration)
300 {
301         if( lifeDuration <= 0)
302         {
303                 return;
304         }
305
306         if( __pLifeDurationTimer == null)
307         {
308                 __pLifeDurationTimer = new Timer;
309                 __pLifeDurationTimer->Construct(*this);
310                 SysLog(NID_APP, "Life duration timer is constructed.");
311         }
312         else
313         {
314                 __pLifeDurationTimer->Cancel();
315         }
316
317         if( __pauseLifeDurationTimer == false)
318         {
319                 bool isPlatformPrivilege = (GetPrivilegeLevel() == _API_VISIBILITY_PARTNER_MANUFACTURER)? true : false;
320                 const int maxTimeOut = (isPlatformPrivilege == true)? LIFE_DURATION_MSEC_MAX_PLATFORM : LIFE_DURATION_MSEC_MAX;
321
322                 const int timeout = (__lifeDuration > maxTimeOut) ? maxTimeOut : __lifeDuration;
323                 __pLifeDurationTimer->Start(timeout);
324                 SysLog(NID_APP, "Life duration timer is started (%d millis)", timeout );
325         }
326 }
327
328 void
329 _ServiceAppImpl::OnTimerExpired(Timer& timer)
330 {
331         timer.Cancel();
332         if( __pAppTerminatingInternalEventListener || __lifeDuration != 1800000 ) //Fixme: Remove this workaround code after all LifeDuration value of pre-loaded apps are fixed.
333         {
334                 SysLog(NID_APP, "Life duration timer is expired, so terminating the application.");
335                 App::GetInstance()->Terminate();
336         }
337         else
338         {
339                 SysLog(NID_APP, "Life duration timer is ignored.");
340         }
341 }
342
343 void
344 _ServiceAppImpl::SetAppTerminatingInternalEventListener(_IAppEventListener* pListener)
345 {
346         __pAppTerminatingInternalEventListener = pListener;
347 }
348
349 void
350 _ServiceAppImpl::PauseLifeDurationTimer(void)
351 {
352         __pauseLifeDurationTimer = true;
353         if( __pLifeDurationTimer)
354         {
355                 __pLifeDurationTimer->Cancel();
356                 SysLog(NID_APP, "Life duration timer is paused." );
357         }
358 }
359
360 void
361 _ServiceAppImpl::ResumeLifeDurationTimer(void)
362 {
363         __pauseLifeDurationTimer = false;
364         SysLog(NID_APP, "Life duration timer will be resumed." );
365 }
366
367 void
368 _ServiceAppImpl::ResetLifeDurationTimer(void)
369 {
370         SetLifeDurationTimer(__lifeDuration);
371 }
372
373 int
374 _ServiceAppImpl::GetPrivilegeLevel(void)
375 {
376         static int visibility = _API_VISIBILITY_NONE;
377         if( visibility == _API_VISIBILITY_NONE)
378         {
379                 const AppId appId = _AppInfo::GetApplicationId();
380
381                 _PackageManagerImpl* pPkg = _PackageManagerImpl::GetInstance();
382                 SysTryReturn(NID_APP, pPkg != null, _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid PackageManager instance.");
383
384                 String pkgId = _PackageManagerImpl::GetPackageIdByAppId(appId);
385                 SysTryReturn(NID_APP, !pkgId.IsEmpty(), _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid appId(%ls).", appId.GetPointer() );
386
387                 std::unique_ptr<PackageInfo> pPkgInfo (pPkg->GetPackageInfoN(pkgId) );
388                 SysTryReturn(NID_APP, pPkgInfo != null, _API_VISIBILITY_PUBLIC, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
389
390                 const _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
391                 SysTryReturn(NID_APP, pPkgInfoImpl != null, _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid PackageInfo instance.");
392
393                 visibility = pPkgInfoImpl->GetApiVisibility();
394         }
395         return visibility;
396 }
397
398 } } //Tizen::App