Add datacontrol.consumer privilege to DataControl
[platform/framework/native/appfw.git] / src / app / FApp_AppImpl.cpp
index 60d73ac..e41f92a 100644 (file)
@@ -24,6 +24,7 @@
 #include <notification/notification.h>
 #include <appsvc/appsvc.h>
 #include <vconf.h>
+#include <appinfo.h>
 
 #include <FBaseInteger.h>
 #include <FBaseColArrayList.h>
@@ -38,8 +39,9 @@
 #include <FAppIAppControlProviderEventListener.h>
 #include <FAppIAppLaunchConditionEventListener.h>
 #include <FIoFile.h>
-
 #include <FBaseSysLog.h>
+
+#include <FBase_StringConverter.h>
 #include <FIo_RegistryImpl.h>
 #include <FSys_EnvironmentImpl.h>
 #include <FSys_PowerManagerImpl.h>
@@ -54,6 +56,7 @@
 #include "FApp_AppArg.h"
 #include "FApp_IAppImpl.h"
 #include "FApp_AppResourceImpl.h"
+#include <FSys_SettingInfoImpl.h>
 
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
@@ -79,6 +82,9 @@ namespace Tizen { namespace App
 
 _AppImpl* _AppImpl::__pAppImpl = null;
 bool _AppImpl::__isTerminationRequested = false;
+
+static const char TIZEN_APPCONTROL_DATA_MULTI_WINDOW[] = "http://tizen.org/appcontrol/data/multiwindow";
+
 #if 0
 static const int _DATACONTROL_PACKET_INDEX_APPID = 0;
 static const int _DATACONTROL_PACKET_INDEX_REQUESTTYPE = 1;
@@ -135,18 +141,20 @@ _AppImpl::Construct(const IList* pArgs)
 
        _AppInfo::SetAppState(INITIALIZING);
 
+       r = _SettingInfoImpl::AddSettingEventListenerForInternal(*this);
+       SysTryLog(NID_APP, !IsFailed(r), "[%s] failed to add setting event listener.", GetErrorMessage(r));
+
        return E_SUCCESS;
 }
 
-
 result
 _AppImpl::Execute(_IAppImpl* pIAppImpl)
 {
        SysLogTag(NID_APP, "LAUNCH","[%ls:<Initialize Application>:start]", _AppInfo::GetAppExecutableName().GetPointer());
        result r = E_SUCCESS;
        int eflResult = APP_ERROR_NONE;
-       int argc = _AppInfo::GetArgc();
-       char** pArgv = _AppInfo::GetArgv();
+       int argc = 0;
+       char** pArgv = NULL;
        _IAppManager* pAppManager = null;
 
        _AppImpl* pAppImpl = _AppImpl::GetInstance();
@@ -171,7 +179,7 @@ _AppImpl::Execute(_IAppImpl* pIAppImpl)
 
        state_handler.low_memory = &OnLowMemory;
        state_handler.low_battery = NULL;
-       state_handler.language_changed = &OnLanguageChanged;
+       state_handler.language_changed = NULL;
        state_handler.region_format_changed = NULL;
 
        _Aul::SetPowerOffNotiListener(OnPowerOffNotiReceived, this);
@@ -181,14 +189,10 @@ _AppImpl::Execute(_IAppImpl* pIAppImpl)
 //             SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Application registration failed.", GetErrorMessage(r));
 //     }
 
+       appinfo_get_argv(&argc, &pArgv);
        eflResult = app_efl_main(&argc, &pArgv, &state_handler, this);
-
        SysTryLog(NID_APP, eflResult == APP_ERROR_NONE, "app_efl_main failed with error (%d): Unknown", eflResult);
 
-       // clear outstanding ongoing notification
-       notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
-
-//CATCH:
        pAppManager = _AppManagerProxy::GetService();
 
        if (pAppManager != null)
@@ -196,6 +200,9 @@ _AppImpl::Execute(_IAppImpl* pIAppImpl)
                pAppManager->UnregisterApplication(_AppInfo::GetProcessId());
        }
 
+       // clear outstanding ongoing notification
+       notification_delete_all_by_type(appinfo_get_appid(), NOTIFICATION_TYPE_ONGOING);
+
        return r;
 }
 
