Merge "Add pointer check" into tizen_2.1
[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 <unique_ptr.h>
24
25 #include <appsvc/appsvc.h>
26
27 #include <FBaseInteger.h>
28 #include <FBaseString.h>
29 #include <FBaseRtIEventArg.h>
30 #include <FIoFile.h>
31 #include <FAppMapDataControl.h>
32 #include <FAppIMapDataControlResponseListener.h>
33
34 #include <FBaseSysLog.h>
35
36 #include "FApp_AppControlManager.h"
37 #include "FApp_MapDataControlImpl.h"
38 #include "FApp_AppArg.h"
39 #include "FApp_DataControlManager.h"
40
41 using namespace std;
42
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Base::Runtime;
46 using namespace Tizen::App;
47 using namespace Tizen::Io;
48
49 namespace Tizen { namespace App
50 {
51
52 static const int MAX_REQUEST_COUNT = 128;
53 static const int REQUEST_THRESHOLD = 100;
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         : __access(_DATACONTROL_ACCESS_UNDEFINED)
134         , __pPreviousListener(null)
135         , __pMapDataControlEvent(null)
136 {
137 }
138
139 _MapDataControlImpl::~_MapDataControlImpl(void)
140 {
141         delete __pMapDataControlEvent;
142 }
143
144 _MapDataControlImpl*
145 _MapDataControlImpl::GetInstance(MapDataControl& dc)
146 {
147         return dc.__pMapDataControlImpl;
148 }
149
150 const _MapDataControlImpl*
151 _MapDataControlImpl::GetInstance(const MapDataControl& dc)
152 {
153         return dc.__pMapDataControlImpl;
154 }
155
156 result
157 _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int* pReq)
158 {
159         Integer* pReqId = null;
160         _DataControlRequestInfo* pReqInfo = null;
161         result r = E_SUCCESS;
162
163         int req = -1;
164         _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
165
166         // Check the request count of DataControl operation
167         int count = pDcMgr->GetRequestCount();
168         SysLog(NID_APP, "Current launch request count: %d", count);
169
170         if (count > REQUEST_THRESHOLD)
171         {
172                 _AppManagerImpl* pImpl = _AppManagerImpl::GetInstance();
173
174                 // Clear the request queue if the provider is terminated
175                 if (!pImpl->IsRunning(__appId))
176                 {
177                         SysLog(NID_APP, "The request queue is cleared due to the invalid provider.");
178
179                         pDcMgr->RemoveAllRequests();
180                 }
181         }
182
183         SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
184
185         _AppArg* pArg = new(std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
186         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
187
188         pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
189
190         _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
191
192         if (__pMapDataControlEvent)
193         {
194                 // reqId is system-wide id because the bundle is system-wide.
195 #if 0
196                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, MapDataControlCallback, __pMapDataControlEvent, -1);
197                 req = reqObj.GetRequestNumber();
198 #else
199                 _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
200                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, MapDataControlCallback, pDcMgr, -1);
201                 req = reqObj.GetRequestNumber();
202 #endif
203                 pReqId = new (std::nothrow) Integer(req);
204                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
205                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
206
207                 pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pMapDataControlEvent);
208                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
209                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
210
211                 r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
212                 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
213
214                 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
215                 if (IsFailed(r))
216                 {
217                         SysPropagate(NID_APP, r);
218                         reqObj.Invalidate();
219                         pDcMgr->RemoveRequestInfo(*pReqId);
220                         return r;
221                 }
222
223                 if (pReq)
224                 {
225                         *pReq = req;
226                 }
227         }
228         else
229         {
230                 r = pAppManagerImpl->LaunchApp(__appId, pArg);
231                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
232                 delete pArg;
233         }
234
235         return E_SUCCESS;
236
237 CATCH:
238         delete pArg;
239         delete pReqId;
240         delete pReqInfo;
241         return r;
242 }
243
244 result
245 _MapDataControlImpl::GetValue(const String& dataId, const String& key,
246                 RequestId& reqId, int pageNo, int countPerPage)
247 {
248         SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
249         SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
250         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
251                         "The GetValue query is not permitted by DataControl provider.");
252
253         String* pPageNo = null;
254         String* pCountPerPage = null;
255         int id = 0;
256         result r = E_SUCCESS;
257
258         ArrayList* pArgList = new ArrayList();
259         pArgList->Construct();
260
261         pArgList->Add(*(new String(dataId)));
262         pArgList->Add(*(new String(key)));
263         long long argSize = dataId.GetLength() * sizeof(wchar_t);
264         argSize += key.GetLength() * sizeof(wchar_t);
265         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
266                         "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
267
268         pPageNo = new (std::nothrow) String();
269         pPageNo->Append(pageNo);
270         pArgList->Add(*pPageNo);
271
272         pCountPerPage = new (std::nothrow) String();
273         pCountPerPage->Append(countPerPage);
274         pArgList->Add(*pCountPerPage);
275
276         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, pArgList, &id);
277         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
278
279         reqId = static_cast< RequestId >(id);
280
281         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, req: %d, pageNo: %d, countPerPage: %d",
282                         dataId.GetPointer(), key.GetPointer(), reqId, pageNo, countPerPage);
283
284         // fall through
285 CATCH:
286         pArgList->RemoveAll(true);
287         delete pArgList;
288
289         return r;
290 }
291
292 result
293 _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int option)
294 {
295         ArrayList* pResultList = null;
296         String version;
297         String* pResult = null;
298         String* pProviderId = null;
299         String* pDataId = null;
300         String* pErrorMessage = null;
301         String* pErrorMsg = null;
302         String* pResultCount = null;
303         String* pPath = null;
304         ArrayList* pResultValueList = null;
305         int resultCount = 0;
306         int requestType = 0;
307         int reqId = 0;
308         int launchReqId = 0;
309         int providerRes = 0;
310         bool providerResult = true;
311         //bundle* origBundle = null;
312         bundle* resBundle = null;
313         _MapDataControlEventArg* pEventArg = null;
314         const char* p = null;
315         result r = E_SUCCESS;
316
317         SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
318         SysLog(NID_APP, "appsvc result value: %d", res);
319
320         resBundle = pResArg->GetBundle();
321         if (resBundle)
322         {
323                 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
324                 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
325                 reqId = atoi(p);
326                 Integer key(reqId);
327
328                 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
329                 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
330                 if (pReqInfo == null)
331                 {
332                         SysLog(NID_APP, "No request info of reqId %d", reqId);
333                         return E_SUCCESS;
334                 }
335
336                 _MapDataControlEvent* pMapDataControlEvent = dynamic_cast< _MapDataControlEvent* >(pReqInfo->GetEvent());
337                 SysTryCatch(NID_APP, pMapDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
338                                 "[E_SYSTEM] invalid request info");
339
340                 // Remove the request from the queue
341                 SysLog(NID_APP, "Remove the request, req: %d", reqId);
342
343                 pDcMgr->RemoveRequestInfo(key);
344
345                 if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
346                 {
347
348                         // result list
349                         pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
350                         SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
351
352                         pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
353                         SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
354                         Integer::Parse(*pResult, providerRes);
355                         providerResult = static_cast< bool >(providerRes);
356
357                         pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
358                         SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
359                         pErrorMsg = new (std::nothrow) String(*pErrorMessage);
360                         SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
361                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
362
363                         // request info
364                         //origBundle = pArg->GetBundle();
365
366                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
367                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
368                         requestType = atoi(p);
369
370                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
371                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
372                         pProviderId = new (std::nothrow) String(p);
373                         SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
374                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
375
376                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
377                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
378                         pDataId = new (std::nothrow) String(p);
379                         SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
380                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
381
382                         version = pResArg->GetValue(OSP_K_DATACONTROL_PROTOCOL_VERSION);
383
384                         SysSecureLog(NID_APP, "[DC_CALLER_RECV] version: %ls, provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls",
385                                         version.GetPointer(), providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
386
387                         switch (static_cast< _DataControlRequestType >(requestType))
388                         {
389                         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
390                         {
391                                 pResultValueList = new (std::nothrow) ArrayList(SingleObjectDeleter);
392                                 SysTryCatch(NID_APP, pResultValueList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
393                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
394
395                                 if (providerResult == true)
396                                 {
397                                         pResultCount = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
398                                         SysTryCatch(NID_APP, pResultCount, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
399                                         Integer::Parse(*pResultCount, resultCount);
400                                         SysLog(NID_APP, "[DC_CALLER_RECV] result count: %d", resultCount);
401
402                                         if (resultCount > 0)
403                                         {
404                                                 if (version == L"ver_2.1.0.1" || version == L"ver_2.1.0.2" || version == L"ver_2.1.0.3")
405                                                 {
406                                                         pPath = dynamic_cast< String* >(pResultList->GetAt(3)); // result list[3]
407                                                         SysTryCatch(NID_APP, pPath, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
408                                                         SysLog(NID_APP, "[DC_CALLER_RECV] path: %ls", pPath->GetPointer());
409
410                                                         unique_ptr< File > pFile(new (std::nothrow) File());
411                                                         SysTryCatch(NID_APP, pFile, delete pResultValueList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
412
413                                                         r = pFile->Construct(*pPath, "r");
414                                                         SysTryCatch(NID_APP, !IsFailed(r), delete pResultValueList, r, "[%s] Failed to open result set (%ls)",
415                                                                         GetErrorMessage(r), pPath->GetPointer());
416
417                                                         while (resultCount)
418                                                         {
419                                                                 int length = 0;
420                                                                 int ret = pFile->Read(&length, sizeof(int));
421                                                                 SysTryCatch(NID_APP, ret, delete pResultValueList, E_SYSTEM,
422                                                                                 "[E_SYSTEM] Failed to read data from the result set of data control provider.");
423
424                                                                 char* pValue = new (std::nothrow) char[length + 1];
425                                                                 SysTryCatch(NID_APP, pValue, delete pResultValueList, E_OUT_OF_MEMORY,
426                                                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
427
428                                                                 ret = pFile->Read(pValue, length);
429                                                                 if (ret == 0)
430                                                                 {
431                                                                         SysLogException(NID_APP, E_SYSTEM,
432                                                                                         "[E_SYSTEM] Failed to read data from the result set of data control provider.");
433                                                                         delete pResultValueList;
434                                                                         delete[] pValue;
435                                                                         goto CATCH;
436                                                                 }
437                                                                 pValue[length] = '\0';
438
439                                                                 pResultValueList->Add(new (std::nothrow) String(pValue));
440                                                                 delete[] pValue;
441                                                                 --resultCount;
442                                                         }
443                                                 }
444                                                 else
445                                                 {
446                                                         int index = 3;
447                                                         while (resultCount)
448                                                         {
449                                                                 String* pValue = dynamic_cast< String* >(pResultList->GetAt(index)); // result list[3] ~
450                                                                 SysTryCatch(NID_APP, pValue, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
451
452                                                                 pResultValueList->Add((new (std::nothrow) String(*pValue)));
453                                                                 --resultCount;
454                                                                 ++index;
455                                                         }
456                                                 }
457                                         }
458
459                                         if (pPath)
460                                         {
461                                                 r = File::Remove(*pPath);
462                                                 SysTryLog(NID_APP, !IsFailed(r), "Failed to remove result: %ls", pPath->GetPointer());
463                                         }
464                                 }
465
466                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, static_cast <RequestId>(reqId),
467                                                 *pProviderId, *pDataId, pResultValueList, providerResult, pErrorMsg);
468                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
469                                 pMapDataControlEvent->Fire(*pEventArg);
470
471                                 break;
472                         }
473                         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
474                         {
475                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, static_cast <RequestId>(reqId),
476                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
477                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
478                                 pMapDataControlEvent->Fire(*pEventArg);
479                                 break;
480                         }
481                         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
482                         {
483                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, static_cast <RequestId>(reqId),
484                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
485                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
486                                 pMapDataControlEvent->Fire(*pEventArg);
487                                 break;
488                         }
489                         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
490                         {
491                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, static_cast <RequestId>(reqId),
492                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
493                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
494                                 pMapDataControlEvent->Fire(*pEventArg);
495                                 break;
496                         }
497                         default:
498                                 break;
499                         }
500
501                         pResultList->RemoveAll(true);
502                         delete pResultList;
503                         delete pProviderId;
504                 }
505         }
506
507         p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
508         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
509         launchReqId = atoi(p);
510
511         return E_SUCCESS;
512
513 CATCH:
514         if (pResultList)
515         {
516                 pResultList->RemoveAll(true);
517                 delete pResultList;
518         }
519         delete pProviderId;
520
521         return r;
522 }
523
524 result
525 _MapDataControlImpl::AddValue(const String& dataId, const String& key,
526                 const String& value, RequestId& reqId)
527 {
528         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
529                         "The AddValue query is not permitted by DataControl provider.");
530
531         int id = 0;
532         result r = E_SUCCESS;
533
534         ArrayList* pArgList = null;
535         pArgList = new ArrayList();
536         pArgList->Construct();
537
538         pArgList->Add(*(new String(dataId)));
539         pArgList->Add(*(new String(key)));
540         pArgList->Add(*(new String(value)));
541         long long argSize = dataId.GetLength() * sizeof(wchar_t);
542         argSize += key.GetLength() * sizeof(wchar_t);
543         argSize += value.GetLength() * sizeof(wchar_t);
544         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
545                         "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
546
547         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
548         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
549
550         reqId = static_cast< RequestId >(id);
551
552         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, value: %ls, req: %d",
553                         dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
554
555         // fall through
556 CATCH:
557         pArgList->RemoveAll(true);
558         delete pArgList;
559
560         return r;
561 }
562
563 result
564 _MapDataControlImpl::SetValue(const String& dataId, const String& key,
565                 const String& oldValue, const String& newValue, RequestId& reqId)
566 {
567         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
568                         "The SetValue query is not permitted by DataControl provider.");
569
570         int id = 0;
571         result r = E_SUCCESS;
572
573         ArrayList* pArgList = null;
574         pArgList = new ArrayList();
575         pArgList->Construct();
576
577         pArgList->Add(*(new String(dataId)));
578         pArgList->Add(*(new String(key)));
579         pArgList->Add(*(new String(oldValue)));
580         pArgList->Add(*(new String(newValue)));
581         long long argSize = dataId.GetLength() * sizeof(wchar_t);
582         argSize += key.GetLength() * sizeof(wchar_t);
583         argSize += oldValue.GetLength() * sizeof(wchar_t);
584         argSize += newValue.GetLength() * sizeof(wchar_t);
585         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
586                         "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
587
588         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
589         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
590
591         reqId = static_cast< RequestId >(id);
592
593         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, oldValue: %ls, newValue: %ls, req: %d",
594                         dataId.GetPointer(), key.GetPointer(), oldValue.GetPointer(), newValue.GetPointer(), reqId);
595
596         // fall through
597 CATCH:
598         pArgList->RemoveAll(true);
599         delete pArgList;
600
601         return r;
602 }
603
604 result
605 _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
606                 const String& value, RequestId& reqId)
607 {
608         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
609                         "The RemoveValue query is not permitted by DataControl provider.");
610
611         int id = 0;
612         result r = E_SUCCESS;
613
614         ArrayList* pArgList = null;
615         pArgList = new ArrayList();
616         pArgList->Construct();
617
618         pArgList->Add(*(new String(dataId)));
619         pArgList->Add(*(new String(key)));
620         pArgList->Add(*(new String(value)));
621         long long argSize = dataId.GetLength() * sizeof(wchar_t);
622         argSize += key.GetLength() * sizeof(wchar_t);
623         argSize += value.GetLength() * sizeof(wchar_t);
624         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
625                         "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
626
627         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
628         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
629
630         reqId = static_cast< RequestId >(id);
631
632         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, value: %ls, req: %d",
633                         dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
634
635         // fall through
636 CATCH:
637         pArgList->RemoveAll(true);
638         delete pArgList;
639
640         return r;
641 }
642
643 result
644 _MapDataControlImpl::SetMapDataControlResponseListener(IMapDataControlResponseListener* pListener)
645 {
646         result r = E_SUCCESS;
647
648         if (__pPreviousListener != null)
649         {
650                 r =  __pMapDataControlEvent->RemoveListener(*__pPreviousListener);
651                 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
652                  __pPreviousListener = null;
653         }
654
655         if (pListener != null)
656         {
657                 r =  __pMapDataControlEvent->AddListener(*pListener);
658                 if (IsFailed(r))
659                 {
660                         switch (r)
661                         {
662                         case E_OBJ_ALREADY_EXIST:
663                                 return E_SUCCESS;
664                         case E_INVALID_OPERATION:
665                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
666                                 return E_SYSTEM;
667                         default:
668                                 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
669                                 return r;
670                         }
671                 }
672         }
673
674         __pPreviousListener = pListener;
675
676         return E_SUCCESS;
677 }
678
679 // private
680 MapDataControl*
681 _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& providerId, const String& access)
682 {
683         unique_ptr<MapDataControl> pDc(new (std::nothrow) MapDataControl);
684         SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
685
686         _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
687         unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
688         SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
689
690         pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
691         pDcImpl->__appId = appId;
692         pDcImpl->__providerId = providerId;
693
694         if (access == L"readonly")
695         {
696                 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
697         }
698         else if (access == L"writeonly")
699         {
700                 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
701         }
702         else if (access == L"readwrite")
703         {
704                 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
705         }
706         else
707         {
708                 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
709                 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
710         }
711
712         return pDc.release();
713 }
714
715 }} // Tizen::App
716