2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FApp_DataControlManager.cpp
19 * @brief This is the implementation for the _DataControlManager class.
23 #include <unique_ptr.h>
28 #include <security-server.h>
30 #include <FBaseInteger.h>
31 #include <FBaseSysLog.h>
32 #include <FBaseColIEnumerator.h>
33 #include <FBaseRtMutexGuard.h>
35 #include <FBase_StringConverter.h>
36 #include <FAppPkg_PackageManagerImpl.h>
37 #include "FApp_DataControlManager.h"
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Runtime;
43 using namespace Tizen::App::Package;
45 namespace Tizen { namespace App
48 _DataControlManager* _DataControlManager::__pDataControlManagerInstance = null;
50 _DataControlManager::_DataControlManager(void)
51 : __pDataControlRequestList(null)
52 , __pProviderList(null)
58 _DataControlManager::Construct(void)
60 unique_ptr< HashMap > pDataControlRequestList(new (std::nothrow) HashMap(SingleObjectDeleter));
61 SysTryReturnResult(NID_APP, pDataControlRequestList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
63 result r = pDataControlRequestList->Construct();
64 SysTryReturnResult(NID_APP, !IsFailed(r), r, "Propagating to caller...");
66 unique_ptr< LinkedList > pProviderList(new (std::nothrow) LinkedList(SingleObjectDeleter));
67 SysTryReturnResult(NID_APP, pProviderList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
70 SysTryReturnResult(NID_APP, !IsFailed(r), r, "Mutex initialization failed.");
72 __pDataControlRequestList = pDataControlRequestList.release();
73 __pProviderList = pProviderList.release();
78 _DataControlManager::~_DataControlManager(void)
80 delete __pDataControlRequestList;
81 delete __pProviderList;
85 _DataControlManager::InitSingleton(void)
87 _DataControlManager* pInst = new (std::nothrow) _DataControlManager();
88 SysTryReturnVoidResult(NID_APP, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
90 result r = pInst->Construct();
91 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
93 __pDataControlManagerInstance = pInst;
95 std::atexit(DestroySingleton);
103 _DataControlManager::DestroySingleton(void)
105 delete __pDataControlManagerInstance;
109 _DataControlManager::GetInstance(void)
111 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
113 if (__pDataControlManagerInstance == null)
116 pthread_once(&onceBlock, InitSingleton);
117 result r = GetLastResult();
120 onceBlock = PTHREAD_ONCE_INIT;
121 SysPropagate(NID_APP, r);
125 return __pDataControlManagerInstance;
129 _DataControlManager::GetRequestCount(void) const
131 MutexGuard lock(__mutex);
133 return __pDataControlRequestList->GetCount();
137 _DataControlManager::AddRequestInfo(Integer* pReqId, _DataControlRequestInfo* pReqInfo)
139 //SysLog(NID_APP, "DataControl request list count: %d", __pDataControlRequestList->GetCount());
140 MutexGuard lock(__mutex);
142 return __pDataControlRequestList->Add(pReqId, pReqInfo);
145 _DataControlRequestInfo*
146 _DataControlManager::GetRequestInfo(Integer& reqId)
148 result r = E_SUCCESS;
150 MutexGuard lock(__mutex);
152 Object* pObj = __pDataControlRequestList->GetValue(reqId);
153 SysTryReturn(NID_APP, pObj != null, null, GetLastResult(), "[%s] Propagating to caller...", GetErrorMessage(GetLastResult()));
155 _DataControlRequestInfo* pReqInfo = dynamic_cast< _DataControlRequestInfo* >(pObj);
156 SysTryReturn(NID_APP, pReqInfo != null, null, r, "[E_SYSTEM] invalid request info");
162 _DataControlManager::RemoveRequestInfo(Integer& reqId)
164 MutexGuard lock(__mutex);
166 __pDataControlRequestList->Remove(reqId);
170 _DataControlManager::RemoveAllRequests(void)
172 MutexGuard lock(__mutex);
174 __pDataControlRequestList->RemoveAll();
178 _DataControlManager::GetUniqueId(void)
181 //__sync_fetch_and_add(&__uniqueId, 1);
182 g_atomic_int_inc(&__uniqueId);
187 _DataControlManager::Cache(const AppId& appId)
189 __pProviderList->Add(new (std::nothrow) String(appId));
193 _DataControlManager::IsCached(const AppId& appId)
195 unique_ptr< IEnumerator > pEnum(__pProviderList->GetEnumeratorN());
196 while (pEnum->MoveNext() == E_SUCCESS)
198 String* pCachedAppId = dynamic_cast< String* >(pEnum->GetCurrent());
199 if (pCachedAppId != null && pCachedAppId->Equals(appId) == true)
208 _DataControlManager::AllowAccess(const AppId& appId)
210 //if (IsCached(appId) == false)
212 const PackageId& pkgId = _PackageManagerImpl::GetPackageIdByAppId(appId);
213 unique_ptr< char[] > pPkgId(_StringConverter::CopyToCharArrayN(pkgId));
214 SysTryReturnResult(NID_APP, pPkgId != null, E_SYSTEM, "The method cannot proceed due to a severe system error.");
216 int ret = security_server_app_give_access(pPkgId.get(), -1);
217 SysTryReturnResult(NID_APP, ret == 0, E_SYSTEM,
218 "Failed to call security_server_app_give_access(), provider: %s, ret: %d", pPkgId.get(), ret);
223 SysLog(NID_APP, "[DC_CALLER_SEND] Allow %ls to access", appId.GetPointer());