Add defensive codes for broken AppRegistry file
[platform/framework/native/appfw.git] / src / app / FApp_AppControlRegistry.cpp
index 866f405..2775ba4 100644 (file)
@@ -21,8 +21,9 @@
 
 #include <new>
 #include <unique_ptr.h>
-#include <app.h>
 #include <bundle.h>
+#include <appsvc.h>
+#include <new>
 
 #include <FBaseErrors.h>
 #include <FBaseSysLog.h>
 #include <FBase_StringConverter.h>
 #include <FIo_RegistryImpl.h>
 
-#include "FAppPkg_PackageManagerImpl.h"
 #include "FApp_AppControlImpl.h"
-#include "FApp_Aul.h"
 #include "FApp_AppControlRegistry.h"
 #include "FApp_AppMessageImpl.h"
 #include "FApp_TemplateUtil.h"
 
-using namespace Tizen::App::Package;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
 using namespace Tizen::Io;
@@ -52,14 +50,11 @@ namespace
 
 const wchar_t ACTL_DEFAULT_FILE[] = L"/usr/etc/app-control-info.ini";
 const wchar_t ACTL_LEGACY_FILE[] = L"/usr/etc/app-control-alias.ini";
-const wchar_t ACTL_ALIAS_FILE[] = L"/usr/etc/app-control-appid.ini";
-
-const wchar_t TIZEN_ALIAS_APPID_PREFIX[] = L"tizen.";
+const wchar_t ACTL_DEFAULT_PLUGIN[] = L"libosp-ac-platform.so";
 
 const String ACTL_REGISTRY_PATH = L"Path";
 const String ACTL_REGISTRY_ALIAS_PROVIDER = L"PROVIDER_ALIAS";
 
-const int PKG_APPID_LEN = 256;
 const int REG_VALUE_BUFFER_LEN = 256;
 
 }
@@ -75,17 +70,11 @@ _AppControlRegistry::_AppControlRegistry(void)
        __tizenList.Construct();
 
        __aliasList.Construct();
-
-       __aliasAppId.Construct();
-
-       __runtimeAlias.Construct();
 }
 
 _AppControlRegistry::~_AppControlRegistry(void)
 {
        _DeleteCollectionMapValue<String, _AppControlAliasEntry>(__aliasList);
-
-       _DeleteCollection<_AppControlAliasEntry>(__runtimeAlias);
 }
 
 _AppControlRegistry*
@@ -101,8 +90,6 @@ _AppControlRegistry::GetInstance(void)
                __pSelf->LoadTizenAppControlRegistry();
 
                __pSelf->LoadLegacyList();
-
-               __pSelf->LoadAliasList();
        }
 
        return __pSelf;
@@ -117,7 +104,11 @@ _AppControlRegistry::LoadTizenAppControlRegistry(void)
        const String regPath = ACTL_DEFAULT_FILE;
 
        result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
