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>
36 #include <FBaseRtWaitingLoop.h>
38 #include "FApp_AppControlManager.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 static const int MAX_REQUEST_COUNT = 128;
55 class _MapDataControlEventArg
59 _MapDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
60 IList* pResultValueList, bool providerResult, String* pErrorMsg)
61 : __requestType(requestType)
63 , __providerId(providerId)
65 , __pResultValueList(pResultValueList)
66 , __providerResult(providerResult)
67 , __pErrorMsg(pErrorMsg)
70 ~_MapDataControlEventArg(void)
72 if (__pResultValueList)
74 __pResultValueList->RemoveAll(true);
75 delete __pResultValueList;
80 _DataControlRequestType __requestType;
84 IList* __pResultValueList;
85 bool __providerResult;
89 class _MapDataControlEvent
93 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
97 _MapDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
99 const _MapDataControlEventArg* pArg = dynamic_cast<const _MapDataControlEventArg*>(&arg);
102 IMapDataControlResponseListener* pListener = dynamic_cast<IMapDataControlResponseListener*> (&listener);
103 if (pListener != null)
105 switch (pArg->__requestType)
107 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
108 pListener->OnMapDataControlGetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
109 *(pArg->__pResultValueList), pArg->__providerResult, pArg->__pErrorMsg);
111 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
112 pListener->OnMapDataControlAddValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
113 pArg->__providerResult, pArg->__pErrorMsg);
115 case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
116 pListener->OnMapDataControlSetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
117 pArg->__providerResult, pArg->__pErrorMsg);
119 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
120 pListener->OnMapDataControlRemoveValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
121 pArg->__providerResult, pArg->__pErrorMsg);
131 _MapDataControlImpl::_MapDataControlImpl(void)
134 , __access(_DATACONTROL_ACCESS_UNDEFINED)
135 , __pPreviousListener(null)
136 , __pMapDataControlEvent(null)
140 _MapDataControlImpl::~_MapDataControlImpl(void)
142 delete __pMapDataControlEvent;
146 _MapDataControlImpl::GetInstance(MapDataControl& dc)
148 return dc.__pMapDataControlImpl;
151 const _MapDataControlImpl*
152 _MapDataControlImpl::GetInstance(const MapDataControl& dc)
154 return dc.__pMapDataControlImpl;
158 _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int* pReq)
160 result r = E_SUCCESS;
163 _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
165 // Check the request count of DataControl operation
166 int count = pAppManagerImpl->GetLaunchRequestCount();
167 SysLog(NID_APP, "Current launch request count: %d", count);
169 SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
171 _AppArg* pArg = new(std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
172 SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
174 pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
176 if (__pMapDataControlEvent)
178 // reqId is system-wide id because the bundle is system-wide.
179 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, MapDataControlCallback, __pMapDataControlEvent, -1);
180 req = reqObj.GetRequestNumber();
182 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
183 SysTryCatch(NID_APP, r == E_SUCCESS, reqObj.Invalidate(), r, "[%s] Propgated.", GetErrorMessage(r));
192 r = pAppManagerImpl->LaunchApp(__appId, pArg);
201 _MapDataControlImpl::GetValue(const String& dataId, const String& key,
202 RequestId& reqId, int pageNo, int countPerPage)
204 SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
205 SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
206 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
207 "The GetValue query is not permitted by DataControl provider.");
210 result r = E_SUCCESS;
212 ArrayList* pArgList = null;
213 pArgList = new ArrayList();
214 pArgList->Construct();
216 pArgList->Add(*(new String(dataId)));
217 pArgList->Add(*(new String(key)));
219 String* pPageNo = new String();
220 pPageNo->Append(pageNo);
221 pArgList->Add(*pPageNo);
223 String* pCountPerPage = new String();
224 pCountPerPage->Append(countPerPage);
225 pArgList->Add(*pCountPerPage);
227 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, pArgList, &id);
228 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
230 reqId = static_cast< RequestId >(id);
232 SysLog(NID_APP, "dataId: %ls, key: %ls, reqId: %d, pageNo: %d, countPerPage: %d",
233 dataId.GetPointer(), key.GetPointer(), reqId, pageNo, countPerPage);
237 pArgList->RemoveAll(true);
244 _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop)
246 ArrayList* pResultList = null;
247 ArrayList* pOrigList = null;
248 String* pRequestType = null;
249 String* pResult = null;
250 String* pReqId = null;
251 String* pProviderId = null;
252 String* pDataId = null;
253 String* pErrorMessage = null;
254 String* pErrorMsg = null;
255 String* pResultCount = null;
256 String* pValue = null;
257 ArrayList* pResultValueList = null;
262 bool providerResult = true;
263 bundle* origBundle = null;
264 bundle* resBundle = null;
265 _MapDataControlEventArg* pEventArg = null;
266 result r = E_SUCCESS;
268 SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
269 SysLog(NID_APP, "appsvc result value: %d", res);
271 resBundle = pResArg->GetBundle();
274 _MapDataControlEvent* pMapDataControlEvent = static_cast< _MapDataControlEvent* >(data);
276 if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
278 const char* p = null;
281 pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
282 SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
284 pResult = dynamic_cast <String*>(pResultList->GetAt(0));
285 SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
286 Integer::Parse(*pResult, providerRes);
287 providerResult = static_cast< bool >(providerRes);
289 pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1));
290 SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
291 pErrorMsg = new (std::nothrow) String(*pErrorMessage);
292 SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
293 "[E_OUT_OF_MEMORY] The memory is insufficient.");
296 origBundle = pArg->GetBundle();
298 p = appsvc_get_data(origBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
299 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
300 pRequestType = new (std::nothrow) String(p);
301 SysTryCatch(NID_APP, pRequestType, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
302 "[E_OUT_OF_MEMORY] The memory is insufficient.");
303 Integer::Parse(*pRequestType, requestType);
305 p = appsvc_get_data(origBundle, OSP_K_REQUEST_ID);
306 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
307 pReqId = new (std::nothrow) String(p);
308 SysTryCatch(NID_APP, pReqId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
309 "[E_OUT_OF_MEMORY] The memory is insufficient.");
310 Integer::Parse(*pReqId, reqId);
312 p = appsvc_get_data(origBundle, OSP_K_DATACONTROL_PROVIDER);
313 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
314 pProviderId = new (std::nothrow) String(p);
315 SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
316 "[E_OUT_OF_MEMORY] The memory is insufficient.");
318 pOrigList = _AppArg::GetListN(origBundle, OSP_K_ARG);
319 SysTryCatch(NID_APP, pOrigList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
320 pDataId = dynamic_cast <String*>(pOrigList->GetAt(0));
321 SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
323 SysLog(NID_APP, "result: %ld, requestType: %d, reqId: %d, providerId: %ls, dataId: %ls, errorMsg: %ls ",
324 providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
326 switch (static_cast< _DataControlRequestType >(requestType))
328 case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
330 pResultValueList = new (std::nothrow) ArrayList();
331 SysTryCatch(NID_APP, pResultValueList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
332 "[E_OUT_OF_MEMORY] The memory is insufficient.");
334 if (providerResult == true)
336 pResultCount = dynamic_cast< String* >(pResultList->GetAt(2));
337 if (pResultCount == null)
339 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
340 pResultValueList->RemoveAll(true);
341 delete pResultValueList;
344 Integer::Parse(*pResultCount, resultCount);
349 pValue = dynamic_cast< String* >(pResultList->GetAt(index));
352 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
353 pResultValueList->RemoveAll(true);
354 delete pResultValueList;
358 pResultValueList->Add(*(new (std::nothrow) String(*pValue)));
364 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, static_cast <RequestId>(reqId),
365 *pProviderId, *pDataId, pResultValueList, 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);
371 case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
373 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, 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_UPDATE:
381 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, 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);
387 case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
389 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, static_cast <RequestId>(reqId),
390 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
391 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
392 pMapDataControlEvent->Fire(*pEventArg);
399 pResultList->RemoveAll(true);
402 pOrigList->RemoveAll(true);
411 // Remove the request count
412 SysLog(NID_APP, "Remove a launch request: reqId: %d", reqId);
413 _AppControlManager::GetInstance()->RemoveLaunchRequest(reqId);
420 pResultList->RemoveAll(true);
426 pOrigList->RemoveAll(true);
438 _MapDataControlImpl::AddValue(const String& dataId, const String& key,
439 const String& value, RequestId& reqId)
441 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
442 "The AddValue query is not permitted by DataControl provider.");
445 result r = E_SUCCESS;
447 ArrayList* pArgList = null;
448 pArgList = new ArrayList();
449 pArgList->Construct();
451 pArgList->Add(*(new String(dataId)));
452 pArgList->Add(*(new String(key)));
453 pArgList->Add(*(new String(value)));
455 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
456 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
458 reqId = static_cast< RequestId >(id);
460 SysLog(NID_APP, "dataId: %ls, key: %ls, value: %ls, reqId: %d",
461 dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
465 pArgList->RemoveAll(true);
472 _MapDataControlImpl::SetValue(const String& dataId, const String& key,
473 const String& oldValue, const String& newValue, RequestId& reqId)
475 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
476 "The SetValue query is not permitted by DataControl provider.");
479 result r = E_SUCCESS;
481 ArrayList* pArgList = null;
482 pArgList = new ArrayList();
483 pArgList->Construct();
485 pArgList->Add(*(new String(dataId)));
486 pArgList->Add(*(new String(key)));
487 pArgList->Add(*(new String(oldValue)));
488 pArgList->Add(*(new String(newValue)));
490 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
491 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
493 reqId = static_cast< RequestId >(id);
495 SysLog(NID_APP, "dataId: %ls, key: %ls, oldValue: %ls, newValue: %ls, reqId: %d",
496 dataId.GetPointer(), key.GetPointer(), oldValue.GetPointer(), newValue.GetPointer(), reqId);
500 pArgList->RemoveAll(true);
507 _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
508 const String& value, RequestId& reqId)
510 SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
511 "The RemoveValue query is not permitted by DataControl provider.");
514 result r = E_SUCCESS;
516 ArrayList* pArgList = null;
517 pArgList = new ArrayList();
518 pArgList->Construct();
520 pArgList->Add(*(new String(dataId)));
521 pArgList->Add(*(new String(key)));
522 pArgList->Add(*(new String(value)));
524 r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
525 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
527 reqId = static_cast< RequestId >(id);
529 SysLog(NID_APP, "dataId: %ls, key: %ls, value: %ls, reqId: %d",
530 dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
534 pArgList->RemoveAll(true);
541 _MapDataControlImpl::SetMapDataControlResponseListener(IMapDataControlResponseListener* pListener)
543 result r = E_SUCCESS;
545 if (__pPreviousListener != null)
547 r = __pMapDataControlEvent->RemoveListener(*__pPreviousListener);
548 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
549 __pPreviousListener = null;
552 if (pListener != null)
554 r = __pMapDataControlEvent->AddListener(*pListener);
559 case E_OBJ_ALREADY_EXIST:
561 case E_INVALID_OPERATION:
562 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
565 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
571 __pPreviousListener = pListener;
578 _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& providerId, const String& access)
580 unique_ptr<MapDataControl> pDc(new (std::nothrow) MapDataControl);
581 SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
583 _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
584 unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
585 SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
587 pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
588 pDcImpl->__appId = appId;
589 pDcImpl->__providerId = providerId;
591 if (access == L"readonly")
593 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
595 else if (access == L"writeonly")
597 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
599 else if (access == L"readwrite")
601 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
605 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
606 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
609 return pDc.release();