2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FApp_AppImpl.cpp
20 * @brief This is the implementation for the _AppImpl class.
23 #include <unique_ptr.h>
25 #include <notification/notification.h>
26 #include <appsvc/appsvc.h>
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>
43 #include <FBaseSysLog.h>
44 #include <FIo_RegistryImpl.h>
45 #include <FSys_EnvironmentImpl.h>
46 #include <FSys_PowerManagerImpl.h>
48 #include <FSys_AlarmManager.h>
50 #include "FApp_IAppManager.h"
51 #include "FApp_AppManagerProxy.h"
52 #include "FApp_AppImpl.h"
53 #include "FApp_AppInfo.h"
54 #include "FApp_AppControlManager.h"
55 #include "FApp_AppArg.h"
56 #include "FApp_IAppImpl.h"
57 #include "FApp_AppResourceImpl.h"
59 using namespace Tizen::Base;
60 using namespace Tizen::Base::Collection;
61 using namespace Tizen::Base::Runtime;
62 using namespace Tizen::Io;
63 using namespace Tizen::System;
68 const int HEARTBEAT_WAIT_COUNT = 3;
69 const wchar_t SERVICE_APPID[] = L"aospd00043.osp-app-service";
70 const char SELECTOR_RESPONSE_KEY[] = "__APP_SVC_START_INFO__";
71 const wchar_t FILE_SCHEME_WITH_DELIMITER[] = L"file://";
72 #ifndef VCONFKEY_APPSERVICE_STATUS
73 #define VCONFKEY_APPSERVICE_STATUS "memory/appservice/status"
78 namespace Tizen { namespace App
81 _AppImpl* _AppImpl::__pAppImpl = null;
82 bool _AppImpl::__isTerminationRequested = false;
84 static const int _DATACONTROL_PACKET_INDEX_APPID = 0;
85 static const int _DATACONTROL_PACKET_INDEX_REQUESTTYPE = 1;
86 static const int _DATACONTROL_PACKET_INDEX_REQID = 2;
87 static const int _DATACONTROL_PACKET_INDEX_PROVIDERID = 3;
89 static const int _DATACONTROL_PACKET_INDEX_DATAID = 0;
90 static const int _DATACONTROL_PACKET_INDEX_COLUMNCOUNT = 1;
91 static const int _DATACONTROL_PACKET_INDEX_DELETEWHERE = 1;
92 static const int _DATACONTROL_PACKET_INDEX_MAPKEY = 1;
93 static const int _DATACONTROL_PACKET_INDEX_COLUMNLIST = 2;
94 static const int _DATACONTROL_PACKET_INDEX_INSERTMAP = 2;
95 static const int _DATACONTROL_PACKET_INDEX_UPDATEMAP = 2;
99 void operator()(char* pValue)
109 _AppImpl::_AppImpl(App* pApp)
110 : __pCheckpointEventListener(null)
111 , __pSqlDataControlProviderEventListener(null)
112 , __pMapDataControlProviderEventListener(null)
113 , __pAppControlProviderEventListener(null)
114 , __pAppControlProviderInternalEventListener(null)
115 , __pAppLaunchConditionEventListener(null)
118 , __pRequestHandler(&_AppImpl::HandleAppRequest)
119 , __pLegacyRequestHandler(&_AppImpl::HandleDummyAppRequest)
120 , __forcedTermination(false)
127 _AppImpl::Construct(const IList* pArgs)
129 SysTryReturnResult(NID_APP, pArgs != null, E_INVALID_ARG, "pArguments must not be null.");
130 SysAssertf(__pApp != null, "Getting App instance failed.");
132 result r = __appUserEvent.Construct();
133 SysTryLog(NID_APP, !IsFailed(r), "[%s] User event handler failure.", GetErrorMessage(r));
135 __appUserEvent.AddListener(*this);
137 _AppInfo::SetAppState(INITIALIZING);
144 _AppImpl::Execute(_IAppImpl* pIAppImpl)
146 SysLogTag(NID_APP, "LAUNCH","[%ls:<Initialize Application>:start]", _AppInfo::GetAppExecutableName().GetPointer());
147 result r = E_SUCCESS;
148 int eflResult = APP_ERROR_NONE;
149 int argc = _AppInfo::GetArgc();
150 char** pArgv = _AppInfo::GetArgv();
151 _IAppManager* pAppManager = null;
153 _AppImpl* pAppImpl = _AppImpl::GetInstance();
154 SysTryReturnResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
155 pAppImpl->__pIAppImpl = pIAppImpl;
157 // handle legacy version handling
158 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
160 pAppImpl->SetLegacyRequestHandler();
163 // clear previous notification
164 // [INFO] behavior change : platform does not clear previous notification
165 notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
167 app_event_callback_s state_handler;
168 memset(&state_handler, 0, sizeof(app_event_callback_s));
170 state_handler.create = &OnCreate;
171 state_handler.service = &OnService;
172 state_handler.terminate = &OnTerminate;
173 state_handler.pause = &OnPause;
174 state_handler.resume = &OnResume;
175 state_handler.device_orientation = &OnDeviceOrientationChanged;
177 state_handler.low_memory = &OnLowMemory;
178 state_handler.low_battery = &OnLowBattery;
179 state_handler.language_changed = &OnLanguageChanged;
180 state_handler.region_format_changed = &OnRegionFormatChanged;
182 _Aul::SetPowerOffNotiListener(OnPowerOffNotiReceived, this);
183 // if (pAppManager != null)
185 // r = pAppManager->RegisterApplication(_AppInfo::GetPackageId(), _AppInfo::GetAppExecutableName(), _AppInfo::GetAppType() , _AppInfo::GetProcessId());
186 // SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Application registration failed.", GetErrorMessage(r));
189 eflResult = app_efl_main(&argc, &pArgv, &state_handler, this);
191 SysTryLog(NID_APP, eflResult == APP_ERROR_NONE, "app_efl_main failed with error (%d): Unknown", eflResult);
193 // clear outstanding ongoing notification
194 notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
197 pAppManager = _AppManagerProxy::GetService();
199 if (pAppManager != null)
201 pAppManager->UnregisterApplication(_AppInfo::GetProcessId());
209 _AppImpl::OnCreate(void* user_data)
211 SysLog(NID_APP, "Platform creation event.");
213 _AppImpl* pAppImpl = static_cast<_AppImpl*>(user_data);
214 if (pAppImpl == null)
216 _AppInfo::SetAppState(TERMINATED);
217 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] Invalid platform state.");
221 _AppInfo::SetAppState(INITIALIZING);
223 if (!ConfirmAppService())
225 _AppInfo::SetAppState(TERMINATED);
226 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] Osp AppService is not running.");
230 SysLogTag(NID_APP, "LAUNCH","[%ls:<OnAppInitializing>:start]", _AppInfo::GetAppExecutableName().GetPointer());
231 if (!pAppImpl->__pIAppImpl->OnAppInitializing())
233 _AppInfo::SetAppState(TERMINATED);
234 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] The initialization of your application failed.");
237 SysLogTag(NID_APP, "LAUNCH","[%ls:<OnAppInitializing>:done]", _AppInfo::GetAppExecutableName().GetPointer());
239 SysLogTag(NID_APP, "LAUNCH","[%ls:<RegisterApplication>:start]", _AppInfo::GetAppExecutableName().GetPointer());
240 _IAppManager* pAppManager = _AppManagerProxy::GetService();
242 if (pAppManager != null)
244 result r = pAppManager->RegisterApplication(_AppInfo::GetPackageId(), _AppInfo::GetAppExecutableName(), static_cast<_AppType>(_AppInfo::GetAppType()), _AppInfo::GetProcessId());
245 SysTryLog(NID_APP, !IsFailed(r), "[%s] Application registration failed.", GetErrorMessage(r));
247 SysLogTag(NID_APP, "LAUNCH","[%ls:<RegisterApplication>:done]", _AppInfo::GetAppExecutableName().GetPointer());
248 // hide splash window
250 _PowerManagerImpl::InitBatteryEvent();
257 _AppImpl::OnService(service_s* service, void* user_data)
259 _AppImpl* pAppImpl = _AppImpl::GetInstance();
260 SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
261 SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
263 // ugly code for app selector event handling
264 bundle* pBundle = _AppArg::GetBundleFromSvc(service);
267 const char* pValue = appsvc_get_data(pBundle, SELECTOR_RESPONSE_KEY);
270 const int req = _AppArg::GetRequestIdFromBundle(pBundle);
272 const char* pOperation = appsvc_get_operation(pBundle);
274 SysLog(NID_APP, "App selector response [%s, %s], req(%d).", pValue, pOperation, req);
276 //_AppArg::Print(pBundle);
277 _AppControlManager::GetInstance()->SendAppControlStartResponse(req, pValue, pOperation);
282 //_AppArg::Print(pBundle);
284 _AppHandler handler = _APP_HANDLER_NONE;
285 pAppImpl->RegisterAppRequest(service, req, handler);
287 bool initialLaunch = false;
290 // (1) AppControl / DataControl provider listener
291 // (2) OnAppInitialized()
292 // (3) OnResume() (OnForeground()) (UiApp only)
293 // (4) OnUserEventReceivedN()
296 // (1) AppControl / DataControl provider listener
297 // (2) OnResume() (OnForeground()) (UiApp only)
298 // (3) OnUserEventReceivedN()
300 if (_AppInfo::GetAppState() == INITIALIZING)
302 initialLaunch = true;
304 unsigned int winId = 0;
305 service_get_window(service, &winId);
308 _AppInfo::GetAppInfo()->SetParentWindowHandle(winId);
309 SysLog(NID_APP, "Saving window handle 0x%x.", winId);
311 // call for callbacks
312 // ptr to member function
313 SysLogTag(NID_APP, "LAUNCH","[%ls:<RequestHandler and OnAppInitialized>:start]", _AppInfo::GetAppExecutableName().GetPointer());
314 (pAppImpl->*pAppImpl->__pRequestHandler)(service, req, handler);
316 if (!pAppImpl->__pIAppImpl->OnAppInitialized())
318 _AppInfo::SetAppState(TERMINATING);
319 SysLog(NID_APP, "[E_INIT_FAILED] The Initialization of your application failed.");
323 SysLogTag(NID_APP, "LAUNCH","[%ls:<RequestHandler and OnAppInitialized>:done]", _AppInfo::GetAppExecutableName().GetPointer());
325 _AppInfo::SetAppState(RUNNING);
327 SysLogTag(NID_APP, "LAUNCH","[%ls:<Initialize Application>:done]", _AppInfo::GetAppExecutableName().GetPointer());
333 // call for callbacks
334 // ptr to member function
335 (pAppImpl->*pAppImpl->__pRequestHandler)(service, req, handler);
337 std::unique_ptr<char[], charDeleter> pOperation(null);
340 int errVal = service_get_operation(service, &pBuf);
341 pOperation.reset(pBuf);
343 if((errVal == SERVICE_ERROR_NONE) && (!strcmp((const char*)pOperation.get(), "osp.operation.ALARM")))
345 std::unique_ptr<char[], charDeleter> pAlarmId(null);
348 errVal = service_get_extra_data(service, SERVICE_DATA_ALARM_ID, &pBuf);
349 pAlarmId.reset(pBuf);
350 SysTryReturnVoidResult(NID_SYS, errVal == SERVICE_ERROR_NONE, E_SYSTEM, "It is failed to get reserved alarm id.");
351 int alarmId = atoi((const char*)pAlarmId.get());
353 _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
354 pAlarmManager->OnAlarmExpired(alarmId);
360 pAppImpl->__pIAppImpl->OnService(service, initialLaunch);
362 // ptr to member function
363 (pAppImpl->*pAppImpl->__pLegacyRequestHandler)(service, req, handler);
368 _AppImpl::OnTerminate(void* user_data)
370 SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
372 _AppImpl* pAppImpl = _AppImpl::GetInstance();
373 SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
374 SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
376 __isTerminationRequested = true;
377 pAppImpl->__pIAppImpl->OnTerminate();
382 _AppImpl::OnResume(void* user_data)
384 SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
385 SysLogTag(NID_APP, "LAUNCH","[%ls:<OnResume>:start]", _AppInfo::GetAppExecutableName().GetPointer());
387 _AppImpl* pAppImpl = _AppImpl::GetInstance();
388 SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
389 SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
390 pAppImpl->__pIAppImpl->OnResume();
391 SysLogTag(NID_APP, "LAUNCH","[%ls:<OnResume>:done]", _AppInfo::GetAppExecutableName().GetPointer());
396 _AppImpl::OnPause(void* user_data)
398 SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
399 SysLogTag(NID_APP, "LAUNCH","[%ls:<OnPause>:start]", _AppInfo::GetAppExecutableName().GetPointer());
401 _AppImpl* pAppImpl = _AppImpl::GetInstance();
402 SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
403 SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
404 pAppImpl->__pIAppImpl->OnPause();
405 pAppImpl->OnAppCheckpoint();
406 SysLogTag(NID_APP, "LAUNCH","[%ls:<OnPause>:done]", _AppInfo::GetAppExecutableName().GetPointer());
411 _AppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation, void* user_data)
413 _AppImpl* pAppImpl = _AppImpl::GetInstance();
414 SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
415 SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
417 const AppState state = _AppInfo::GetAppState();
418 SysTryReturnVoidResult(NID_APP, state == INITIALIZING || state == RUNNING, E_INVALID_STATE, "[E_INVALID_STATE] Invalid application state : %d.", state);
420 pAppImpl->__pIAppImpl->OnDeviceOrientationChanged(orientation);
424 _AppImpl::~_AppImpl(void)
431 _AppImpl::GetInstance(void)
438 _AppImpl::GetAppInstance(void)
445 _AppImpl::GetAppArgumentListN(void) const
447 ArrayList* pList = null;
448 const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
452 pList = pArg->GetArgListN(0);
457 pList = new (std::nothrow) ArrayList();
458 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
459 String* pData = new (std::nothrow) String(LEGACY_LAUNCH_REASON_NORMAL);
460 SysTryReturn(NID_APP, pData != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
461 String* pData2 = new (std::nothrow) String(L"osp.operation.MAIN");
462 SysTryReturn(NID_APP, pData2 != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
474 _AppImpl::Terminate(void)
476 result r = E_SUCCESS;
478 __isTerminationRequested = true;
486 _AppImpl::OnAppInitializing(void)
493 _AppImpl::OnAppWillTerminate(void)
495 SysTryReturn(NID_APP, __pApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
497 return __pApp->OnAppWillTerminate();
502 _AppImpl::RegisterAppRequest(service_s* service, int& req, _AppHandler& handler)
504 _AppControlManager* pManager = _AppControlManager::GetInstance();
505 SysTryReturnResult(NID_APP, pManager != null, E_SYSTEM, "Wrong system state.");
507 result r = pManager->RegisterRequest(service, req, handler);
508 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
510 int providerType = _AppInfo::GetAppHandlerType();
511 providerType |= (_APP_HANDLER_LAUNCH_NORMAL | _APP_HANDLER_LAUNCH_COND);
513 SysTryLog(NID_APP, (handler & providerType) != 0, "Request 0x%x received for 0x%x.", handler,
514 _AppInfo::GetAppHandlerType());
520 _AppImpl::HandleAppRequest(service_s* service, int req, _AppHandler handler)
522 const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(req);
523 SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
529 case _APP_HANDLER_DATACONTROL:
530 SysLog(NID_APP, "Handling DataControl request");
532 OnDataControlRequestReceived(*pArg, static_cast< RequestId >(req));
534 _AppControlManager::GetInstance()->RemoveResultRequest(req);
537 case _APP_HANDLER_LAUNCH_COND:
538 SysLog(NID_APP, "Handling conditional launch request");
540 OnConditionRequestReceived(*pArg, static_cast< RequestId >(req));
544 SysLog(NID_APP, "Handling AppControl request");
546 OnAppControlRequestReceived(*pArg, static_cast< RequestId >(req));
555 _AppImpl::HandleLegacyAppRequest(service_s* service, int req, _AppHandler handler)
557 const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
558 SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
564 case _APP_HANDLER_LAUNCH_NORMAL:
565 SysLog(NID_APP, "Handling normal launch request");
566 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req));
568 // [TODO] request handle memory cleanup confirm
569 _AppControlManager::GetInstance()->RemoveResultRequest(req);
572 case _APP_HANDLER_LAUNCH_COND:
573 SysLog(NID_APP, "Handling conditional launch request");
575 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req));
577 // [TODO] request handle memory cleanup confirm
578 _AppControlManager::GetInstance()->RemoveResultRequest(req);
590 _AppImpl::HandleDummyAppRequest(service_s* service, int req, _AppHandler handler)
592 SysLog(NID_APP, "Dummy handler");
599 _AppImpl::SetLegacyRequestHandler(void)
601 SysLog(NID_APP, "Setting request handler for legacy application.");
603 __pRequestHandler = &_AppImpl::HandleDummyAppRequest;
604 __pLegacyRequestHandler = &_AppImpl::HandleLegacyAppRequest;
611 _AppImpl::OnLowMemory(void* user_data)
613 _AppImpl* pAppImpl = _AppImpl::GetInstance();
615 SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
616 SysTryReturnVoidResult(NID_APP, pAppImpl->__pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
618 if (_AppInfo::GetAppState() == RUNNING)
620 pAppImpl->__pApp->OnLowMemory();
626 _AppImpl::OnLowBattery(void* user_data)
628 // Osp doesn't implement this callback.
632 _AppImpl::OnBatteryLevelChanged(BatteryLevel batteryLevel)
634 SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
636 __pApp->OnBatteryLevelChanged(batteryLevel);
640 _AppImpl::OnLanguageChanged(void* user_data)
643 _AppInfo::UpdatePackageInfo(true);
645 _AppResourceImpl::Reinitialize();
646 SysLog(NID_APP, "Language change done.");
650 _AppImpl::OnRegionFormatChanged(void* user_data)
652 // Osp doesn't implement this callback.
657 _AppImpl::SendUserEvent(RequestId requestId, const IList* pArgs)
659 _AppUserEventArg* pArg = new (std::nothrow) _AppUserEventArg(requestId, pArgs);
660 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "User event creation failure for %d.", requestId);
662 result r = __appUserEvent.FireAsync(*pArg);
669 _AppImpl::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
671 SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
673 __pApp->OnUserEventReceivedN(requestId, pArgs);
678 _AppImpl::OnAppControlRequestReceived(const _AppArg& arg, RequestId reqId)
680 IAppControlProviderEventListener* pAppControlProviderEventListener = null;
682 const String internalOperation = arg.GetValue(OSP_K_APPCONTROL_INTERNAL_OPERATION);
683 if( !internalOperation.IsEmpty() )
685 SysLog(NID_APP, "internalOperation:%ls", internalOperation.GetPointer() );
686 pAppControlProviderEventListener = __pAppControlProviderInternalEventListener;
690 pAppControlProviderEventListener = __pAppControlProviderEventListener;
693 if( pAppControlProviderEventListener == null)
695 SysLog(NID_APP, "No AppControl provider event listener set for request %d.", reqId);
699 const String& appId = _AppInfo::GetApplicationId();
701 const char* p = appsvc_get_operation(arg.GetBundle());
702 String operationId = (p) ? String(p) : TIZEN_OPERATION_MAIN;
703 if (operationId == L"http://tizen.org/appcontrol/operation/default")
705 operationId = TIZEN_OPERATION_MAIN;
708 SysLog(NID_APP, "AppControl (%ls, %ls).", appId.GetPointer(), operationId.GetPointer());
712 const String* pUri = null;
713 const String* pMime = null;
714 p = appsvc_get_uri(arg.GetBundle());
720 // SLP deals with the URI as plain /opt/.../a.jpg.
721 uri.Insert(FILE_SCHEME_WITH_DELIMITER, 0);
725 SysLog(NID_APP, "Delivered uri is [%ls].", pUri->GetPointer());
728 p = appsvc_get_mime(arg.GetBundle());
735 std::unique_ptr<HashMap> pMap(arg.GetArgMapN());
737 pAppControlProviderEventListener->OnAppControlRequestReceived(reqId, operationId, pUri, pMime, pMap.get());
739 SysLog(NID_APP, "AppControl (%ls, %ls) invocation finished.", appId.GetPointer(), operationId.GetPointer());
744 _AppImpl::OnDataControlRequestReceived(const _AppArg& arg, RequestId reqId)
747 ISqlDataControlProviderEventListener* pSqlListener = null;
748 IMapDataControlProviderEventListener* pMapListener = null;
749 ArrayList* pColumnList = null;
750 HashMap* pInsertMap = null;
751 HashMap* pUpdateMap = null;
755 String* pDataId = null;
757 String* pColumnCount = null;
758 String* pColumn = null;
759 String* pValue = null;
760 String* pWhere = null;
761 String* pOrder = null;
762 String* pErrorMsg = null;
764 String* pCount = null;
765 String empty(L"NULL");
768 _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
771 int countPerPage = 0;
774 result r = E_SUCCESS;
775 result res = E_SUCCESS;
776 ArrayList* pList = null;
778 String* pNewValue = null;
779 String* pOldValue = null;
781 SysLog(NID_APP, "[DC_PROV_RECV] Data control request is received.");
782 //arg.Print(); // request info
785 appId = arg.GetCallerAppId();
787 reqType = arg.GetValue(OSP_K_DATACONTROL_REQUEST_TYPE);
788 Integer::Parse(reqType, type);
789 requestType = static_cast< _DataControlRequestType >(type);
791 callerReqId = arg.GetValue(OSP_K_REQUEST_ID);
792 providerId = arg.GetValue(OSP_K_DATACONTROL_PROVIDER);
794 // list-based request
795 pList = _AppArg::GetListN(arg.GetBundle(), OSP_K_ARG);
796 SysTryCatch(NID_APP, pList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
798 pDataId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DATAID));// request list[0]: data ID
799 SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
801 if (requestType >= _DATACONTROL_REQUEST_TYPE_SQL_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_SQL_DELETE)
803 pSqlListener = __pSqlDataControlProviderEventListener;
804 SysTryCatch(NID_APP, pSqlListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
806 else if (requestType >= _DATACONTROL_REQUEST_TYPE_MAP_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_MAP_DELETE)
808 pKey = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_MAPKEY)); // request list[1]
809 SysTryCatch(NID_APP, pKey, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
811 pMapListener = __pMapDataControlProviderEventListener;
812 SysTryCatch(NID_APP, pMapListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
816 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid request");
822 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
823 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl SELECT");
825 // request list[1]: selected column count
826 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
827 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
829 if (pColumnCount->Equals(empty) == true)
835 Integer::Parse(*pColumnCount, columnCount);
836 pColumnList = new (std::nothrow) ArrayList();
837 SysTryReturnVoidResult(NID_APP, pColumnList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
838 pColumnList->Construct();
839 SysLog(NID_APP, "[DC_PROV_RECV] selected column count: %d", columnCount);
842 while (i < columnCount) // request list[2]: column list
844 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNLIST + i));
845 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
847 pColumnList->Add(*(new String(*pColumn)));
848 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", i, pColumn->GetPointer());
853 i += _DATACONTROL_PACKET_INDEX_COLUMNLIST;
854 pWhere = dynamic_cast< String* >(pList->GetAt(i)); // request list: where clause
855 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
857 if (pWhere->Equals(empty) == true)
863 SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
867 pOrder = dynamic_cast< String* >(pList->GetAt(i)); // request list: order clause
868 SysTryCatch(NID_APP, pOrder, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
870 if (pOrder->Equals(empty) == true)
876 SysLog(NID_APP, "[DC_PROV_RECV] pOrder: %ls", pOrder->GetPointer());
880 pNo = dynamic_cast <String*>(pList->GetAt(i)); // request list: page number
881 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
882 Integer::Parse(*pNo, pageNo);
885 pCount = dynamic_cast <String*>(pList->GetAt(i)); // request list: count per page
886 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
887 Integer::Parse(*pCount, countPerPage);
889 pSqlListener->OnSqlDataControlSelectRequestReceived(reqId, providerId, *pDataId, pColumnList,
892 SysLog(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",
893 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
894 pDataId->GetPointer(), pColumnList, pWhere, pOrder, pageNo, countPerPage);
896 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
897 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl INSERT");
899 // request list[1]: insert column count
900 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
901 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
903 Integer::Parse(*pColumnCount, columnCount);
904 SysLog(NID_APP, "[DC_PROV_RECV] inserted column count: %d", columnCount);
906 pInsertMap = new (std::nothrow) HashMap();
907 SysTryReturnVoidResult(NID_APP, pInsertMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
908 pInsertMap->Construct();
912 while (i < columnCount * 2) // request list[2]: column-value pairs
914 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
915 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
916 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", index, pColumn->GetPointer());
918 pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
919 SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
920 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %ls", index, pValue->GetPointer());
922 pInsertMap->Add(*(new String(*pColumn)), *(new String(*pValue)));
926 pSqlListener->OnSqlDataControlInsertRequestReceived(reqId, providerId, *pDataId, *pInsertMap);
928 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pInsertMap: 0x%x",
929 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
930 pDataId->GetPointer(), pInsertMap);
932 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
933 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl UPDATE");
935 // request list[1]: updated column count
936 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
937 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
939 Integer::Parse(*pColumnCount, columnCount);
940 SysLog(NID_APP, "[DC_PROV_RECV] updated column count: %d", columnCount);
942 pUpdateMap = new (std::nothrow) HashMap();
943 SysTryReturnVoidResult(NID_APP, pUpdateMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
944 pUpdateMap->Construct();
948 while (i < columnCount * 2) // request list[2]: column-value pairs
950 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
951 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
952 SysLog(NID_APP, "[DC_PROV_RECV] column[%d]: %ls", index, pColumn->GetPointer());
954 pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
955 SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
956 SysLog(NID_APP, "[DC_PROV_RECV] value[%d]: %ls", index, pValue->GetPointer());
958 pUpdateMap->Add(*(new String(*pColumn)), *(new String(*pValue)));
962 // request list: where clause
963 pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i));
964 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
966 if (pWhere->Equals(empty) == true)
972 SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
975 pSqlListener->OnSqlDataControlUpdateRequestReceived(reqId, providerId, *pDataId, *pUpdateMap, pWhere);
977 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pUpdateMap: 0x%x, pWhere: 0x%x",
978 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
979 pDataId->GetPointer(), pUpdateMap, pWhere);
981 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
982 SysLog(NID_APP, "[DC_PROV_RECV] SqlDataControl DELETE");
984 // request list[1]: where clause
985 pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DELETEWHERE));
986 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
988 if (pWhere->Equals(empty) == true)
994 SysLog(NID_APP, "[DC_PROV_RECV] pWhere: %ls", pWhere->GetPointer());
997 pSqlListener->OnSqlDataControlDeleteRequestReceived(reqId, providerId, *pDataId, pWhere);
999 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, pWhere: 0x%x",
1000 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1001 pDataId->GetPointer(), pWhere);
1003 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
1004 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl GetValue");
1006 pNo = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1007 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1008 Integer::Parse(*pNo, pageNo);
1010 pCount = dynamic_cast <String*>(pList->GetAt(3)); // request list[3]
1011 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1012 Integer::Parse(*pCount, countPerPage);
1014 pMapListener->OnMapDataControlGetValueRequestReceived(reqId, providerId, *pDataId, *pKey);
1016 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, pageNo: %d, countPerPage: %d",
1017 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1018 pDataId->GetPointer(), pKey->GetPointer(), pageNo, countPerPage);
1020 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
1021 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl AddValue");
1023 pValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1024 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1026 pMapListener->OnMapDataControlAddValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pValue);
1028 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, value: %ls",
1029 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1030 pDataId->GetPointer(), pKey->GetPointer(), pValue->GetPointer());
1032 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
1033 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl SetValue");
1035 pOldValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1036 SysTryCatch(NID_APP, pOldValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1038 pNewValue = dynamic_cast <String*>(pList->GetAt(3)); // request list[3]
1039 SysTryCatch(NID_APP, pNewValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1041 pMapListener->OnMapDataControlSetValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pOldValue, *pNewValue);
1043 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, oldValue: %ls, newValue: %ls",
1044 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1045 pDataId->GetPointer(), pKey->GetPointer(), pOldValue->GetPointer(), pNewValue->GetPointer());
1047 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
1048 SysLog(NID_APP, "[DC_PROV_RECV] MapDataControl RemoveValue");
1050 pValue = dynamic_cast <String*>(pList->GetAt(2)); // request list[2]
1051 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1053 pMapListener->OnMapDataControlRemoveValueRequestReceived(reqId, providerId, *pDataId, *pKey, *pValue);
1055 SysLog(NID_APP, "[DC_PROV_RECV] caller app: %ls, requestType: %d, caller req: %ls, provider: %ls, data: %ls, key: %ls, value: %ls",
1056 appId.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(),
1057 pDataId->GetPointer(), pKey->GetPointer(), pValue->GetPointer());
1060 SysTryCatch(NID_APP, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1066 pColumnList->RemoveAll(true);
1072 pInsertMap->RemoveAll(true);
1078 pUpdateMap->RemoveAll(true);
1087 pErrorMsg = new (std::nothrow) String("[E_SYSTEM] A system error has occurred.");
1089 else if (r == E_OUT_OF_MEMORY)
1091 pErrorMsg = new (std::nothrow) String("[E_OUT_OF_MEMORY] The memory was insufficient.");
1093 SysTryReturnVoidResult(NID_APP, pErrorMsg, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
1095 res = DataControlProviderManager::GetInstance()->SendDataControlError(reqId, *pErrorMsg);
1099 pColumnList->RemoveAll(true);
1105 pInsertMap->RemoveAll(true);
1111 pUpdateMap->RemoveAll(true);
1122 _AppImpl::OnConditionRequestReceived(const _AppArg& arg, RequestId reqId)
1124 if (__pAppLaunchConditionEventListener == null)
1126 SysLog(NID_APP, "No App launch condition event listener");
1130 String condition = arg.GetValue(OSP_K_COND);
1131 //Object* pExtraData = null;//TODO: retrieve NFC extra data.
1132 HashMap* pExtraData = arg.GetArgMapN();
1133 ArrayList* pList = arg.GetArgListN();
1135 __pAppLaunchConditionEventListener->OnAppLaunchConditionMetN(condition, pExtraData, pList);
1140 _AppImpl::GetWindowHandle(void) const
1142 SysTryReturn(NID_APP, __pIAppImpl != null, -1, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
1144 return __pIAppImpl->OnWindowHandleRequest();
1149 _AppImpl::OnAppCheckpoint(void)
1151 if (__pCheckpointEventListener)
1153 __pCheckpointEventListener->OnAppCheckpointing(*(AppRegistry::GetInstance()));
1159 _AppImpl::ConfirmAppService(void)
1161 if (_AppInfo::GetApplicationId() == SERVICE_APPID)
1168 long interval = 100;
1172 int res = vconf_get_int(VCONFKEY_APPSERVICE_STATUS, &value);
1178 if (count >= HEARTBEAT_WAIT_COUNT)
1183 SysLog(NID_APP, "Waiting for app service %dth time(%d msec).", count, interval);
1185 Thread::Sleep(interval);
1193 _AppImpl::SetListener(_AppEvent appEvent, IEventListener* pListener)
1198 SysTryReturnResult(NID_APP, __pCheckpointEventListener == null, E_OBJ_ALREADY_EXIST, "Checkpoint listener is already set.");
1201 case AE_CLEAR_LISTENER:
1202 __pCheckpointEventListener = dynamic_cast <IAppCheckpointEventListener*>(pListener);
1214 _AppImpl::SetSqlDataControlProviderEventListener(ISqlDataControlProviderEventListener* pListener)
1216 __pSqlDataControlProviderEventListener = pListener;
1217 int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1218 tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1219 _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1225 _AppImpl::SetMapDataControlProviderEventListener(IMapDataControlProviderEventListener* pListener)
1227 __pMapDataControlProviderEventListener = pListener;
1228 int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1229 tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1230 _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1236 _AppImpl::SetAppControlProviderEventListener(IAppControlProviderEventListener* pListener)
1238 __pAppControlProviderEventListener = pListener;
1239 int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1240 tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1241 _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1246 _AppImpl::SetAppControlProviderInternalEventListener(IAppControlProviderEventListener* pListener)
1248 __pAppControlProviderInternalEventListener = pListener;
1249 int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1250 tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1251 _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1257 _AppImpl::SetAppLaunchConditionEventListener(IAppLaunchConditionEventListener* pListener)
1259 __pAppLaunchConditionEventListener = pListener;
1260 int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1261 tmpAppHandlerType |= _APP_HANDLER_LAUNCH_COND;
1262 _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1269 _AppImpl::IsTerminationRequested(void)
1271 return __isTerminationRequested;
1275 _AppImpl::OnPowerOffNotiReceived(void* user_data)
1277 SysLog(NID_APP, "Application is being terminated by power off.");
1279 _AppImpl* pAppImpl = _AppImpl::GetInstance();
1280 if (pAppImpl != null)
1282 pAppImpl->__forcedTermination = true;
1289 _AppImpl::IsForcedTermination(void)
1291 return __forcedTermination;