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