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>
34 #include <FBase_StringConverter.h>
35 #include "FApp_DataControlManager.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
41 namespace Tizen { namespace App
44 _DataControlManager* _DataControlManager::__pDataControlManagerInstance = null;
46 _DataControlManager::_DataControlManager(void)
47 : __pDataControlRequestList(null)
48 , __pProviderList(null)
51 __pDataControlRequestList = new (std::nothrow) HashMap(SingleObjectDeleter);
52 SysTryReturnVoidResult(NID_APP, __pDataControlRequestList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
53 __pDataControlRequestList->Construct();
55 __pProviderList = new (std::nothrow) LinkedList(SingleObjectDeleter);
56 SysTryReturnVoidResult(NID_APP, __pProviderList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
59 _DataControlManager::~_DataControlManager(void)
61 delete __pDataControlRequestList;
62 delete __pProviderList;
66 _DataControlManager::InitSingleton(void)
68 _DataControlManager* pInst = new (std::nothrow) _DataControlManager();
69 SysTryReturnVoidResult(NID_APP, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
71 __pDataControlManagerInstance = pInst;
73 std::atexit(DestroySingleton);
78 _DataControlManager::DestroySingleton(void)
80 delete __pDataControlManagerInstance;
84 _DataControlManager::GetInstance(void)
86 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
88 if (__pDataControlManagerInstance == null)
91 pthread_once(&onceBlock, InitSingleton);
92 result r = GetLastResult();
95 onceBlock = PTHREAD_ONCE_INIT;
96 SysPropagate(NID_APP, r);
100 return __pDataControlManagerInstance;
104 _DataControlManager::GetRequestCount(void) const
106 return __pDataControlRequestList->GetCount();
110 _DataControlManager::AddRequestInfo(Integer* pReqId, _DataControlRequestInfo* pReqInfo)
112 //SysLog(NID_APP, "DataControl request list count: %d", __pDataControlRequestList->GetCount());
113 return __pDataControlRequestList->Add(pReqId, pReqInfo);
116 _DataControlRequestInfo*
117 _DataControlManager::GetRequestInfo(Integer& reqId)
119 result r = E_SUCCESS;
121 Object* pObj = __pDataControlRequestList->GetValue(reqId);
122 SysTryReturn(NID_APP, pObj != null, null, GetLastResult(), "[%s] Propagating to caller...", GetErrorMessage(GetLastResult()));
124 _DataControlRequestInfo* pReqInfo = dynamic_cast< _DataControlRequestInfo* >(pObj);
125 SysTryReturn(NID_APP, pReqInfo != null, null, r, "[E_SYSTEM] invalid request info");
131 _DataControlManager::RemoveRequestInfo(Integer& reqId)
133 __pDataControlRequestList->Remove(reqId);
137 _DataControlManager::RemoveAllRequests(void)
139 __pDataControlRequestList->RemoveAll();
143 _DataControlManager::GetUniqueId(void)
146 //__sync_fetch_and_add(&__uniqueId, 1);
147 g_atomic_int_inc(&__uniqueId);
152 _DataControlManager::Cache(String& appId)
154 __pProviderList->Add(new (std::nothrow) String(appId));
158 _DataControlManager::IsCached(String& appId)
160 unique_ptr< IEnumerator > pEnum(__pProviderList->GetEnumeratorN());
161 while (pEnum->MoveNext() == E_SUCCESS)
163 String* pCachedAppId = dynamic_cast< String* >(pEnum->GetCurrent());
164 if (pCachedAppId != null && pCachedAppId->Equals(appId) == true)
173 _DataControlManager::AllowAccess(String& appId)
175 if (IsCached(appId) == false)
177 unique_ptr< char[] > pPkgId(_StringConverter::CopyToCharArrayN(appId));
178 SysTryReturnResult(NID_APP, pPkgId != null, E_SYSTEM, "The method cannot proceed due to a severe system error.");
179 pPkgId.get()[10] = '\0';
181 int ret = security_server_app_give_access(pPkgId.get(), -1);
182 SysTryReturnResult(NID_APP, ret == 0, E_SYSTEM,
183 "Failed to call security_server_app_give_access(), provider: %s, ret: %d", pPkgId.get(), ret);
188 SysLog(NID_APP, "[DC_CALLER_SEND] Allow %ls to access", appId.GetPointer());