+       if (IsFailed(r))
+       {
+               SysPropagate(NID_APP, r);
+               return r;
+       }
 
        const int secCount = reg.GetAllSectionCount();
        SysTryReturnResult(NID_APP, !(secCount <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
@@ -166,7 +157,11 @@ _AppControlRegistry::LoadLegacyList(void)
        const String& regPath(ACTL_LEGACY_FILE);
 
        result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
+       if (IsFailed(r))
+       {
+               SysPropagate(NID_APP, r);
+               return r;
+       }
 
        const int sec_count = reg.GetAllSectionCount();
        SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
@@ -215,7 +210,7 @@ _AppControlRegistry::LoadLegacyList(void)
                        r = reg.GetValue(secName, *pStr, value);
                        if (IsFailed(r))
                        {
-                               SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
+                               SysPropagate(NID_APP, r);
                                continue;
                        }
 
@@ -239,49 +234,6 @@ _AppControlRegistry::LoadLegacyList(void)
        return E_SUCCESS;
 }
 
-result
-_AppControlRegistry::LoadAliasList(void)
-{
-       _RegistryImpl reg;
-
-       const String regPath = ACTL_ALIAS_FILE;
-
-       result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
-
-       HashMap* pMap = null;
-       r = reg.GetEntryListN(L"Alias", &pMap);
-       if (r != E_SUCCESS)
-       {
-               SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
-               pMap->RemoveAll(true);
-               delete pMap;
-               return r;
-       }
-
-       String* pKey = null;
-       String* pVal = null;
-       std::unique_ptr<IMapEnumerator> pEnum(pMap->GetMapEnumeratorN());
-       SysTryCatch(NID_APP, pEnum.get(), r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory error.");
-
-       while(pEnum->MoveNext() == E_SUCCESS)
-       {
-               pKey = static_cast<String*>(pEnum->GetKey());
-               pVal = static_cast<String*>(pEnum->GetValue());
-
-               __aliasAppId.Add(*pKey, *pVal);
-               //SysLog(NID_APP, "(%ls, %ls)", pKey->GetPointer(), pVal->GetPointer());
-       }
-
-       SysLog(NID_APP, "Loading %d alias sections", __aliasAppId.GetCount());
-
-CATCH:
-       pMap->RemoveAll(true);
-       delete pMap;
-
-       return r;
-}
-
 
 AppControl*
 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) const
@@ -289,6 +241,8 @@ _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) c
        int count = 0;
        const String* pAppId = &aId;
        const String* pOperation = &oId;
+
+       // legacy check first
        do
        {
                const _AppControlAliasEntry* const pEntry = GetAppControlAliasEntry(*pAppId, *pOperation);
@@ -312,35 +266,7 @@ _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) c
 
        const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
 
-       if (soName.IsEmpty())
-       {
-               AppControl* pAc = GetAppControlN(*pAppId, *pOperation);
-               if (pAc)
-               {
-                       _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
-                       pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
-
-                       const _AppControlAliasEntry* pLookupEntry = GetReverseRuntimeAliasEntry(*pAppId, *pOperation);
-                       if (pLookupEntry == null)
-                       {
-                               // no entry found : add new one
-                               _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(aId, oId, *pAppId, *pOperation);
-                               if (pEntry)
-                               {
-                                       // may fail
-                                       __runtimeAlias.Add(pEntry);
-                                       SysLog(NID_APP, "Setting alias app (%ls -> %ls).", aId.GetPointer(), pAppId->GetPointer());
-                               }
-                       }
-               }
-               else
-               {
-                       SysLog(NID_APP, "No AppControl instance for (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
-               }
-               return pAc;
-       }
-
-       return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, L"", _APPCONTROL_PROPERTY_PUBLIC | _APPCONTROL_PROPERTY_SLP);
+       return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, _APPCONTROL_PROPERTY_PUBLIC);
 }
 
 
@@ -355,8 +281,8 @@ _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const Strin
                return val;
        }
 
-       SysLog(NID_APP, "No platform AppControl and use custom AppControl instead.");
-       return L"";
+       SysLog(NID_APP, "Default platform AppControl %ls is used.", ACTL_DEFAULT_PLUGIN);
+       return String(ACTL_DEFAULT_PLUGIN);
 }
 
 
@@ -366,8 +292,9 @@ _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const Strin
 struct AppSvcIterData
 {
 public:
-       AppSvcIterData(ArrayList* pArr, const String& op) : pArray(pArr), operation(op) {}
+       AppSvcIterData(const _AppControlRegistry* pRegs, ArrayList* pArr, const String& op) : pThis(pRegs), pArray(pArr), operation(op) {}
 
+       const _AppControlRegistry* pThis;
        ArrayList* pArray;
        const String& operation;
 };
@@ -383,6 +310,8 @@ AppSvcIterFnCb(const char* pAppId, void* pData)
        AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
        ArrayList* pList = pAppSvcIterData->pArray;
        SysAssert(pList != null);
+       const _AppControlRegistry* pThis = pAppSvcIterData->pThis;
+       SysAssert(pThis != null);
        const String& operation = pAppSvcIterData->operation;
 
        if (pAppId == NULL)
@@ -392,7 +321,7 @@ AppSvcIterFnCb(const char* pAppId, void* pData)
        }
 
        String appId = pAppId;
