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