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