Fix the boiler plate codes
[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                         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] data: %ls, key: %ls, req: %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, int option)
282 {
283         ArrayList* pResultList = null;
284         String version;
285         String* pResult = null;
286         String* pProviderId = null;
287         String* pDataId = null;
288         String* pErrorMessage = null;
289         String* pErrorMsg = null;
290         String* pResultCount = null;
291         String* pPath = null;
292         ArrayList* pResultValueList = null;
293         int resultCount = 0;
294         int requestType = 0;
295         int reqId = 0;
296         int launchReqId = 0;
297         int providerRes = 0;
298         bool providerResult = true;
299         //bundle* origBundle = null;
300         bundle* resBundle = null;
301         _MapDataControlEventArg* pEventArg = null;
302         const char* p = null;
303         result r = E_SUCCESS;
304
305         SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
306         SysLog(NID_APP, "appsvc result value: %d", res);
307
308         resBundle = pResArg->GetBundle();
309         if (resBundle)
310         {
311                 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
312                 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
313                 reqId = atoi(p);
314                 Integer key(reqId);
315
316                 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
317                 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
318                 SysTryCatch(NID_APP, pReqInfo != null, r = E_SYSTEM, E_SYSTEM,
319                                 "[E_SYSTEM] Failed to get request info");
320
321                 _MapDataControlEvent* pMapDataControlEvent = dynamic_cast< _MapDataControlEvent* >(pReqInfo->GetEvent());
322                 SysTryCatch(NID_APP, pMapDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
323                                 "[E_SYSTEM] invalid request info");
324
325                 pDcMgr->RemoveRequestInfo(key);
326
327                 if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
328                 {
329
330                         // result list
331                         pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
332                         SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
333
334                         pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
335                         SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
336                         Integer::Parse(*pResult, providerRes);
337                         providerResult = static_cast< bool >(providerRes);
338
339                         pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
340                         SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
341                         pErrorMsg = new (std::nothrow) String(*pErrorMessage);
342                         SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
343                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
344
345                         // request info
346                         //origBundle = pArg->GetBundle();
347
348                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
349                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
350                         requestType = atoi(p);
351
352                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
353                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
354                         pProviderId = new (std::nothrow) String(p);
355                         SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
356                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
357
358                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
359                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
360                         pDataId = new (std::nothrow) String(p);
361                         SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
362                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
363
364                         version = pResArg->GetValue(OSP_K_DATACONTROL_PROTOCOL_VERSION);
365
366                         SysSecureLog(NID_APP, "[DC_CALLER_RECV] version: %ls, provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls",
367                                         version.GetPointer(), providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
368
369                         switch (static_cast< _DataControlRequestType >(requestType))
370                         {
371                         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
372                         {
373                                 pResultValueList = new (std::nothrow) ArrayList(SingleObjectDeleter);
374                                 SysTryCatch(NID_APP, pResultValueList, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
375                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
376
377                                 if (providerResult == true)
378                                 {
379                                         pResultCount = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
380                                         SysTryCatch(NID_APP, pResultCount, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
381                                         Integer::Parse(*pResultCount, resultCount);
382                                         SysLog(NID_APP, "[DC_CALLER_RECV] result count: %d", resultCount);
383
384                                         if (resultCount > 0)
385                                         {
386                                                 if (version == L"ver_2.1.0.1" || version == L"ver_2.1.0.2")
387                                                 {
388                                                         pPath = dynamic_cast< String* >(pResultList->GetAt(3)); // result list[3]
389                                                         SysTryCatch(NID_APP, pPath, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
390                                                         SysLog(NID_APP, "[DC_CALLER_RECV] path: %ls", pPath->GetPointer());
391
392                                                         unique_ptr< File > pFile(new (std::nothrow) File());
393                                                         SysTryCatch(NID_APP, pFile, delete pResultValueList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
394
395                                                         r = pFile->Construct(*pPath, "r");
396                                                         SysTryCatch(NID_APP, !IsFailed(r), delete pResultValueList, r, "[%s] Failed to open result set (%ls)",
397                                                                         GetErrorMessage(r), pPath->GetPointer());
398
399                                                         while (resultCount)
400                                                         {
401                                                                 int length = 0;
402                                                                 int ret = pFile->Read(&length, sizeof(int));
403                                                                 SysTryCatch(NID_APP, ret, delete pResultValueList, E_SYSTEM,
404                                                                                 "[E_SYSTEM] Failed to read data from the result set of data control provider.");
405
406                                                                 char* pValue = new (std::nothrow) char[length + 1];
407                                                                 SysTryCatch(NID_APP, pValue, delete pResultValueList, E_OUT_OF_MEMORY,
408                                                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
409
410                                                                 ret = pFile->Read(pValue, length);
411                                                                 if (ret == 0)
412                                                                 {
413                                                                         SysLogException(NID_APP, E_SYSTEM,
414                                                                                         "[E_SYSTEM] Failed to read data from the result set of data control provider.");
415                                                                         delete pResultValueList;
416                                                                         delete[] pValue;
417                                                                         goto CATCH;
418                                                                 }
419                                                                 pValue[length] = '\0';
420
421                                                                 pResultValueList->Add(new (std::nothrow) String(pValue));
422                                                                 delete[] pValue;
423                                                                 --resultCount;
424                                                         }
425                                                 }
426                                                 else
427                                                 {
428                                                         int index = 3;
429                                                         while (resultCount)
430                                                         {
431                                                                 String* pValue = dynamic_cast< String* >(pResultList->GetAt(index)); // result list[3] ~
432                                                                 SysTryCatch(NID_APP, pValue, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
433
434                                                                 pResultValueList->Add((new (std::nothrow) String(*pValue)));
435                                                                 --resultCount;
436                                                                 ++index;
437                                                         }
438                                                 }
439                                         }
440
441                                         if (pPath)
442                                         {
443                                                 r = File::Remove(*pPath);
444                                                 SysTryLog(NID_APP, !IsFailed(r), "Failed to remove result: %ls", pPath->GetPointer());
445                                         }
446                                 }
447
448                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_QUERY, static_cast <RequestId>(reqId),
449                                                 *pProviderId, *pDataId, pResultValueList, providerResult, pErrorMsg);
450                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
451                                 pMapDataControlEvent->Fire(*pEventArg);
452
453                                 break;
454                         }
455                         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
456                         {
457                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, static_cast <RequestId>(reqId),
458                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
459                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
460                                 pMapDataControlEvent->Fire(*pEventArg);
461                                 break;
462                         }
463                         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
464                         {
465                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, static_cast <RequestId>(reqId),
466                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
467                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
468                                 pMapDataControlEvent->Fire(*pEventArg);
469                                 break;
470                         }
471                         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
472                         {
473                                 pEventArg = new (std::nothrow) _MapDataControlEventArg(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, static_cast <RequestId>(reqId),
474                                                 *pProviderId, *pDataId, null, providerResult, pErrorMsg);
475                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
476                                 pMapDataControlEvent->Fire(*pEventArg);
477                                 break;
478                         }
479                         default:
480                                 break;
481                         }
482
483                         pResultList->RemoveAll(true);
484                         delete pResultList;
485                         delete pProviderId;
486                 }
487         }
488
489         p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
490         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
491         launchReqId = atoi(p);
492
493         // Remove the request count
494         SysLog(NID_APP, "Remove a launch request: req: %d", launchReqId);
495         _AppControlManager::GetInstance()->RemoveLaunchRequest(launchReqId);
496
497         return E_SUCCESS;
498
499 CATCH:
500         if (pResultList)
501         {
502                 pResultList->RemoveAll(true);
503                 delete pResultList;
504         }
505         delete pProviderId;
506
507         return r;
508 }
509
510 result
511 _MapDataControlImpl::AddValue(const String& dataId, const String& key,
512                 const String& value, RequestId& reqId)
513 {
514         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
515                         "The AddValue query is not permitted by DataControl provider.");
516
517         int id = 0;
518         result r = E_SUCCESS;
519
520         ArrayList* pArgList = null;
521         pArgList = new ArrayList();
522         pArgList->Construct();
523
524         pArgList->Add(*(new String(dataId)));
525         pArgList->Add(*(new String(key)));
526         pArgList->Add(*(new String(value)));
527         long long argSize = dataId.GetLength() * sizeof(wchar_t);
528         argSize += key.GetLength() * sizeof(wchar_t);
529         argSize += value.GetLength() * sizeof(wchar_t);
530         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
531                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
532
533         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
534         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
535
536         reqId = static_cast< RequestId >(id);
537
538         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, value: %ls, req: %d",
539                         dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
540
541         // fall through
542 CATCH:
543         pArgList->RemoveAll(true);
544         delete pArgList;
545
546         return r;
547 }
548
549 result
550 _MapDataControlImpl::SetValue(const String& dataId, const String& key,
551                 const String& oldValue, const String& newValue, RequestId& reqId)
552 {
553         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
554                         "The SetValue query is not permitted by DataControl provider.");
555
556         int id = 0;
557         result r = E_SUCCESS;
558
559         ArrayList* pArgList = null;
560         pArgList = new ArrayList();
561         pArgList->Construct();
562
563         pArgList->Add(*(new String(dataId)));
564         pArgList->Add(*(new String(key)));
565         pArgList->Add(*(new String(oldValue)));
566         pArgList->Add(*(new String(newValue)));
567         long long argSize = dataId.GetLength() * sizeof(wchar_t);
568         argSize += key.GetLength() * sizeof(wchar_t);
569         argSize += oldValue.GetLength() * sizeof(wchar_t);
570         argSize += newValue.GetLength() * sizeof(wchar_t);
571         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
572                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
573
574         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
575         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
576
577         reqId = static_cast< RequestId >(id);
578
579         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, oldValue: %ls, newValue: %ls, req: %d",
580                         dataId.GetPointer(), key.GetPointer(), oldValue.GetPointer(), newValue.GetPointer(), reqId);
581
582         // fall through
583 CATCH:
584         pArgList->RemoveAll(true);
585         delete pArgList;
586
587         return r;
588 }
589
590 result
591 _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
592                 const String& value, RequestId& reqId)
593 {
594         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
595                         "The RemoveValue query is not permitted by DataControl provider.");
596
597         int id = 0;
598         result r = E_SUCCESS;
599
600         ArrayList* pArgList = null;
601         pArgList = new ArrayList();
602         pArgList->Construct();
603
604         pArgList->Add(*(new String(dataId)));
605         pArgList->Add(*(new String(key)));
606         pArgList->Add(*(new String(value)));
607         long long argSize = dataId.GetLength() * sizeof(wchar_t);
608         argSize += key.GetLength() * sizeof(wchar_t);
609         argSize += value.GetLength() * sizeof(wchar_t);
610         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
611                         "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
612
613         r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
614         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
615
616         reqId = static_cast< RequestId >(id);
617
618         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, key: %ls, value: %ls, req: %d",
619                         dataId.GetPointer(), key.GetPointer(), value.GetPointer(), reqId);
620
621         // fall through
622 CATCH:
623         pArgList->RemoveAll(true);
624         delete pArgList;
625
626         return r;
627 }
628
629 result
630 _MapDataControlImpl::SetMapDataControlResponseListener(IMapDataControlResponseListener* pListener)
631 {
632         result r = E_SUCCESS;
633
634         if (__pPreviousListener != null)
635         {
636                 r =  __pMapDataControlEvent->RemoveListener(*__pPreviousListener);
637                 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
638                  __pPreviousListener = null;
639         }
640
641         if (pListener != null)
642         {
643                 r =  __pMapDataControlEvent->AddListener(*pListener);
644                 if (IsFailed(r))
645                 {
646                         switch (r)
647                         {
648                         case E_OBJ_ALREADY_EXIST:
649                                 return E_SUCCESS;
650                         case E_INVALID_OPERATION:
651                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
652                                 return E_SYSTEM;
653                         default:
654                                 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
655                                 return r;
656                         }
657                 }
658         }
659
660         __pPreviousListener = pListener;
661
662         return E_SUCCESS;
663 }
664
665 // private
666 MapDataControl*
667 _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& providerId, const String& access)
668 {
669         unique_ptr<MapDataControl> pDc(new (std::nothrow) MapDataControl);
670         SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
671
672         _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
673         unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
674         SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
675
676         pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
677         pDcImpl->__appId = appId;
678         pDcImpl->__providerId = providerId;
679
680         if (access == L"readonly")
681         {
682                 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
683         }
684         else if (access == L"writeonly")
685         {
686                 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
687         }
688         else if (access == L"readwrite")
689         {
690                 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
691         }
692         else
693         {
694                 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
695                 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
696         }
697
698         return pDc.release();
699 }
700
701 }} // Tizen::App
702