Refactor DataControl request list (2nd)
[platform/framework/native/appfw.git] / src / app / FApp_MapDataControlImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        FApp_MapDataControlImpl.cpp
20  * @brief       This is the implementation for the %_MapDataControlImpl class.
21  */
22
23 #include <typeinfo>
24 #include <new>
25 #include <unique_ptr.h>
26
27 #include <appsvc/appsvc.h>
28
29 #include <FBaseInteger.h>
30 #include <FBaseString.h>
31 #include <FBaseRtIEventArg.h>
32 #include <FAppMapDataControl.h>
33 #include <FAppIMapDataControlResponseListener.h>
34
35 #include <FBaseSysLog.h>
36
37 #include "FApp_AppControlManager.h"
38 #include "FApp_MapDataControlImpl.h"
39 #include "FApp_AppArg.h"
40 #include "FApp_DataControlManager.h"
41
42 using namespace std;
43
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;
49
50 namespace Tizen { namespace App
51 {
52
53 static const int MAX_REQUEST_COUNT = 128;
54 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
55
56 class _MapDataControlEventArg
57         : public IEventArg
58 {
59 public:
60                 _MapDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
61                                 IList* pResultValueList, bool providerResult, String* pErrorMsg)
62                         : __requestType(requestType)
63                         , __reqId(reqId)
64                         , __providerId(providerId)
65                         , __dataId(dataId)
66                         , __pResultValueList(pResultValueList)
67                         , __providerResult(providerResult)
68                         , __pErrorMsg(pErrorMsg)
69                 {
70                 }
71                 ~_MapDataControlEventArg(void)
72                 {
73                         if (__pResultValueList)
74                         {
75                                 __pResultValueList->RemoveAll(true);
76                                 delete __pResultValueList;
77                         }
78                         delete __pErrorMsg;
79                 }
80
81                 _DataControlRequestType __requestType;
82                 RequestId __reqId;
83                 String __providerId;
84                 String __dataId;
85                 IList* __pResultValueList;
86                 bool __providerResult;
87                 String* __pErrorMsg;
88 };
89
90 class _MapDataControlEvent
91         : public Event
92 {
93 protected:
94                 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
95 };
96
97 void
98 _MapDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
99 {
100         const _MapDataControlEventArg* pArg = dynamic_cast<const _MapDataControlEventArg*>(&arg);
101         if (pArg != null)
102         {
103                 IMapDataControlResponseListener* pListener = dynamic_cast<IMapDataControlResponseListener*> (&listener);
104                 if (pListener != null)
105                 {
106                         switch (pArg->__requestType)
107                         {
108                         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
109                                 pListener->OnMapDataControlGetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
110                                                 *(pArg->__pResultValueList), pArg->__providerResult, pArg->__pErrorMsg);
111                                 break;
112                         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
113                                 pListener->OnMapDataControlAddValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
114                                                  pArg->__providerResult, pArg->__pErrorMsg);
115                                 break;
116                         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
117                                 pListener->OnMapDataControlSetValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
118                                                 pArg->__providerResult, pArg->__pErrorMsg);
119                                 break;
120                         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
121                                 pListener->OnMapDataControlRemoveValueResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
122                                                 pArg->__providerResult, pArg->__pErrorMsg);
123                                 break;
124                         default:
125                                 break;
126                         }
127                 }
128         }
129 }
130
131 // private
132 _MapDataControlImpl::_MapDataControlImpl(void)
133         : __appId(L"")
134         , __providerId(L"")
135         , __access(_DATACONTROL_ACCESS_UNDEFINED)
136         , __pPreviousListener(null)
137         , __pMapDataControlEvent(null)
138 {
139 }
140
141 _MapDataControlImpl::~_MapDataControlImpl(void)
142 {
143         delete __pMapDataControlEvent;
144 }
145
146 _MapDataControlImpl*
147 _MapDataControlImpl::GetInstance(MapDataControl& dc)
148 {
149         return dc.__pMapDataControlImpl;
150 }
151
152 const _MapDataControlImpl*
153 _MapDataControlImpl::GetInstance(const MapDataControl& dc)
154 {
155         return dc.__pMapDataControlImpl;
156 }
157
158 result
159 _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int* pReq)
160 {
161         Integer* pReqId = null;
162         _DataControlRequestInfo* pReqInfo = null;
163         result r = E_SUCCESS;
164
165         int req = -1;
166         _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
167
168         // Check the request count of DataControl operation
169         int count = pAppManagerImpl->GetLaunchRequestCount();
170         SysLog(NID_APP, "Current launch request count: %d", count);
171
172         SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
173
174         _AppArg* pArg = new(std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
175         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
176
177         pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
178
179         if (__pMapDataControlEvent)
180         {
181                 // reqId is system-wide id because the bundle is system-wide.
182 #if 0
183                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, MapDataControlCallback, __pMapDataControlEvent, -1);
184                 req = reqObj.GetRequestNumber();
185 #else
186                 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
187                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, MapDataControlCallback, pDcMgr, -1);
188                 req = reqObj.GetRequestNumber();
189 #endif
190                 pReqId = new (std::nothrow) Integer(req);
191                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
192                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
193
194                 pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pMapDataControlEvent);
195                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
196                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
197
198                 r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
199                 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
200
201                 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
202                 if (IsFailed(r))
203                 {
204                         SysPropagate(NID_APP, r);
205                         reqObj.Invalidate();
206                         pDcMgr->RemoveRequestInfo(*pReqId);
207                         delete pArg;
208                         return r;
209                 }
210
211                 if (pReq)
212                 {
213                         *pReq = req;
214                 }
215         }
216         else
217         {
218                 r = pAppManagerImpl->LaunchApp(__appId, pArg);
219                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
220                 delete pArg;
221         }
222
223         return E_SUCCESS;
224
225 CATCH:
226         delete pArg;
227         delete pReqId;
228         delete pReqInfo;
229         return r;
230 }
231
232 result
233 _MapDataControlImpl::GetValue(const String& dataId, const String& key,
234                 RequestId& reqId, int pageNo, int countPerPage)
235 {
236         SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
237         SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
238         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
239                         "The GetValue query is not permitted by DataControl provider.");
240
241         String* pPageNo = null;
242         String* pCountPerPage = null;
243         int id = 0;
244         result r = E_SUCCESS;
245
246         ArrayList* pArgList = new ArrayList();
247         pArgList->Construct();
248
249         pArgList->Add(*(new String(dataId)));
250         pArgList->Add(*(new String(key)));
251         long long argSize = dataId.GetLength() * sizeof(wchar_t);
252         argSize += key.GetLength() * sizeof(wchar_t);
253         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
254                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
255
256         pPageNo = new (std::nothrow) String();
257         pPageNo->Append(pageNo);
258         pArgList->Add(*pPageNo);
259
260         pCountPerPage = new (std::nothrow) String();
261         pCountPerPage->Append(countPerPage);
262         pArgList->Add(*pCountPerPage);
263
264         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, pArgList, &id);
265         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
266
267         reqId = static_cast< RequestId >(id);
268
269         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, key: %ls, reqId: %d, pageNo: %d, countPerPage: %d",
270                         dataId.GetPointer(), key.GetPointer(), reqId, pageNo, countPerPage);
271
272         // fall through
273 CATCH:
274         pArgList->RemoveAll(true);
275         delete pArgList;
276
277         return r;
278 }
279
280 result
281 _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop)
282 {
283         ArrayList* pResultList = null;
284         String* pResult = null;
285         String* pProviderId = null;
286         String* pDataId = null;
287         String* pErrorMessage = null;
288         String* pErrorMsg = null;
289         String* pResultCount = null;
290         String* pValue = null;
291         ArrayList* pResultValueList = null;
292         int resultCount = 0;
293         int requestType = 0;
294         int reqId = 0;
295         int providerRes = 0;
296         bool providerResult = true;
297         //bundle* origBundle = null;
298         bundle* resBundle = null;
299         _MapDataControlEventArg* pEventArg = null;
300         result r = E_SUCCESS;
301
302         SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
303         SysLog(NID_APP, "appsvc result value: %d", res);
304
305         resBundle = pResArg->GetBundle();
306         if (resBundle)
307         {
308                 const char* p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
309                 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
310                 reqId = atoi(p);
311                 Integer key(reqId);
312
313                 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
314                 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
315                 SysTryCatch(NID_APP, pReqInfo != null, r = E_SYSTEM, E_SYSTEM,
316                                 "[E_SYSTEM] Failed to get request info");
317
318                 _MapDataControlEvent* pMapDataControlEvent = dynamic_cast< _MapDataControlEvent* >(pReqInfo->GetEvent());
319                 SysTryCatch(NID_APP, pMapDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
320                                 "[E_SYSTEM] invalid request info");
321
322                 pDcMgr->RemoveRequestInfo(key);
323
324                 if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
325                 {
326
327                         // result list
328                         pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
329                         SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
330
331                         pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
332                         SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
333                         Integer::Parse(*pResult, providerRes);
334                         providerResult = static_cast< bool >(providerRes);
335
336                         pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
337                         SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
338                         pErrorMsg = new (std::nothrow) String(*pErrorMessage);
339                         SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
340                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
341
342                         // request info
343                         //origBundle = pArg->GetBundle();
344
345                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
346                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
347                         requestType = atoi(p);
348
349                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
350                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
351                         pProviderId = new (std::nothrow) String(p);
352                         SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
353                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
354
355                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
356                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
357                         pDataId = new (std::nothrow) String(p);
358                         SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
359                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
360
361                         SysLog(NID_APP, "[DC_CALLER_RECV] provider result: %ld, requestType: %d, reqId: %d, providerId: %ls, dataId: %ls, errorMsg: %ls ",
362                                         providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
363
364                         switch (static_cast< _DataControlRequestType >(requestType))
365                         {
366                         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
367                         {
368                                 pResultValueList = new (std::nothrow) ArrayList();
369                                 SysTryCatch(NID_APP, pResultValueList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
370                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
371
372                                 if (providerResult == true)
373                                 {
374                                         pResultCount = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
375                                         if (pResultCount == null)
376                                         {
377                                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
378                                                 pResultValueList->RemoveAll(true);
379                                                 delete pResultValueList;
380                                                 goto CATCH;
381                                         }
382                                         Integer::Parse(*pResultCount, resultCount);
383
384                                         int index = 3;
385                                         while (resultCount)
386                                         {
387                                                 pValue = dynamic_cast< String* >(pResultList->GetAt(index));
388                                                 if (pValue == null)
389                                                 {
390                                                         SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
391                                                         pResultValueList->RemoveAll(true);
392                                                         delete pResultValueList;
393                                                         goto CATCH;
394                                                 }
395
396                                                 pResultValueList->Add(*(new (std::nothrow) String(*pValue)));
397                                                 resultCount--;
398                                                 index++;
399                                         }
400                                 }
401
402                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, static_cast <RequestId>(reqId),
403                                                 *pProviderId, *pDataId, pResultValueList, providerResult, pErrorMsg);
404                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
405                                 pMapDataControlEvent->Fire(*pEventArg);
406
407                                 break;
408                         }
409                         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
410                         {
411                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, static_cast <RequestId>(reqId),
412                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
413                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
414                                 pMapDataControlEvent->Fire(*pEventArg);
415                                 break;
416                         }
417                         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
418                         {
419                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, static_cast <RequestId>(reqId),
420                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
421                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
422                                 pMapDataControlEvent->Fire(*pEventArg);
423                                 break;
424                         }
425                         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
426                         {
427                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, static_cast <RequestId>(reqId),
428                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
429                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
430                                 pMapDataControlEvent->Fire(*pEventArg);
431                                 break;
432                         }
433                         default:
434                                 break;
435                         }
436
437                         pResultList->RemoveAll(true);
438                         delete pResultList;
439                         delete pProviderId;
440                 }
441         }
442
443         // Remove the request count
444         SysLog(NID_APP, "Remove a launch request: reqId: %d", reqId);
445         _AppControlManager::GetInstance()->RemoveLaunchRequest(reqId);
446
447         return E_SUCCESS;
448
449 CATCH:
450         if (pResultList)
451         {
452                 pResultList->RemoveAll(true);
453                 delete pResultList;
454         }
455         delete pProviderId;
456
457         return r;
458 }
459
460 result
461 _MapDataControlImpl::AddValue(const String& dataId, const String& key,
462                 const String& value, RequestId& reqId)
463 {
464         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
465                         "The AddValue query is not permitted by DataControl provider.");
466
467         int id = 0;
468         result r = E_SUCCESS;
469
470         ArrayList* pArgList = null;
471         pArgList = new ArrayList();
472         pArgList->Construct();
473
474         pArgList->Add(*(new String(dataId)));
475         pArgList->Add(*(new String(key)));
476         pArgList->Add(*(new String(value)));
477         long long argSize = dataId.GetLength() * sizeof(wchar_t);
478         argSize += key.GetLength() * sizeof(wchar_t);
479         argSize += value.GetLength() * sizeof(wchar_t);
480         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
481                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
482
483         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
484         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
485
486         reqId = static_cast< RequestId >(id);
487
488         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, key: %ls, value: %ls, reqId: %d",
489                         dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
490
491         // fall through
492 CATCH:
493         pArgList->RemoveAll(true);
494         delete pArgList;
495
496         return r;
497 }
498
499 result
500 _MapDataControlImpl::SetValue(const String& dataId, const String& key,
501                 const String& oldValue, const String& newValue, RequestId& reqId)
502 {
503         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
504                         "The SetValue query is not permitted by DataControl provider.");
505
506         int id = 0;
507         result r = E_SUCCESS;
508
509         ArrayList* pArgList = null;
510         pArgList = new ArrayList();
511         pArgList->Construct();
512
513         pArgList->Add(*(new String(dataId)));
514         pArgList->Add(*(new String(key)));
515         pArgList->Add(*(new String(oldValue)));
516         pArgList->Add(*(new String(newValue)));
517         long long argSize = dataId.GetLength() * sizeof(wchar_t);
518         argSize += key.GetLength() * sizeof(wchar_t);
519         argSize += oldValue.GetLength() * sizeof(wchar_t);
520         argSize += newValue.GetLength() * sizeof(wchar_t);
521         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
522                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
523
524         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
525         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
526
527         reqId = static_cast< RequestId >(id);
528
529         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, key: %ls, oldValue: %ls, newValue: %ls, reqId: %d",
530                         dataId.GetPointer(), key.GetPointer(), oldValue.GetPointer(), newValue.GetPointer(), reqId);
531
532         // fall through
533 CATCH:
534         pArgList->RemoveAll(true);
535         delete pArgList;
536
537         return r;
538 }
539
540 result
541 _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
542                 const String& value, RequestId& reqId)
543 {
544         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
545                         "The RemoveValue query is not permitted by DataControl provider.");
546
547         int id = 0;
548         result r = E_SUCCESS;
549
550         ArrayList* pArgList = null;
551         pArgList = new ArrayList();
552         pArgList->Construct();
553
554         pArgList->Add(*(new String(dataId)));
555         pArgList->Add(*(new String(key)));
556         pArgList->Add(*(new String(value)));
557         long long argSize = dataId.GetLength() * sizeof(wchar_t);
558         argSize += key.GetLength() * sizeof(wchar_t);
559         argSize += value.GetLength() * sizeof(wchar_t);
560         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
561                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
562
563         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
564         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
565
566         reqId = static_cast< RequestId >(id);
567
568         SysLog(NID_APP, "[DC_CALLER_SEND] dataId: %ls, key: %ls, value: %ls, reqId: %d",
569                         dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
570
571         // fall through
572 CATCH:
573         pArgList->RemoveAll(true);
574         delete pArgList;
575
576         return r;
577 }
578
579 result
580 _MapDataControlImpl::SetMapDataControlResponseListener(IMapDataControlResponseListener* pListener)
581 {
582         result r = E_SUCCESS;
583
584         if (__pPreviousListener != null)
585         {
586                 r =  __pMapDataControlEvent->RemoveListener(*__pPreviousListener);
587                 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
588                  __pPreviousListener = null;
589         }
590
591         if (pListener != null)
592         {
593                 r =  __pMapDataControlEvent->AddListener(*pListener);
594                 if (IsFailed(r))
595                 {
596                         switch (r)
597                         {
598                         case E_OBJ_ALREADY_EXIST:
599                                 return E_SUCCESS;
600                         case E_INVALID_OPERATION:
601                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
602                                 return E_SYSTEM;
603                         default:
604                                 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
605                                 return r;
606                         }
607                 }
608         }
609
610         __pPreviousListener = pListener;
611
612         return E_SUCCESS;
613 }
614
615 // private
616 MapDataControl*
617 _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& providerId, const String& access)
618 {
619         unique_ptr<MapDataControl> pDc(new (std::nothrow) MapDataControl);
620         SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
621
622         _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
623         unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
624         SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
625
626         pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
627         pDcImpl->__appId = appId;
628         pDcImpl->__providerId = providerId;
629
630         if (access == L"readonly")
631         {
632                 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
633         }
634         else if (access == L"writeonly")
635         {
636                 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
637         }
638         else if (access == L"readwrite")
639         {
640                 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
641         }
642         else
643         {
644                 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
645                 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
646         }
647
648         return pDc.release();
649 }
650
651 }} // Tizen::App
652