-       AppControl* pAc = _AppControlImpl::CreateN(appId, operation, false);
+       AppControl* pAc = pThis->GetTizenAppControlN(appId, operation);
        if (pAc == null)
        {
                SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
@@ -433,7 +362,7 @@ _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri,
        SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
        pList->Construct();
 
-       AppSvcIterData data(pList, operation);
+       AppSvcIterData data(this, pList, operation);
 
        appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
 
@@ -474,108 +403,26 @@ _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oI
 }
 
 
-const _AppControlRegistry::_AppControlAliasEntry*
-_AppControlRegistry::GetReverseAppControlAliasEntry(const String& aId, const String& oId) const
-{
-       std::unique_ptr< IMapEnumeratorT<String, _AppControlAliasEntry*> > pEnum(__aliasList.GetMapEnumeratorN());
-
-       while (pEnum->MoveNext() == E_SUCCESS)
-       {
-               _AppControlAliasEntry* pEntry = null;
-               pEnum->GetValue(pEntry);
-               if (pEntry->provider2 == aId && pEntry->operation2 == oId)
-               {
-                       SysLog(NID_APP, "Found matching AppControl (%ls, %ls)<-(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider.GetPointer(), pEntry->operation.GetPointer());
-
-                       return pEntry;
-               }
-       }
-
-       return null;
-}
-
-
-const _AppControlRegistry::_AppControlAliasEntry*
-_AppControlRegistry::GetReverseRuntimeAliasEntry(const String& aId, const String& oId) const
-{
-       std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__runtimeAlias.GetEnumeratorN());
-
-       while (pEnum->MoveNext() == E_SUCCESS)
-       {
-               _AppControlAliasEntry* pEntry = null;
-               pEnum->GetCurrent(pEntry);
-               if (pEntry->provider2 == aId && pEntry->operation2 == oId)
-               {
-                       SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
-
-                       return pEntry;
-               }
-       }
-
-       return null;
-}
-
-
-AppControl*
-_AppControlRegistry::GetAppControlN(const String& appId, const String& operationId) const
-{
-       bool changeAppId = false;
-
-       String actualAppId = appId;
-       if (appId.StartsWith(TIZEN_ALIAS_APPID_PREFIX, 0))
-       {
-               const String& tmp = GetAliasAppId(appId);
-
-               if (!tmp.IsEmpty())
-               {
-                       actualAppId = tmp;
-                       SysLog(NID_APP, "Found alias app (%ls -> %ls).", appId.GetPointer(), tmp.GetPointer());
-
-                       changeAppId = true;
-               }
-       }
-
-       bool b = _Aul::IsInstalled(actualAppId);
-       SysTryReturn(NID_APP, b == true, null, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] %ls not installed.", actualAppId.GetPointer());
-
-       return _AppControlImpl::CreateN(actualAppId, operationId, changeAppId);
-}
-
-
 AppId
 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
 {
-       String tmp;
-       result r = __aliasAppId.GetValue(appId, tmp);
-       if (r != E_SUCCESS)
-       {
-               tmp.Clear();
-       }
-
-       return tmp;
-}
+       std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
 
+       if (pBundle.get())
+       {
+               std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
 
-AppId
-_AppControlRegistry::GetReverseAliasAppId(const AppId& appId) const
-{
-       std::unique_ptr< IMapEnumeratorT<String, String> > pEnum(__aliasAppId.GetMapEnumeratorN());
+               // appsvc_set_appid() lookup for actual app ID internally
+               appsvc_set_appid(pBundle.get(), pAppId.get());
+               const char* pTmp = appsvc_get_appid(pBundle.get());
 
-       String key;
-       String value;
-       while (pEnum->MoveNext() == E_SUCCESS)
-       {
-               pEnum->GetKey(key);
-               pEnum->GetValue(value);
-               if (value == appId)
+               if (pTmp)
                {
-                       return key;
+                       return String(pTmp);
                }
        }
 
-       SysLog(NID_APP, "No entry found for %ls", appId.GetPointer());
-       return L"";
+       return String();
 }
 
-
 } } // Tizen::App