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