@@ -236,7 +243,7 @@ _AppImpl::OnCreate(void* user_data)
 
        if (pAppManager != null)
        {
-               result r = pAppManager->RegisterApplication(_AppInfo::GetPackageId(), _AppInfo::GetAppExecutableName(), static_cast<_AppType>(_AppInfo::GetAppType()), _AppInfo::GetProcessId());
+               result r = pAppManager->RegisterApplication(_AppInfo::GetApplicationId(), static_cast<_AppType>(_AppInfo::GetAppType()), _AppInfo::GetProcessId());
                SysTryLog(NID_APP, !IsFailed(r), "[%s] Application registration failed.", GetErrorMessage(r));
        }
        SysLogTag(NID_APP, "LAUNCH","[%ls:<RegisterApplication>:done]", _AppInfo::GetAppExecutableName().GetPointer());
@@ -300,9 +307,22 @@ _AppImpl::OnService(service_s* service, void* user_data)
                service_get_window(service, &winId);
                if (winId > 0)
                {
-                       _AppInfo::GetAppInfo()->SetParentWindowHandle(winId);
+                       _AppInfo::SetParentWindowHandle(winId);
                        SysLog(NID_APP, "Saving window handle 0x%x.", winId);
                }
+
+               // set an appinfo value for multi-window mode
+               char* pBuf = null;
+               int errVal = service_get_extra_data(service, TIZEN_APPCONTROL_DATA_MULTI_WINDOW, &pBuf);
+               if (errVal == SERVICE_ERROR_NONE)
+               {
+                       if (!strcmp(pBuf, "on"))
+                       {
+                               _AppInfo::SetMultiWindow(true);
+                       }
+                       free(pBuf);
+               }
+
                // call for callbacks
                // ptr to member function
                SysLogTag(NID_APP, "LAUNCH","[%ls:<RequestHandler and OnAppInitialized>:start]", _AppInfo::GetAppExecutableName().GetPointer());
@@ -368,6 +388,9 @@ _AppImpl::OnTerminate(void* user_data)
        SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
        SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
 
+       result r = _SettingInfoImpl::RemoveSettingEventListenerForInternal(*pAppImpl);
+       SysTryLog(NID_APP, !IsFailed(r), "[%s] failed to remove setting event listener.", GetErrorMessage(r));
+
        __isTerminationRequested = true;
        pAppImpl->__pIAppImpl->OnTerminate();
 }
@@ -439,7 +462,7 @@ _AppImpl::GetAppArgumentListN(void) const
                SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
                String* pData = new (std::nothrow) String(LEGACY_LAUNCH_REASON_NORMAL);
                SysTryReturn(NID_APP, pData != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
-               String* pData2 = new (std::nothrow) String(L"osp.operation.MAIN");
+               String* pData2 = new (std::nothrow) String(LEGACY_OPERATION_MAIN);
                SysTryReturn(NID_APP, pData2 != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
 
                pList->Construct();
@@ -456,6 +479,8 @@ _AppImpl::Terminate(void)
 {
        result r = E_SUCCESS;
 
+       SysLog(NID_APP, "Terminate() is called by application itself");
+
        __isTerminationRequested = true;
 
        app_efl_exit();
@@ -486,7 +511,11 @@ _AppImpl::RegisterAppRequest(service_s* service, int& req, _AppHandler& handler)
        SysTryReturnResult(NID_APP, pManager != null, E_SYSTEM, "Wrong system state.");
 
        result r = pManager->RegisterRequest(service, req, handler);
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
+       if (IsFailed(r))
+       {
+               SysPropagate(NID_APP, r);
+               return r;
+       }
 
        int providerType = _AppInfo::GetAppHandlerType();
        providerType |= (_APP_HANDLER_LAUNCH_NORMAL | _APP_HANDLER_LAUNCH_COND);
@@ -611,17 +640,6 @@ _AppImpl::OnBatteryLevelChanged(BatteryLevel batteryLevel)
        __pApp->OnBatteryLevelChanged(batteryLevel);
 }
 
-void
-_AppImpl::OnLanguageChanged(void* user_data)
-{
-       SysLog(NID_APP, "");
-       _AppInfo::UpdatePackageInfo(true);
-
-       _AppResourceImpl::Reinitialize();
-       SysLog(NID_APP, "Language change done.");
-}
-
-
 result
 _AppImpl::SendUserEvent(RequestId requestId, const IList* pArgs, bool isPublic)
 {
@@ -685,7 +703,7 @@ _AppImpl::OnAppControlRequestReceived(const _AppArg& arg, RequestId reqId)
                return;
        }
 
-       const String& appId = _AppInfo::GetApplicationId();
+       const char* pAppId = appinfo_get_appid();
 
        const char* p = appsvc_get_operation(arg.GetBundle());
        String operationId = (p) ? String(p) : TIZEN_OPERATION_MAIN;
@@ -694,7 +712,7 @@ _AppImpl::OnAppControlRequestReceived(const _AppArg& arg, RequestId reqId)
                operationId = TIZEN_OPERATION_MAIN;
        }
 
-       SysLog(NID_APP, "AppControl (%ls, %ls).", appId.GetPointer(), operationId.GetPointer());
+       SysLog(NID_APP, "AppControl (%s, %ls).", pAppId, operationId.GetPointer());
 
        String uri;
        String mime;
@@ -725,7 +743,7 @@ _AppImpl::OnAppControlRequestReceived(const _AppArg& arg, RequestId reqId)
 
        pAppControlProviderEventListener->OnAppControlRequestReceived(reqId, operationId, pUri, pMime, pMap.get());
 
-       SysLog(NID_APP, "AppControl (%ls, %ls) invocation finished.", appId.GetPointer(), operationId.GetPointer());
+       SysLog(NID_APP, "AppControl (%s, %ls) invocation finished.", pAppId, operationId.GetPointer());
 }
 
 
