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