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