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