@@ -1188,18 +1206,6 @@ _AppImpl::OnDataControlRequestReceived(const _AppArg& arg, RequestId reqId)
        return;
 
 CATCH:
-       if (r == E_SYSTEM)
-       {
-               pErrorMsg = new (std::nothrow) String("[E_SYSTEM] A system error has occurred.");
-       }
-       else if (r == E_OUT_OF_MEMORY)
-       {
-               pErrorMsg = new (std::nothrow) String("[E_OUT_OF_MEMORY] The memory was insufficient.");
-       }
-       SysTryReturnVoidResult(NID_APP, pErrorMsg, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
-
-       res = DataControlProviderManager::GetInstance()->SendDataControlError(reqId, *pErrorMsg);
-
        if (pColumnList)
        {
                pColumnList->RemoveAll(true);
@@ -1218,6 +1224,18 @@ CATCH:
                delete pUpdateMap;
        }
 
+       if (r == E_SYSTEM)
+       {
+               pErrorMsg = new (std::nothrow) String("[E_SYSTEM] A system error has occurred.");
+       }
+       else if (r == E_OUT_OF_MEMORY)
+       {
+               pErrorMsg = new (std::nothrow) String("[E_OUT_OF_MEMORY] The memory was insufficient.");
+       }
+       SysTryReturnVoidResult(NID_APP, pErrorMsg, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
+
+       res = DataControlProviderManager::GetInstance()->SendDataControlError(reqId, *pErrorMsg);
+
        delete pErrorMsg;
 
        return;
@@ -1386,21 +1404,46 @@ _AppImpl::IsTerminationRequested(void)
        return __isTerminationRequested;
 }
 
+typedef void (*cbForVconf)(keynode_t* node, void *pData);
+
 void
-_AppImpl::OnPowerOffNotiReceived(void* user_data)
+_AppImpl::OnPowerOffNotiReceived(void* node, void* user_data)
 {
-       SysLog(NID_APP, "Application is being terminated by power off.");
+       int value = -1;
+       if (!vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &value))
+       {
+               if (value == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || value == VCONFKEY_SYSMAN_POWER_OFF_RESTART)
+               {
+                       SysLog(NID_APP, "Application is being terminated by power off.");
 
-       _AppImpl* pAppImpl = _AppImpl::GetInstance();
-       if (pAppImpl != null)
+                       if (vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, (cbForVconf)_AppImpl::OnPowerOffNotiReceived))
+                       {
+                               SysLog(NID_APP, "Fail to ignore vconf for key(%s)", VCONFKEY_SYSMAN_POWER_OFF_STATUS);
+                       }
+
+                       _AppImpl* pAppImpl = _AppImpl::GetInstance();
+                       if (pAppImpl != null)
+                       {
+                               pAppImpl->__forcedTermination = true;
+                       }
+
+                       app_efl_exit();
+               }
+       }
+}
+
+void
+_AppImpl::OnSettingChanged(String& key)
+{
+       if (key == L"http://tizen.org/setting/locale.language")
        {
-               pAppImpl->__forcedTermination = true;
+               _AppInfo::UpdatePackageInfo(true);
+               _AppResourceImpl::Reinitialize();
+               SysLog(NID_APP, "Reinitialized resources due to locale change.");
        }
-       
-       app_efl_exit();
 }
 
-bool 
+bool
 _AppImpl::IsForcedTermination(void)
 {
        return __forcedTermination;