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 FApp_ServiceAppImpl.cpp
19 * @brief This is the implementation for the _ServiceAppImpl class.
23 #include <linux/limits.h>
25 #include <FAppAppRegistry.h>
26 #include <FBaseErrors.h>
27 #include <FBaseSysLog.h>
29 #include <FBaseRt_LibraryImpl.h>
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"
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;
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;
52 static const RequestId HANDLER_REQUEST_ALARMID = 2;
54 namespace Tizen { namespace App
57 static const wchar_t* ALARM_PLUGIN_LIBRARY_PATH = L"/opt/apps/aospd00043/lib/libosp-cond-alarm.so";
58 typedef void (*OnAlarmForLaunch)(int alarmId);
60 _ServiceAppImpl* _ServiceAppImpl::__pServiceAppImpl = null;
63 _ServiceAppImpl::_ServiceAppImpl(ServiceApp* pServiceApp)
64 : __pAppImpl(_AppImpl::GetInstance())
65 , __pServiceApp(pServiceApp)
66 , __pAppTerminatingInternalEventListener(null)
67 , __pLifeDurationTimer(null)
69 , __pauseLifeDurationTimer(false)
71 __pServiceAppImpl = this;
72 SysTryReturnVoidResult(NID_APP, __pAppImpl, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
76 _ServiceAppImpl::~_ServiceAppImpl(void)
78 __pServiceAppImpl = null;
79 if( __pLifeDurationTimer )
81 __pLifeDurationTimer->Cancel();
82 delete __pLifeDurationTimer;
88 _ServiceAppImpl::OnCreate(void)
90 SysLog(NID_APP, "Platform creation event.");
92 _AppInfo::SetAppState(INITIALIZING);
99 _ServiceAppImpl::OnService(service_s* service, bool initial)
101 SysLog(NID_APP, "Service requested.");
102 char* pOperation = NULL;
103 int errVal = service_get_operation(service, &pOperation);
105 if ( (errVal == SERVICE_ERROR_NONE) && (!strcmp(pOperation, "osp.appsvc.operation.ALARM")) )
107 OnAlarmOperation(service);
115 if( __lifeDuration > 0 && __pauseLifeDurationTimer == false)
117 SetLifeDurationTimer(__lifeDuration);
122 _ServiceAppImpl::OnAlarmOperation(service_s* service)
124 char* pAlarmId = NULL;
126 int errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pAlarmId);
127 if (errVal == SERVICE_ERROR_NONE)
129 int alarmId = atoi(pAlarmId);
131 SysLog(NID_SYS, "Start to load external lib");
133 OnAlarmForLaunch pOnAlarmForLaunch = null;
134 result r = lib.Construct(ALARM_PLUGIN_LIBRARY_PATH);
138 SysLog(NID_SYS, "Open alarm condition library");
139 pOnAlarmForLaunch = (OnAlarmForLaunch)lib.GetProcAddress(L"OnAlarmForLaunch");
140 if(pOnAlarmForLaunch != null)
142 SysLog(NID_SYS, "Function is found");
143 pOnAlarmForLaunch(alarmId);
144 SysLog(NID_SYS, "Requested to check current alarm id to AlarmConditionHandler %d", alarmId);
148 SysLog(NID_SYS, "Fail to find alarm function");
153 SysLog(NID_SYS, "Fail to open alarm condition library");
164 _ServiceAppImpl::OnTerminate(void)
166 SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
168 if (_AppInfo::GetAppState() == TERMINATED)
173 _AppInfo::SetAppState(TERMINATING);
175 if (OnServiceAppImplTerminating(__pAppImpl->IsForcedTermination()) != true)
177 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
180 _AppInfo::SetAppState(TERMINATED);
185 _ServiceAppImpl::OnResume(void)
187 SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
192 _ServiceAppImpl::OnPause(void)
194 SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
199 _ServiceAppImpl::OnWindowHandleRequest(void)
206 _ServiceAppImpl::OnFrameRaiseRequested(void)
213 _ServiceAppImpl::GetInstance(void)
215 return __pServiceAppImpl;
220 _ServiceAppImpl::GetServiceAppInstance(void)
222 return __pServiceApp;
227 _ServiceAppImpl::OnAppInitializing(void)
229 const String& packageId = _AppInfo::GetPackageId();
230 const String& exeName = _AppInfo::GetAppExecutableName();
232 HashMapT<String, _AppFeatureInfoImpl*>* pInfo = _PackageManagerImpl::GetInstance()->GetPackageAppFeatureMapN(packageId, exeName);
236 _AppFeatureInfoImpl* pFeature = null;
238 result r = pInfo->GetValue(USE_UI_KEY, pFeature);
242 const String& val = pFeature->GetValue();
243 if (val == USE_UI_VAL_TRUE)
245 SysLog(NID_APP, "Using remote ui on service application.");
247 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
248 result (*pInit)(void) = null;
250 pInit = reinterpret_cast<result (*)()>(lib.GetProcAddress(L"InitializeUiFramework"));
254 SysLog(NID_APP, "[%s] UI initialized.", GetErrorMessage(r));
259 r = pInfo->GetValue(LIFE_DURATION_KEY, pFeature);
262 const String& val = pFeature->GetValue();
263 r = Integer::Parse(val, __lifeDuration);
266 SysLog(NID_APP, "LifeDuration is (%d) millis.", __lifeDuration);
270 _DeleteCollectionMapValue<String, _AppFeatureInfoImpl>(*pInfo);
274 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
275 return __pServiceApp->OnAppInitializing(*(AppRegistry::GetInstance()));
279 _ServiceAppImpl::OnAppInitialized(void)
281 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
282 return __pServiceApp->OnAppInitialized();
286 _ServiceAppImpl::OnServiceAppImplTerminating(bool forcedTermination)
288 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
290 if( __pAppTerminatingInternalEventListener)
292 __pAppTerminatingInternalEventListener->OnApplicationTerminated(L"", 0);
294 return __pServiceApp->OnAppTerminating(*(AppRegistry::GetInstance()), forcedTermination);
298 _ServiceAppImpl::SetLifeDurationTimer(int lifeDuration)
300 if( lifeDuration <= 0)
305 if( __pLifeDurationTimer == null)
307 __pLifeDurationTimer = new Timer;
308 __pLifeDurationTimer->Construct(*this);
309 SysLog(NID_APP, "Life duration timer is constructed.");
313 __pLifeDurationTimer->Cancel();
316 if( __pauseLifeDurationTimer == false)
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;
321 const int timeout = (__lifeDuration > maxTimeOut) ? maxTimeOut : __lifeDuration;
322 __pLifeDurationTimer->Start(timeout);
323 SysLog(NID_APP, "Life duration timer is started (%d millis)", timeout );
328 _ServiceAppImpl::OnTimerExpired(Timer& timer)
331 if( __pAppTerminatingInternalEventListener || __lifeDuration != 1800000 ) //Fixme: Remove this workaround code after all LifeDuration value of pre-loaded apps are fixed.
333 SysLog(NID_APP, "Life duration timer is expired, so terminating the application.");
334 App::GetInstance()->Terminate();
338 SysLog(NID_APP, "Life duration timer is ignored.");
343 _ServiceAppImpl::SetAppTerminatingInternalEventListener(_IAppEventListener* pListener)
345 __pAppTerminatingInternalEventListener = pListener;
349 _ServiceAppImpl::PauseLifeDurationTimer(void)
351 __pauseLifeDurationTimer = true;
352 if( __pLifeDurationTimer)
354 __pLifeDurationTimer->Cancel();
355 SysLog(NID_APP, "Life duration timer is paused." );
360 _ServiceAppImpl::ResumeLifeDurationTimer(void)
362 __pauseLifeDurationTimer = false;
363 SysLog(NID_APP, "Life duration timer will be resumed." );
367 _ServiceAppImpl::ResetLifeDurationTimer(void)
369 SetLifeDurationTimer(__lifeDuration);
373 _ServiceAppImpl::GetPrivilegeLevel(void)
375 const AppId appId = _AppInfo::GetApplicationId();
377 _PackageManagerImpl* pPkg = _PackageManagerImpl::GetInstance();
378 SysTryReturn(NID_APP, pPkg != null, _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid PackageManager instance.");
380 String pkgId = _PackageManagerImpl::GetPackageIdByAppId(appId);
381 SysTryReturn(NID_APP, !pkgId.IsEmpty(), _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid appId(%ls).", appId.GetPointer() );
383 PackageInfo* pInfo = pPkg->GetPackageInfoN(pkgId);
384 SysTryReturn(NID_APP, pInfo != null, _API_VISIBILITY_PUBLIC, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
386 const _PackageInfoImpl* pPkgInfo = _PackageInfoImpl::GetInstance(pInfo);
387 SysTryReturn(NID_APP, pPkgInfo != null, _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid PackageInfo instance.");
389 return pPkgInfo->GetApiVisibility();