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_AppManagerProxy.h"
39 #include "FApp_MapDataControlImpl.h"
40 #include "FApp_AppArg.h"
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Base::Runtime;
47 using namespace Tizen::App;
48 using namespace Tizen::Io;
50 namespace Tizen { namespace App
53 class _MapDataControlEventArg
57 _MapDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
58 IList* pResultValueList, bool providerResult, String* pErrorMsg)
59 : __requestType(requestType)
61 , __providerId(providerId)
63 , __pResultValueList(pResultValueList)
64 , __providerResult(providerResult)
65 , __pErrorMsg(pErrorMsg)
68 ~_MapDataControlEventArg(void)
70 if (__pResultValueList)
72 __pResultValueList->RemoveAll(true);
73 delete __pResultValueList;
78 _DataControlRequestType __requestType;
82 IList* __pResultValueList;
83 bool __providerResult;
87 class _MapDataControlEvent
91 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
95 _MapDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
97 const _MapDataControlEventArg* pArg = dynamic_cast<const _MapDataControlEventArg*>(&arg);
100 IMapDataControlResponseListener* pListener = dynamic_cast<IMapDataControlResponseListener*> (&listener);
101 if (pListener != null)
103 switch (pArg->__requestType)
105 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
106 pListener->OnMapDataControlGetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
107 *(pArg->__pResultValueList), pArg->__providerResult, pArg->__pErrorMsg);
109 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
110 pListener->OnMapDataControlAddValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
111 pArg->__providerResult, pArg->__pErrorMsg);
113 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
114 pListener->OnMapDataControlSetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
115 pArg->__providerResult, pArg->__pErrorMsg);
117 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
118 pListener->OnMapDataControlRemoveValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
119 pArg->__providerResult, pArg->__pErrorMsg);
129 _MapDataControlImpl::_MapDataControlImpl(void)
132 , __access(_DATACONTROL_ACCESS_UNDEFINED)
133 , __pPreviousListener(null)
134 , __pMapDataControlEvent(null)
138 _MapDataControlImpl::~_MapDataControlImpl(void)
140 delete __pMapDataControlEvent;
144 _MapDataControlImpl::GetInstance(MapDataControl& dc)
146 return dc.__pMapDataControlImpl;
149 const _MapDataControlImpl*
150 _MapDataControlImpl::GetInstance(const MapDataControl& dc)
152 return dc.__pMapDataControlImpl;
156 _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int* pReq)
158 result r = E_SUCCESS;
160 _AppArg* pArg = new(std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
161 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
163 pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
165 _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
168 if (__pMapDataControlEvent)
170 // reqId is system-wide id because the bundle is system-wide.
171 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, MapDataControlCallback, __pMapDataControlEvent, -1);
172 req = reqObj.GetRequestNumber();
174 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
175 SysTryCatch(NID_APP, r == E_SUCCESS, reqObj.Invalidate(), r, "[%s] Propgated.", GetErrorMessage(r));
184 r = pAppManagerImpl->LaunchApp(__appId, pArg);
193 _MapDataControlImpl::GetValue(const String& dataId, const String& key,
194 RequestId& reqId, int pageNo, int countPerPage)
196 SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
197 SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
198 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
199 "The GetValue query is not permitted by DataControl provider.");
202 result r = E_SUCCESS;
204 ArrayList* pArgList = null;
205 pArgList = new ArrayList();
206 pArgList->Construct();
208 pArgList->Add(*(new String(dataId)));
209 pArgList->Add(*(new String(key)));
211 String* pPageNo = new String();
212 pPageNo->Append(pageNo);
213 pArgList->Add(*pPageNo);
215 String* pCountPerPage = new String();
216 pCountPerPage->Append(countPerPage);
217 pArgList->Add(*pCountPerPage);
219 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, pArgList, &id);
220 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
222 reqId = static_cast< RequestId >(id);
224 SysLog(NID_APP, "dataId: %ls, key: %ls, reqId: %d, pageNo: %d, countPerPage: %d",
225 dataId.GetPointer(), key.GetPointer(), reqId, pageNo, countPerPage);
229 pArgList->RemoveAll(true);
236 _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop)
238 ArrayList* pResultList = null;
239 ArrayList* pOrigList = null;
240 String* pRequestType = null;
241 String* pResult = null;
242 String* pReqId = null;
243 String* pProviderId = null;
244 String* pDataId = null;
245 String* pErrorMessage = null;
246 String* pErrorMsg = null;
247 String* pResultCount = null;
248 String* pValue = null;
249 ArrayList* pResultValueList = null;
254 bool providerResult = true;
255 bundle* origBundle = null;
256 bundle* resBundle = null;
257 _MapDataControlEventArg* pEventArg = null;
258 result r = E_SUCCESS;
260 SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
261 SysLog(NID_APP, "appsvc result value: %d", res);
263 resBundle = pResArg->GetBundle();
266 _MapDataControlEvent* pMapDataControlEvent = static_cast< _MapDataControlEvent* >(data);
268 if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
270 const char* p = null;
273 pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
274 SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
276 pResult = dynamic_cast <String*>(pResultList->GetAt(0));
277 SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
278 Integer::Parse(*pResult, providerRes);
279 providerResult = static_cast< bool >(providerRes);
281 pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1));
282 SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
283 pErrorMsg = new (std::nothrow) String(*pErrorMessage);
284 SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
285 "[E_OUT_OF_MEMORY] The memory is insufficient.");
288 origBundle = pArg->GetBundle();
290 p = appsvc_get_data(origBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
291 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
292 pRequestType = new (std::nothrow) String(p);
293 SysTryCatch(NID_APP, pRequestType, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
294 "[E_OUT_OF_MEMORY] The memory is insufficient.");
295 Integer::Parse(*pRequestType, requestType);
297 p = appsvc_get_data(origBundle, OSP_K_REQUEST_ID);
298 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
299 pReqId = new (std::nothrow) String(p);
300 SysTryCatch(NID_APP, pReqId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
301 "[E_OUT_OF_MEMORY] The memory is insufficient.");
302 Integer::Parse(*pReqId, reqId);
304 p = appsvc_get_data(origBundle, OSP_K_DATACONTROL_PROVIDER);
305 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
306 pProviderId = new (std::nothrow) String(p);
307 SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
308 "[E_OUT_OF_MEMORY] The memory is insufficient.");
310 pOrigList = _AppArg::GetListN(origBundle, OSP_K_ARG);
311 SysTryCatch(NID_APP, pOrigList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
312 pDataId = dynamic_cast <String*>(pOrigList->GetAt(0));
313 SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
315 SysLog(NID_APP, "result: %ld, requestType: %d, reqId: %d, providerId: %ls, dataId: %ls, errorMsg: %ls ",
316 providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
318 switch (static_cast< _DataControlRequestType >(requestType))
320 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
322 pResultValueList = new (std::nothrow) ArrayList();
323 SysTryCatch(NID_APP, pResultValueList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
324 "[E_OUT_OF_MEMORY] The memory is insufficient.");
326 if (providerResult == true)
328 pResultCount = dynamic_cast< String* >(pResultList->GetAt(2));
329 if (pResultCount == null)
331 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
332 pResultValueList->RemoveAll(true);
333 delete pResultValueList;
336 Integer::Parse(*pResultCount, resultCount);
341 pValue = dynamic_cast< String* >(pResultList->GetAt(index));
344 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
345 pResultValueList->RemoveAll(true);
346 delete pResultValueList;
350 pResultValueList->Add(*(new (std::nothrow) String(*pValue)));
356 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, static_cast <RequestId>(reqId),
357 *pProviderId, *pDataId, pResultValueList, providerResult, pErrorMsg);
358 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
359 pMapDataControlEvent->Fire(*pEventArg);
363 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
365 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, static_cast <RequestId>(reqId),
366 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
367 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
368 pMapDataControlEvent->Fire(*pEventArg);
371 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
373 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, static_cast <RequestId>(reqId),
374 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
375 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
376 pMapDataControlEvent->Fire(*pEventArg);
379 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
381 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, static_cast <RequestId>(reqId),
382 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
383 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
384 pMapDataControlEvent->Fire(*pEventArg);
391 pResultList->RemoveAll(true);
394 pOrigList->RemoveAll(true);
408 pResultList->RemoveAll(true);
414 pOrigList->RemoveAll(true);
426 _MapDataControlImpl::AddValue(const String& dataId, const String& key,
427 const String& value, RequestId& reqId)
429 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
430 "The AddValue query is not permitted by DataControl provider.");
433 result r = E_SUCCESS;
435 ArrayList* pArgList = null;
436 pArgList = new ArrayList();
437 pArgList->Construct();
439 pArgList->Add(*(new String(dataId)));
440 pArgList->Add(*(new String(key)));
441 pArgList->Add(*(new String(value)));
443 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
444 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
446 reqId = static_cast< RequestId >(id);
448 SysLog(NID_APP, "dataId: %ls, key: %ls, value: %ls, reqId: %d",
449 dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
453 pArgList->RemoveAll(true);
460 _MapDataControlImpl::SetValue(const String& dataId, const String& key,
461 const String& oldValue, const String& newValue, RequestId& reqId)
463 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
464 "The SetValue query is not permitted by DataControl provider.");
467 result r = E_SUCCESS;
469 ArrayList* pArgList = null;
470 pArgList = new ArrayList();
471 pArgList->Construct();
473 pArgList->Add(*(new String(dataId)));
474 pArgList->Add(*(new String(key)));
475 pArgList->Add(*(new String(oldValue)));
476 pArgList->Add(*(new String(newValue)));
478 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
479 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
481 reqId = static_cast< RequestId >(id);
483 SysLog(NID_APP, "dataId: %ls, key: %ls, oldValue: %ls, newValue: %ls, reqId: %d",
484 dataId.GetPointer(), key.GetPointer(), oldValue.GetPointer(), newValue.GetPointer(), reqId);
488 pArgList->RemoveAll(true);
495 _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
496 const String& value, RequestId& reqId)
498 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
499 "The RemoveValue query is not permitted by DataControl provider.");
502 result r = E_SUCCESS;
504 ArrayList* pArgList = null;
505 pArgList = new ArrayList();
506 pArgList->Construct();
508 pArgList->Add(*(new String(dataId)));
509 pArgList->Add(*(new String(key)));
510 pArgList->Add(*(new String(value)));
512 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
513 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
515 reqId = static_cast< RequestId >(id);
517 SysLog(NID_APP, "dataId: %ls, key: %ls, value: %ls, reqId: %d",
518 dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
522 pArgList->RemoveAll(true);
529 _MapDataControlImpl::SetMapDataControlResponseListener(IMapDataControlResponseListener* pListener)
531 result r = E_SUCCESS;
533 if (__pPreviousListener != null)
535 r = __pMapDataControlEvent->RemoveListener(*__pPreviousListener);
536 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
537 __pPreviousListener = null;
540 if (pListener != null)
542 r = __pMapDataControlEvent->AddListener(*pListener);
547 case E_OBJ_ALREADY_EXIST:
549 case E_INVALID_OPERATION:
550 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
553 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
559 __pPreviousListener = pListener;
566 _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& providerId, const String& access)
568 unique_ptr<MapDataControl> pDc(new (std::nothrow) MapDataControl);
569 SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
571 _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
572 unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
573 SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
575 pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
576 pDcImpl->__appId = appId;
577 pDcImpl->__providerId = providerId;
579 if (access == L"readonly")
581 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
583 else if (access == L"writeonly")
585 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
587 else if (access == L"readwrite")
589 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
593 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
594 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
597 return pDc.release();