2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FApp_MapDataControlImpl.cpp
20 * @brief This is the implementation for the %_MapDataControlImpl class.
25 #include <unique_ptr.h>
27 #include <appsvc/appsvc.h>
29 #include <FBaseInteger.h>
30 #include <FBaseString.h>
31 #include <FBaseRtIEventArg.h>
32 #include <FAppMapDataControl.h>
33 #include <FAppIMapDataControlResponseListener.h>
35 #include <FBaseSysLog.h>
37 #include "FApp_AppControlManager.h"
38 #include "FApp_MapDataControlImpl.h"
39 #include "FApp_AppArg.h"
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Base::Runtime;
46 using namespace Tizen::App;
47 using namespace Tizen::Io;
49 namespace Tizen { namespace App
52 static const int MAX_REQUEST_COUNT = 128;
54 class _MapDataControlEventArg
58 _MapDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
59 IList* pResultValueList, bool providerResult, String* pErrorMsg)
60 : __requestType(requestType)
62 , __providerId(providerId)
64 , __pResultValueList(pResultValueList)
65 , __providerResult(providerResult)
66 , __pErrorMsg(pErrorMsg)
69 ~_MapDataControlEventArg(void)
71 if (__pResultValueList)
73 __pResultValueList->RemoveAll(true);
74 delete __pResultValueList;
79 _DataControlRequestType __requestType;
83 IList* __pResultValueList;
84 bool __providerResult;
88 class _MapDataControlEvent
92 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
96 _MapDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
98 const _MapDataControlEventArg* pArg = dynamic_cast<const _MapDataControlEventArg*>(&arg);
101 IMapDataControlResponseListener* pListener = dynamic_cast<IMapDataControlResponseListener*> (&listener);
102 if (pListener != null)
104 switch (pArg->__requestType)
106 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
107 pListener->OnMapDataControlGetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
108 *(pArg->__pResultValueList), pArg->__providerResult, pArg->__pErrorMsg);
110 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
111 pListener->OnMapDataControlAddValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
112 pArg->__providerResult, pArg->__pErrorMsg);
114 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
115 pListener->OnMapDataControlSetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
116 pArg->__providerResult, pArg->__pErrorMsg);
118 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
119 pListener->OnMapDataControlRemoveValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
120 pArg->__providerResult, pArg->__pErrorMsg);
130 _MapDataControlImpl::_MapDataControlImpl(void)
133 , __access(_DATACONTROL_ACCESS_UNDEFINED)
134 , __pPreviousListener(null)
135 , __pMapDataControlEvent(null)
139 _MapDataControlImpl::~_MapDataControlImpl(void)
141 delete __pMapDataControlEvent;
145 _MapDataControlImpl::GetInstance(MapDataControl& dc)
147 return dc.__pMapDataControlImpl;
150 const _MapDataControlImpl*
151 _MapDataControlImpl::GetInstance(const MapDataControl& dc)
153 return dc.__pMapDataControlImpl;
157 _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int* pReq)
159 result r = E_SUCCESS;
162 _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
164 // Check the request count of DataControl operation
165 int count = pAppManagerImpl->GetLaunchRequestCount();
166 SysLog(NID_APP, "Current launch request count: %d", count);
168 SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
170 _AppArg* pArg = new(std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
171 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
173 pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
175 if (__pMapDataControlEvent)
177 // reqId is system-wide id because the bundle is system-wide.
178 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, MapDataControlCallback, __pMapDataControlEvent, -1);
179 req = reqObj.GetRequestNumber();
181 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
182 SysTryCatch(NID_APP, r == E_SUCCESS, reqObj.Invalidate(), r, "[%s] Propgated.", GetErrorMessage(r));
191 r = pAppManagerImpl->LaunchApp(__appId, pArg);
200 _MapDataControlImpl::GetValue(const String& dataId, const String& key,
201 RequestId& reqId, int pageNo, int countPerPage)
203 SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
204 SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
205 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
206 "The GetValue query is not permitted by DataControl provider.");
209 result r = E_SUCCESS;
211 ArrayList* pArgList = null;
212 pArgList = new ArrayList();
213 pArgList->Construct();
215 pArgList->Add(*(new String(dataId)));
216 pArgList->Add(*(new String(key)));
218 String* pPageNo = new String();
219 pPageNo->Append(pageNo);
220 pArgList->Add(*pPageNo);
222 String* pCountPerPage = new String();
223 pCountPerPage->Append(countPerPage);
224 pArgList->Add(*pCountPerPage);
226 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, pArgList, &id);
227 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
229 reqId = static_cast< RequestId >(id);
231 SysLog(NID_APP, "dataId: %ls, key: %ls, reqId: %d, pageNo: %d, countPerPage: %d",
232 dataId.GetPointer(), key.GetPointer(), reqId, pageNo, countPerPage);
236 pArgList->RemoveAll(true);
243 _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop)
245 ArrayList* pResultList = null;
246 ArrayList* pOrigList = null;
247 String* pRequestType = null;
248 String* pResult = null;
249 String* pReqId = null;
250 String* pProviderId = null;
251 String* pDataId = null;
252 String* pErrorMessage = null;
253 String* pErrorMsg = null;
254 String* pResultCount = null;
255 String* pValue = null;
256 ArrayList* pResultValueList = null;
261 bool providerResult = true;
262 bundle* origBundle = null;
263 bundle* resBundle = null;
264 _MapDataControlEventArg* pEventArg = null;
265 result r = E_SUCCESS;
267 SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
268 SysLog(NID_APP, "appsvc result value: %d", res);
270 resBundle = pResArg->GetBundle();
273 _MapDataControlEvent* pMapDataControlEvent = static_cast< _MapDataControlEvent* >(data);
275 if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
277 const char* p = null;
280 pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
281 SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
283 pResult = dynamic_cast <String*>(pResultList->GetAt(0));
284 SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
285 Integer::Parse(*pResult, providerRes);
286 providerResult = static_cast< bool >(providerRes);
288 pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1));
289 SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
290 pErrorMsg = new (std::nothrow) String(*pErrorMessage);
291 SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
292 "[E_OUT_OF_MEMORY] The memory is insufficient.");
295 origBundle = pArg->GetBundle();
297 p = appsvc_get_data(origBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
298 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
299 pRequestType = new (std::nothrow) String(p);
300 SysTryCatch(NID_APP, pRequestType, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
301 "[E_OUT_OF_MEMORY] The memory is insufficient.");
302 Integer::Parse(*pRequestType, requestType);
304 p = appsvc_get_data(origBundle, OSP_K_REQUEST_ID);
305 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
306 pReqId = new (std::nothrow) String(p);
307 SysTryCatch(NID_APP, pReqId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
308 "[E_OUT_OF_MEMORY] The memory is insufficient.");
309 Integer::Parse(*pReqId, reqId);
311 p = appsvc_get_data(origBundle, OSP_K_DATACONTROL_PROVIDER);
312 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
313 pProviderId = new (std::nothrow) String(p);
314 SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
315 "[E_OUT_OF_MEMORY] The memory is insufficient.");
317 pOrigList = _AppArg::GetListN(origBundle, OSP_K_ARG);
318 SysTryCatch(NID_APP, pOrigList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
319 pDataId = dynamic_cast <String*>(pOrigList->GetAt(0));
320 SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
322 SysLog(NID_APP, "result: %ld, requestType: %d, reqId: %d, providerId: %ls, dataId: %ls, errorMsg: %ls ",
323 providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
325 switch (static_cast< _DataControlRequestType >(requestType))
327 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
329 pResultValueList = new (std::nothrow) ArrayList();
330 SysTryCatch(NID_APP, pResultValueList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
331 "[E_OUT_OF_MEMORY] The memory is insufficient.");
333 if (providerResult == true)
335 pResultCount = dynamic_cast< String* >(pResultList->GetAt(2));
336 if (pResultCount == null)
338 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
339 pResultValueList->RemoveAll(true);
340 delete pResultValueList;
343 Integer::Parse(*pResultCount, resultCount);
348 pValue = dynamic_cast< String* >(pResultList->GetAt(index));
351 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
352 pResultValueList->RemoveAll(true);
353 delete pResultValueList;
357 pResultValueList->Add(*(new (std::nothrow) String(*pValue)));
363 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, static_cast <RequestId>(reqId),
364 *pProviderId, *pDataId, pResultValueList, providerResult, pErrorMsg);
365 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
366 pMapDataControlEvent->Fire(*pEventArg);
370 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
372 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, static_cast <RequestId>(reqId),
373 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
374 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
375 pMapDataControlEvent->Fire(*pEventArg);
378 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
380 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, static_cast <RequestId>(reqId),
381 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
382 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
383 pMapDataControlEvent->Fire(*pEventArg);
386 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
388 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, static_cast <RequestId>(reqId),
389 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
390 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
391 pMapDataControlEvent->Fire(*pEventArg);
398 pResultList->RemoveAll(true);
401 pOrigList->RemoveAll(true);
410 // Remove the request count
411 SysLog(NID_APP, "Remove a launch request: reqId: %d", reqId);
412 _AppControlManager::GetInstance()->RemoveLaunchRequest(reqId);
419 pResultList->RemoveAll(true);
425 pOrigList->RemoveAll(true);
437 _MapDataControlImpl::AddValue(const String& dataId, const String& key,
438 const String& value, RequestId& reqId)
440 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
441 "The AddValue query is not permitted by DataControl provider.");
444 result r = E_SUCCESS;
446 ArrayList* pArgList = null;
447 pArgList = new ArrayList();
448 pArgList->Construct();
450 pArgList->Add(*(new String(dataId)));
451 pArgList->Add(*(new String(key)));
452 pArgList->Add(*(new String(value)));
454 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
455 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
457 reqId = static_cast< RequestId >(id);
459 SysLog(NID_APP, "dataId: %ls, key: %ls, value: %ls, reqId: %d",
460 dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
464 pArgList->RemoveAll(true);
471 _MapDataControlImpl::SetValue(const String& dataId, const String& key,
472 const String& oldValue, const String& newValue, RequestId& reqId)
474 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
475 "The SetValue query is not permitted by DataControl provider.");
478 result r = E_SUCCESS;
480 ArrayList* pArgList = null;
481 pArgList = new ArrayList();
482 pArgList->Construct();
484 pArgList->Add(*(new String(dataId)));
485 pArgList->Add(*(new String(key)));
486 pArgList->Add(*(new String(oldValue)));
487 pArgList->Add(*(new String(newValue)));
489 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
490 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
492 reqId = static_cast< RequestId >(id);
494 SysLog(NID_APP, "dataId: %ls, key: %ls, oldValue: %ls, newValue: %ls, reqId: %d",
495 dataId.GetPointer(), key.GetPointer(), oldValue.GetPointer(), newValue.GetPointer(), reqId);
499 pArgList->RemoveAll(true);
506 _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
507 const String& value, RequestId& reqId)
509 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
510 "The RemoveValue query is not permitted by DataControl provider.");
513 result r = E_SUCCESS;
515 ArrayList* pArgList = null;
516 pArgList = new ArrayList();
517 pArgList->Construct();
519 pArgList->Add(*(new String(dataId)));
520 pArgList->Add(*(new String(key)));
521 pArgList->Add(*(new String(value)));
523 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
524 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
526 reqId = static_cast< RequestId >(id);
528 SysLog(NID_APP, "dataId: %ls, key: %ls, value: %ls, reqId: %d",
529 dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
533 pArgList->RemoveAll(true);
540 _MapDataControlImpl::SetMapDataControlResponseListener(IMapDataControlResponseListener* pListener)
542 result r = E_SUCCESS;
544 if (__pPreviousListener != null)
546 r = __pMapDataControlEvent->RemoveListener(*__pPreviousListener);
547 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
548 __pPreviousListener = null;
551 if (pListener != null)
553 r = __pMapDataControlEvent->AddListener(*pListener);
558 case E_OBJ_ALREADY_EXIST:
560 case E_INVALID_OPERATION:
561 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
564 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
570 __pPreviousListener = pListener;
577 _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& providerId, const String& access)
579 unique_ptr<MapDataControl> pDc(new (std::nothrow) MapDataControl);
580 SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
582 _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
583 unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
584 SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
586 pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
587 pDcImpl->__appId = appId;
588 pDcImpl->__providerId = providerId;
590 if (access == L"readonly")
592 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
594 else if (access == L"writeonly")
596 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
598 else if (access == L"readwrite")
600 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
604 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
605 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
608 return pDc.release();