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