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