2 // Copyright (c) 2012 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_DataControlProviderManagerImpl.cpp
19 * @brief This is the implementation for the %_DataControlProviderManagerImpl class.
23 #include <unique_ptr.h>
24 #include <security-server.h>
26 #include <FBaseDataType.h>
27 #include <FBaseInteger.h>
28 #include <FBaseString.h>
29 #include <FBaseLongLong.h>
30 #include <FBaseColArrayList.h>
31 #include <FBaseColIList.h>
32 #include <FBaseSysLog.h>
33 #include <FBaseErrors.h>
34 #include <FAppTypes.h>
35 #include <FAppAppManager.h>
36 #include <FAppSqlDataControl.h>
37 #include <FAppMapDataControl.h>
38 #include <FAppDataControlProviderManager.h>
41 #include <FBase_StringConverter.h>
42 #include <FIo_DataControlResultSetImpl.h>
43 #include <FAppPkg_PackageManagerImpl.h>
46 #include "FApp_AppArg.h"
47 #include "FApp_AppInfo.h"
48 #include "FApp_AppImpl.h"
49 #include "FApp_AppControlManager.h"
50 #include "FApp_SqlDataControlImpl.h"
51 #include "FApp_MapDataControlImpl.h"
52 #include "FApp_DataControlProviderManagerImpl.h"
53 #include "FApp_DataControlManager.h"
56 using namespace Tizen::Base;
57 using namespace Tizen::Base::Collection;
58 using namespace Tizen::Io;
59 using namespace Tizen::App::Package;
61 extern const char* _DATACONTROL_RESULT_DIR;
62 extern const char* _DATACONTROL_RESULT_COMPAT_DIR;
64 namespace Tizen { namespace App
67 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
70 _DataControlProviderManagerImpl::SetSqlDataControlProviderEventListener(ISqlDataControlProviderEventListener* pListener)
72 _AppImpl* pAppImpl = _AppImpl::GetInstance();
73 SysTryReturn(NID_APP, pAppImpl, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting _AppImpl instance failed.");
75 return pAppImpl->SetSqlDataControlProviderEventListener(pListener);
79 _DataControlProviderManagerImpl::SetMapDataControlProviderEventListener(IMapDataControlProviderEventListener* pListener)
81 _AppImpl* pAppImpl = _AppImpl::GetInstance();
82 SysTryReturn(NID_APP, pAppImpl, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting _AppImpl instance failed.");
84 return pAppImpl->SetMapDataControlProviderEventListener(pListener);
88 _DataControlProviderManagerImpl::SendDataControlResult(RequestId reqId, _DataControlRequestType apiType,
89 IDbEnumerator* pDbEnum, IList* pResultValueList, long long insertRowId, bool providerResult, const String* pErrorMsg)
91 ArrayList* pList = null;
97 _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
100 ArrayList* pResultArgList = null;
101 String* pResult = null;
102 String* pErrorMessage = null;
103 String* pTempFilePath = null;
106 String* pCount = null;
107 int countPerPage = 0;
108 String* pResultCount = null;
109 String* pResultValue = null;
111 String* pDataId = null;
112 result r = E_SUCCESS;
115 _AppControlManager* pAppMgr = _AppControlManager::GetInstance();
116 SysTryReturnResult(NID_APP, pAppMgr, E_SYSTEM, "Failed to get instance.");
118 _ResultInfo* pResultInfo = pAppMgr->__resultManager.FindItem(static_cast< int >(reqId)); // provider reqId
119 SysTryReturnResult(NID_APP, pResultInfo, E_OBJ_NOT_FOUND,
120 "The data control request specified with the req (%ld) did not exist.", reqId);
122 const _AppArg& arg = pResultInfo->arg; // request info
125 reqType = arg.GetValue(OSP_K_DATACONTROL_REQUEST_TYPE);
126 Integer::Parse(reqType, type);
127 requestType = static_cast< _DataControlRequestType >(type);
128 if (providerResult == true && apiType != requestType)
130 if ((apiType == _DATACONTROL_REQUEST_TYPE_SQL_UPDATE /*UpdateDelete*/ && requestType == _DATACONTROL_REQUEST_TYPE_SQL_DELETE) ||
131 apiType == _DATACONTROL_REQUEST_TYPE_UNDEFINED /*MAP*/)
138 SysLog(NID_APP, "[E_INVALID_ARG] This method cannot send the result set for the specified reqId.");
143 callerAppId = arg.GetCallerAppId();
144 callerPid = arg.GetCallerPid();
145 version = arg.GetValue(OSP_K_DATACONTROL_PROTOCOL_VERSION);
146 callerReqId = arg.GetValue(OSP_K_REQUEST_ID);
147 providerId = arg.GetValue(OSP_K_DATACONTROL_PROVIDER);
149 // list-based request
150 pList = _AppArg::GetListN(arg.GetBundle(), OSP_K_ARG);
151 SysTryCatch(NID_APP, pList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
153 pDataId = dynamic_cast< String* >(pList->GetAt(0)); // request list[0]
154 SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
156 SysSecureLog(NID_APP, "[DC_PROV_SEND] > caller app: %ls, caller proc: %d, version: %ls requestType: %d, callerReq: %ls, provider: %ls, data: %ls",
157 callerAppId.GetPointer(), callerPid, version.GetPointer(), requestType, callerReqId.GetPointer(), providerId.GetPointer(), pDataId->GetPointer());
160 pResultArgList = new (std::nothrow) ArrayList();
161 SysTryCatch(NID_APP, pResultArgList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
162 "[E_OUT_OF_MEMORY] The memory was insufficient.");
163 pResultArgList->Construct();
165 pResult = new (std::nothrow) String();
166 SysTryCatch(NID_APP, pResult, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
167 "[E_OUT_OF_MEMORY] The memory was insufficient.");
168 pResult->Append(static_cast< int >(providerResult));
169 pResultArgList->Add(*pResult); // result list[0]
171 if (pErrorMsg == null)
173 pErrorMessage = new (std::nothrow) String();
177 pErrorMessage = new (std::nothrow) String(*pErrorMsg);
179 SysTryCatch(NID_APP, pErrorMessage, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
180 "[E_OUT_OF_MEMORY] The memory was insufficient.");
181 SysTryCatch(NID_APP, pErrorMessage->GetLength() <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
182 "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", pErrorMessage->GetLength());
183 pResultArgList->Add(*pErrorMessage); // result list[1]
187 case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
191 unique_ptr<_DataControlResultSetImpl> pResultSet(new (std::nothrow) _DataControlResultSetImpl(reqId));
192 SysTryCatch(NID_APP, pResultSet, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
193 "[E_OUT_OF_MEMORY] The memory is insufficient.");
195 r = pResultSet->FillWith(pDbEnum, version);
196 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM,
197 "[E_SYSTEM] The method cannot proceed due to a severe system error.");
200 if (version == L"ver_2.1.0.3")
202 tempFilePath.Append(_DATACONTROL_RESULT_DIR);
204 DataControlProviderManager* pDcMgr = DataControlProviderManager::GetInstance();
205 SysTryCatch(NID_APP, pDcMgr != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get DataControlProviderManager instance.");
206 r = pDcMgr->__pDataControlProviderManagerImpl->AllowAccess(callerAppId);
207 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
211 tempFilePath.Append(_DATACONTROL_RESULT_COMPAT_DIR);
213 tempFilePath.Append(callerAppId);
214 tempFilePath.Append(callerReqId);
215 pTempFilePath = new (std::nothrow) String(tempFilePath);
219 pTempFilePath = new (std::nothrow) String(L"NoResultSet");
221 SysTryCatch(NID_APP, pTempFilePath, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
222 "[E_OUT_OF_MEMORY] The memory is insufficient.");
224 pResultArgList->Add(*pTempFilePath); // result list[2]
227 case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
228 pResultArgList->Add(*(new String(LongLong::ToString(insertRowId)))); // result list[2]
231 case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
233 case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
236 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
238 SysTryCatch(NID_APP, !(providerResult == true && pResultValueList == null), r = E_INVALID_ARG, E_INVALID_ARG,
239 "[E_INVALID_ARG] The specified pResultValueList should not be null if the request is GetValue query.");
241 if (pResultValueList)
243 // list-based request
244 pNo = dynamic_cast< String* >(pList->GetAt(2)); // request list[2]
245 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
246 Integer::Parse(*pNo, pageNo);
248 pCount = dynamic_cast< String* >(pList->GetAt(3)); // request list[3]
249 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
250 Integer::Parse(*pCount, countPerPage);
251 SysLog(NID_APP, "[DC_PROV_SEND] pageNo: %d, countPerPage: %d", pageNo, countPerPage);
254 int num = pResultValueList->GetCount();
255 int currentoffset = (pageNo - 1) * countPerPage;
256 int remainingNum = num - currentoffset;
257 remainingNum = (remainingNum > 0) ? remainingNum : 0; // round off to zero if negative num is found
258 int addItemCount = (countPerPage > remainingNum) ? remainingNum : countPerPage;
259 resultCount.Append(addItemCount);
261 pResultCount = new (std::nothrow) String(resultCount);
262 SysTryCatch(NID_APP, pResultCount, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
263 "[E_OUT_OF_MEMORY] The memory was insufficient.");
264 pResultArgList->Add(*pResultCount); // result list[2]
265 SysLog(NID_APP, "[DC_PROV_SEND] result count: %ls", resultCount.GetPointer());
267 long long argSize = 0;
268 if (addItemCount > 0)
270 if (version == L"ver_2.1.0.1" || version == L"ver_2.1.0.2" || version == L"ver_2.1.0.3")
273 if (version == L"ver_2.1.0.3")
275 tempFilePath.Append(_DATACONTROL_RESULT_DIR);
279 tempFilePath.Append(_DATACONTROL_RESULT_COMPAT_DIR);
281 tempFilePath.Append(callerAppId);
282 tempFilePath.Append(callerReqId);
283 pTempFilePath = new (std::nothrow) String(tempFilePath);
284 pResultArgList->Add(*pTempFilePath); // result list[3]
285 SysLog(NID_APP, "[DC_PROV_SEND] protocol version: %ls, path: %ls", version.GetPointer(), pTempFilePath->GetPointer());
287 unique_ptr< File > pFile(new (std::nothrow) File());
288 SysTryCatch(NID_APP, pFile != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
290 r = pFile->Construct(*pTempFilePath, L"w+", true);
291 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Failed to create temp file (%ls) for result set.",
292 GetErrorMessage(r), pTempFilePath->GetPointer());
294 for (int i = currentoffset; i < num; ++i)
296 String* pValue = dynamic_cast< String* >(pResultValueList->GetAt(i));
297 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
298 "[E_INVALID_ARG] The specified pResultValueList parameter should be String class.");
300 unique_ptr< char[] > pData(_StringConverter::CopyToCharArrayN(*pValue));
301 SysTryCatch(NID_APP, pData != null, , GetLastResult(), "[%s] Invalid result value",
302 GetErrorMessage(GetLastResult()));
304 int length = strlen(pData.get());
305 r = pFile->Write(&length, sizeof(int)); // data length
306 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send result.", GetErrorMessage(r));
308 r = pFile->Write(pData.get(), length); // data
309 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send result.", GetErrorMessage(r));
313 if (version == L"ver_2.1.0.3")
315 DataControlProviderManager* pDcMgr = DataControlProviderManager::GetInstance();
316 SysTryCatch(NID_APP, pDcMgr != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get DataControlProviderManager instance.");
317 r = pDcMgr->__pDataControlProviderManagerImpl->AllowAccess(callerAppId);
318 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
323 SysLog(NID_APP, "[DC_PROV_SEND] protocol version: none");
324 for (int i = currentoffset; i < num; ++i)
326 String* pValue = dynamic_cast< String* >(pResultValueList->GetAt(i));
327 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
328 "[E_INVALID_ARG] The specified pResultValueList parameter should be String class.");
330 pResultValue = new (std::nothrow) String(*pValue);
331 SysTryCatch(NID_APP, pResultValue, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
332 "[E_OUT_OF_MEMORY] The memory was insufficient.");
333 pResultArgList->Add(*pResultValue); // list[3] ~
334 argSize += pValue->GetLength() * sizeof(wchar_t);
336 SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
337 "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
343 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
345 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
347 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
348 SysTryCatch(NID_APP, !(providerResult == true && pResultValueList != null), r = E_INVALID_ARG, E_INVALID_ARG,
349 "[E_INVALID_ARG] The specified pResultValueList should be null if the request is \
350 one of AddValue, SetValue, RemoveValue queries.");
354 SysTryCatch(NID_APP, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result object");
358 r = resultArg.ConstructResult(arg, pResultArgList);
359 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, r, "[%s] Propagating.", GetErrorMessage(r));
360 resultArg.UpdateKeyValue(OSP_K_REQUEST_ID, callerReqId);
361 resultArg.UpdateKeyValue(OSP_K_DATACONTROL_REQUEST_TYPE, reqType);
362 resultArg.UpdateKeyValue(OSP_K_DATACONTROL_PROVIDER, providerId);
363 resultArg.UpdateKeyValue(OSP_K_DATACONTROL_DATA, *pDataId);
364 resultArg.UpdateKeyValue(OSP_K_DATACONTROL_PROTOCOL_VERSION, version);
367 r = _Aul::SendResult(resultArg.GetBundle(), static_cast< appsvc_result_val >(0), false, false);
368 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Failed to send result.", GetErrorMessage(r));
370 // Erases _AppArg after sending the result back to the caller
371 pAppMgr->__resultManager.RemoveItem(static_cast< int >(reqId));
376 pList->RemoveAll(true);
382 pResultArgList->RemoveAll(true);
384 delete pResultArgList;
390 _DataControlProviderManagerImpl::SendSqlDataControlSelectResult(RequestId reqId, IDbEnumerator* pDbEnum)
392 return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_QUERY,
393 pDbEnum, null, -1, true, null);
397 _DataControlProviderManagerImpl::SendSqlDataControlInsertResult(RequestId reqId, long long insertRowId)
399 return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_INSERT,
400 null, null, insertRowId, true, null);
404 _DataControlProviderManagerImpl::SendSqlDataControlUpdateDeleteResult(RequestId reqId)
406 return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_SQL_UPDATE,
407 null, null, -1, true, null);
411 _DataControlProviderManagerImpl::SendMapDataControlResult(RequestId reqId, IList* pResultValueList)
413 return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_UNDEFINED,
414 null, pResultValueList, -1, true, null);
418 _DataControlProviderManagerImpl::SendDataControlError(RequestId reqId, const String& errorMsg)
420 return _DataControlProviderManagerImpl::SendDataControlResult(reqId, _DATACONTROL_REQUEST_TYPE_UNDEFINED,
421 null, null, -1, false, &errorMsg);
424 DataControlProviderManager*
425 _DataControlProviderManagerImpl::GetInstance(void)
427 static DataControlProviderManager* pDcMgr = null;
431 pDcMgr = new (std::nothrow) DataControlProviderManager();
432 SysTryReturn(NID_APP, pDcMgr, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
439 _DataControlProviderManagerImpl::Cache(const AppId& appId)
441 __pProviderList->Add(new (std::nothrow) String(appId));
445 _DataControlProviderManagerImpl::IsCached(const AppId& appId)
447 unique_ptr< IEnumerator > pEnum(__pProviderList->GetEnumeratorN());
448 while (pEnum->MoveNext() == E_SUCCESS)
450 String* pCachedAppId = dynamic_cast< String* >(pEnum->GetCurrent());
451 if (pCachedAppId != null && pCachedAppId->Equals(appId) == true)
460 _DataControlProviderManagerImpl::AllowAccess(const AppId& appId)
462 //if (IsCached(appId) == false)
464 const PackageId& pkgId = _PackageManagerImpl::GetPackageIdByAppId(appId);
465 unique_ptr< char[] > pPkgId(_StringConverter::CopyToCharArrayN(pkgId));
466 SysTryReturnResult(NID_APP, pPkgId != null, E_SYSTEM, "The method cannot proceed due to a severe system error.");
468 int ret = security_server_app_give_access(pPkgId.get(), -1);
469 SysTryReturnResult(NID_APP, ret == 0, E_SYSTEM,
470 "Failed to call security_server_app_give_access(), provider: %s, ret: %d", pPkgId.get(), ret);
475 SysLog(NID_APP, "[DC_PROV_SEND] Allow %ls to access", appId.GetPointer());
479 _DataControlProviderManagerImpl::_DataControlProviderManagerImpl(void)
480 : __pProviderList(null)
482 __pProviderList = new (std::nothrow) LinkedList(SingleObjectDeleter);
483 SysTryReturnVoidResult(NID_APP, __pProviderList != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
486 _DataControlProviderManagerImpl::~_DataControlProviderManagerImpl(void)
488 delete __pProviderList;