Use security_server_app_give_access() API for updating smack rule
[platform/framework/native/appfw.git] / src / app / FApp_DataControlManager.cpp
index efb211c..aef876c 100644 (file)
  */
 
 #include <new>
+#include <unique_ptr.h>
 #include <cstdlib>
 #include <pthread.h>
 #include <typeinfo>
 #include <glib.h>
+#include <security-server.h>
 
 #include <FBaseInteger.h>
 #include <FBaseSysLog.h>
+#include <FBaseColIEnumerator.h>
 
+#include <FBase_StringConverter.h>
 #include "FApp_DataControlManager.h"
 
+using namespace std;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
 
@@ -40,17 +45,21 @@ _DataControlManager* _DataControlManager::__pDataControlManagerInstance = null;
 
 _DataControlManager::_DataControlManager(void)
        : __pDataControlRequestList(null)
+       , __pProviderList(null)
        , __uniqueId(-1)
 {
        __pDataControlRequestList = new (std::nothrow) HashMap(SingleObjectDeleter);
        SysTryReturnVoidResult(NID_APP, __pDataControlRequestList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
-
        __pDataControlRequestList->Construct();
+
+       __pProviderList = new (std::nothrow) LinkedList(SingleObjectDeleter);
+       SysTryReturnVoidResult(NID_APP, __pProviderList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 }
 
 _DataControlManager::~_DataControlManager(void)
 {
        delete __pDataControlRequestList;
+       delete __pProviderList;
 }
 
 void
@@ -139,5 +148,46 @@ _DataControlManager::GetUniqueId(void)
        return __uniqueId;
 }
 
+void
+_DataControlManager::Cache(String& appId)
+{
+       __pProviderList->Add(new (std::nothrow) String(appId));
+}
+
+bool
+_DataControlManager::IsCached(String& appId)
+{
+       unique_ptr< IEnumerator > pEnum(__pProviderList->GetEnumeratorN());
+       while (pEnum->MoveNext() == E_SUCCESS)
+       {
+               String* pCachedAppId = dynamic_cast< String* >(pEnum->GetCurrent());
+               if (pCachedAppId != null && pCachedAppId->Equals(appId) == true)
+               {
+                       return true;
+               }
+       }
+       return false;
+}
+
+result
+_DataControlManager::AllowAccess(String& appId)
+{
+       if (IsCached(appId) == false)
+       {
+               unique_ptr< char[] > pPkgId(_StringConverter::CopyToCharArrayN(appId));
+               SysTryReturnResult(NID_APP, pPkgId != null, E_SYSTEM, "The method cannot proceed due to a severe system error.");
+               pPkgId.get()[10] = '\0';
+
+               int ret = security_server_app_give_access(pPkgId.get(), -1);
+               SysTryReturnResult(NID_APP, ret == 0, E_SYSTEM,
+                               "Failed to call security_server_app_give_access(), provider: %s, ret: %d", pPkgId.get(), ret);
+
+               Cache(appId);
+       }
+
+       SysLog(NID_APP, "[DC_CALLER_SEND] Allow %ls to access", appId.GetPointer());
+       return E_SUCCESS;
+}
+
 }} // Tizen::App