Remove double free
[platform/framework/native/appfw.git] / src / app / FApp_MapDataControlImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FApp_MapDataControlImpl.cpp
19  * @brief       This is the implementation for the %_MapDataControlImpl class.
20  */
21
22 #include <typeinfo>
23 #include <new>
24 #include <unique_ptr.h>
25
26 #include <appsvc/appsvc.h>
27
28 #include <FBaseInteger.h>
29 #include <FBaseString.h>
30 #include <FBaseRtIEventArg.h>
31 #include <FIoFile.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                         return r;
208                 }
209
210                 if (pReq)
211                 {
212                         *pReq = req;
213                 }
214         }
215         else
216         {
217                 r = pAppManagerImpl->LaunchApp(__appId, pArg);
218                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
219                 delete pArg;
220         }
221
222         return E_SUCCESS;
223
224 CATCH:
225         delete pArg;
226         delete pReqId;
227         delete pReqInfo;
228         return r;
229 }
230
231 result
232 _MapDataControlImpl::GetValue(const String& dataId, const String& key,
233                 RequestId& reqId, int pageNo, int countPerPage)
234 {
235         SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
236         SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
237         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
238                         "The GetValue query is not permitted by DataControl provider.");
239
240         String* pPageNo = null;
241         String* pCountPerPage = null;
242         int id = 0;
243         result r = E_SUCCESS;
244
245         ArrayList* pArgList = new ArrayList();
246         pArgList->Construct();
247
248         pArgList->Add(*(new String(dataId)));
249         pArgList->Add(*(new String(key)));
250         long long argSize = dataId.GetLength() * sizeof(wchar_t);
251         argSize += key.GetLength() * sizeof(wchar_t);
252         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
253                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
254
255         pPageNo = new (std::nothrow) String();
256         pPageNo->Append(pageNo);
257         pArgList->Add(*pPageNo);
258
259         pCountPerPage = new (std::nothrow) String();
260         pCountPerPage->Append(countPerPage);
261         pArgList->Add(*pCountPerPage);
262
263         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, pArgList, &id);
264         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
265
266         reqId = static_cast< RequestId >(id);
267
268         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, req: %d, pageNo: %d, countPerPage: %d",
269                         dataId.GetPointer(), key.GetPointer(), reqId, pageNo, countPerPage);
270
271         // fall through
272 CATCH:
273         pArgList->RemoveAll(true);
274         delete pArgList;
275
276         return r;
277 }
278
279 result
280 _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int option)
281 {
282         ArrayList* pResultList = null;
283         String version;
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* pPath = null;
291         ArrayList* pResultValueList = null;
292         int resultCount = 0;
293         int requestType = 0;
294         int reqId = 0;
295         int launchReqId = 0;
296         int providerRes = 0;
297         bool providerResult = true;
298         //bundle* origBundle = null;
299         bundle* resBundle = null;
300         _MapDataControlEventArg* pEventArg = null;
301         const char* p = null;
302         result r = E_SUCCESS;
303
304         SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
305         SysLog(NID_APP, "appsvc result value: %d", res);
306
307         resBundle = pResArg->GetBundle();
308         if (resBundle)
309         {
310                 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
311                 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
312                 reqId = atoi(p);
313                 Integer key(reqId);
314
315                 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
316                 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
317                 SysTryCatch(NID_APP, pReqInfo != null, r = E_SYSTEM, E_SYSTEM,
318                                 "[E_SYSTEM] Failed to get request info");
319
320                 _MapDataControlEvent* pMapDataControlEvent = dynamic_cast< _MapDataControlEvent* >(pReqInfo->GetEvent());
321                 SysTryCatch(NID_APP, pMapDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
322                                 "[E_SYSTEM] invalid request info");
323
324                 pDcMgr->RemoveRequestInfo(key);
325
326                 if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
327                 {
328
329                         // result list
330                         pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
331                         SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
332
333                         pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
334                         SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
335                         Integer::Parse(*pResult, providerRes);
336                         providerResult = static_cast< bool >(providerRes);
337
338                         pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
339                         SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
340                         pErrorMsg = new (std::nothrow) String(*pErrorMessage);
341                         SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
342                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
343
344                         // request info
345                         //origBundle = pArg->GetBundle();
346
347                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
348                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
349                         requestType = atoi(p);
350
351                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
352                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
353                         pProviderId = new (std::nothrow) String(p);
354                         SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
355                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
356
357                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
358                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
359                         pDataId = new (std::nothrow) String(p);
360                         SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
361                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
362
363                         version = pResArg->GetValue(OSP_K_DATACONTROL_PROTOCOL_VERSION);
364
365                         SysSecureLog(NID_APP, "[DC_CALLER_RECV] version: %ls, provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls",
366                                         version.GetPointer(), providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
367
368                         switch (static_cast< _DataControlRequestType >(requestType))
369                         {
370                         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
371                         {
372                                 pResultValueList = new (std::nothrow) ArrayList(SingleObjectDeleter);
373                                 SysTryCatch(NID_APP, pResultValueList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
374                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
375
376                                 if (providerResult == true)
377                                 {
378                                         pResultCount = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
379                                         SysTryCatch(NID_APP, pResultCount, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
380                                         Integer::Parse(*pResultCount, resultCount);
381                                         SysLog(NID_APP, "[DC_CALLER_RECV] result count: %d", resultCount);
382
383                                         if (resultCount > 0)
384                                         {
385                                                 if (version == L"ver_2.1.0.1" || version == L"ver_2.1.0.2")
386                                                 {
387                                                         pPath = dynamic_cast< String* >(pResultList->GetAt(3)); // result list[3]
388                                                         SysTryCatch(NID_APP, pPath, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
389                                                         SysLog(NID_APP, "[DC_CALLER_RECV] path: %ls", pPath->GetPointer());
390
391                                                         unique_ptr< File > pFile(new (std::nothrow) File());
392                                                         SysTryCatch(NID_APP, pFile, delete pResultValueList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
393
394                                                         r = pFile->Construct(*pPath, "r");
395                                                         SysTryCatch(NID_APP, !IsFailed(r), delete pResultValueList, r, "[%s] Failed to open result set (%ls)",
396                                                                         GetErrorMessage(r), pPath->GetPointer());
397
398                                                         while (resultCount)
399                                                         {
400                                                                 int length = 0;
401                                                                 int ret = pFile->Read(&length, sizeof(int));
402                                                                 SysTryCatch(NID_APP, ret, delete pResultValueList, E_SYSTEM,
403                                                                                 "[E_SYSTEM] Failed to read data from the result set of data control provider.");
404
405                                                                 char* pValue = new (std::nothrow) char[length + 1];
406                                                                 SysTryCatch(NID_APP, pValue, delete pResultValueList, E_OUT_OF_MEMORY,
407                                                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
408
409                                                                 ret = pFile->Read(pValue, length);
410                                                                 if (ret == 0)
411                                                                 {
412                                                                         SysLogException(NID_APP, E_SYSTEM,
413                                                                                         "[E_SYSTEM] Failed to read data from the result set of data control provider.");
414                                                                         delete pResultValueList;
415                                                                         delete[] pValue;
416                                                                         goto CATCH;
417                                                                 }
418                                                                 pValue[length] = '\0';
419
420                                                                 pResultValueList->Add(new (std::nothrow) String(pValue));
421                                                                 delete[] pValue;
422                                                                 --resultCount;
423                                                         }
424                                                 }
425                                                 else
426                                                 {
427                                                         int index = 3;
428                                                         while (resultCount)
429                                                         {
430                                                                 String* pValue = dynamic_cast< String* >(pResultList->GetAt(index)); // result list[3] ~
431                                                                 SysTryCatch(NID_APP, pValue, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
432
433                                                                 pResultValueList->Add((new (std::nothrow) String(*pValue)));
434                                                                 --resultCount;
435                                                                 ++index;
436                                                         }
437                                                 }
438                                         }
439
440                                         if (pPath)
441                                         {
442                                                 r = File::Remove(*pPath);
443                                                 SysTryLog(NID_APP, !IsFailed(r), "Failed to remove result: %ls", pPath->GetPointer());
444                                         }
445                                 }
446
447                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, static_cast <RequestId>(reqId),
448                                                 *pProviderId, *pDataId, pResultValueList, providerResult, pErrorMsg);
449                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
450                                 pMapDataControlEvent->Fire(*pEventArg);
451
452                                 break;
453                         }
454                         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
455                         {
456                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, static_cast <RequestId>(reqId),
457                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
458                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
459                                 pMapDataControlEvent->Fire(*pEventArg);
460                                 break;
461                         }
462                         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
463                         {
464                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, static_cast <RequestId>(reqId),
465                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
466                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
467                                 pMapDataControlEvent->Fire(*pEventArg);
468                                 break;
469                         }
470                         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
471                         {
472                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, static_cast <RequestId>(reqId),
473                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
474                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
475                                 pMapDataControlEvent->Fire(*pEventArg);
476                                 break;
477                         }
478                         default:
479                                 break;
480                         }
481
482                         pResultList->RemoveAll(true);
483                         delete pResultList;
484                         delete pProviderId;
485                 }
486         }
487
488         p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
489         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
490         launchReqId = atoi(p);
491
492         // Remove the request count
493         SysLog(NID_APP, "Remove a launch request: req: %d", launchReqId);
494         _AppControlManager::GetInstance()->RemoveLaunchRequest(launchReqId);
495
496         return E_SUCCESS;
497
498 CATCH:
499         if (pResultList)
500         {
501                 pResultList->RemoveAll(true);
502                 delete pResultList;
503         }
504         delete pProviderId;
505
506         return r;
507 }
508
509 result
510 _MapDataControlImpl::AddValue(const String& dataId, const String& key,
511                 const String& value, RequestId& reqId)
512 {
513         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
514                         "The AddValue query is not permitted by DataControl provider.");
515
516         int id = 0;
517         result r = E_SUCCESS;
518
519         ArrayList* pArgList = null;
520         pArgList = new ArrayList();
521         pArgList->Construct();
522
523         pArgList->Add(*(new String(dataId)));
524         pArgList->Add(*(new String(key)));
525         pArgList->Add(*(new String(value)));
526         long long argSize = dataId.GetLength() * sizeof(wchar_t);
527         argSize += key.GetLength() * sizeof(wchar_t);
528         argSize += value.GetLength() * sizeof(wchar_t);
529         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
530                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
531
532         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
533         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
534
535         reqId = static_cast< RequestId >(id);
536
537         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, value: %ls, req: %d",
538                         dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
539
540         // fall through
541 CATCH:
542         pArgList->RemoveAll(true);
543         delete pArgList;
544
545         return r;
546 }
547
548 result
549 _MapDataControlImpl::SetValue(const String& dataId, const String& key,
550                 const String& oldValue, const String& newValue, RequestId& reqId)
551 {
552         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
553                         "The SetValue query is not permitted by DataControl provider.");
554
555         int id = 0;
556         result r = E_SUCCESS;
557
558         ArrayList* pArgList = null;
559         pArgList = new ArrayList();
560         pArgList->Construct();
561
562         pArgList->Add(*(new String(dataId)));
563         pArgList->Add(*(new String(key)));
564         pArgList->Add(*(new String(oldValue)));
565         pArgList->Add(*(new String(newValue)));
566         long long argSize = dataId.GetLength() * sizeof(wchar_t);
567         argSize += key.GetLength() * sizeof(wchar_t);
568         argSize += oldValue.GetLength() * sizeof(wchar_t);
569         argSize += newValue.GetLength() * sizeof(wchar_t);
570         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
571                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
572
573         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
574         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
575
576         reqId = static_cast< RequestId >(id);
577
578         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, oldValue: %ls, newValue: %ls, req: %d",
579                         dataId.GetPointer(), key.GetPointer(), oldValue.GetPointer(), newValue.GetPointer(), reqId);
580
581         // fall through
582 CATCH:
583         pArgList->RemoveAll(true);
584         delete pArgList;
585
586         return r;
587 }
588
589 result
590 _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
591                 const String& value, RequestId& reqId)
592 {
593         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
594                         "The RemoveValue query is not permitted by DataControl provider.");
595
596         int id = 0;
597         result r = E_SUCCESS;
598
599         ArrayList* pArgList = null;
600         pArgList = new ArrayList();
601         pArgList->Construct();
602
603         pArgList->Add(*(new String(dataId)));
604         pArgList->Add(*(new String(key)));
605         pArgList->Add(*(new String(value)));
606         long long argSize = dataId.GetLength() * sizeof(wchar_t);
607         argSize += key.GetLength() * sizeof(wchar_t);
608         argSize += value.GetLength() * sizeof(wchar_t);
609         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
610                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
611
612         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
613         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
614
615         reqId = static_cast< RequestId >(id);
616
617         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, value: %ls, req: %d",
618                         dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
619
620         // fall through
621 CATCH:
622         pArgList->RemoveAll(true);
623         delete pArgList;
624
625         return r;
626 }
627
628 result
629 _MapDataControlImpl::SetMapDataControlResponseListener(IMapDataControlResponseListener* pListener)
630 {
631         result r = E_SUCCESS;
632
633         if (__pPreviousListener != null)
634         {
635                 r =  __pMapDataControlEvent->RemoveListener(*__pPreviousListener);
636                 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
637                  __pPreviousListener = null;
638         }
639
640         if (pListener != null)
641         {
642                 r =  __pMapDataControlEvent->AddListener(*pListener);
643                 if (IsFailed(r))
644                 {
645                         switch (r)
646                         {
647                         case E_OBJ_ALREADY_EXIST:
648                                 return E_SUCCESS;
649                         case E_INVALID_OPERATION:
650                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
651                                 return E_SYSTEM;
652                         default:
653                                 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
654                                 return r;
655                         }
656                 }
657         }
658
659         __pPreviousListener = pListener;
660
661         return E_SUCCESS;
662 }
663
664 // private
665 MapDataControl*
666 _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& providerId, const String& access)
667 {
668         unique_ptr<MapDataControl> pDc(new (std::nothrow) MapDataControl);
669         SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
670
671         _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
672         unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
673         SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
674
675         pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
676         pDcImpl->__appId = appId;
677         pDcImpl->__providerId = providerId;
678
679         if (access == L"readonly")
680         {
681                 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
682         }
683         else if (access == L"writeonly")
684         {
685                 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
686         }
687         else if (access == L"readwrite")
688         {
689                 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
690         }
691         else
692         {
693                 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
694                 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
695         }
696
697         return pDc.release();
698 }
699
700 }} // Tizen::App
701