02a33d8132b748075642e5e0bd94eab34564139a
[platform/framework/native/appfw.git] / src / app / FApp_DataControlManager.cpp
1 //
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FApp_DataControlManager.cpp
19  * @brief       This is the implementation for the _DataControlManager class.
20  */
21
22 #include <new>
23 #include <unique_ptr.h>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include <typeinfo>
27 #include <glib.h>
28 #include <security-server.h>
29
30 #include <FBaseInteger.h>
31 #include <FBaseSysLog.h>
32 #include <FBaseColIEnumerator.h>
33
34 #include <FBase_StringConverter.h>
35 #include <FAppPkg_PackageManagerImpl.h>
36 #include "FApp_DataControlManager.h"
37
38 using namespace std;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::App::Package;
42
43 namespace Tizen { namespace App
44 {
45
46 _DataControlManager* _DataControlManager::__pDataControlManagerInstance = null;
47
48 _DataControlManager::_DataControlManager(void)
49         : __pDataControlRequestList(null)
50         , __pProviderList(null)
51         , __uniqueId(-1)
52 {
53         __pDataControlRequestList = new (std::nothrow) HashMap(SingleObjectDeleter);
54         SysTryReturnVoidResult(NID_APP, __pDataControlRequestList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
55         __pDataControlRequestList->Construct();
56
57         __pProviderList = new (std::nothrow) LinkedList(SingleObjectDeleter);
58         SysTryReturnVoidResult(NID_APP, __pProviderList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
59 }
60
61 _DataControlManager::~_DataControlManager(void)
62 {
63         delete __pDataControlRequestList;
64         delete __pProviderList;
65 }
66
67 void
68 _DataControlManager::InitSingleton(void)
69 {
70         _DataControlManager* pInst = new (std::nothrow) _DataControlManager();
71         SysTryReturnVoidResult(NID_APP, pInst != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
72
73         __pDataControlManagerInstance = pInst;
74                 
75         std::atexit(DestroySingleton);
76         return;
77 }
78
79 void
80 _DataControlManager::DestroySingleton(void)
81 {
82         delete __pDataControlManagerInstance;
83 }
84
85 _DataControlManager*
86 _DataControlManager::GetInstance(void)
87 {
88         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
89         
90         if (__pDataControlManagerInstance == null)
91         {
92                 ClearLastResult();
93                 pthread_once(&onceBlock, InitSingleton);
94                 result r = GetLastResult();
95                 if (IsFailed(r))
96                 {
97                         onceBlock = PTHREAD_ONCE_INIT;
98                         SysPropagate(NID_APP, r);
99                 }
100         }
101
102         return __pDataControlManagerInstance;
103 }
104
105 int
106 _DataControlManager::GetRequestCount(void) const
107 {
108         return __pDataControlRequestList->GetCount();
109 }
110
111 result
112 _DataControlManager::AddRequestInfo(Integer* pReqId, _DataControlRequestInfo* pReqInfo)
113 {
114         //SysLog(NID_APP, "DataControl request list count: %d", __pDataControlRequestList->GetCount());
115         return __pDataControlRequestList->Add(pReqId, pReqInfo);
116 }
117
118 _DataControlRequestInfo*
119 _DataControlManager::GetRequestInfo(Integer& reqId) 
120 {
121         result r = E_SUCCESS;
122
123         Object* pObj = __pDataControlRequestList->GetValue(reqId);
124         SysTryReturn(NID_APP, pObj != null, null, GetLastResult(), "[%s] Propagating to caller...", GetErrorMessage(GetLastResult()));
125
126         _DataControlRequestInfo* pReqInfo = dynamic_cast< _DataControlRequestInfo* >(pObj);
127         SysTryReturn(NID_APP, pReqInfo != null, null, r, "[E_SYSTEM] invalid request info");
128
129         return pReqInfo;
130 }
131
132 void
133 _DataControlManager::RemoveRequestInfo(Integer& reqId)
134 {
135         __pDataControlRequestList->Remove(reqId);
136 }
137
138 void
139 _DataControlManager::RemoveAllRequests(void)
140 {
141         __pDataControlRequestList->RemoveAll();
142 }
143
144 int
145 _DataControlManager::GetUniqueId(void)
146 {
147         //++__uniqueId;
148         //__sync_fetch_and_add(&__uniqueId, 1);
149         g_atomic_int_inc(&__uniqueId);
150         return __uniqueId;
151 }
152
153 void
154 _DataControlManager::Cache(const AppId& appId)
155 {
156         __pProviderList->Add(new (std::nothrow) String(appId));
157 }
158
159 bool
160 _DataControlManager::IsCached(const AppId& appId)
161 {
162         unique_ptr< IEnumerator > pEnum(__pProviderList->GetEnumeratorN());
163         while (pEnum->MoveNext() == E_SUCCESS)
164         {
165                 String* pCachedAppId = dynamic_cast< String* >(pEnum->GetCurrent());
166                 if (pCachedAppId != null && pCachedAppId->Equals(appId) == true)
167                 {
168                         return true;
169                 }
170         }
171         return false;
172 }
173
174 result
175 _DataControlManager::AllowAccess(const AppId& appId)
176 {
177         //if (IsCached(appId) == false)
178         //{
179                 const PackageId& pkgId = _PackageManagerImpl::GetPackageIdByAppId(appId);
180                 unique_ptr< char[] > pPkgId(_StringConverter::CopyToCharArrayN(pkgId));
181                 SysTryReturnResult(NID_APP, pPkgId != null, E_SYSTEM, "The method cannot proceed due to a severe system error.");
182
183                 int ret = security_server_app_give_access(pPkgId.get(), -1);
184                 SysTryReturnResult(NID_APP, ret == 0, E_SYSTEM,
185                                 "Failed to call security_server_app_give_access(), provider: %s, ret: %d", pPkgId.get(), ret);
186
187         //      Cache(appId);
188         //}
189
190         SysLog(NID_APP, "[DC_CALLER_SEND] Allow %ls to access", appId.GetPointer());
191         return E_SUCCESS;
192 }
193
194 }} // Tizen::App
195