Merge "Add a _LocalizedNumParser class and 4 static functions" into tizen_2.1
[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
28 #include <FBaseInteger.h>
29 #include <FBaseColArrayList.h>
30 #include <FBaseColHashMap.h>
31 #include <FBaseRtThread.h>
32 #include <FAppAppRegistry.h>
33 #include <FAppAppControl.h>
34 #include <FAppDataControlProviderManager.h>
35 #include <FAppIAppCheckPointEventListener.h>
36 #include <FAppISqlDataControlProviderEventListener.h>
37 #include <FAppIMapDataControlProviderEventListener.h>
38 #include <FAppIAppControlProviderEventListener.h>
39 #include <FAppIAppLaunchConditionEventListener.h>
40 #include <FIoFile.h>
41
42 #include <FBaseSysLog.h>
43 #include <FIo_RegistryImpl.h>
44 #include <FSys_EnvironmentImpl.h>
45 #include <FSys_PowerManagerImpl.h>
46 #include <FApp_Aul.h>
47 #include <FSys_AlarmManager.h>
48
49 #include "FApp_IAppManager.h"
50 #include "FApp_AppManagerProxy.h"
51 #include "FApp_AppImpl.h"
52 #include "FApp_AppInfo.h"
53 #include "FApp_AppControlManager.h"
54 #include "FApp_AppArg.h"
55 #include "FApp_IAppImpl.h"
56 #include "FApp_AppResourceImpl.h"
57
58 using namespace Tizen::Base;
59 using namespace Tizen::Base::Collection;
60 using namespace Tizen::Base::Runtime;
61 using namespace Tizen::Io;
62 using namespace Tizen::System;
63
64 namespace
65 {
66
67 const int HEARTBEAT_WAIT_COUNT = 3;
68 const wchar_t SERVICE_APPID[] = L"aospd00043.osp-app-service";
69 const char SELECTOR_RESPONSE_KEY[] = "__APP_SVC_START_INFO__";
70 const wchar_t FILE_SCHEME_WITH_DELIMITER[] = L"file://";
71 #ifndef VCONFKEY_APPSERVICE_STATUS
72 #define VCONFKEY_APPSERVICE_STATUS  "memory/appservice/status"
73 #endif
74
75 }
76
77 namespace Tizen { namespace App
78 {
79
80 _AppImpl* _AppImpl::__pAppImpl = null;
81 bool _AppImpl::__isTerminationRequested = false;
82 #if 0
83 static const int _DATACONTROL_PACKET_INDEX_APPID = 0;
84 static const int _DATACONTROL_PACKET_INDEX_REQUESTTYPE = 1;
85 static const int _DATACONTROL_PACKET_INDEX_REQID = 2;
86 static const int _DATACONTROL_PACKET_INDEX_PROVIDERID = 3;
87 #endif
88 static const int _DATACONTROL_PACKET_INDEX_DATAID = 0;
89 static const int _DATACONTROL_PACKET_INDEX_COLUMNCOUNT = 1;
90 static const int _DATACONTROL_PACKET_INDEX_DELETEWHERE = 1;
91 static const int _DATACONTROL_PACKET_INDEX_MAPKEY = 1;
92 static const int _DATACONTROL_PACKET_INDEX_COLUMNLIST = 2;
93 static const int _DATACONTROL_PACKET_INDEX_INSERTMAP = 2;
94 static const int _DATACONTROL_PACKET_INDEX_UPDATEMAP = 2;
95
96 struct charDeleter
97 {
98         void operator()(char* pValue)
99         {
100                 if(pValue != null)
101                 {
102                         free(pValue);
103                         pValue = null;
104                 }
105         }
106 };
107
108 _AppImpl::_AppImpl(App* pApp)
109         : __pCheckpointEventListener(null)
110         , __pSqlDataControlProviderEventListener(null)
111         , __pMapDataControlProviderEventListener(null)
112         , __pAppControlProviderEventListener(null)
113         , __pAppControlProviderInternalEventListener(null)
114         , __pAppLaunchConditionEventListener(null)
115         , __pApp(pApp)
116         , __pIAppImpl(null)
117         , __pRequestHandler(&_AppImpl::HandleAppRequest)
118         , __pLegacyRequestHandler(&_AppImpl::HandleDummyAppRequest)
119         , __forcedTermination(false)
120 {
121         __pAppImpl = this;
122 }
123
124
125 result
126 _AppImpl::Construct(const IList* pArgs)
127 {
128         SysTryReturnResult(NID_APP, pArgs != null, E_INVALID_ARG, "pArguments must not be null.");
129         SysAssertf(__pApp != null, "Getting App instance failed.");
130
131         result r = __appUserEvent.Construct();
132         SysTryLog(NID_APP, !IsFailed(r), "[%s] User event handler failure.", GetErrorMessage(r));
133
134         __appUserEvent.AddListener(*this);
135
136         _AppInfo::SetAppState(INITIALIZING);
137
138         return E_SUCCESS;
139 }
140
141
142 result
143 _AppImpl::Execute(_IAppImpl* pIAppImpl)
144 {
145         SysLogTag(NID_APP, "LAUNCH","[%ls:<Initialize Application>:start]", _AppInfo::GetAppExecutableName().GetPointer());
146         result r = E_SUCCESS;
147         int eflResult = APP_ERROR_NONE;
148         int argc = _AppInfo::GetArgc();
149         char** pArgv = _AppInfo::GetArgv();
150         _IAppManager* pAppManager = null;
151
152         _AppImpl* pAppImpl = _AppImpl::GetInstance();
153         SysTryReturnResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
154         pAppImpl->__pIAppImpl = pIAppImpl;
155
156         // handle legacy version handling
157         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
158         {
159                 pAppImpl->SetLegacyRequestHandler();
160         }
161
162         // clear previous notification
163         // [INFO] behavior change : platform does not clear previous notification
164         notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
165
166         app_event_callback_s state_handler;
167         memset(&state_handler, 0, sizeof(app_event_callback_s));
168
169         state_handler.create = &OnCreate;
170         state_handler.service = &OnService;
171         state_handler.terminate = &OnTerminate;
172         state_handler.pause = &OnPause;
173         state_handler.resume = &OnResume;
174         state_handler.device_orientation = NULL;
175
176         state_handler.low_memory = &OnLowMemory;
177         state_handler.low_battery = NULL;
178         state_handler.language_changed = &OnLanguageChanged;
179         state_handler.region_format_changed = NULL;
180
181         _Aul::SetPowerOffNotiListener(OnPowerOffNotiReceived, this);
182 //      if (pAppManager != null)
183 //      {
184 //              r = pAppManager->RegisterApplication(_AppInfo::GetPackageId(), _AppInfo::GetAppExecutableName(), _AppInfo::GetAppType() , _AppInfo::GetProcessId());
185 //              SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Application registration failed.", GetErrorMessage(r));
186 //      }
187
188         eflResult = app_efl_main(&argc, &pArgv, &state_handler, this);
189
190         SysTryLog(NID_APP, eflResult == APP_ERROR_NONE, "app_efl_main failed with error (%d): Unknown", eflResult);
191
192         // clear outstanding ongoing notification
193         notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
194
195 //CATCH:
196         pAppManager = _AppManagerProxy::GetService();
197
198         if (pAppManager != null)
199         {
200                 pAppManager->UnregisterApplication(_AppInfo::GetProcessId());
201         }
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::GetPackageId(), _AppInfo::GetAppExecutableName(), 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::GetAppInfo()->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         __isTerminationRequested = true;
376         pAppImpl->__pIAppImpl->OnTerminate();
377 }
378
379
380 void
381 _AppImpl::OnResume(void* user_data)
382 {
383         SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
384         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnResume>:start]", _AppInfo::GetAppExecutableName().GetPointer());
385
386         _AppImpl* pAppImpl = _AppImpl::GetInstance();
387         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
388         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
389         pAppImpl->__pIAppImpl->OnResume();
390         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnResume>:done]", _AppInfo::GetAppExecutableName().GetPointer());
391 }
392
393
394 void
395 _AppImpl::OnPause(void* user_data)
396 {
397         SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
398         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnPause>:start]", _AppInfo::GetAppExecutableName().GetPointer());
399
400         _AppImpl* pAppImpl = _AppImpl::GetInstance();
401         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
402         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
403         pAppImpl->__pIAppImpl->OnPause();
404         pAppImpl->OnAppCheckpoint();
405         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnPause>:done]", _AppInfo::GetAppExecutableName().GetPointer());
406 }
407
408
409 _AppImpl::~_AppImpl(void)
410 {
411         __pAppImpl = null;
412 }
413
414
415 _AppImpl*
416 _AppImpl::GetInstance(void)
417 {
418         return __pAppImpl;
419 }
420
421
422 App*
423 _AppImpl::GetAppInstance(void)
424 {
425         return __pApp;
426 }
427
428
429 IList*
430 _AppImpl::GetAppArgumentListN(void) const
431 {
432         ArrayList* pList = null;
433         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
434
435         if (pArg)
436         {
437                 pList = pArg->GetArgListN(0);
438         }
439         else
440         {
441                 // fallback
442                 pList = new (std::nothrow) ArrayList();
443                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
444                 String* pData = new (std::nothrow) String(LEGACY_LAUNCH_REASON_NORMAL);
445                 SysTryReturn(NID_APP, pData != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
446                 String* pData2 = new (std::nothrow) String(L"osp.operation.MAIN");
447                 SysTryReturn(NID_APP, pData2 != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
448
449                 pList->Construct();
450                 pList->Add(*pData);
451                 pList->Add(*pData2);
452         }
453
454         return pList;
455 }
456
457
458 result
459 _AppImpl::Terminate(void)
460 {
461         result r = E_SUCCESS;
462
463         __isTerminationRequested = true;
464
465         app_efl_exit();
466         return r;
467 }
468
469
470 bool
471 _AppImpl::OnAppInitializing(void)
472 {
473         return false;
474 }
475
476
477 bool
478 _AppImpl::OnAppWillTerminate(void)
479 {
480         SysTryReturn(NID_APP, __pApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
481
482         return __pApp->OnAppWillTerminate();
483 }
484
485
486 result
487 _AppImpl::RegisterAppRequest(service_s* service, int& req, _AppHandler& handler)
488 {
489         _AppControlManager* pManager = _AppControlManager::GetInstance();
490         SysTryReturnResult(NID_APP, pManager != null, E_SYSTEM, "Wrong system state.");
491
492         result r = pManager->RegisterRequest(service, req, handler);
493         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
494
495         int providerType = _AppInfo::GetAppHandlerType();
496         providerType |= (_APP_HANDLER_LAUNCH_NORMAL | _APP_HANDLER_LAUNCH_COND);
497
498         SysTryLog(NID_APP, (handler & providerType) != 0, "Request 0x%x received for 0x%x.", handler,
499                         _AppInfo::GetAppHandlerType());
500         return E_SUCCESS;
501 }
502
503
504 result
505 _AppImpl::HandleAppRequest(service_s* service, int req, _AppHandler handler)
506 {
507         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(req);
508         SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
509
510         pArg->Print();
511
512         switch (handler)
513         {
514         case _APP_HANDLER_DATACONTROL:
515                 SysLog(NID_APP, "Handling DataControl request");
516
517                 OnDataControlRequestReceived(*pArg, static_cast< RequestId >(req));
518
519                 //_AppControlManager::GetInstance()->RemoveResultRequest(req);
520                 break;
521
522         case _APP_HANDLER_LAUNCH_COND:
523                 SysLog(NID_APP, "Handling conditional launch request");
524
525                 OnConditionRequestReceived(*pArg, static_cast< RequestId >(req));
526                 break;
527
528         default:
529                 SysLog(NID_APP, "Handling AppControl request");
530
531                 OnAppControlRequestReceived(*pArg, static_cast< RequestId >(req));
532                 break;
533         }
534
535         return E_SUCCESS;
536 }
537
538
539 result
540 _AppImpl::HandleLegacyAppRequest(service_s* service, int req, _AppHandler handler)
541 {
542         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
543         SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
544
545         //pArg->Print();
546
547         switch (handler)
548         {
549         case _APP_HANDLER_LAUNCH_NORMAL:
550                 SysLog(NID_APP, "Handling normal launch request");
551                 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req));
552
553                 // [TODO] request handle memory cleanup confirm
554                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
555                 break;
556
557         case _APP_HANDLER_LAUNCH_COND:
558                 SysLog(NID_APP, "Handling conditional launch request");
559
560                 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req));
561
562                 // [TODO] request handle memory cleanup confirm
563                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
564                 break;
565
566         default:
567                 break;
568         }
569
570         return E_SUCCESS;
571 }
572
573
574 result
575 _AppImpl::HandleDummyAppRequest(service_s* service, int req, _AppHandler handler)
576 {
577         SysLog(NID_APP, "Dummy handler");
578
579         return E_SUCCESS;
580 }
581
582
583 result
584 _AppImpl::SetLegacyRequestHandler(void)
585 {
586         SysLog(NID_APP, "Setting request handler for legacy application.");
587
588         __pRequestHandler = &_AppImpl::HandleDummyAppRequest;
589         __pLegacyRequestHandler = &_AppImpl::HandleLegacyAppRequest;
590
591         return E_SUCCESS;
592 }
593
594
595 void
596 _AppImpl::OnLowMemory(void* user_data)
597 {
598         _AppImpl* pAppImpl = _AppImpl::GetInstance();
599
600         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
601         SysTryReturnVoidResult(NID_APP, pAppImpl->__pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
602
603         if (_AppInfo::GetAppState() == RUNNING)
604         {
605                 pAppImpl->__pApp->OnLowMemory();
606         }
607 }
608
609
610 void
611 _AppImpl::OnBatteryLevelChanged(BatteryLevel batteryLevel)
612 {
613         SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
614
615         __pApp->OnBatteryLevelChanged(batteryLevel);
616 }
617
618 void
619 _AppImpl::OnLanguageChanged(void* user_data)
620 {
621         SysLog(NID_APP, "");
622         _AppInfo::UpdatePackageInfo(true);
623
624         _AppResourceImpl::Reinitialize();
625         SysLog(NID_APP, "Language change done.");
626 }
627
628
629 result
630 _AppImpl::SendUserEvent(RequestId requestId, const IList* pArgs)
631 {
632         _AppUserEventArg* pArg = new (std::nothrow) _AppUserEventArg(requestId, pArgs);
633         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "User event creation failure for %d.", requestId);
634
635         result r = __appUserEvent.FireAsync(*pArg);
636
637         return r;
638 }
639
640
641 void
642 _AppImpl::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
643 {
644         SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
645
646         __pApp->OnUserEventReceivedN(requestId, pArgs);
647 }
648
649
650 void
651 _AppImpl::OnAppControlRequestReceived(const _AppArg& arg, RequestId reqId)
652 {
653         IAppControlProviderEventListener* pAppControlProviderEventListener = null;
654
655         const String internalOperation = arg.GetValue(OSP_K_APPCONTROL_INTERNAL_OPERATION);
656         if( !internalOperation.IsEmpty() )
657         {
658                 SysLog(NID_APP, "internalOperation:%ls", internalOperation.GetPointer() );
659                 pAppControlProviderEventListener = __pAppControlProviderInternalEventListener;
660         }
661         else
662         {
663                 pAppControlProviderEventListener = __pAppControlProviderEventListener;
664         }
665
666         if( pAppControlProviderEventListener == null)
667         {
668                 SysLog(NID_APP, "No AppControl provider event listener set for request %d.", reqId);
669                 return;
670         }
671
672         const String& appId = _AppInfo::GetApplicationId();
673
674         const char* p = appsvc_get_operation(arg.GetBundle());
675         String operationId = (p) ? String(p) : TIZEN_OPERATION_MAIN;
676         if (operationId == L"http://tizen.org/appcontrol/operation/default")
677         {
678                 operationId = TIZEN_OPERATION_MAIN;
679         }
680
681         SysLog(NID_APP, "AppControl (%ls, %ls).", appId.GetPointer(), operationId.GetPointer());
682
683         String uri;
684         String mime;
685         const String* pUri = null;
686         const String* pMime = null;
687         p = appsvc_get_uri(arg.GetBundle());
688         if (p)
689         {
690                 uri = p;
691                 if (uri[0] == L'/')
692                 {
693                         // SLP deals with the URI as plain /opt/.../a.jpg.
694                         uri.Insert(FILE_SCHEME_WITH_DELIMITER, 0);
695                 }
696                 pUri = &uri;
697
698                 SysLog(NID_APP, "Delivered uri is [%ls].", pUri->GetPointer());
699         }
700
701         p = appsvc_get_mime(arg.GetBundle());
702         if (p)
703         {
704                 mime = p;
705                 pMime = &mime;
706         }
707
708         std::unique_ptr<HashMap> pMap(arg.GetArgMapN());
709
710         pAppControlProviderEventListener->OnAppControlRequestReceived(reqId, operationId, pUri, pMime, pMap.get());
711
712         SysLog(NID_APP, "AppControl (%ls, %ls) invocation finished.", appId.GetPointer(), operationId.GetPointer());
713 }
714
715
716 void
717 _AppImpl::OnDataControlRequestReceived(const _AppArg& arg, RequestId reqId)
718 {
719         String tempFilePath;
720         ISqlDataControlProviderEventListener* pSqlListener = null;
721         IMapDataControlProviderEventListener* pMapListener = null;
722         ArrayList* pColumnList = null;
723         HashMap* pInsertMap = null;
724         HashMap* pUpdateMap = null;
725         String appId;
726         String reqType;
727         String providerId;
728         String version;
729         String* pDataId = null;
730         String callerReqId;
731         String* pColumnCount = null;
732         String* pColumn = null;
733         String* pValue = null;
734         String* pWhere = null;
735         String* pOrder = null;
736         String* pErrorMsg = null;
737         String* pNo = null;
738         String* pCount = null;
739         String* pTmpPath = null;
740         String empty(L"NULL");
741         _AppArg resultArg;
742         int type = 0;
743         _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
744         int columnCount = 0;
745         int pageNo = 0;
746         int countPerPage = 0;
747         int i = 0;
748         int index = 0;
749         result r = E_SUCCESS;
750         result res = E_SUCCESS;
751         ArrayList* pList = null;
752         String* pKey = null;
753         String* pNewValue = null;
754         String* pOldValue = null;
755
756         SysLog(NID_APP, "[DC_PROV_RECV] Data control request is received.");
757         //arg.Print(); // request info
758
759         // key-based request
760         appId = arg.GetCallerAppId();
761
762         reqType = arg.GetValue(OSP_K_DATACONTROL_REQUEST_TYPE);
763         Integer::Parse(reqType, type);
764         requestType = static_cast< _DataControlRequestType >(type);
765
766         version = arg.GetValue(OSP_K_DATACONTROL_PROTOCOL_VERSION);
767         callerReqId = arg.GetValue(OSP_K_REQUEST_ID);
768         providerId = arg.GetValue(OSP_K_DATACONTROL_PROVIDER);
769
770         // list-based request
771         pList = _AppArg::GetListN(arg.GetBundle(), OSP_K_ARG);
772         SysTryCatch(NID_APP, pList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
773
774         pDataId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DATAID));// request list[0]: data ID
775         SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
776
777         if (requestType >= _DATACONTROL_REQUEST_TYPE_SQL_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_SQL_DELETE)
778         {
779                 pSqlListener = __pSqlDataControlProviderEventListener;
780                 SysTryCatch(NID_APP, pSqlListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
781         }
782         else if (requestType >= _DATACONTROL_REQUEST_TYPE_MAP_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_MAP_DELETE)
783         {
784                 pKey = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_MAPKEY)); // request list[1]
785                 SysTryCatch(NID_APP, pKey, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
786
787                 pMapListener = __pMapDataControlProviderEventListener;
788                 SysTryCatch(NID_APP, pMapListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
789         }
790         else
791         {
792                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid request");
793                 r = E_SYSTEM;
794         }
795
796         switch (requestType)
797         {
798         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
799                 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl SELECT");
800
801                 // request list[1]: selected column count
802                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
803                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
804
805                 if (pColumnCount->Equals(empty) == true)
806                 {
807                         pColumnList = null;
808                 }
809                 else
810                 {
811                         Integer::Parse(*pColumnCount, columnCount);
812                         pColumnList = new (std::nothrow) ArrayList();
813                         SysTryReturnVoidResult(NID_APP, pColumnList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
814                         pColumnList->Construct();
815                         SysLog(NID_APP, "[DC_PROV_RECV] selected column count: %d", columnCount);
816
817                         i = 0;
818                         while (i < columnCount) // request list[2]: column list
819                         {
820                                 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNLIST + i));
821                                 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
822
823                                 pColumnList->Add(*(new String(*pColumn)));
824                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", i, pColumn->GetPointer());
825                                 i++;
826                         }
827                 }
828
829                 i += _DATACONTROL_PACKET_INDEX_COLUMNLIST;
830                 pWhere = dynamic_cast< String* >(pList->GetAt(i)); // request list: where clause
831                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
832
833                 if (pWhere->Equals(empty) == true)
834                 {
835                         pWhere = null;
836                 }
837                 else
838                 {
839                         SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
840                 }
841
842                 i++;
843                 pOrder = dynamic_cast< String* >(pList->GetAt(i)); // request list: order clause
844                 SysTryCatch(NID_APP, pOrder, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
845
846                 if (pOrder->Equals(empty) == true)
847                 {
848                         pOrder = null;
849                 }
850                 else
851                 {
852                         SysLog(NID_APP, "[DC_PROV_RECV] pOrder: %ls", pOrder->GetPointer());
853                 }
854
855                 i++;
856                 pNo = dynamic_cast <String*>(pList->GetAt(i)); // request list: page number
857                 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
858                 Integer::Parse(*pNo, pageNo);
859
860                 i++;
861                 pCount = dynamic_cast <String*>(pList->GetAt(i)); // request list: count per page
862                 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
863                 Integer::Parse(*pCount, countPerPage);
864
865                 pSqlListener->OnSqlDataControlSelectRequestReceived(reqId, providerId, *pDataId, pColumnList,
866                                 pWhere, pOrder);
867
868                 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",
869                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
870                                 pDataId->GetPointer(), pColumnList, pWhere, pOrder, pageNo, countPerPage);
871                 break;
872         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
873                 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl INSERT");
874
875                 // request list[1]: inserted column count
876                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
877                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
878
879                 Integer::Parse(*pColumnCount, columnCount);
880                 SysLog(NID_APP, "[DC_PROV_RECV] inserted column count: %d", columnCount);
881
882                 pInsertMap = new (std::nothrow) HashMap();
883                 SysTryReturnVoidResult(NID_APP, pInsertMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
884                 pInsertMap->Construct();
885
886                 SysLog(NID_APP, "[DC_PROV_RECV] version: %ls", version.GetPointer());
887                 if (version == L"ver_2.1.0.2" || version == L"ver_2.1.0.3")
888                 {
889                         pTmpPath = dynamic_cast< String* >(pList->GetAt(2)); // request list[2]: path
890                         SysTryCatch(NID_APP, pTmpPath != null, , E_SYSTEM, "[E_SYSTEM] invalid request");
891                         SysLog(NID_APP, "[DC_PROV_RECV] request: %ls", pTmpPath->GetPointer());
892
893                         File request;
894                         r = request.Construct(*pTmpPath, "r");
895                         SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to open request (%ls).",
896                                         GetErrorMessage(r), pTmpPath->GetPointer());
897
898                         for (i = 0; i < columnCount; ++i) // column-value pairs
899                         {
900                                 int length = -1;
901                                 int ret = request.Read(&length, sizeof(int));
902                                 SysTryCatch(NID_APP, ret > 0, , E_SYSTEM, "[%s] Failed to receive request.",
903                                                 GetErrorMessage(GetLastResult()));
904
905                                 char* pColumn = new (std::nothrow) char[length + 1];
906                                 ret = request.Read(pColumn, length);
907                                 SysTryCatch(NID_APP, ret > 0, delete pColumn, E_SYSTEM, "[%s] Failed to receive request.",
908                                                 GetErrorMessage(GetLastResult()));
909                                 pColumn[length] = '\0';
910                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %s", i, pColumn);
911
912                                 ret = request.Read(&length, sizeof(int));
913                                 SysTryCatch(NID_APP, ret > 0, delete pColumn, E_SYSTEM, "[%s] Failed to receive request.",
914                                                 GetErrorMessage(GetLastResult()));
915
916                                 char* pValue = new (std::nothrow) char[length + 1];
917                                 ret = request.Read(pValue, length);
918                                 if (ret == 0)
919                                 {
920                                         SysLogException(NID_APP, E_SYSTEM, "[%s] Failed to receive request.", GetErrorMessage(GetLastResult()));
921                                         delete pColumn;
922                                         delete pValue;
923                                 }
924                                 pValue[length] = '\0';
925                                 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %s", i, pValue);
926
927                                 pInsertMap->Add(new (std::nothrow) String(pColumn), new (std::nothrow) String(pValue));
928                                 delete pColumn;
929                                 delete pValue;
930                         }
931                 }
932                 else
933                 {
934                         i = 0;
935                         index = 0;
936                         while (i < columnCount * 2) // request list: column-value pairs
937                         {
938                                 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
939                                 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
940                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", index, pColumn->GetPointer());
941
942                                 pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
943                                 SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
944                                 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %ls", index, pValue->GetPointer());
945
946                                 pInsertMap->Add(new (std::nothrow) String(*pColumn), new (std::nothrow) String(*pValue));
947                                 index++;
948                         }
949                 }
950
951                 if (pTmpPath)
952                 {
953                         r = File::Remove(*pTmpPath);
954                         SysTryLog(NID_APP, !IsFailed(r), "Failed to remove result: %ls", pTmpPath->GetPointer());
955                 }
956
957                 pSqlListener->OnSqlDataControlInsertRequestReceived(reqId, providerId, *pDataId, *pInsertMap);
958
959                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pInsertMap: 0x%x",
960                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
961                                 pDataId->GetPointer(), pInsertMap);
962                 break;
963         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
964                 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl UPDATE");
965
966                 // request list[1]: updated column count
967                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
968                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
969
970                 Integer::Parse(*pColumnCount, columnCount);
971                 SysLog(NID_APP, "[DC_PROV_RECV] updated column count: %d", columnCount);
972
973                 pUpdateMap = new (std::nothrow) HashMap();
974                 SysTryReturnVoidResult(NID_APP, pUpdateMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
975                 pUpdateMap->Construct();
976
977                 SysLog(NID_APP, "[DC_PROV_RECV] version: %ls", version.GetPointer());
978                 if (version == L"ver_2.1.0.2" || version == L"ver_2.1.0.3")
979                 {
980                         pTmpPath = dynamic_cast< String* >(pList->GetAt(2)); // request list[2]: path
981                         SysTryCatch(NID_APP, pTmpPath != null, , E_SYSTEM, "[E_SYSTEM] invalid request");
982                         SysLog(NID_APP, "[DC_PROV_RECV] request: %ls", pTmpPath->GetPointer());
983
984                         File request;
985                         r = request.Construct(*pTmpPath, "r");
986                         SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to open request (%ls).",
987                                         GetErrorMessage(r), pTmpPath->GetPointer());
988
989                         for (i = 0; i < columnCount; ++i) // column-value pairs
990                         {
991                                 int length = -1;
992                                 int ret = request.Read(&length, sizeof(int));
993                                 SysTryCatch(NID_APP, ret > 0, , E_SYSTEM, "[%s] Failed to receive request.",
994                                                 GetErrorMessage(GetLastResult()));
995
996                                 char* pColumn = new (std::nothrow) char[length + 1];
997                                 ret = request.Read(pColumn, length);
998                                 SysTryCatch(NID_APP, ret > 0, delete pColumn, E_SYSTEM, "[%s] Failed to receive request.",
999                                                 GetErrorMessage(GetLastResult()));
1000                                 pColumn[length] = '\0';
1001                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %s", i, pColumn);
1002
1003                                 ret = request.Read(&length, sizeof(int));
1004                                 SysTryCatch(NID_APP, ret > 0, delete pColumn, E_SYSTEM, "[%s] Failed to receive request.",
1005                                                 GetErrorMessage(GetLastResult()));
1006
1007                                 char* pValue = new (std::nothrow) char[length + 1];
1008                                 ret = request.Read(pValue, length);
1009                                 if (ret == 0)
1010                                 {
1011                                         SysLogException(NID_APP, E_SYSTEM, "[%s] Failed to receive request.", GetErrorMessage(GetLastResult()));
1012                                         delete pColumn;
1013                                         delete pValue;
1014                                 }
1015                                 pValue[length] = '\0';
1016                                 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %s", i, pValue);
1017
1018                                 pUpdateMap->Add(new (std::nothrow) String(pColumn), new (std::nothrow) String(pValue));
1019                                 delete pColumn;
1020                                 delete pValue;
1021                         }
1022
1023                         // request list: where clause
1024                         pWhere = dynamic_cast< String* >(pList->GetAt(3));
1025                         SysTryCatch(NID_APP, pWhere != null, , E_SYSTEM, "[E_SYSTEM] invalid request");
1026                 }
1027                 else
1028                 {
1029                         i = 0;
1030                         index = 0;
1031                         while (i < columnCount * 2) // request list: column-value pairs
1032                         {
1033                                 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
1034                                 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1035                                 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", index, pColumn->GetPointer());
1036
1037                                 pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
1038                                 SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1039                                 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %ls", index, pValue->GetPointer());
1040
1041                                 pUpdateMap->Add(new (std::nothrow) String(*pColumn), new (std::nothrow) String(*pValue));
1042                                 index++;
1043                         }
1044
1045                         // request list: where clause
1046                         pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i));
1047                         SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1048                 }
1049
1050                 if (pWhere->Equals(empty) == true)
1051                 {
1052                         pWhere = null;
1053                 }
1054                 else
1055                 {
1056                         SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
1057                 }
1058
1059                 if (pTmpPath)
1060                 {
1061                         r = File::Remove(*pTmpPath);
1062                         SysTryLog(NID_APP, !IsFailed(r), "Failed to remove result: %ls", pTmpPath->GetPointer());
1063                 }
1064
1065                 pSqlListener->OnSqlDataControlUpdateRequestReceived(reqId, providerId, *pDataId, *pUpdateMap, pWhere);
1066
1067                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pUpdateMap: 0x%x, pWhere: 0x%x",
1068                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1069                                 pDataId->GetPointer(), pUpdateMap, pWhere);
1070                 break;
1071         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
1072                 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl DELETE");
1073
1074                 // request list[1]: where clause
1075                 pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DELETEWHERE));
1076                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1077
1078                 if (pWhere->Equals(empty) == true)
1079                 {
1080                         pWhere = null;
1081                 }
1082                 else
1083                 {
1084                         SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
1085                 }
1086
1087                 pSqlListener->OnSqlDataControlDeleteRequestReceived(reqId, providerId, *pDataId, pWhere);
1088
1089                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pWhere: 0x%x",
1090                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1091                                 pDataId->GetPointer(), pWhere);
1092                 break;
1093         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
1094                 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl GetValue");
1095
1096                 pNo = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1097                 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1098                 Integer::Parse(*pNo, pageNo);
1099
1100                 pCount = dynamic_cast <String*>(pList->GetAt(3)); // request list[3]
1101                 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1102                 Integer::Parse(*pCount, countPerPage);
1103
1104                 pMapListener->OnMapDataControlGetValueRequestReceived(reqId, providerId, *pDataId, *pKey);
1105
1106                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, pageNo: %d, countPerPage: %d",
1107                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1108                                 pDataId->GetPointer(), pKey->GetPointer(), pageNo, countPerPage);
1109                 break;
1110         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
1111                 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl AddValue");
1112
1113                 pValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1114                 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1115
1116                 pMapListener->OnMapDataControlAddValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pValue);
1117
1118                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, value: %ls",
1119                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1120                                 pDataId->GetPointer(), pKey->GetPointer(), pValue->GetPointer());
1121                 break;
1122         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
1123                 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl SetValue");
1124
1125                 pOldValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1126                 SysTryCatch(NID_APP, pOldValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1127
1128                 pNewValue = dynamic_cast <String*>(pList->GetAt(3)); // request list[3]
1129                 SysTryCatch(NID_APP, pNewValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1130
1131                 pMapListener->OnMapDataControlSetValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pOldValue, *pNewValue);
1132
1133                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, oldValue: %ls, newValue: %ls",
1134                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1135                                 pDataId->GetPointer(), pKey->GetPointer(), pOldValue->GetPointer(), pNewValue->GetPointer());
1136                 break;
1137         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
1138                 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl RemoveValue");
1139
1140                 pValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1141                 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1142
1143                 pMapListener->OnMapDataControlRemoveValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pValue);
1144
1145                 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, value: %ls",
1146                                 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1147                                 pDataId->GetPointer(), pKey->GetPointer(), pValue->GetPointer());
1148                 break;
1149         default:
1150                 SysTryCatch(NID_APP, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1151                 break;
1152         }
1153
1154         if (pColumnList)
1155         {
1156                 pColumnList->RemoveAll(true);
1157                 delete pColumnList;
1158         }
1159
1160         if (pInsertMap)
1161         {
1162                 pInsertMap->RemoveAll(true);
1163                 delete pInsertMap;
1164         }
1165
1166         if (pUpdateMap)
1167         {
1168                 pUpdateMap->RemoveAll(true);
1169                 delete pUpdateMap;
1170         }
1171
1172         return;
1173
1174 CATCH:
1175         if (r == E_SYSTEM)
1176         {
1177                 pErrorMsg = new (std::nothrow) String("[E_SYSTEM] A system error has occurred.");
1178         }
1179         else if (r == E_OUT_OF_MEMORY)
1180         {
1181                 pErrorMsg = new (std::nothrow) String("[E_OUT_OF_MEMORY] The memory was insufficient.");
1182         }
1183         SysTryReturnVoidResult(NID_APP, pErrorMsg, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
1184
1185         res = DataControlProviderManager::GetInstance()->SendDataControlError(reqId, *pErrorMsg);
1186
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         delete pErrorMsg;
1206
1207         return;
1208 }
1209
1210
1211 void
1212 _AppImpl::OnConditionRequestReceived(const _AppArg& arg, RequestId reqId)
1213 {
1214         if (__pAppLaunchConditionEventListener == null)
1215         {
1216                 SysLog(NID_APP, "No App launch condition event listener");
1217                 return;
1218         }
1219
1220         String condition = arg.GetValue(OSP_K_COND);
1221         //Object* pExtraData = null;//TODO: retrieve NFC extra data.
1222     HashMap* pExtraData = arg.GetArgMapN();
1223         ArrayList* pList = arg.GetArgListN();
1224
1225         __pAppLaunchConditionEventListener->OnAppLaunchConditionMetN(condition, pExtraData, pList);
1226 }
1227
1228
1229 long
1230 _AppImpl::GetWindowHandle(void) const
1231 {
1232         SysTryReturn(NID_APP, __pIAppImpl != null, -1, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
1233
1234         return __pIAppImpl->OnWindowHandleRequest();
1235 }
1236
1237
1238 void
1239 _AppImpl::RaiseWindow(void)
1240 {
1241         SysTryReturnVoidResult(NID_APP, __pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
1242
1243         __pIAppImpl->OnFrameRaiseRequested();
1244 }
1245
1246
1247 void
1248 _AppImpl::OnAppCheckpoint(void)
1249 {
1250         if (__pCheckpointEventListener)
1251         {
1252                 __pCheckpointEventListener->OnAppCheckpointing(*(AppRegistry::GetInstance()));
1253         }
1254 }
1255
1256
1257 bool
1258 _AppImpl::ConfirmAppService(void)
1259 {
1260         if (_AppInfo::GetApplicationId() == SERVICE_APPID)
1261         {
1262                 return true;
1263         }
1264
1265         int value = -1;
1266         int count = 0;
1267         long interval = 100;
1268
1269         while (true)
1270         {
1271                 int res = vconf_get_int(VCONFKEY_APPSERVICE_STATUS, &value);
1272                 if (value > 0)
1273                 {
1274                         return true;
1275                 }
1276
1277                 if (count >= HEARTBEAT_WAIT_COUNT)
1278                 {
1279                         return false;
1280                 }
1281
1282                 SysLog(NID_APP, "Waiting for app service %dth time(%d msec).", count, interval);
1283                 count++;
1284                 Thread::Sleep(interval);
1285                 interval += 100;
1286         }
1287
1288         return false;
1289 }
1290
1291 result
1292 _AppImpl::SetListener(_AppEvent appEvent, IEventListener* pListener)
1293 {
1294         switch (appEvent)
1295         {
1296         case AE_CHECKPOINT:
1297                 SysTryReturnResult(NID_APP, __pCheckpointEventListener == null, E_OBJ_ALREADY_EXIST, "Checkpoint listener is already set.");
1298
1299                 // fall through
1300         case AE_CLEAR_LISTENER:
1301                 __pCheckpointEventListener = dynamic_cast <IAppCheckpointEventListener*>(pListener);
1302                 break;
1303
1304         default:
1305                 break;
1306         }
1307
1308         return E_SUCCESS;
1309 }
1310
1311
1312 result
1313 _AppImpl::SetSqlDataControlProviderEventListener(ISqlDataControlProviderEventListener* pListener)
1314 {
1315         __pSqlDataControlProviderEventListener = pListener;
1316         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1317         tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1318         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1319         return E_SUCCESS;
1320 }
1321
1322
1323 result
1324 _AppImpl::SetMapDataControlProviderEventListener(IMapDataControlProviderEventListener* pListener)
1325 {
1326         __pMapDataControlProviderEventListener = pListener;
1327         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1328         tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1329         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1330         return E_SUCCESS;
1331 }
1332
1333
1334 result
1335 _AppImpl::SetAppControlProviderEventListener(IAppControlProviderEventListener* pListener)
1336 {
1337         __pAppControlProviderEventListener = pListener;
1338         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1339         tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1340         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1341         return E_SUCCESS;
1342 }
1343
1344 result
1345 _AppImpl::SetAppControlProviderInternalEventListener(IAppControlProviderEventListener* pListener)
1346 {
1347         __pAppControlProviderInternalEventListener = pListener;
1348         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1349         tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1350         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1351         return E_SUCCESS;
1352 }
1353
1354
1355 result
1356 _AppImpl::SetAppLaunchConditionEventListener(IAppLaunchConditionEventListener* pListener)
1357 {
1358         __pAppLaunchConditionEventListener = pListener;
1359         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1360         tmpAppHandlerType |= _APP_HANDLER_LAUNCH_COND;
1361         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1362
1363         return E_SUCCESS;
1364 }
1365
1366
1367 bool
1368 _AppImpl::IsTerminationRequested(void)
1369 {
1370         return __isTerminationRequested;
1371 }
1372
1373 void
1374 _AppImpl::OnPowerOffNotiReceived(void* user_data)
1375 {
1376         SysLog(NID_APP, "Application is being terminated by power off.");
1377
1378         _AppImpl* pAppImpl = _AppImpl::GetInstance();
1379         if (pAppImpl != null)
1380         {
1381                 pAppImpl->__forcedTermination = true;
1382         }
1383         
1384         app_efl_exit();
1385 }
1386
1387 bool 
1388 _AppImpl::IsForcedTermination(void)
1389 {
1390         return __forcedTermination;
1391 }
1392
1393 }} //Tizen::App
1394