Merge "[Pkcs] Fixed the TC's failure" into tizen_2.1
[platform/framework/native/appfw.git] / src / app / FApp_AppImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FApp_AppImpl.cpp
20  * @brief       This is the implementation for the _AppImpl class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <notification/notification.h>
26 #include <appsvc/appsvc.h>
27 #include <vconf.h>
28
29 #include <FBaseInteger.h>
30 #include <FBaseColArrayList.h>
31 #include <FBaseColHashMap.h>
32 #include <FBaseRtThread.h>
33 #include <FAppAppRegistry.h>
34 #include <FAppAppControl.h>
35 #include <FAppDataControlProviderManager.h>
36 #include <FAppIAppCheckPointEventListener.h>
37 #include <FAppISqlDataControlProviderEventListener.h>
38 #include <FAppIMapDataControlProviderEventListener.h>
39 #include <FAppIAppControlProviderEventListener.h>
40 #include <FAppIAppLaunchConditionEventListener.h>
41 #include <FIoFile.h>
42
43 #include <FBaseSysLog.h>
44 #include <FIo_RegistryImpl.h>
45 #include <FSys_EnvironmentImpl.h>
46 #include <FSys_PowerManagerImpl.h>
47 #include <FApp_Aul.h>
48 #include <FSys_AlarmManager.h>
49
50 #include "FApp_IAppManager.h"
51 #include "FApp_AppManagerProxy.h"
52 #include "FApp_AppImpl.h"
53 #include "FApp_AppInfo.h"
54 #include "FApp_AppControlManager.h"
55 #include "FApp_AppArg.h"
56 #include "FApp_IAppImpl.h"
57 #include "FApp_AppResourceImpl.h"
58
59 using namespace Tizen::Base;
60 using namespace Tizen::Base::Collection;
61 using namespace Tizen::Base::Runtime;
62 using namespace Tizen::Io;
63 using namespace Tizen::System;
64
65 namespace
66 {
67
68 const int HEARTBEAT_WAIT_COUNT = 3;
69 const wchar_t SERVICE_APPID[] = L"aospd00043.osp-app-service";
70 const char SELECTOR_RESPONSE_KEY[] = "__APP_SVC_START_INFO__";
71 #ifndef VCONFKEY_APPSERVICE_STATUS
72 #define VCONFKEY_APPSERVICE_STATUS  "memory/appservice/status"
73 #endif
74
75 }
76
77 namespace Tizen { namespace App
78 {
79
80 _AppImpl* _AppImpl::__pAppImpl = null;
81 bool _AppImpl::__isTerminationRequested = false;
82 static const int _DATACONTROL_PACKET_INDEX_APPID = 0;
83 static const int _DATACONTROL_PACKET_INDEX_REQUESTTYPE = 1;
84 static const int _DATACONTROL_PACKET_INDEX_REQID = 2;
85 static const int _DATACONTROL_PACKET_INDEX_PROVIDERID = 3;
86 static const int _DATACONTROL_PACKET_INDEX_DATAID = 4;
87 static const int _DATACONTROL_PACKET_INDEX_COLUMNCOUNT = 5;
88 static const int _DATACONTROL_PACKET_INDEX_DELETEWHERE = 5;
89 static const int _DATACONTROL_PACKET_INDEX_COLUMNLIST = 6;
90 static const int _DATACONTROL_PACKET_INDEX_INSERTMAP = 6;
91 static const int _DATACONTROL_PACKET_INDEX_UPDATEMAP = 6;
92
93 struct charDeleter
94 {
95         void operator()(char* pValue)
96         {
97                 if(pValue != null)
98                 {
99                         free(pValue);
100                         pValue = null;
101                 }
102         }
103 };
104
105 _AppImpl::_AppImpl(App* pApp)
106         : __pCheckpointEventListener(null)
107         , __pSqlDataControlProviderEventListener(null)
108         , __pMapDataControlProviderEventListener(null)
109         , __pAppControlProviderEventListener(null)
110         , __pAppControlProviderInternalEventListener(null)
111         , __pAppLaunchConditionEventListener(null)
112         , __pApp(pApp)
113         , __pIAppImpl(null)
114         , __pRequestHandler(&_AppImpl::HandleAppRequest)
115         , __pLegacyRequestHandler(&_AppImpl::HandleDummyAppRequest)
116         , __forcedTermination(false)
117 {
118         __pAppImpl = this;
119 }
120
121
122 result
123 _AppImpl::Construct(const IList* pArgs)
124 {
125         SysTryReturnResult(NID_APP, pArgs != null, E_INVALID_ARG, "pArguments must not be null.");
126         SysAssertf(__pApp != null, "Getting App instance failed.");
127
128         result r = __appUserEvent.Construct();
129         SysTryLog(NID_APP, !IsFailed(r), "[%s] User event handler failure.", GetErrorMessage(r));
130
131         __appUserEvent.AddListener(*this);
132
133         _AppInfo::SetAppState(INITIALIZING);
134
135         return E_SUCCESS;
136 }
137
138
139 result
140 _AppImpl::Execute(_IAppImpl* pIAppImpl)
141 {
142         SysLogTag(NID_APP, "LAUNCH","[%ls:<Initialize Application>:start]", _AppInfo::GetAppExecutableName().GetPointer());
143         result r = E_SUCCESS;
144         int eflResult = APP_ERROR_NONE;
145         int argc = _AppInfo::GetArgc();
146         char** pArgv = _AppInfo::GetArgv();
147         _IAppManager* pAppManager = null;
148
149         _AppImpl* pAppImpl = _AppImpl::GetInstance();
150         SysTryReturnResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
151         pAppImpl->__pIAppImpl = pIAppImpl;
152
153         // handle legacy version handling
154         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
155         {
156                 pAppImpl->SetLegacyRequestHandler();
157         }
158
159         // clear previous notification
160         // [INFO] behavior change : platform does not clear previous notification
161         notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
162
163         app_event_callback_s state_handler;
164         memset(&state_handler, 0, sizeof(app_event_callback_s));
165
166         state_handler.create = &OnCreate;
167         state_handler.service = &OnService;
168         state_handler.terminate = &OnTerminate;
169         state_handler.pause = &OnPause;
170         state_handler.resume = &OnResume;
171         state_handler.device_orientation = &OnDeviceOrientationChanged;
172
173         state_handler.low_memory = &OnLowMemory;
174         state_handler.low_battery = &OnLowBattery;
175         state_handler.language_changed = &OnLanguageChanged;
176         state_handler.region_format_changed = &OnRegionFormatChanged;
177
178         _Aul::SetPowerOffNotiListener(OnPowerOffNotiReceived, this);
179 //      if (pAppManager != null)
180 //      {
181 //              r = pAppManager->RegisterApplication(_AppInfo::GetPackageId(), _AppInfo::GetAppExecutableName(), _AppInfo::GetAppType() , _AppInfo::GetProcessId());
182 //              SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Application registration failed.", GetErrorMessage(r));
183 //      }
184
185         eflResult = app_efl_main(&argc, &pArgv, &state_handler, this);
186
187         SysTryLog(NID_APP, eflResult == APP_ERROR_NONE, "app_efl_main failed with error (%d): Unknown", eflResult);
188
189         // clear outstanding ongoing notification
190         notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
191
192 //CATCH:
193         pAppManager = _AppManagerProxy::GetService();
194
195         if (pAppManager != null)
196         {
197                 pAppManager->UnregisterApplication(_AppInfo::GetProcessId());
198         }
199
200         return r;
201 }
202
203
204 bool
205 _AppImpl::OnCreate(void* user_data)
206 {
207         SysLog(NID_APP, "Platform creation event.");
208
209         _AppImpl* pAppImpl = static_cast<_AppImpl*>(user_data);
210         if (pAppImpl == null)
211         {
212                 _AppInfo::SetAppState(TERMINATED);
213                 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] Invalid platform state.");
214                 return false;
215         }
216
217         _AppInfo::SetAppState(INITIALIZING);
218
219         if (!ConfirmAppService())
220         {
221                 _AppInfo::SetAppState(TERMINATED);
222                 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] Osp AppService is not running.");
223                 return false;
224         }
225
226         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnAppInitializing>:start]", _AppInfo::GetAppExecutableName().GetPointer());
227         if (!pAppImpl->__pIAppImpl->OnAppInitializing())
228         {
229                 _AppInfo::SetAppState(TERMINATED);
230                 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] The initialization of your application failed.");
231                 return false;
232         }
233         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnAppInitializing>:done]", _AppInfo::GetAppExecutableName().GetPointer());
234
235         SysLogTag(NID_APP, "LAUNCH","[%ls:<RegisterApplication>:start]", _AppInfo::GetAppExecutableName().GetPointer());
236         _IAppManager* pAppManager = _AppManagerProxy::GetService();
237
238         if (pAppManager != null)
239         {
240                 result r = pAppManager->RegisterApplication(_AppInfo::GetPackageId(), _AppInfo::GetAppExecutableName(), static_cast<_AppType>(_AppInfo::GetAppType()), _AppInfo::GetProcessId());
241                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Application registration failed.", GetErrorMessage(r));
242         }
243         SysLogTag(NID_APP, "LAUNCH","[%ls:<RegisterApplication>:done]", _AppInfo::GetAppExecutableName().GetPointer());
244         // hide splash window
245
246         _PowerManagerImpl::InitBatteryEvent();
247
248         return true;
249 }
250
251
252 void
253 _AppImpl::OnService(service_s* service, void* user_data)
254 {
255         _AppImpl* pAppImpl = _AppImpl::GetInstance();
256         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
257         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
258
259         // ugly code for app selector event handling
260         bundle* pBundle = _AppArg::GetBundleFromSvc(service);
261         if (pBundle)
262         {
263                 const char* pValue = appsvc_get_data(pBundle, SELECTOR_RESPONSE_KEY);
264                 if (pValue)
265                 {
266                         const int req = _AppArg::GetRequestIdFromBundle(pBundle);
267
268                         const char* pOperation = appsvc_get_operation(pBundle);
269
270                         SysLog(NID_APP, "App selector response [%s, %s], req(%d).", pValue, pOperation, req);
271
272                         //_AppArg::Print(pBundle);
273                         _AppControlManager::GetInstance()->SendAppControlStartResponse(req, pValue, pOperation);
274                         return;
275                 }
276         }
277
278         //_AppArg::Print(pBundle);
279         int req = -1;
280         _AppHandler handler = _APP_HANDLER_NONE;
281         pAppImpl->RegisterAppRequest(service, req, handler);
282
283         bool initialLaunch = false;
284
285         // initial launch
286         // (1) AppControl / DataControl provider listener
287         // (2) OnAppInitialized()
288         // (3) OnResume() (OnForeground()) (UiApp only)
289         // (4) OnUserEventReceivedN()
290
291         // already launched
292         // (1) AppControl / DataControl provider listener
293         // (2) OnResume() (OnForeground()) (UiApp only)
294         // (3) OnUserEventReceivedN()
295
296         if (_AppInfo::GetAppState() == INITIALIZING)
297         {
298                 initialLaunch = true;
299
300                 unsigned int winId = 0;
301                 service_get_window(service, &winId);
302                 if (winId > 0)
303                 {
304                         _AppInfo::GetAppInfo()->SetParentWindowHandle(winId);
305                         SysLog(NID_APP, "Saving window handle 0x%x.", winId);
306                 }
307                 // call for callbacks
308                 // ptr to member function
309                 SysLogTag(NID_APP, "LAUNCH","[%ls:<RequestHandler and OnAppInitialized>:start]", _AppInfo::GetAppExecutableName().GetPointer());
310                 (pAppImpl->*pAppImpl->__pRequestHandler)(service, req, handler);
311
312                 if (!pAppImpl->__pIAppImpl->OnAppInitialized())
313                 {
314                         _AppInfo::SetAppState(TERMINATING);
315                         SysLog(NID_APP, "[E_INIT_FAILED] The Initialization of your application failed.");
316                         app_efl_exit();
317                         return;
318                 }
319                 SysLogTag(NID_APP, "LAUNCH","[%ls:<RequestHandler and OnAppInitialized>:done]", _AppInfo::GetAppExecutableName().GetPointer());
320
321                 _AppInfo::SetAppState(RUNNING);
322
323                 SysLogTag(NID_APP, "LAUNCH","[%ls:<Initialize Application>:done]", _AppInfo::GetAppExecutableName().GetPointer());
324         }
325         else
326         {
327                 // already running
328
329                 // call for callbacks
330                 // ptr to member function
331                 (pAppImpl->*pAppImpl->__pRequestHandler)(service, req, handler);
332
333                 std::unique_ptr<char[], charDeleter> pOperation(null);
334                 char* pBuf = null;
335
336                 int errVal = service_get_operation(service, &pBuf);
337                 pOperation.reset(pBuf);
338
339                 if((errVal == SERVICE_ERROR_NONE) && (!strcmp((const char*)pOperation.get(), "osp.operation.ALARM")))
340                 {
341                         std::unique_ptr<char[], charDeleter> pAlarmId(null);
342                         char* pBuf = null;
343
344                         errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pBuf);
345                         pAlarmId.reset(pBuf);
346                         SysTryReturnVoidResult(NID_SYS, errVal == SERVICE_ERROR_NONE, E_SYSTEM, "It is failed to get reserved alarm id.");
347                         int alarmId = atoi((const char*)pAlarmId.get());
348
349                         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
350                         pAlarmManager->OnAlarmExpired(alarmId);
351
352                         return;
353                 }
354         }
355
356         pAppImpl->__pIAppImpl->OnService(service, initialLaunch);
357
358         // ptr to member function
359         (pAppImpl->*pAppImpl->__pLegacyRequestHandler)(service, req, handler);
360 }
361
362
363 void
364 _AppImpl::OnTerminate(void* user_data)
365 {
366         SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
367
368         _AppImpl* pAppImpl = _AppImpl::GetInstance();
369         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
370         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
371
372         __isTerminationRequested = true;
373         pAppImpl->__pIAppImpl->OnTerminate();
374 }
375
376
377 void
378 _AppImpl::OnResume(void* user_data)
379 {
380         SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
381         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnResume>:start]", _AppInfo::GetAppExecutableName().GetPointer());
382
383         _AppImpl* pAppImpl = _AppImpl::GetInstance();
384         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
385         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
386         pAppImpl->__pIAppImpl->OnResume();
387         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnResume>:done]", _AppInfo::GetAppExecutableName().GetPointer());
388 }
389
390
391 void
392 _AppImpl::OnPause(void* user_data)
393 {
394         SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
395         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnPause>:start]", _AppInfo::GetAppExecutableName().GetPointer());
396
397         _AppImpl* pAppImpl = _AppImpl::GetInstance();
398         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
399         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
400         pAppImpl->__pIAppImpl->OnPause();
401         pAppImpl->OnAppCheckpoint();
402         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnPause>:done]", _AppInfo::GetAppExecutableName().GetPointer());
403 }
404
405
406 void
407 _AppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation, void* user_data)
408 {
409         _AppImpl* pAppImpl = _AppImpl::GetInstance();
410         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
411         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
412
413         const AppState state = _AppInfo::GetAppState();
414         SysTryReturnVoidResult(NID_APP, state == INITIALIZING || state == RUNNING, E_INVALID_STATE, "[E_INVALID_STATE] Invalid application state : %d.", state);
415
416         pAppImpl->__pIAppImpl->OnDeviceOrientationChanged(orientation);
417 }
418
419
420 _AppImpl::~_AppImpl(void)
421 {
422         __pAppImpl = null;
423 }
424
425
426 _AppImpl*
427 _AppImpl::GetInstance(void)
428 {
429         return __pAppImpl;
430 }
431
432
433 App*
434 _AppImpl::GetAppInstance(void)
435 {
436         return __pApp;
437 }
438
439
440 IList*
441 _AppImpl::GetAppArgumentListN(void) const
442 {
443         ArrayList* pList = null;
444         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
445
446         if (pArg)
447         {
448                 pList = pArg->GetArgListN(0);
449         }
450         else
451         {
452                 // fallback
453                 pList = new (std::nothrow) ArrayList();
454                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
455                 String* pData = new (std::nothrow) String(LEGACY_LAUNCH_REASON_NORMAL);
456                 SysTryReturn(NID_APP, pData != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
457                 String* pData2 = new (std::nothrow) String(L"osp.operation.MAIN");
458                 SysTryReturn(NID_APP, pData2 != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
459
460                 pList->Construct();
461                 pList->Add(*pData);
462                 pList->Add(*pData2);
463         }
464
465         return pList;
466 }
467
468
469 result
470 _AppImpl::Terminate(void)
471 {
472         result r = E_SUCCESS;
473
474         __isTerminationRequested = true;
475
476         app_efl_exit();
477         return r;
478 }
479
480
481 bool
482 _AppImpl::OnAppInitializing(void)
483 {
484         return false;
485 }
486
487
488 bool
489 _AppImpl::OnAppWillTerminate(void)
490 {
491         SysTryReturn(NID_APP, __pApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
492
493         return __pApp->OnAppWillTerminate();
494 }
495
496
497 result
498 _AppImpl::RegisterAppRequest(service_s* service, int& req, _AppHandler& handler)
499 {
500         _AppControlManager* pManager = _AppControlManager::GetInstance();
501         SysTryReturnResult(NID_APP, pManager != null, E_SYSTEM, "Wrong system state.");
502
503         result r = pManager->RegisterRequest(service, req, handler);
504         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
505
506         int providerType = _AppInfo::GetAppHandlerType();
507         providerType |= (_APP_HANDLER_LAUNCH_NORMAL | _APP_HANDLER_LAUNCH_COND);
508
509         SysTryLog(NID_APP, (handler & providerType) != 0, "Request 0x%x received for 0x%x.", handler,
510                         _AppInfo::GetAppHandlerType());
511         return E_SUCCESS;
512 }
513
514
515 result
516 _AppImpl::HandleAppRequest(service_s* service, int req, _AppHandler handler)
517 {
518         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(req);
519         SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
520
521         pArg->Print();
522
523         switch (handler)
524         {
525         case _APP_HANDLER_DATACONTROL:
526                 SysLog(NID_APP, "Handling DataControl request");
527
528                 OnDataControlRequestReceived(*pArg, static_cast< RequestId >(req));
529
530                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
531                 break;
532
533         case _APP_HANDLER_LAUNCH_COND:
534                 SysLog(NID_APP, "Handling conditional launch request");
535
536                 OnConditionRequestReceived(*pArg, static_cast< RequestId >(req));
537                 break;
538
539         default:
540                 SysLog(NID_APP, "Handling AppControl request");
541
542                 OnAppControlRequestReceived(*pArg, static_cast< RequestId >(req));
543                 break;
544         }
545
546         return E_SUCCESS;
547 }
548
549
550 result
551 _AppImpl::HandleLegacyAppRequest(service_s* service, int req, _AppHandler handler)
552 {
553         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
554         SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
555
556         //pArg->Print();
557
558         switch (handler)
559         {
560         case _APP_HANDLER_LAUNCH_NORMAL:
561                 SysLog(NID_APP, "Handling normal launch request");
562                 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req));
563
564                 // [TODO] request handle memory cleanup confirm
565                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
566                 break;
567
568         case _APP_HANDLER_LAUNCH_COND:
569                 SysLog(NID_APP, "Handling conditional launch request");
570
571                 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req));
572
573                 // [TODO] request handle memory cleanup confirm
574                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
575                 break;
576
577         default:
578                 break;
579         }
580
581         return E_SUCCESS;
582 }
583
584
585 result
586 _AppImpl::HandleDummyAppRequest(service_s* service, int req, _AppHandler handler)
587 {
588         SysLog(NID_APP, "Dummy handler");
589
590         return E_SUCCESS;
591 }
592
593
594 result
595 _AppImpl::SetLegacyRequestHandler(void)
596 {
597         SysLog(NID_APP, "Setting request handler for legacy application.");
598
599         __pRequestHandler = &_AppImpl::HandleDummyAppRequest;
600         __pLegacyRequestHandler = &_AppImpl::HandleLegacyAppRequest;
601
602         return E_SUCCESS;
603 }
604
605
606 void
607 _AppImpl::OnLowMemory(void* user_data)
608 {
609         _AppImpl* pAppImpl = _AppImpl::GetInstance();
610
611         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
612         SysTryReturnVoidResult(NID_APP, pAppImpl->__pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
613
614         if (_AppInfo::GetAppState() == RUNNING)
615         {
616                 pAppImpl->__pApp->OnLowMemory();
617         }
618 }
619
620
621 void
622 _AppImpl::OnLowBattery(void* user_data)
623 {
624         // Osp doesn't implement this callback.
625 }
626
627 void
628 _AppImpl::OnBatteryLevelChanged(BatteryLevel batteryLevel)
629 {
630         SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
631
632         __pApp->OnBatteryLevelChanged(batteryLevel);
633 }
634
635 void
636 _AppImpl::OnLanguageChanged(void* user_data)
637 {
638         SysLog(NID_APP, "");
639         _AppInfo::UpdatePackageInfo(true);
640
641         _AppResourceImpl::Reinitialize();
642         SysLog(NID_APP, "Language change done.");
643 }
644
645 void
646 _AppImpl::OnRegionFormatChanged(void* user_data)
647 {
648         // Osp doesn't implement this callback.
649 }
650
651
652 result
653 _AppImpl::SendUserEvent(RequestId requestId, const IList* pArgs)
654 {
655         _AppUserEventArg* pArg = new (std::nothrow) _AppUserEventArg(requestId, pArgs);
656         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "User event creation failure for %d.", requestId);
657
658         result r = __appUserEvent.FireAsync(*pArg);
659
660         return r;
661 }
662
663
664 void
665 _AppImpl::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
666 {
667         SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
668
669         __pApp->OnUserEventReceivedN(requestId, pArgs);
670 }
671
672
673 void
674 _AppImpl::OnAppControlRequestReceived(const _AppArg& arg, RequestId reqId)
675 {
676         IAppControlProviderEventListener* pAppControlProviderEventListener = null;
677
678         const String internalOperation = arg.GetValue(OSP_K_APPCONTROL_INTERNAL_OPERATION);
679         if( !internalOperation.IsEmpty() )
680         {
681                 SysLog(NID_APP, "internalOperation:%ls", internalOperation.GetPointer() );
682                 pAppControlProviderEventListener = __pAppControlProviderInternalEventListener;
683         }
684         else
685         {
686                 pAppControlProviderEventListener = __pAppControlProviderEventListener;
687         }
688
689         if( pAppControlProviderEventListener == null)
690         {
691                 SysLog(NID_APP, "No AppControl provider event listener set for request %d.", reqId);
692                 return;
693         }
694
695         const String& appId = _AppInfo::GetApplicationId();
696
697         const char* p = appsvc_get_operation(arg.GetBundle());
698         String operationId = (p) ? String(p) : TIZEN_OPERATION_MAIN;
699         if (operationId == L"http://tizen.org/appcontrol/operation/default")
700         {
701                 operationId = TIZEN_OPERATION_MAIN;
702         }
703
704         SysLog(NID_APP, "AppControl (%ls, %ls).", appId.GetPointer(), operationId.GetPointer());
705
706         String uri;
707         String mime;
708         const String* pUri = null;
709         const String* pMime = null;
710         p = appsvc_get_uri(arg.GetBundle());
711         if (p)
712         {
713                 uri = p;
714                 pUri = &uri;
715
716                 SysLog(NID_APP, "Delivered uri is [%ls].", pUri->GetPointer());
717         }
718
719         p = appsvc_get_mime(arg.GetBundle());
720         if (p)
721         {
722                 mime = p;
723                 pMime = &mime;
724         }
725
726         std::unique_ptr<HashMap> pMap(arg.GetArgMapN());
727
728         pAppControlProviderEventListener->OnAppControlRequestReceived(reqId, operationId, pUri, pMime, pMap.get());
729
730         SysLog(NID_APP, "AppControl (%ls, %ls) invocation finished.", appId.GetPointer(), operationId.GetPointer());
731 }
732
733
734 void
735 _AppImpl::OnDataControlRequestReceived(const _AppArg& arg, RequestId reqId)
736 {
737         String tempFilePath;
738         ISqlDataControlProviderEventListener* pSqlListener = null;
739         IMapDataControlProviderEventListener* pMapListener = null;
740         ArrayList* pColumnList = null;
741         HashMap* pInsertMap = null;
742         HashMap* pUpdateMap = null;
743         String* pAppId = null;
744         String* pRequestType = null;
745         String* pProviderId = null;
746         String* pDataId = null;
747         String* pReqId = null;
748         String* pColumnCount = null;
749         String* pColumn = null;
750         String* pValue = null;
751         String* pWhere = null;
752         String* pOrder = null;
753         String* pErrorMsg = null;
754         String* pNo = null;
755         String* pCount = null;
756         String empty(L"NULL");
757 #if 0
758         _DataControlResultSetImpl* pResultSetImpl = null;
759         String* pResult = null;
760         String* pObject = null;
761         String* pTempObject = null;
762         String* pTempFilePath = null;
763 #endif
764         _AppArg resultArg;
765         int type = 0;
766         _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
767         int columnCount = 0;
768         int pageNo = 0;
769         int countPerPage = 0;
770         int i = 0;
771         int index = 0;
772         //ArrayList* pResultArgList = null;
773         result r = E_SUCCESS;
774         result res = E_SUCCESS;
775         ArrayList* pList = null;
776         String* pKey = null;
777         String* pNewValue = null;
778         String* pOldValue = null;
779 #if 0
780         String* pResultCount = null;
781         String* pResultValue = null;
782 #endif
783
784         SysLog(NID_APP, "[DC_PROVIDER] Data control request is received.");
785         //arg.Print();
786
787         pList = arg.GetArgListN(0);
788         SysTryCatch(NID_APP, pList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
789
790         // key-based
791         pAppId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_APPID)); // key(0)
792         SysTryCatch(NID_APP, pAppId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
793
794         pRequestType = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_REQUESTTYPE)); // key(1)
795         SysTryCatch(NID_APP, pRequestType, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
796         Integer::Parse(*pRequestType, type);
797         requestType = static_cast< _DataControlRequestType >(type);
798
799         pReqId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_REQID)); // key(2)
800         SysTryCatch(NID_APP, pReqId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
801
802         pProviderId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_PROVIDERID)); // key(3)
803         SysTryCatch(NID_APP, pProviderId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
804
805         // list-based
806         pDataId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DATAID));// list(0): data ID
807         SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
808
809         if (requestType >= _DATACONTROL_REQUEST_TYPE_SQL_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_SQL_DELETE)
810         {
811                 pSqlListener = __pSqlDataControlProviderEventListener;
812                 SysTryCatch(NID_APP, pSqlListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
813         }
814         else if (requestType >= _DATACONTROL_REQUEST_TYPE_MAP_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_MAP_DELETE)
815         {
816                 pKey = dynamic_cast< String* >(pList->GetAt(5)); // list[1]
817                 SysTryCatch(NID_APP, pKey, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
818
819                 pMapListener = __pMapDataControlProviderEventListener;
820                 SysTryCatch(NID_APP, pMapListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
821         }
822         else
823         {
824                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid request");
825                 r = E_SYSTEM;
826         }
827
828         switch (requestType)
829         {
830         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
831                 SysLog(NID_APP, "[DC_PROVIDER] SqlDataControl SELECT");
832
833                 // list(1): selected column count
834                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
835                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
836
837                 if (pColumnCount->Equals(empty) == true)
838                 {
839                         pColumnList = null;
840                 }
841                 else
842                 {
843                         Integer::Parse(*pColumnCount, columnCount);
844                         pColumnList = new (std::nothrow) ArrayList();
845                         SysTryReturnVoidResult(NID_APP, pColumnList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
846                         pColumnList->Construct();
847                         SysLog(NID_APP, "[DC_PROVIDER] selected column count: %d", columnCount);
848
849                         i = 0;
850                         while (i < columnCount) // list(2): column list
851                         {
852                                 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNLIST + i));
853                                 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
854
855                                 pColumnList->Add(*(new String(*pColumn)));
856                                 SysLog(NID_APP, "[DC_PROVIDER] column[%d]: %ls", i, pColumn->GetPointer());
857                                 i++;
858                         }
859                 }
860
861                 i += _DATACONTROL_PACKET_INDEX_COLUMNLIST;
862                 pWhere = dynamic_cast< String* >(pList->GetAt(i)); // list(3): where clause
863                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
864
865                 if (pWhere->Equals(empty) == true)
866                 {
867                         pWhere = null;
868                 }
869                 else
870                 {
871                         SysLog(NID_APP, "[DC_PROVIDER] pWhere: %ls", pWhere->GetPointer());
872                 }
873
874                 i++;
875                 pOrder = dynamic_cast< String* >(pList->GetAt(i)); // list(4): order clause
876                 SysTryCatch(NID_APP, pOrder, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
877
878                 if (pOrder->Equals(empty) == true)
879                 {
880                         pOrder = null;
881                 }
882                 else
883                 {
884                         SysLog(NID_APP, "[DC_PROVIDER] pOrder: %ls", pOrder->GetPointer());
885                 }
886
887                 i++;
888                 pNo = dynamic_cast <String*>(pList->GetAt(i)); // list(5): page number
889                 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
890                 Integer::Parse(*pNo, pageNo);
891
892                 i++;
893                 pCount = dynamic_cast <String*>(pList->GetAt(i)); // list(6): count per page
894                 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
895                 Integer::Parse(*pCount, countPerPage);
896
897                 pSqlListener->OnSqlDataControlSelectRequestReceived(reqId, *pProviderId, *pDataId, pColumnList,
898                                 pWhere, pOrder);
899
900                 SysLog(NID_APP, "[DC_PROVIDER] caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, pColumnList: 0x%0x, pWhere: 0x%x, pOrder: 0x%x, pageNo: %d, countPerPage: %d",
901                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
902                                 pDataId->GetPointer(), pColumnList, pWhere, pOrder, pageNo, countPerPage);
903                 break;
904         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
905                 SysLog(NID_APP, "[DC_PROVIDER] SqlDataControl INSERT");
906
907                 // list(1): insert column count
908                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
909                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
910
911                 Integer::Parse(*pColumnCount, columnCount);
912                 SysLog(NID_APP, "[DC_PROVIDER] inserted column count: %d", columnCount);
913
914                 pInsertMap = new (std::nothrow) HashMap();
915                 SysTryReturnVoidResult(NID_APP, pInsertMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
916                 pInsertMap->Construct();
917
918                 i = 0;
919                 index = 0;
920                 while (i < columnCount * 2) // list(2): column-value pairs
921                 {
922                         pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
923                         SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
924                         SysLog(NID_APP, "[DC_PROVIDER] column[%d]: %ls", index, pColumn->GetPointer());
925
926                         pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
927                         SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
928                         SysLog(NID_APP, "[DC_PROVIDER] value[%d]: %ls", index, pValue->GetPointer());
929
930                         pInsertMap->Add(*(new String(*pColumn)), *(new String(*pValue)));
931                         index++;
932                 }
933
934                 pSqlListener->OnSqlDataControlInsertRequestReceived(reqId, *pProviderId, *pDataId, *pInsertMap);
935
936                 SysLog(NID_APP, "[DC_PROVIDER] caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, pInsertMap: 0x%x",
937                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
938                                 pDataId->GetPointer(), pInsertMap);
939                 break;
940         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
941                 SysLog(NID_APP, "[DC_PROVIDER] SqlDataControl UPDATE");
942
943                 // list(1): updated column count
944                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
945                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
946
947                 Integer::Parse(*pColumnCount, columnCount);
948                 SysLog(NID_APP, "[DC_PROVIDER] updated column count: %d", columnCount);
949
950                 pUpdateMap = new (std::nothrow) HashMap();
951                 SysTryReturnVoidResult(NID_APP, pUpdateMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
952                 pUpdateMap->Construct();
953
954                 i = 0;
955                 index = 0;
956                 while (i < columnCount * 2) // list(2): column-value pairs
957                 {
958                         pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
959                         SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
960                         SysLog(NID_APP, "[DC_PROVIDER] column[%d]: %ls", index, pColumn->GetPointer());
961
962                         pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
963                         SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
964                         SysLog(NID_APP, "[DC_PROVIDER] value[%d]: %ls", index, pValue->GetPointer());
965
966                         pUpdateMap->Add(*(new String(*pColumn)), *(new String(*pValue)));
967                         index++;
968                 }
969
970                 // list(3): where clause
971                 pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i));
972                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
973
974                 if (pWhere->Equals(empty) == true)
975                 {
976                         pWhere = null;
977                 }
978                 else
979                 {
980                         SysLog(NID_APP, "[DC_PROVIDER] pWhere: %ls", pWhere->GetPointer());
981                 }
982
983                 pSqlListener->OnSqlDataControlUpdateRequestReceived(reqId, *pProviderId, *pDataId, *pUpdateMap, pWhere);
984
985                 SysLog(NID_APP, "[DC_PROVIDER] caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, pUpdateMap: 0x%x, pWhere: 0x%x",
986                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
987                                 pDataId->GetPointer(), pUpdateMap, pWhere);
988                 break;
989         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
990                 SysLog(NID_APP, "[DC_PROVIDER] SqlDataControl DELETE");
991
992                 // list(1): where clause
993                 pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DELETEWHERE));
994                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
995
996                 if (pWhere->Equals(empty) == true)
997                 {
998                         pWhere = null;
999                 }
1000                 else
1001                 {
1002                         SysLog(NID_APP, "[DC_PROVIDER] pWhere: %ls", pWhere->GetPointer());
1003                 }
1004
1005                 pSqlListener->OnSqlDataControlDeleteRequestReceived(reqId, *pProviderId, *pDataId, pWhere);
1006
1007                 SysLog(NID_APP, "[DC_PROVIDER] caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, pWhere: 0x%x",
1008                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
1009                                 pDataId->GetPointer(), pWhere);
1010                 break;
1011         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
1012                 pNo = dynamic_cast <String*>(pList->GetAt(6)); // list[2]
1013                 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1014                 Integer::Parse(*pNo, pageNo);
1015
1016                 pCount = dynamic_cast <String*>(pList->GetAt(7)); //list[3]
1017                 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1018                 Integer::Parse(*pCount, countPerPage);
1019
1020                 pMapListener->OnMapDataControlGetValueRequestReceived(reqId, *pProviderId, *pDataId, *pKey);
1021                 break;
1022         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
1023                 pValue = dynamic_cast <String*>(pList->GetAt(6)); // list[2]
1024                 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1025
1026                 pMapListener->OnMapDataControlAddValueRequestReceived(reqId, *pProviderId, *pDataId, *pKey, *pValue);
1027                 break;
1028         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
1029                 pOldValue = dynamic_cast <String*>(pList->GetAt(6)); // list[2]
1030                 SysTryCatch(NID_APP, pOldValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1031
1032                 pNewValue = dynamic_cast <String*>(pList->GetAt(7)); // list[3]
1033                 SysTryCatch(NID_APP, pNewValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1034
1035                 pMapListener->OnMapDataControlSetValueRequestReceived(reqId, *pProviderId, *pDataId, *pKey, *pOldValue, *pNewValue);
1036                 break;
1037         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
1038                 pValue = dynamic_cast <String*>(pList->GetAt(6)); // list[2]
1039                 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1040
1041                 pMapListener->OnMapDataControlRemoveValueRequestReceived(reqId, *pProviderId, *pDataId, *pKey, *pValue);
1042                 break;
1043         default:
1044                 SysTryCatch(NID_APP, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1045                 break;
1046         }
1047
1048         if (requestType >= _DATACONTROL_REQUEST_TYPE_MAP_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_MAP_DELETE)
1049         {
1050                 SysLog(NID_APP, "caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, key: %ls",
1051                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
1052                                 pDataId->GetPointer(), pKey->GetPointer());
1053         }
1054
1055         if (pColumnList)
1056         {
1057                 pColumnList->RemoveAll(true);
1058                 delete pColumnList;
1059         }
1060
1061         if (pInsertMap)
1062         {
1063                 pInsertMap->RemoveAll(true);
1064                 delete pInsertMap;
1065         }
1066
1067         if (pUpdateMap)
1068         {
1069                 pUpdateMap->RemoveAll(true);
1070                 delete pUpdateMap;
1071         }
1072
1073         return;
1074
1075 CATCH:
1076         if (r == E_SYSTEM)
1077         {
1078                 pErrorMsg = new (std::nothrow) String("[E_SYSTEM] A system error has occurred.");
1079         }
1080         else if (r == E_OUT_OF_MEMORY)
1081         {
1082                 pErrorMsg = new (std::nothrow) String("[E_OUT_OF_MEMORY] The memory was insufficient.");
1083         }
1084         SysTryReturnVoidResult(NID_APP, pErrorMsg, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
1085
1086         res = DataControlProviderManager::GetInstance()->SendDataControlError(reqId, *pErrorMsg);
1087
1088         if (pColumnList)
1089         {
1090                 pColumnList->RemoveAll(true);
1091                 delete pColumnList;
1092         }
1093
1094         if (pInsertMap)
1095         {
1096                 pInsertMap->RemoveAll(true);
1097                 delete pInsertMap;
1098         }
1099
1100         if (pUpdateMap)
1101         {
1102                 pUpdateMap->RemoveAll(true);
1103                 delete pUpdateMap;
1104         }
1105
1106         delete pErrorMsg;
1107
1108         return;
1109 }
1110
1111
1112 void
1113 _AppImpl::OnConditionRequestReceived(const _AppArg& arg, RequestId reqId)
1114 {
1115         if (__pAppLaunchConditionEventListener == null)
1116         {
1117                 SysLog(NID_APP, "No App launch condition event listener");
1118                 return;
1119         }
1120
1121         String condition = arg.GetValue(OSP_K_COND);
1122         //Object* pExtraData = null;//TODO: retrieve NFC extra data.
1123     HashMap* pExtraData = arg.GetArgMapN();
1124         ArrayList* pList = arg.GetArgListN();
1125
1126         __pAppLaunchConditionEventListener->OnAppLaunchConditionMetN(condition, pExtraData, pList);
1127 }
1128
1129
1130 long
1131 _AppImpl::GetWindowHandle(void) const
1132 {
1133         SysTryReturn(NID_APP, __pIAppImpl != null, -1, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
1134
1135         return __pIAppImpl->OnWindowHandleRequest();
1136 }
1137
1138
1139 void
1140 _AppImpl::OnAppCheckpoint(void)
1141 {
1142         if (__pCheckpointEventListener)
1143         {
1144                 __pCheckpointEventListener->OnAppCheckpointing(*(AppRegistry::GetInstance()));
1145         }
1146 }
1147
1148
1149 bool
1150 _AppImpl::ConfirmAppService(void)
1151 {
1152         if (_AppInfo::GetApplicationId() == SERVICE_APPID)
1153         {
1154                 return true;
1155         }
1156
1157         int value = -1;
1158         int count = 0;
1159         long interval = 100;
1160
1161         while (true)
1162         {
1163                 int res = vconf_get_int(VCONFKEY_APPSERVICE_STATUS, &value);
1164                 if (value > 0)
1165                 {
1166                         return true;
1167                 }
1168
1169                 if (count >= HEARTBEAT_WAIT_COUNT)
1170                 {
1171                         return false;
1172                 }
1173
1174                 SysLog(NID_APP, "Waiting for app service %dth time(%d msec).", count, interval);
1175                 count++;
1176                 Thread::Sleep(interval);
1177                 interval += 100;
1178         }
1179
1180         return false;
1181 }
1182
1183 result
1184 _AppImpl::SetListener(_AppEvent appEvent, IEventListener* pListener)
1185 {
1186         switch (appEvent)
1187         {
1188         case AE_CHECKPOINT:
1189                 SysTryReturnResult(NID_APP, __pCheckpointEventListener == null, E_OBJ_ALREADY_EXIST, "Checkpoint listener is already set.");
1190
1191                 // fall through
1192         case AE_CLEAR_LISTENER:
1193                 __pCheckpointEventListener = dynamic_cast <IAppCheckpointEventListener*>(pListener);
1194                 break;
1195
1196         default:
1197                 break;
1198         }
1199
1200         return E_SUCCESS;
1201 }
1202
1203
1204 result
1205 _AppImpl::SetSqlDataControlProviderEventListener(ISqlDataControlProviderEventListener* pListener)
1206 {
1207         __pSqlDataControlProviderEventListener = pListener;
1208         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1209         tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1210         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1211         return E_SUCCESS;
1212 }
1213
1214
1215 result
1216 _AppImpl::SetMapDataControlProviderEventListener(IMapDataControlProviderEventListener* pListener)
1217 {
1218         __pMapDataControlProviderEventListener = pListener;
1219         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1220         tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1221         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1222         return E_SUCCESS;
1223 }
1224
1225
1226 result
1227 _AppImpl::SetAppControlProviderEventListener(IAppControlProviderEventListener* pListener)
1228 {
1229         __pAppControlProviderEventListener = pListener;
1230         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1231         tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1232         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1233         return E_SUCCESS;
1234 }
1235
1236 result
1237 _AppImpl::SetAppControlProviderInternalEventListener(IAppControlProviderEventListener* pListener)
1238 {
1239         __pAppControlProviderInternalEventListener = pListener;
1240         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1241         tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1242         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1243         return E_SUCCESS;
1244 }
1245
1246
1247 result
1248 _AppImpl::SetAppLaunchConditionEventListener(IAppLaunchConditionEventListener* pListener)
1249 {
1250         __pAppLaunchConditionEventListener = pListener;
1251         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1252         tmpAppHandlerType |= _APP_HANDLER_LAUNCH_COND;
1253         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1254
1255         return E_SUCCESS;
1256 }
1257
1258
1259 bool
1260 _AppImpl::IsTerminationRequested(void)
1261 {
1262         return __isTerminationRequested;
1263 }
1264
1265 void
1266 _AppImpl::OnPowerOffNotiReceived(void* user_data)
1267 {
1268         SysLog(NID_APP, "Application is being terminated by power off.");
1269
1270         _AppImpl* pAppImpl = _AppImpl::GetInstance();
1271         if (pAppImpl != null)
1272         {
1273                 pAppImpl->__forcedTermination = true;
1274         }
1275         
1276         app_efl_exit();
1277 }
1278
1279 bool 
1280 _AppImpl::IsForcedTermination(void)
1281 {
1282         return __forcedTermination;
1283 }
1284
1285 }} //Tizen::App
1286