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>
28 #include <FAppPkgPackageInfo.h>
30 #include <FBaseRt_LibraryImpl.h>
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"
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;
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;
53 static const RequestId HANDLER_REQUEST_ALARMID = 2;
55 namespace Tizen { namespace App
58 static const wchar_t* ALARM_PLUGIN_LIBRARY_PATH = L"/opt/apps/aospd00043/lib/libosp-cond-alarm.so";
59 typedef void (*OnAlarmForLaunch)(int alarmId);
61 _ServiceAppImpl* _ServiceAppImpl::__pServiceAppImpl = null;
64 _ServiceAppImpl::_ServiceAppImpl(ServiceApp* pServiceApp)
65 : __pAppImpl(_AppImpl::GetInstance())
66 , __pServiceApp(pServiceApp)
67 , __pAppTerminatingInternalEventListener(null)
68 , __pLifeDurationTimer(null)
70 , __pauseLifeDurationTimer(false)
72 __pServiceAppImpl = this;
73 SysTryReturnVoidResult(NID_APP, __pAppImpl, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
77 _ServiceAppImpl::~_ServiceAppImpl(void)
79 __pServiceAppImpl = null;
80 if( __pLifeDurationTimer )
82 __pLifeDurationTimer->Cancel();
83 delete __pLifeDurationTimer;
89 _ServiceAppImpl::OnCreate(void)
91 SysLog(NID_APP, "Platform creation event.");
93 _AppInfo::SetAppState(INITIALIZING);
100 _ServiceAppImpl::OnService(service_s* service, bool initial)
102 SysLog(NID_APP, "Service requested.");
103 char* pOperation = NULL;
104 int errVal = service_get_operation(service, &pOperation);
106 if ( (errVal == SERVICE_ERROR_NONE) && (!strcmp(pOperation, "osp.appsvc.operation.ALARM")) )
108 OnAlarmOperation(service);
116 if( __lifeDuration > 0 && __pauseLifeDurationTimer == false)
118 SetLifeDurationTimer(__lifeDuration);
123 _ServiceAppImpl::OnAlarmOperation(service_s* service)
125 char* pAlarmId = NULL;
127 int errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pAlarmId);
128 if (errVal == SERVICE_ERROR_NONE)
130 int alarmId = atoi(pAlarmId);
132 SysLog(NID_SYS, "Start to load external lib");
134 OnAlarmForLaunch pOnAlarmForLaunch = null;
135 result r = lib.Construct(ALARM_PLUGIN_LIBRARY_PATH);
139 SysLog(NID_SYS, "Open alarm condition library");
140 pOnAlarmForLaunch = (OnAlarmForLaunch)lib.GetProcAddress(L"OnAlarmForLaunch");
141 if(pOnAlarmForLaunch != null)
143 SysLog(NID_SYS, "Function is found");
144 pOnAlarmForLaunch(alarmId);
145 SysLog(NID_SYS, "Requested to check current alarm id to AlarmConditionHandler %d", alarmId);
149 SysLog(NID_SYS, "Fail to find alarm function");
154 SysLog(NID_SYS, "Fail to open alarm condition library");
165 _ServiceAppImpl::OnTerminate(void)
167 SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
169 if (_AppInfo::GetAppState() == TERMINATED)
174 _AppInfo::SetAppState(TERMINATING);
176 if (OnServiceAppImplTerminating(__pAppImpl->IsForcedTermination()) != true)
178 SysLog(NID_APP, "[E_SYSTEM] The Termination of application failed.");
181 _AppInfo::SetAppState(TERMINATED);
186 _ServiceAppImpl::OnResume(void)
188 SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
193 _ServiceAppImpl::OnPause(void)
195 SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
200 _ServiceAppImpl::OnWindowHandleRequest(void)
207 _ServiceAppImpl::OnFrameRaiseRequested(void)
214 _ServiceAppImpl::GetInstance(void)
216 return __pServiceAppImpl;
221 _ServiceAppImpl::GetServiceAppInstance(void)
223 return __pServiceApp;
228 _ServiceAppImpl::OnAppInitializing(void)
230 const String& packageId = _AppInfo::GetPackageId();
231 const String& exeName = _AppInfo::GetAppExecutableName();
233 HashMapT<String, _AppFeatureInfoImpl*>* pInfo = _PackageManagerImpl::GetInstance()->GetPackageAppFeatureMapN(packageId, exeName);
237 _AppFeatureInfoImpl* pFeature = null;
239 result r = pInfo->GetValue(USE_UI_KEY, pFeature);
243 const String& val = pFeature->GetValue();
244 if (val == USE_UI_VAL_TRUE)
246 SysLog(NID_APP, "Using remote ui on service application.");
248 _LibraryImpl& lib = _AppManagerImpl::GetInstance()->GetUiLibraryImpl();
249 result (*pInit)(void) = null;
251 pInit = reinterpret_cast<result (*)()>(lib.GetProcAddress(L"InitializeUiFramework"));
255 SysLog(NID_APP, "[%s] UI initialized.", GetErrorMessage(r));
260 r = pInfo->GetValue(LIFE_DURATION_KEY, pFeature);
263 const String& val = pFeature->GetValue();
264 r = Integer::Parse(val, __lifeDuration);
267 SysLog(NID_APP, "LifeDuration is (%d) millis.", __lifeDuration);
271 _DeleteCollectionMapValue<String, _AppFeatureInfoImpl>(*pInfo);
275 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
276 return __pServiceApp->OnAppInitializing(*(AppRegistry::GetInstance()));
280 _ServiceAppImpl::OnAppInitialized(void)
282 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
283 return __pServiceApp->OnAppInitialized();
287 _ServiceAppImpl::OnServiceAppImplTerminating(bool forcedTermination)
289 SysTryReturn(NID_APP, __pServiceApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting ServiceApp instance failed.");
291 if( __pAppTerminatingInternalEventListener)
293 __pAppTerminatingInternalEventListener->OnApplicationTerminated(L"", 0);
295 return __pServiceApp->OnAppTerminating(*(AppRegistry::GetInstance()), forcedTermination);
299 _ServiceAppImpl::SetLifeDurationTimer(int lifeDuration)
301 if( lifeDuration <= 0)
306 if( __pLifeDurationTimer == null)
308 __pLifeDurationTimer = new Timer;
309 __pLifeDurationTimer->Construct(*this);
310 SysLog(NID_APP, "Life duration timer is constructed.");
314 __pLifeDurationTimer->Cancel();
317 if( __pauseLifeDurationTimer == false)
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;
322 const int timeout = (__lifeDuration > maxTimeOut) ? maxTimeOut : __lifeDuration;
323 __pLifeDurationTimer->Start(timeout);
324 SysLog(NID_APP, "Life duration timer is started (%d millis)", timeout );
329 _ServiceAppImpl::OnTimerExpired(Timer& timer)
332 if( __pAppTerminatingInternalEventListener || __lifeDuration != 1800000 ) //Fixme: Remove this workaround code after all LifeDuration value of pre-loaded apps are fixed.
334 SysLog(NID_APP, "Life duration timer is expired, so terminating the application.");
335 App::GetInstance()->Terminate();
339 SysLog(NID_APP, "Life duration timer is ignored.");
344 _ServiceAppImpl::SetAppTerminatingInternalEventListener(_IAppEventListener* pListener)
346 __pAppTerminatingInternalEventListener = pListener;
350 _ServiceAppImpl::PauseLifeDurationTimer(void)
352 __pauseLifeDurationTimer = true;
353 if( __pLifeDurationTimer)
355 __pLifeDurationTimer->Cancel();
356 SysLog(NID_APP, "Life duration timer is paused." );
361 _ServiceAppImpl::ResumeLifeDurationTimer(void)
363 __pauseLifeDurationTimer = false;
364 SysLog(NID_APP, "Life duration timer will be resumed." );
368 _ServiceAppImpl::ResetLifeDurationTimer(void)
370 SetLifeDurationTimer(__lifeDuration);
374 _ServiceAppImpl::GetPrivilegeLevel(void)
376 static int visibility = _API_VISIBILITY_NONE;
377 if( visibility == _API_VISIBILITY_NONE)
379 const AppId appId = _AppInfo::GetApplicationId();
381 _PackageManagerImpl* pPkg = _PackageManagerImpl::GetInstance();
382 SysTryReturn(NID_APP, pPkg != null, _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid PackageManager instance.");
384 String pkgId = _PackageManagerImpl::GetPackageIdByAppId(appId);
385 SysTryReturn(NID_APP, !pkgId.IsEmpty(), _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid appId(%ls).", appId.GetPointer() );
387 std::unique_ptr<PackageInfo> pPkgInfo (pPkg->GetPackageInfoN(pkgId) );
388 SysTryReturn(NID_APP, pPkgInfo != null, _API_VISIBILITY_PUBLIC, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
390 const _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPkgInfo.get());
391 SysTryReturn(NID_APP, pPkgInfoImpl != null, _API_VISIBILITY_PUBLIC, E_INVALID_STATE, "Invalid PackageInfo instance.");
393 visibility = pPkgInfoImpl->GetApiVisibility();