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