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