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