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 class _MapDataControlEventArg
56 _MapDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
57 IList* pResultValueList, bool providerResult, String* pErrorMsg)
58 : __requestType(requestType)
60 , __providerId(providerId)
62 , __pResultValueList(pResultValueList)
63 , __providerResult(providerResult)
64 , __pErrorMsg(pErrorMsg)
67 ~_MapDataControlEventArg(void)
69 if (__pResultValueList)
71 __pResultValueList->RemoveAll(true);
72 delete __pResultValueList;
77 _DataControlRequestType __requestType;
81 IList* __pResultValueList;
82 bool __providerResult;
86 class _MapDataControlEvent
90 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
94 _MapDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
96 const _MapDataControlEventArg* pArg = dynamic_cast<const _MapDataControlEventArg*>(&arg);
99 IMapDataControlResponseListener* pListener = dynamic_cast<IMapDataControlResponseListener*> (&listener);
100 if (pListener != null)
102 switch (pArg->__requestType)
104 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
105 pListener->OnMapDataControlGetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
106 *(pArg->__pResultValueList), pArg->__providerResult, pArg->__pErrorMsg);
108 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
109 pListener->OnMapDataControlAddValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
110 pArg->__providerResult, pArg->__pErrorMsg);
112 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
113 pListener->OnMapDataControlSetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
114 pArg->__providerResult, pArg->__pErrorMsg);
116 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
117 pListener->OnMapDataControlRemoveValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
118 pArg->__providerResult, pArg->__pErrorMsg);
128 _MapDataControlImpl::_MapDataControlImpl(void)
131 , __access(_DATACONTROL_ACCESS_UNDEFINED)
132 , __pPreviousListener(null)
133 , __pMapDataControlEvent(null)
137 _MapDataControlImpl::~_MapDataControlImpl(void)
139 delete __pMapDataControlEvent;
143 _MapDataControlImpl::GetInstance(MapDataControl& dc)
145 return dc.__pMapDataControlImpl;
148 const _MapDataControlImpl*
149 _MapDataControlImpl::GetInstance(const MapDataControl& dc)
151 return dc.__pMapDataControlImpl;
155 _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int* pReq)
157 result r = E_SUCCESS;
159 _AppArg* pArg = new(std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
160 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
162 pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
164 _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
167 if (__pMapDataControlEvent)
169 // reqId is system-wide id because the bundle is system-wide.
170 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, MapDataControlCallback, __pMapDataControlEvent, -1);
171 req = reqObj.GetRequestNumber();
173 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
174 SysTryCatch(NID_APP, r == E_SUCCESS, reqObj.Invalidate(), r, "[%s] Propgated.", GetErrorMessage(r));
183 r = pAppManagerImpl->LaunchApp(__appId, pArg);
192 _MapDataControlImpl::GetValue(const String& dataId, const String& key,
193 RequestId& reqId, int pageNo, int countPerPage)
195 SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
196 SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
197 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
198 "The GetValue query is not permitted by DataControl provider.");
201 result r = E_SUCCESS;
203 ArrayList* pArgList = null;
204 pArgList = new ArrayList();
205 pArgList->Construct();
207 pArgList->Add(*(new String(dataId)));
208 pArgList->Add(*(new String(key)));
210 String* pPageNo = new String();
211 pPageNo->Append(pageNo);
212 pArgList->Add(*pPageNo);
214 String* pCountPerPage = new String();
215 pCountPerPage->Append(countPerPage);
216 pArgList->Add(*pCountPerPage);
218 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, pArgList, &id);
219 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
221 reqId = static_cast< RequestId >(id);
223 SysLog(NID_APP, "dataId: %ls, key: %ls, reqId: %d, pageNo: %d, countPerPage: %d",
224 dataId.GetPointer(), key.GetPointer(), reqId, pageNo, countPerPage);
228 pArgList->RemoveAll(true);
235 _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop)
237 ArrayList* pResultList = null;
238 ArrayList* pOrigList = null;
239 String* pRequestType = null;
240 String* pResult = null;
241 String* pReqId = null;
242 String* pProviderId = null;
243 String* pDataId = null;
244 String* pErrorMessage = null;
245 String* pErrorMsg = null;
246 String* pResultCount = null;
247 String* pValue = null;
248 ArrayList* pResultValueList = null;
253 bool providerResult = true;
254 bundle* origBundle = null;
255 bundle* resBundle = null;
256 _MapDataControlEventArg* pEventArg = null;
257 result r = E_SUCCESS;
259 SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
260 SysLog(NID_APP, "appsvc result value: %d", res);
262 resBundle = pResArg->GetBundle();
265 _MapDataControlEvent* pMapDataControlEvent = static_cast< _MapDataControlEvent* >(data);
267 if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
269 const char* p = null;
272 pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
273 SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
275 pResult = dynamic_cast <String*>(pResultList->GetAt(0));
276 SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
277 Integer::Parse(*pResult, providerRes);
278 providerResult = static_cast< bool >(providerRes);
280 pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1));
281 SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
282 pErrorMsg = new (std::nothrow) String(*pErrorMessage);
283 SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
284 "[E_OUT_OF_MEMORY] The memory is insufficient.");
287 origBundle = pArg->GetBundle();
289 p = appsvc_get_data(origBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
290 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
291 pRequestType = new (std::nothrow) String(p);
292 SysTryCatch(NID_APP, pRequestType, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
293 "[E_OUT_OF_MEMORY] The memory is insufficient.");
294 Integer::Parse(*pRequestType, requestType);
296 p = appsvc_get_data(origBundle, OSP_K_REQUEST_ID);
297 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
298 pReqId = new (std::nothrow) String(p);
299 SysTryCatch(NID_APP, pReqId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
300 "[E_OUT_OF_MEMORY] The memory is insufficient.");
301 Integer::Parse(*pReqId, reqId);
303 p = appsvc_get_data(origBundle, OSP_K_DATACONTROL_PROVIDER);
304 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
305 pProviderId = new (std::nothrow) String(p);
306 SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
307 "[E_OUT_OF_MEMORY] The memory is insufficient.");
309 pOrigList = _AppArg::GetListN(origBundle, OSP_K_ARG);
310 SysTryCatch(NID_APP, pOrigList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
311 pDataId = dynamic_cast <String*>(pOrigList->GetAt(0));
312 SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
314 SysLog(NID_APP, "result: %ld, requestType: %d, reqId: %d, providerId: %ls, dataId: %ls, errorMsg: %ls ",
315 providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
317 switch (static_cast< _DataControlRequestType >(requestType))
319 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
321 pResultValueList = new (std::nothrow) ArrayList();
322 SysTryCatch(NID_APP, pResultValueList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
323 "[E_OUT_OF_MEMORY] The memory is insufficient.");
325 if (providerResult == true)
327 pResultCount = dynamic_cast< String* >(pResultList->GetAt(2));
328 if (pResultCount == null)
330 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
331 pResultValueList->RemoveAll(true);
332 delete pResultValueList;
335 Integer::Parse(*pResultCount, resultCount);
340 pValue = dynamic_cast< String* >(pResultList->GetAt(index));
343 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
344 pResultValueList->RemoveAll(true);
345 delete pResultValueList;
349 pResultValueList->Add(*(new (std::nothrow) String(*pValue)));
355 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, static_cast <RequestId>(reqId),
356 *pProviderId, *pDataId, pResultValueList, providerResult, pErrorMsg);
357 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
358 pMapDataControlEvent->Fire(*pEventArg);
362 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
364 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, static_cast <RequestId>(reqId),
365 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
366 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
367 pMapDataControlEvent->Fire(*pEventArg);
370 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
372 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, 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_DELETE:
380 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, 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);
390 pResultList->RemoveAll(true);
393 pOrigList->RemoveAll(true);
407 pResultList->RemoveAll(true);
413 pOrigList->RemoveAll(true);
425 _MapDataControlImpl::AddValue(const String& dataId, const String& key,
426 const String& value, RequestId& reqId)
428 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
429 "The AddValue query is not permitted by DataControl provider.");
432 result r = E_SUCCESS;
434 ArrayList* pArgList = null;
435 pArgList = new ArrayList();
436 pArgList->Construct();
438 pArgList->Add(*(new String(dataId)));
439 pArgList->Add(*(new String(key)));
440 pArgList->Add(*(new String(value)));
442 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
443 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
445 reqId = static_cast< RequestId >(id);
447 SysLog(NID_APP, "dataId: %ls, key: %ls, value: %ls, reqId: %d",
448 dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
452 pArgList->RemoveAll(true);
459 _MapDataControlImpl::SetValue(const String& dataId, const String& key,
460 const String& oldValue, const String& newValue, RequestId& reqId)
462 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
463 "The SetValue query is not permitted by DataControl provider.");
466 result r = E_SUCCESS;
468 ArrayList* pArgList = null;
469 pArgList = new ArrayList();
470 pArgList->Construct();
472 pArgList->Add(*(new String(dataId)));
473 pArgList->Add(*(new String(key)));
474 pArgList->Add(*(new String(oldValue)));
475 pArgList->Add(*(new String(newValue)));
477 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
478 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
480 reqId = static_cast< RequestId >(id);
482 SysLog(NID_APP, "dataId: %ls, key: %ls, oldValue: %ls, newValue: %ls, reqId: %d",
483 dataId.GetPointer(), key.GetPointer(), oldValue.GetPointer(), newValue.GetPointer(), reqId);
487 pArgList->RemoveAll(true);
494 _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
495 const String& value, RequestId& reqId)
497 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
498 "The RemoveValue query is not permitted by DataControl provider.");
501 result r = E_SUCCESS;
503 ArrayList* pArgList = null;
504 pArgList = new ArrayList();
505 pArgList->Construct();
507 pArgList->Add(*(new String(dataId)));
508 pArgList->Add(*(new String(key)));
509 pArgList->Add(*(new String(value)));
511 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
512 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
514 reqId = static_cast< RequestId >(id);
516 SysLog(NID_APP, "dataId: %ls, key: %ls, value: %ls, reqId: %d",
517 dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
521 pArgList->RemoveAll(true);
528 _MapDataControlImpl::SetMapDataControlResponseListener(IMapDataControlResponseListener* pListener)
530 result r = E_SUCCESS;
532 if (__pPreviousListener != null)
534 r = __pMapDataControlEvent->RemoveListener(*__pPreviousListener);
535 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
536 __pPreviousListener = null;
539 if (pListener != null)
541 r = __pMapDataControlEvent->AddListener(*pListener);
546 case E_OBJ_ALREADY_EXIST:
548 case E_INVALID_OPERATION:
549 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
552 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
558 __pPreviousListener = pListener;
565 _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& providerId, const String& access)
567 unique_ptr<MapDataControl> pDc(new (std::nothrow) MapDataControl);
568 SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
570 _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
571 unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
572 SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
574 pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
575 pDcImpl->__appId = appId;
576 pDcImpl->__providerId = providerId;
578 if (access == L"readonly")
580 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
582 else if (access == L"writeonly")
584 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
586 else if (access == L"readwrite")
588 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
592 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
593 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
596 return pDc.release();