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