Merge "Fixed get instance for setting client." into tizen_2.2
[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         SysLog(NID_APP, "Terminate() is called by application itself");
467
468         __isTerminationRequested = true;
469
470         app_efl_exit();
471         return r;
472 }
473
474
475 bool
476 _AppImpl::OnAppInitializing(void)
477 {
478         return false;
479 }
480
481
482 bool
483 _AppImpl::OnAppWillTerminate(void)
484 {
485         SysTryReturn(NID_APP, __pApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
486
487         return __pApp->OnAppWillTerminate();
488 }
489
490
491 result
492 _AppImpl::RegisterAppRequest(service_s* service, int& req, _AppHandler& handler)
493 {
494         _AppControlManager* pManager = _AppControlManager::GetInstance();
495         SysTryReturnResult(NID_APP, pManager != null, E_SYSTEM, "Wrong system state.");
496
497         result r = pManager->RegisterRequest(service, req, handler);
498         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
499
500         int providerType = _AppInfo::GetAppHandlerType();
501         providerType |= (_APP_HANDLER_LAUNCH_NORMAL | _APP_HANDLER_LAUNCH_COND);
502
503         SysTryLog(NID_APP, (handler & providerType) != 0, "Request 0x%x received for 0x%x.", handler,
504                         _AppInfo::GetAppHandlerType());
505         return E_SUCCESS;
506 }
507
508
509 result
510 _AppImpl::HandleAppRequest(service_s* service, int req, _AppHandler handler)
511 {
512         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(req);
513         SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
514
515         pArg->Print();
516
517         switch (handler)
518         {
519         case _APP_HANDLER_DATACONTROL:
520                 SysLog(NID_APP, "Handling DataControl request");
521
522                 OnDataControlRequestReceived(*pArg, static_cast< RequestId >(req));
523
524                 //_AppControlManager::GetInstance()->RemoveResultRequest(req);
525                 break;
526
527         case _APP_HANDLER_LAUNCH_COND:
528                 SysLog(NID_APP, "Handling conditional launch request");
529
530                 OnConditionRequestReceived(*pArg, static_cast< RequestId >(req));
531                 break;
532
533         default:
534                 SysLog(NID_APP, "Handling AppControl request");
535
536                 OnAppControlRequestReceived(*pArg, static_cast< RequestId >(req));
537                 break;
538         }
539
540         return E_SUCCESS;
541 }
542
543
544 result
545 _AppImpl::HandleLegacyAppRequest(service_s* service, int req, _AppHandler handler)
546 {
547         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
548         SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
549
550         //pArg->Print();
551
552         switch (handler)
553         {
554         case _APP_HANDLER_LAUNCH_NORMAL:
555                 SysLog(NID_APP, "Handling normal launch request");
556                 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req), true);
557
558                 // [TODO] request handle memory cleanup confirm
559                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
560                 break;
561
562         case _APP_HANDLER_LAUNCH_COND:
563                 SysLog(NID_APP, "Handling conditional launch request");
564
565                 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req), true);
566
567                 // [TODO] request handle memory cleanup confirm
568                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
569                 break;
570
571         default:
572                 break;
573         }
574
575         return E_SUCCESS;
576 }
577
578
579 result
580 _AppImpl::HandleDummyAppRequest(service_s* service, int req, _AppHandler handler)
581 {
582         SysLog(NID_APP, "Dummy handler");
583
584         return E_SUCCESS;
585 }
586
587
588 result
589 _AppImpl::SetLegacyRequestHandler(void)
590 {
591         SysLog(NID_APP, "Setting request handler for legacy application.");
592
593         __pRequestHandler = &_AppImpl::HandleDummyAppRequest;
594         __pLegacyRequestHandler = &_AppImpl::HandleLegacyAppRequest;
595
596         return E_SUCCESS;
597 }
598
599
600 void
601 _AppImpl::OnLowMemory(void* user_data)
602 {
603         _AppImpl* pAppImpl = _AppImpl::GetInstance();
604
605         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
606         SysTryReturnVoidResult(NID_APP, pAppImpl->__pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
607
608         if (_AppInfo::GetAppState() == RUNNING)
609         {
610                 pAppImpl->__pApp->OnLowMemory();
611         }
612 }
613
614
615 void
616 _AppImpl::OnBatteryLevelChanged(BatteryLevel batteryLevel)
617 {
618         SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
619
620         __pApp->OnBatteryLevelChanged(batteryLevel);
621 }
622
623 result
624 _AppImpl::SendUserEvent(RequestId requestId, const IList* pArgs, bool isPublic)
625 {
626         _AppUserEventArg* pArg = new (std::nothrow) _AppUserEventArg(requestId, pArgs, isPublic);
627         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "User event creation failure for %d.", requestId);
628
629         result r = __appUserEvent.FireAsync(*pArg);
630
631         return r;
632 }
633
634
635 void
636 _AppImpl::OnUserEventReceivedN(RequestId requestId, IList* pArgs, bool isPublic)
637 {
638         SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
639
640         if (isPublic)
641         {
642                 __pApp->OnUserEventReceivedN(requestId, pArgs);
643         }
644         else
645         {
646                 switch (requestId)
647                 {
648                 case _APP_EVENT_TERMINATE:
649                         SysLog(NID_APP, "App terminate is requested.");
650                         Terminate();
651                         break;
652                 case _APP_EVENT_RAISE:
653                         SysLog(NID_APP, "App raise is requested.");
654                         RaiseWindow();
655                         break;
656                 default:
657                         SysLog(NID_APP, "Unknown event : 0x%x.", requestId);
658                         break;
659                 }
660         }
661 }
662
663
664 void
665 _AppImpl::OnAppControlRequestReceived(const _AppArg& arg, RequestId reqId)
666 {
667         IAppControlProviderEventListener* pAppControlProviderEventListener = null;
668
669         const String internalOperation = arg.GetValue(OSP_K_APPCONTROL_INTERNAL_OPERATION);
670         if( !internalOperation.IsEmpty() )
671         {
672                 SysLog(NID_APP, "internalOperation:%ls", internalOperation.GetPointer() );
673                 pAppControlProviderEventListener = __pAppControlProviderInternalEventListener;
674         }
675         else
676         {
677                 pAppControlProviderEventListener = __pAppControlProviderEventListener;
678         }
679
680         if( pAppControlProviderEventListener == null)
681         {
682                 SysLog(NID_APP, "No AppControl provider event listener set for request %d.", reqId);
683                 return;
684         }
685
686         const char* pAppId = appinfo_get_appid();
687
688         const char* p = appsvc_get_operation(arg.GetBundle());
689         String operationId = (p) ? String(p) : TIZEN_OPERATION_MAIN;
690         if (operationId == L"http://tizen.org/appcontrol/operation/default")
691         {
692                 operationId = TIZEN_OPERATION_MAIN;
693         }
694
695         SysLog(NID_APP, "AppControl (%s, %ls).", pAppId, operationId.GetPointer());
696
697         String uri;
698         String mime;
699         const String* pUri = null;
700         const String* pMime = null;
701         p = appsvc_get_uri(arg.GetBundle());
702         if (p)
703         {
704                 uri = p;
705                 if (uri[0] == L'/')
706                 {
707                         // SLP deals with the URI as plain /opt/.../a.jpg.
708                         uri.Insert(FILE_SCHEME_WITH_DELIMITER, 0);
709                 }
710                 pUri = &uri;
711
712                 SysLog(NID_APP, "Delivered uri is [%ls].", pUri->GetPointer());
713         }
714
715         p = appsvc_get_mime(arg.GetBundle());
716         if (p)
717         {
718                 mime = p;
719                 pMime = &mime;
720         }
721
722         std::unique_ptr<HashMap> pMap(arg.GetArgMapN());
723
724         pAppControlProviderEventListener->OnAppControlRequestReceived(reqId, operationId, pUri, pMime, pMap.get());
725
726         SysLog(NID_APP, "AppControl (%s, %ls) invocation finished.", pAppId, operationId.GetPointer());
727 }
728
729
730 void
731 _AppImpl::OnDataControlRequestReceived(const _AppArg& arg, RequestId reqId)
732 {
733         String tempFilePath;
734         ISqlDataControlProviderEventListener* pSqlListener = null;
735         IMapDataControlProviderEventListener* pMapListener = null;
736         ArrayList* pColumnList = null;
737         HashMap* pInsertMap = null;
738         HashMap* pUpdateMap = null;
739         String appId;
740         String reqType;
741         String providerId;
742         String version;
743         String* pDataId = null;
744         String callerReqId;
745         String* pColumnCount = null;
746         String* pColumn = null;
747         String* pValue = null;
748         String* pWhere = null;
749         String* pOrder = null;
750         String* pErrorMsg = null;
751         String* pNo = null;
752         String* pCount = null;
753         String* pTmpPath = null;
754         String empty(L"NULL");
755         _AppArg resultArg;
756         int type = 0;
757         _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
758         int columnCount = 0;
759         int pageNo = 0;
760         int countPerPage = 0;
761         int i = 0;
762         int index = 0;
763         result r = E_SUCCESS;
764         result res = E_SUCCESS;
765         ArrayList* pList = null;
766         String* pKey = null;
767         String* pNewValue = null;
768         String* pOldValue = null;
769
770         SysLog(NID_APP, "[DC_PROV_RECV] Data control request is received.");
771         //arg.Print(); // request info
772
773         // key-based request
774         appId = arg.GetCallerAppId();
775
776         reqType = arg.GetValue(OSP_K_DATACONTROL_REQUEST_TYPE);
777         Integer::Parse(reqType, type);
778         requestType = static_cast< _DataControlRequestType >(type);
779
780         version = arg.GetValue(OSP_K_DATACONTROL_PROTOCOL_VERSION);
781         callerReqId = arg.GetValue(OSP_K_REQUEST_ID);
782         providerId = arg.GetValue(OSP_K_DATACONTROL_PROVIDER);
783
784         // list-based request
785         pList = _AppArg::GetListN(arg.GetBundle(), OSP_K_ARG);
786         SysTryCatch(NID_APP, pList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
787
788         pDataId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DATAID));// request list[0]: data ID
789         SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
790
791         if (requestType >= _DATACONTROL_REQUEST_TYPE_SQL_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_SQL_DELETE)
792         {
793                 pSqlListener = __pSqlDataControlProviderEventListener;
794                 SysTryCatch(NID_APP, pSqlListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
795         }
796         else if (requestType >= _DATACONTROL_REQUEST_TYPE_MAP_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_MAP_DELETE)
797         {
798                 pKey = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_MAPKEY)); // request list[1]
799                 SysTryCatch(NID_APP, pKey, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
800
801                 pMapListener = __pMapDataControlProviderEventListener;
802                 SysTryCatch(NID_APP, pMapListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
803         }
804         else
805         {
806                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid request");
807                 r = E_SYSTEM;
808         }
809
810         switch (requestType)
811         {
812         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
813                 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl SELECT");
814
815                 // request list[1]: selected column count
816                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
817                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
818
819                 if (pColumnCount->Equals(empty) == true)
820                 {
821                         pColumnList = null;
822                 }
823                 else
824                 {
825                         Integer::Parse(*pColumnCount, columnCount);
826                         pColumnList = new (std::nothrow) ArrayList();
827                         SysTryReturnVoidResult(NID_APP, pColumnList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
828                         pColumnList->Construct();
829                         SysLog(NID_APP, "[DC_PROV_RECV] selected column count: %d", columnCount);
830
831                         i = 0;
832                         while (i < columnCount) // request list[2]: column list
833                         {
834                                 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNLIST + i));
835                                 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
836
837                                 pColumnList->Add(*(new String(*pColumn)));
838                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", i, pColumn->GetPointer());
839                                 i++;
840                         }
841                 }
842
843                 i += _DATACONTROL_PACKET_INDEX_COLUMNLIST;
844                 pWhere = dynamic_cast< String* >(pList->GetAt(i)); // request list: where clause
845                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
846
847                 if (pWhere->Equals(empty) == true)
848                 {
849                         pWhere = null;
850                 }
851                 else
852                 {
853                         SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
854                 }
855
856                 i++;
857                 pOrder = dynamic_cast< String* >(pList->GetAt(i)); // request list: order clause
858                 SysTryCatch(NID_APP, pOrder, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
859
860                 if (pOrder->Equals(empty) == true)
861                 {
862                         pOrder = null;
863                 }
864                 else
865                 {
866                         SysLog(NID_APP, "[DC_PROV_RECV] pOrder: %ls", pOrder->GetPointer());
867                 }
868
869                 i++;
870                 pNo = dynamic_cast <String*>(pList->GetAt(i)); // request list: page number
871                 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
872                 Integer::Parse(*pNo, pageNo);
873
874                 i++;
875                 pCount = dynamic_cast <String*>(pList->GetAt(i)); // request list: count per page
876                 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
877                 Integer::Parse(*pCount, countPerPage);
878
879                 pSqlListener->OnSqlDataControlSelectRequestReceived(reqId, providerId, *pDataId, pColumnList,
880                                 pWhere, pOrder);
881
882                 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",
883                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
884                                 pDataId->GetPointer(), pColumnList, pWhere, pOrder, pageNo, countPerPage);
885                 break;
886         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
887                 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl INSERT");
888
889                 // request list[1]: inserted column count
890                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
891                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
892
893                 Integer::Parse(*pColumnCount, columnCount);
894                 SysLog(NID_APP, "[DC_PROV_RECV] inserted column count: %d", columnCount);
895
896                 pInsertMap = new (std::nothrow) HashMap();
897                 SysTryReturnVoidResult(NID_APP, pInsertMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
898                 pInsertMap->Construct();
899
900                 SysLog(NID_APP, "[DC_PROV_RECV] version: %ls", version.GetPointer());
901                 if (version == L"ver_2.1.0.2" || version == L"ver_2.1.0.3")
902                 {
903                         pTmpPath = dynamic_cast< String* >(pList->GetAt(2)); // request list[2]: path
904                         SysTryCatch(NID_APP, pTmpPath != null, , E_SYSTEM, "[E_SYSTEM] invalid request");
905                         SysLog(NID_APP, "[DC_PROV_RECV] request: %ls", pTmpPath->GetPointer());
906
907                         File request;
908                         r = request.Construct(*pTmpPath, "r");
909                         SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to open request (%ls).",
910                                         GetErrorMessage(r), pTmpPath->GetPointer());
911
912                         for (i = 0; i < columnCount; ++i) // column-value pairs
913                         {
914                                 int length = -1;
915                                 int ret = request.Read(&length, sizeof(int));
916                                 SysTryCatch(NID_APP, ret > 0, , E_SYSTEM, "[%s] Failed to receive request.",
917                                                 GetErrorMessage(GetLastResult()));
918
919                                 char* pColumn = new (std::nothrow) char[length + 1];
920                                 ret = request.Read(pColumn, length);
921                                 SysTryCatch(NID_APP, ret > 0, delete pColumn, E_SYSTEM, "[%s] Failed to receive request.",
922                                                 GetErrorMessage(GetLastResult()));
923                                 pColumn[length] = '\0';
924                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %s", i, pColumn);
925
926                                 ret = request.Read(&length, sizeof(int));
927                                 SysTryCatch(NID_APP, ret > 0, delete pColumn, E_SYSTEM, "[%s] Failed to receive request.",
928                                                 GetErrorMessage(GetLastResult()));
929
930                                 char* pValue = new (std::nothrow) char[length + 1];
931                                 ret = request.Read(pValue, length);
932                                 if (ret == 0)
933                                 {
934                                         SysLogException(NID_APP, E_SYSTEM, "[%s] Failed to receive request.", GetErrorMessage(GetLastResult()));
935                                         delete pColumn;
936                                         delete pValue;
937                                 }
938                                 pValue[length] = '\0';
939                                 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %s", i, pValue);
940
941                                 pInsertMap->Add(new (std::nothrow) String(pColumn), new (std::nothrow) String(pValue));
942                                 delete pColumn;
943                                 delete pValue;
944                         }
945                 }
946                 else
947                 {
948                         i = 0;
949                         index = 0;
950                         while (i < columnCount * 2) // request list: column-value pairs
951                         {
952                                 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
953                                 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
954                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", index, pColumn->GetPointer());
955
956                                 pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
957                                 SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
958                                 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %ls", index, pValue->GetPointer());
959
960                                 pInsertMap->Add(new (std::nothrow) String(*pColumn), new (std::nothrow) String(*pValue));
961                                 index++;
962                         }
963                 }
964
965                 if (pTmpPath)
966                 {
967                         r = File::Remove(*pTmpPath);
968                         SysTryLog(NID_APP, !IsFailed(r), "Failed to remove result: %ls", pTmpPath->GetPointer());
969                 }
970
971                 pSqlListener->OnSqlDataControlInsertRequestReceived(reqId, providerId, *pDataId, *pInsertMap);
972
973                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pInsertMap: 0x%x",
974                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
975                                 pDataId->GetPointer(), pInsertMap);
976                 break;
977         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
978                 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl UPDATE");
979
980                 // request list[1]: updated column count
981                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
982                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
983
984                 Integer::Parse(*pColumnCount, columnCount);
985                 SysLog(NID_APP, "[DC_PROV_RECV] updated column count: %d", columnCount);
986
987                 pUpdateMap = new (std::nothrow) HashMap();
988                 SysTryReturnVoidResult(NID_APP, pUpdateMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
989                 pUpdateMap->Construct();
990
991                 SysLog(NID_APP, "[DC_PROV_RECV] version: %ls", version.GetPointer());
992                 if (version == L"ver_2.1.0.2" || version == L"ver_2.1.0.3")
993                 {
994                         pTmpPath = dynamic_cast< String* >(pList->GetAt(2)); // request list[2]: path
995                         SysTryCatch(NID_APP, pTmpPath != null, , E_SYSTEM, "[E_SYSTEM] invalid request");
996                         SysLog(NID_APP, "[DC_PROV_RECV] request: %ls", pTmpPath->GetPointer());
997
998                         File request;
999                         r = request.Construct(*pTmpPath, "r");
1000                         SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to open request (%ls).",
1001                                         GetErrorMessage(r), pTmpPath->GetPointer());
1002
1003                         for (i = 0; i < columnCount; ++i) // column-value pairs
1004                         {
1005                                 int length = -1;
1006                                 int ret = request.Read(&length, sizeof(int));
1007                                 SysTryCatch(NID_APP, ret > 0, , E_SYSTEM, "[%s] Failed to receive request.",
1008                                                 GetErrorMessage(GetLastResult()));
1009
1010                                 char* pColumn = new (std::nothrow) char[length + 1];
1011                                 ret = request.Read(pColumn, length);
1012                                 SysTryCatch(NID_APP, ret > 0, delete pColumn, E_SYSTEM, "[%s] Failed to receive request.",
1013                                                 GetErrorMessage(GetLastResult()));
1014                                 pColumn[length] = '\0';
1015                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %s", i, pColumn);
1016
1017                                 ret = request.Read(&length, sizeof(int));
1018                                 SysTryCatch(NID_APP, ret > 0, delete pColumn, E_SYSTEM, "[%s] Failed to receive request.",
1019                                                 GetErrorMessage(GetLastResult()));
1020
1021                                 char* pValue = new (std::nothrow) char[length + 1];
1022                                 ret = request.Read(pValue, length);
1023                                 if (ret == 0)
1024                                 {
1025                                         SysLogException(NID_APP, E_SYSTEM, "[%s] Failed to receive request.", GetErrorMessage(GetLastResult()));
1026                                         delete pColumn;
1027                                         delete pValue;
1028                                 }
1029                                 pValue[length] = '\0';
1030                                 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %s", i, pValue);
1031
1032                                 pUpdateMap->Add(new (std::nothrow) String(pColumn), new (std::nothrow) String(pValue));
1033                                 delete pColumn;
1034                                 delete pValue;
1035                         }
1036
1037                         // request list: where clause
1038                         pWhere = dynamic_cast< String* >(pList->GetAt(3));
1039                         SysTryCatch(NID_APP, pWhere != null, , E_SYSTEM, "[E_SYSTEM] invalid request");
1040                 }
1041                 else
1042                 {
1043                         i = 0;
1044                         index = 0;
1045                         while (i < columnCount * 2) // request list: column-value pairs
1046                         {
1047                                 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
1048                                 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1049                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", index, pColumn->GetPointer());
1050
1051                                 pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
1052                                 SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1053                                 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %ls", index, pValue->GetPointer());
1054
1055                                 pUpdateMap->Add(new (std::nothrow) String(*pColumn), new (std::nothrow) String(*pValue));
1056                                 index++;
1057                         }
1058
1059                         // request list: where clause
1060                         pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i));
1061                         SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1062                 }
1063
1064                 if (pWhere->Equals(empty) == true)
1065                 {
1066                         pWhere = null;
1067                 }
1068                 else
1069                 {
1070                         SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
1071                 }
1072
1073                 if (pTmpPath)
1074                 {
1075                         r = File::Remove(*pTmpPath);
1076                         SysTryLog(NID_APP, !IsFailed(r), "Failed to remove result: %ls", pTmpPath->GetPointer());
1077                 }
1078
1079                 pSqlListener->OnSqlDataControlUpdateRequestReceived(reqId, providerId, *pDataId, *pUpdateMap, pWhere);
1080
1081                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pUpdateMap: 0x%x, pWhere: 0x%x",
1082                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1083                                 pDataId->GetPointer(), pUpdateMap, pWhere);
1084                 break;
1085         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
1086                 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl DELETE");
1087
1088                 // request list[1]: where clause
1089                 pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DELETEWHERE));
1090                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1091
1092                 if (pWhere->Equals(empty) == true)
1093                 {
1094                         pWhere = null;
1095                 }
1096                 else
1097                 {
1098                         SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
1099                 }
1100
1101                 pSqlListener->OnSqlDataControlDeleteRequestReceived(reqId, providerId, *pDataId, pWhere);
1102
1103                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pWhere: 0x%x",
1104                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1105                                 pDataId->GetPointer(), pWhere);
1106                 break;
1107         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
1108                 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl GetValue");
1109
1110                 pNo = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1111                 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1112                 Integer::Parse(*pNo, pageNo);
1113
1114                 pCount = dynamic_cast <String*>(pList->GetAt(3)); // request list[3]
1115                 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1116                 Integer::Parse(*pCount, countPerPage);
1117
1118                 pMapListener->OnMapDataControlGetValueRequestReceived(reqId, providerId, *pDataId, *pKey);
1119
1120                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, pageNo: %d, countPerPage: %d",
1121                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1122                                 pDataId->GetPointer(), pKey->GetPointer(), pageNo, countPerPage);
1123                 break;
1124         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
1125                 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl AddValue");
1126
1127                 pValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1128                 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1129
1130                 pMapListener->OnMapDataControlAddValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pValue);
1131
1132                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, value: %ls",
1133                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1134                                 pDataId->GetPointer(), pKey->GetPointer(), pValue->GetPointer());
1135                 break;
1136         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
1137                 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl SetValue");
1138
1139                 pOldValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1140                 SysTryCatch(NID_APP, pOldValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1141
1142                 pNewValue = dynamic_cast <String*>(pList->GetAt(3)); // request list[3]
1143                 SysTryCatch(NID_APP, pNewValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1144
1145                 pMapListener->OnMapDataControlSetValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pOldValue, *pNewValue);
1146
1147                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, oldValue: %ls, newValue: %ls",
1148                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1149                                 pDataId->GetPointer(), pKey->GetPointer(), pOldValue->GetPointer(), pNewValue->GetPointer());
1150                 break;
1151         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
1152                 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl RemoveValue");
1153
1154                 pValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1155                 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1156
1157                 pMapListener->OnMapDataControlRemoveValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pValue);
1158
1159                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, value: %ls",
1160                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1161                                 pDataId->GetPointer(), pKey->GetPointer(), pValue->GetPointer());
1162                 break;
1163         default:
1164                 SysTryCatch(NID_APP, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1165                 break;
1166         }
1167
1168         if (pColumnList)
1169         {
1170                 pColumnList->RemoveAll(true);
1171                 delete pColumnList;
1172         }
1173
1174         if (pInsertMap)
1175         {
1176                 pInsertMap->RemoveAll(true);
1177                 delete pInsertMap;
1178         }
1179
1180         if (pUpdateMap)
1181         {
1182                 pUpdateMap->RemoveAll(true);
1183                 delete pUpdateMap;
1184         }
1185
1186         return;
1187
1188 CATCH:
1189         if (pColumnList)
1190         {
1191                 pColumnList->RemoveAll(true);
1192                 delete pColumnList;
1193         }
1194
1195         if (pInsertMap)
1196         {
1197                 pInsertMap->RemoveAll(true);
1198                 delete pInsertMap;
1199         }
1200
1201         if (pUpdateMap)
1202         {
1203                 pUpdateMap->RemoveAll(true);
1204                 delete pUpdateMap;
1205         }
1206
1207         if (r == E_SYSTEM)
1208         {
1209                 pErrorMsg = new (std::nothrow) String("[E_SYSTEM] A system error has occurred.");
1210         }
1211         else if (r == E_OUT_OF_MEMORY)
1212         {
1213                 pErrorMsg = new (std::nothrow) String("[E_OUT_OF_MEMORY] The memory was insufficient.");
1214         }
1215         SysTryReturnVoidResult(NID_APP, pErrorMsg, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
1216
1217         res = DataControlProviderManager::GetInstance()->SendDataControlError(reqId, *pErrorMsg);
1218
1219         delete pErrorMsg;
1220
1221         return;
1222 }
1223
1224
1225 void
1226 _AppImpl::OnConditionRequestReceived(const _AppArg& arg, RequestId reqId)
1227 {
1228         if (__pAppLaunchConditionEventListener == null)
1229         {
1230                 SysLog(NID_APP, "No App launch condition event listener");
1231                 return;
1232         }
1233
1234         String condition = arg.GetValue(OSP_K_COND);
1235         //Object* pExtraData = null;//TODO: retrieve NFC extra data.
1236     HashMap* pExtraData = arg.GetArgMapN();
1237         ArrayList* pList = arg.GetArgListN();
1238
1239         __pAppLaunchConditionEventListener->OnAppLaunchConditionMetN(condition, pExtraData, pList);
1240 }
1241
1242
1243 long
1244 _AppImpl::GetWindowHandle(void) const
1245 {
1246         SysTryReturn(NID_APP, __pIAppImpl != null, -1, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
1247
1248         return __pIAppImpl->OnWindowHandleRequest();
1249 }
1250
1251
1252 void
1253 _AppImpl::RaiseWindow(void)
1254 {
1255         SysTryReturnVoidResult(NID_APP, __pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
1256
1257         __pIAppImpl->OnFrameRaiseRequested();
1258 }
1259
1260
1261 void
1262 _AppImpl::OnAppCheckpoint(void)
1263 {
1264         if (__pCheckpointEventListener)
1265         {
1266                 __pCheckpointEventListener->OnAppCheckpointing(*(AppRegistry::GetInstance()));
1267         }
1268 }
1269
1270
1271 bool
1272 _AppImpl::ConfirmAppService(void)
1273 {
1274         if (_AppInfo::GetApplicationId() == SERVICE_APPID)
1275         {
1276                 return true;
1277         }
1278
1279         int value = -1;
1280         int count = 0;
1281         long interval = 100;
1282
1283         while (true)
1284         {
1285                 int res = vconf_get_int(VCONFKEY_APPSERVICE_STATUS, &value);
1286                 if (value > 0)
1287                 {
1288                         return true;
1289                 }
1290
1291                 if (count >= HEARTBEAT_WAIT_COUNT)
1292                 {
1293                         return false;
1294                 }
1295
1296                 SysLog(NID_APP, "Waiting for app service %dth time(%d msec).", count, interval);
1297                 count++;
1298                 Thread::Sleep(interval);
1299                 interval += 100;
1300         }
1301
1302         return false;
1303 }
1304
1305 result
1306 _AppImpl::SetListener(_AppEvent appEvent, IEventListener* pListener)
1307 {
1308         switch (appEvent)
1309         {
1310         case _APP_EVENT_CHECKPOINT:
1311                 SysTryReturnResult(NID_APP, __pCheckpointEventListener == null, E_OBJ_ALREADY_EXIST, "Checkpoint listener is already set.");
1312
1313                 // fall through
1314         case _APP_EVENT_CLEAR_LISTENER:
1315                 __pCheckpointEventListener = dynamic_cast <IAppCheckpointEventListener*>(pListener);
1316                 break;
1317
1318         default:
1319                 break;
1320         }
1321
1322         return E_SUCCESS;
1323 }
1324
1325
1326 result
1327 _AppImpl::SetSqlDataControlProviderEventListener(ISqlDataControlProviderEventListener* pListener)
1328 {
1329         __pSqlDataControlProviderEventListener = pListener;
1330         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1331         tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1332         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1333         return E_SUCCESS;
1334 }
1335
1336
1337 result
1338 _AppImpl::SetMapDataControlProviderEventListener(IMapDataControlProviderEventListener* pListener)
1339 {
1340         __pMapDataControlProviderEventListener = pListener;
1341         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1342         tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1343         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1344         return E_SUCCESS;
1345 }
1346
1347
1348 result
1349 _AppImpl::SetAppControlProviderEventListener(IAppControlProviderEventListener* pListener)
1350 {
1351         __pAppControlProviderEventListener = pListener;
1352         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1353         tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1354         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1355         return E_SUCCESS;
1356 }
1357
1358 result
1359 _AppImpl::SetAppControlProviderInternalEventListener(IAppControlProviderEventListener* pListener)
1360 {
1361         __pAppControlProviderInternalEventListener = pListener;
1362         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1363         tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1364         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1365         return E_SUCCESS;
1366 }
1367
1368
1369 result
1370 _AppImpl::SetAppLaunchConditionEventListener(IAppLaunchConditionEventListener* pListener)
1371 {
1372         __pAppLaunchConditionEventListener = pListener;
1373         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1374         tmpAppHandlerType |= _APP_HANDLER_LAUNCH_COND;
1375         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1376
1377         return E_SUCCESS;
1378 }
1379
1380
1381 bool
1382 _AppImpl::IsTerminationRequested(void)
1383 {
1384         return __isTerminationRequested;
1385 }
1386
1387 typedef void (*cbForVconf)(keynode_t* node, void *pData);
1388
1389 void
1390 _AppImpl::OnPowerOffNotiReceived(void* node, void* user_data)
1391 {
1392         int value = -1;
1393         if (!vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &value))
1394         {
1395                 if (value == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || value == VCONFKEY_SYSMAN_POWER_OFF_RESTART)
1396                 {
1397                         SysLog(NID_APP, "Application is being terminated by power off.");
1398
1399                         if (vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, (cbForVconf)_AppImpl::OnPowerOffNotiReceived))
1400                         {
1401                                 SysLog(NID_APP, "Fail to ignore vconf for key(%s)", VCONFKEY_SYSMAN_POWER_OFF_STATUS);
1402                         }
1403
1404                         _AppImpl* pAppImpl = _AppImpl::GetInstance();
1405                         if (pAppImpl != null)
1406                         {
1407                                 pAppImpl->__forcedTermination = true;
1408                         }
1409
1410                         app_efl_exit();
1411                 }
1412         }
1413 }
1414
1415 void
1416 _AppImpl::OnSettingChanged(String& key)
1417 {
1418         if (key == L"http://tizen.org/setting/locale.language")
1419         {
1420                 _AppInfo::UpdatePackageInfo(true);
1421                 _AppResourceImpl::Reinitialize();
1422                 SysLog(NID_APP, "Reinitialized resources due to locale change.");
1423         }
1424 }
1425
1426 bool
1427 _AppImpl::IsForcedTermination(void)
1428 {
1429         return __forcedTermination;
1430 }
1431
1432 }} //Tizen::App
1433