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