Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / app / FApp_DataControlManager.cpp
index 3a8a6fa..25d3bd9 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
  */
 
 #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 <FBaseRtMutexGuard.h>
+
+#include <FBase_StringConverter.h>
+#include <FAppPkg_PackageManagerImpl.h>
 #include "FApp_DataControlManager.h"
 
+using namespace std;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
+using namespace Tizen::Base::Runtime;
+using namespace Tizen::App::Package;
 
 namespace Tizen { namespace App
 {
 
+_DataControlManager* _DataControlManager::__pDataControlManagerInstance = null;
+
 _DataControlManager::_DataControlManager(void)
+       : __pDataControlRequestList(null)
+       , __pProviderList(null)
+       , __uniqueId(-1)
 {
-       __pDataControlRequestList.Construct();
+}
+
+result
+_DataControlManager::Construct(void)
+{
+       unique_ptr< HashMap > pDataControlRequestList(new (std::nothrow) HashMap(SingleObjectDeleter));
+       SysTryReturnResult(NID_APP, pDataControlRequestList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
+
+       result r = pDataControlRequestList->Construct();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "Propagating to caller...");
+
+       unique_ptr< LinkedList > pProviderList(new (std::nothrow) LinkedList(SingleObjectDeleter));
+       SysTryReturnResult(NID_APP, pProviderList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
+
+       r = __mutex.Create();
+       SysTryReturnResult(NID_APP, !IsFailed(r), r, "Mutex initialization failed.");
+
+       __pDataControlRequestList = pDataControlRequestList.release();
+       __pProviderList = pProviderList.release();
+
+       return E_SUCCESS;
 }
 
 _DataControlManager::~_DataControlManager(void)
 {
+       delete __pDataControlRequestList;
+       delete __pProviderList;
+}
+
+void
+_DataControlManager::InitSingleton(void)
+{
+       _DataControlManager* pInst = new (std::nothrow) _DataControlManager();
+       SysTryReturnVoidResult(NID_APP, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+
+       result r = pInst->Construct();
+       SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
+
+       __pDataControlManagerInstance = pInst;
+
+       std::atexit(DestroySingleton);
+       return;
+
+CATCH:
+       delete pInst;
+}
+
+void
+_DataControlManager::DestroySingleton(void)
+{
+       delete __pDataControlManagerInstance;
 }
 
 _DataControlManager*
 _DataControlManager::GetInstance(void)
 {
-       static _DataControlManager inst;
-       return &inst;
+       static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
+       
+       if (__pDataControlManagerInstance == null)
+       {
+               ClearLastResult();
+               pthread_once(&onceBlock, InitSingleton);
+               result r = GetLastResult();
+               if (IsFailed(r))
+               {
+                       onceBlock = PTHREAD_ONCE_INIT;
+                       SysPropagate(NID_APP, r);
+               }
+       }
+
+       return __pDataControlManagerInstance;
+}
+
+int
+_DataControlManager::GetRequestCount(void) const
+{
+       MutexGuard lock(__mutex);
+
+       return __pDataControlRequestList->GetCount();
+}
+
+result
+_DataControlManager::AddRequestInfo(Integer* pReqId, _DataControlRequestInfo* pReqInfo)
+{
+       //SysLog(NID_APP, "DataControl request list count: %d", __pDataControlRequestList->GetCount());
+       MutexGuard lock(__mutex);
+
+       return __pDataControlRequestList->Add(pReqId, pReqInfo);
+}
+
+_DataControlRequestInfo*
+_DataControlManager::GetRequestInfo(Integer& reqId) 
+{
+       result r = E_SUCCESS;
+
+       MutexGuard lock(__mutex);
+
+       Object* pObj = __pDataControlRequestList->GetValue(reqId);
+       SysTryReturn(NID_APP, pObj != null, null, GetLastResult(), "[%s] Propagating to caller...", GetErrorMessage(GetLastResult()));
+
+       _DataControlRequestInfo* pReqInfo = dynamic_cast< _DataControlRequestInfo* >(pObj);
+       SysTryReturn(NID_APP, pReqInfo != null, null, r, "[E_SYSTEM] invalid request info");
+
+       return pReqInfo;
+}
+
+void
+_DataControlManager::RemoveRequestInfo(Integer& reqId)
+{
+       MutexGuard lock(__mutex);
+
+       __pDataControlRequestList->Remove(reqId);
 }
 
 void
-_DataControlManager::AddEvent(int reqId, Object* pObj)
+_DataControlManager::RemoveAllRequests(void)
 {
-       __pDataControlRequestList.Add(new (std::nothrow) Integer(reqId), pObj);
+       MutexGuard lock(__mutex);
+
+       __pDataControlRequestList->RemoveAll();
+}
+
+int
+_DataControlManager::GetUniqueId(void)
+{
+       //++__uniqueId;
+       //__sync_fetch_and_add(&__uniqueId, 1);
+       g_atomic_int_inc(&__uniqueId);
+       return __uniqueId;
+}
+
+void
+_DataControlManager::Cache(const AppId& appId)
+{
+       __pProviderList->Add(new (std::nothrow) String(appId));
+}
+
+bool
+_DataControlManager::IsCached(const AppId& 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;
 }
 
-Object*
-_DataControlManager::GetEvent(int reqId) 
+result
+_DataControlManager::AllowAccess(const AppId& appId)
 {
-       Integer key(reqId);
-       return __pDataControlRequestList.GetValue(key);
+       //if (IsCached(appId) == false)
+       //{
+               const PackageId& pkgId = _PackageManagerImpl::GetPackageIdByAppId(appId);
+               unique_ptr< char[] > pPkgId(_StringConverter::CopyToCharArrayN(pkgId));
+               SysTryReturnResult(NID_APP, pPkgId != null, E_SYSTEM, "The method cannot proceed due to a severe system error.");
+
+               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