task manager -> task switcher
[platform/framework/native/appfw.git] / src / app / FApp_SqlDataControlImpl.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_SqlDataControlImpl.cpp
19  * @brief       This is the implementation for the %_SqlDataControlImpl 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 <FBaseLongLong.h>
30 #include <FBaseColIList.h>
31 #include <FBaseColIMap.h>
32 #include <FBaseColIMapEnumerator.h>
33 #include <FBaseRtIEventArg.h>
34 #include <FBaseSysLog.h>
35 #include <FIoFile.h>
36 #include <FAppApp.h>
37 #include <FAppSqlDataControl.h>
38 #include <FAppISqlDataControlResponseListener.h>
39
40 #include <FBase_StringConverter.h>
41 #include <FIo_DataControlResultSetEnumerator.h>
42
43 #include "FApp_AppControlManager.h"
44 #include "FApp_SqlDataControlImpl.h"
45 #include "FApp_AppArg.h"
46 #include "FApp_DataControlManager.h"
47 #include "FAppPkg_PackageManagerImpl.h"
48
49 #define DATACONTROL_PROTOCOL_VER_2_1_0_2 // ver_2.1.0.2
50 #define DATACONTROL_PROTOCOL_VER_2_1_0_3 // ver_2.1.0.3
51
52 using namespace std;
53
54 using namespace Tizen::Base;
55 using namespace Tizen::Base::Collection;
56 using namespace Tizen::Base::Runtime;
57 using namespace Tizen::Io;
58 using namespace Tizen::App;
59 using namespace Tizen::App::Package;
60
61 namespace Tizen { namespace App
62 {
63
64 static const int MAX_REQUEST_COUNT = 128;
65 static const int REQUEST_THRESHOLD = 100;
66 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
67 static const int _MAX_REQUEST_ARGUMENT_SIZE = 1048576; // 1MB
68 //static const char* _DATACONTROL_REQUEST_DIR = "/tmp/osp/DataControlRequest/\0";
69 static const char* _DATACONTROL_REQUEST_DIR = "/tmp/osp/data-control/request/\0";
70
71 class _SqlDataControlEventArg
72         : public IEventArg
73 {
74 public:
75                 _SqlDataControlEventArg(_DataControlRequestType requestType, RequestId reqId, String providerId, String dataId,
76                                 _DataControlResultSetEnumerator* pResultSetEnum, long long insertRowId, bool providerResult, String* pErrorMsg)
77                         : __requestType(requestType)
78                         , __reqId(reqId)
79                         , __providerId(providerId)
80                         , __dataId(dataId)
81                         , __pResultSetEnum(pResultSetEnum)
82                         , __insertRowId(insertRowId)
83                         , __providerResult(providerResult)
84                         , __pErrorMsg(pErrorMsg)
85                 {
86                 }
87                 ~_SqlDataControlEventArg(void)
88                 {
89                         delete __pResultSetEnum;
90                         delete __pErrorMsg;
91                 }
92
93                 _DataControlRequestType __requestType;
94                 RequestId __reqId;
95                 String __providerId;
96                 String __dataId;
97                 _DataControlResultSetEnumerator* __pResultSetEnum;
98                 long long __insertRowId;
99                 bool __providerResult;
100                 String* __pErrorMsg;
101 };
102
103 class _SqlDataControlEvent
104         : public Event
105 {
106 protected:
107                 virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
108 };
109
110 void
111 _SqlDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
112 {
113         const _SqlDataControlEventArg* pArg = dynamic_cast<const _SqlDataControlEventArg*>(&arg);
114         if (pArg != null)
115         {
116                 ISqlDataControlResponseListener* pListener = dynamic_cast<ISqlDataControlResponseListener*> (&listener);
117                 if (pListener != null)
118                 {
119                         switch (pArg->__requestType)
120                         {
121                         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
122                                 pListener->OnSqlDataControlSelectResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
123                                                 *(pArg->__pResultSetEnum), pArg->__providerResult, pArg->__pErrorMsg);
124                                 break;
125                         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
126                                 pListener->OnSqlDataControlInsertResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
127                                                  pArg->__insertRowId, pArg->__providerResult, pArg->__pErrorMsg);
128                                 break;
129                         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
130                                 pListener->OnSqlDataControlUpdateResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
131                                                 pArg->__providerResult, pArg->__pErrorMsg);
132                                 break;
133                         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
134                                 pListener->OnSqlDataControlDeleteResponseReceived(pArg->__reqId, pArg->__providerId, pArg->__dataId,
135                                                 pArg->__providerResult, pArg->__pErrorMsg);
136                                 break;
137                         default:
138                                 break;
139                         }
140                 }
141         }
142 }
143
144 // private
145 _SqlDataControlImpl::_SqlDataControlImpl(void)
146         : __access(_DATACONTROL_ACCESS_UNDEFINED)
147         , __pPreviousListener(null)
148         , __pSqlDataControlEvent(null)
149 {
150 }
151
152 _SqlDataControlImpl::~_SqlDataControlImpl(void)
153 {
154         delete __pSqlDataControlEvent;
155 }
156
157 _SqlDataControlImpl*
158 _SqlDataControlImpl::GetInstance(SqlDataControl& dc)
159 {
160         return dc.__pSqlDataControlImpl;
161 }
162
163 const _SqlDataControlImpl*
164 _SqlDataControlImpl::GetInstance(const SqlDataControl& dc)
165 {
166         return dc.__pSqlDataControlImpl;
167 }
168
169 result
170 _SqlDataControlImpl::StartSqlDataControl(int type, const IList* pDataList, int* pReq)
171 {
172         Integer* pReqId = null;
173         _DataControlRequestInfo* pReqInfo = null;
174         result r = E_SUCCESS;
175
176         int req = -1;
177         _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
178
179         // Check the request count of DataControl operation
180         int count = pDcMgr->GetRequestCount();
181         SysLog(NID_APP, "Current launch request count: %d", count);
182
183         if (count > REQUEST_THRESHOLD)
184         {
185                 _AppManagerImpl* pImpl = _AppManagerImpl::GetInstance();
186
187                 // Clear the request queue if the provider is terminated
188                 if (!pImpl->IsRunning(__appId))
189                 {
190                         SysLog(NID_APP, "The request queue is cleared due to the invalid provider.");
191
192                         pDcMgr->RemoveAllRequests();
193                 }
194         }
195
196         SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
197
198         _AppArg* pArg = new (std::nothrow) _AppArg;  // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
199         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "insufficient memory");
200
201         pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
202
203         _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
204
205         if (__pSqlDataControlEvent)
206         {
207                 // reqId is system-wide id because the bundle is system-wide.
208 #if 0
209                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, __pSqlDataControlEvent, -1);
210                 req = reqObj.GetRequestNumber();
211 #else
212                 _AppControlManager::_RequestGuard reqObj = _AppControlManager::_RequestGuard(*pAppManagerImpl, pArg, SqlDataControlCallback, pDcMgr, -1);
213                 req = reqObj.GetRequestNumber();
214 #endif
215                 pReqId = new (std::nothrow) Integer(req);
216                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
217                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
218
219                 pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pSqlDataControlEvent);
220                 SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
221                                 "[E_OUT_OF_MEMORY] The memory is insufficient");
222
223                 r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
224                 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
225
226                 r = pAppManagerImpl->LaunchApp(__appId, pArg, req);
227                 if (IsFailed(r))
228                 {
229                         SysPropagate(NID_APP, r);
230                         reqObj.Invalidate();
231                         pDcMgr->RemoveRequestInfo(*pReqId);
232                         return r;
233                 }
234
235                 if (pReq)
236                 {
237                         *pReq = req;
238                 }
239         }
240         else
241         {
242                 r = pAppManagerImpl->LaunchApp(__appId, pArg);
243                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
244                 delete pArg;
245         }
246
247         return E_SUCCESS;
248
249 CATCH:
250         delete pArg;
251         delete pReqId;
252         delete pReqInfo;
253         return r;
254 }
255
256 result
257 _SqlDataControlImpl::Select(const String& dataId, const IList* pColumnList, const String* pWhere,
258                 const String *pOrder, RequestId& reqId, int pageNo, int countPerPage)
259 {
260         SysTryReturnResult(NID_APP, pageNo > 0, E_INVALID_ARG, "The specified pageNo parameter is less than 1");
261         SysTryReturnResult(NID_APP, countPerPage > 0, E_INVALID_ARG, "The specified countPerPage parameter is less than 1");
262         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_READ) > 0, E_ILLEGAL_ACCESS,
263                         "The SELECT query is not permitted by DataControl provider.");
264
265         const String* pColumn = null;
266         int id = 0;
267         result r = E_SUCCESS;
268         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl SELECT");
269
270         ArrayList* pArgList = new ArrayList();
271         pArgList->Construct();
272
273         pArgList->Add(*(new String(dataId))); // list(0): data ID
274         long long argSize = dataId.GetLength() * sizeof(wchar_t);
275
276         if (pColumnList != null)
277         {
278                 int columnCount = pColumnList->GetCount();
279                 SysTryCatch(NID_APP, columnCount > 0, r = E_INVALID_ARG, E_INVALID_ARG,
280                                 "[E_INVALID_ARG] The specified pColumnList parameter is empty.");
281
282                 pArgList->Add(*(new String(Integer::ToString(columnCount)))); // list(1): selected column count
283                 SysLog(NID_APP, "[DC_CALLER_SEND] selected column count: %d", columnCount);
284
285                 int i = 0;
286                 while (i < columnCount) // list(2): column list
287                 {
288                         pColumn = dynamic_cast< const String* >(pColumnList->GetAt(i));
289                         SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
290                                         "[E_INVALID_ARG] The object is not String class.");
291
292                         pArgList->Add(*(new String(*pColumn)));
293                         argSize += pColumn->GetLength() * sizeof(wchar_t);
294                         SysLog(NID_APP, "[DC_CALLER_SEND] column[%d]: %ls", i, pColumn->GetPointer());
295                         i++;
296                 }
297         }
298         else
299         {
300                 pArgList->Add(*(new String(L"NULL")));
301         }
302
303         if (pWhere != null)     // list(3): where clause
304         {
305                 pArgList->Add(*(new String(*pWhere)));
306                 argSize += pWhere->GetLength() * sizeof(wchar_t);
307                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
308         }
309         else
310         {
311                 pArgList->Add(*(new String(L"NULL")));
312         }
313
314         if (pOrder != null)     // list(4): order clause
315         {
316                 pArgList->Add(*(new String(*pOrder)));
317                 argSize += pOrder->GetLength() * sizeof(wchar_t);
318                 SysLog(NID_APP, "[DC_CALLER_SEND] pOrder: %ls", pOrder->GetPointer());
319         }
320         else
321         {
322                 pArgList->Add(*(new String(L"NULL")));
323         }
324         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
325                         "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
326
327         pArgList->Add(*(new String(Integer::ToString(pageNo)))); // list(5): page number
328
329         pArgList->Add(*(new String(Integer::ToString(countPerPage)))); // list(6): count per page
330
331         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, pArgList, &id);
332         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
333
334         reqId = static_cast< RequestId >(id);
335
336         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pColumnList: 0x%x, pWhere: 0x%x, pOrder: 0x%x, req: %d, pageNo: %d, countPerPage: %d",
337                         dataId.GetPointer(), pColumnList, pWhere, pOrder, reqId, pageNo, countPerPage);
338
339         // fall through
340 CATCH:
341         pArgList->RemoveAll(true);
342         delete pArgList;
343
344         return r;
345 }
346
347 result
348 _SqlDataControlImpl::SqlDataControlCallback(void* data, _AppArg* pArg, _AppArg* pResArg, service_result_e res, int prop, int option)
349 {
350         ArrayList* pResultList = null;
351         String* pResult = null;
352         String* pProviderId = null;
353         String* pDataId = null;
354         String* pErrorMessage = null;
355         String* pErrorMsg = null;
356         String* pTmpPath = null;
357         String* pInsertRowId = null;
358         _DataControlResultSetEnumerator* pResultSetEnum = null;
359         int requestType = 0;
360         int reqId = 0;
361         int launchReqId = 0;
362         int providerRes = 0;
363         bool providerResult = true;
364         //bundle* origBundle = null;
365         bundle* resBundle = null;
366         _SqlDataControlEventArg* pEventArg = null;
367         const char* p = null;
368         result r = E_SUCCESS;
369
370         SysTryReturnResult(NID_APP, pResArg != null, E_INVALID_ARG, "Empty result callback.");
371         SysLog(NID_APP, "appsvc result value: %d", res);
372
373         resBundle = pResArg->GetBundle();
374         if (resBundle)
375         {
376                 p = appsvc_get_data(resBundle, OSP_K_REQUEST_ID);
377                 SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
378                 reqId = atoi(p);
379                 Integer key(reqId);
380
381                 _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
382                 _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
383                 if (pReqInfo == null)
384                 {
385                         SysLog(NID_APP, "No request info of reqId %d", reqId);
386                         return E_SUCCESS;
387                 }
388
389                 _SqlDataControlEvent* pSqlDataControlEvent = dynamic_cast< _SqlDataControlEvent* >(pReqInfo->GetEvent());
390                 SysTryCatch(NID_APP, pSqlDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
391                                 "[E_SYSTEM] invalid request info");
392
393                 // Remove the request from the queue
394                 SysLog(NID_APP, "Remove the request, req: %d", reqId);
395
396                 pDcMgr->RemoveRequestInfo(key);
397
398                 if (pSqlDataControlEvent != null && typeid(pSqlDataControlEvent) == typeid(_SqlDataControlEvent*))
399                 {
400                         // result list
401                         pResultList = _AppArg::GetListN(resBundle, OSP_K_ARG);
402                         SysTryCatch(NID_APP, pResultList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
403
404                         pResult = dynamic_cast <String*>(pResultList->GetAt(0)); // result list[0]
405                         SysTryCatch(NID_APP, pResult, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
406                         Integer::Parse(*pResult, providerRes);
407                         providerResult = static_cast< bool >(providerRes);
408
409                         pErrorMessage = dynamic_cast< String* >(pResultList->GetAt(1)); // result list[1]
410                         SysTryCatch(NID_APP, pErrorMessage, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
411                         pErrorMsg = new (std::nothrow) String(*pErrorMessage);
412                         SysTryCatch(NID_APP, pErrorMsg, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
413                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
414
415                         // request info
416                         //origBundle = pArg->GetBundle();
417
418                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_REQUEST_TYPE);
419                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
420                         requestType = atoi(p);
421
422                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_PROVIDER);
423                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
424                         pProviderId = new (std::nothrow) String(p);
425                         SysTryCatch(NID_APP, pProviderId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
426                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
427
428                         p = appsvc_get_data(resBundle, OSP_K_DATACONTROL_DATA);
429                         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
430                         pDataId = new (std::nothrow) String(p);
431                         SysTryCatch(NID_APP, pDataId, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
432                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
433
434                         SysSecureLog(NID_APP, "[DC_CALLER_RECV] provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls ",
435                                         providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
436
437                         switch (static_cast <_DataControlRequestType>(requestType))
438                         {
439                         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
440                         {
441                                 pResultSetEnum = new (std::nothrow) _DataControlResultSetEnumerator;
442                                 SysTryCatch(NID_APP, pResultSetEnum, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
443                                                 "[E_OUT_OF_MEMORY] The memory is insufficient.");
444
445                                 if (providerResult == true)
446                                 {
447                                         pTmpPath = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
448                                         if (pTmpPath == null)
449                                         {
450                                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid result");
451                                                 delete pResultSetEnum;
452                                                 goto CATCH;
453                                         }
454
455                                         SysLog(NID_APP, "[DC_CALLER_RECV] tempPath: %ls", pTmpPath->GetPointer());
456                                         if (pTmpPath->Equals(L"NoResultSet", true) == false) // Result set exists
457                                         {
458                                                 pResultSetEnum->SetPath(*pTmpPath);
459                                         }
460                                 }
461
462                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_QUERY, static_cast <RequestId>(reqId),
463                                                 *pProviderId, *pDataId, pResultSetEnum, -1, providerResult, pErrorMsg);
464                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
465                                 pSqlDataControlEvent->Fire(*pEventArg);
466
467                                 if (pTmpPath)
468                                 {
469                                         r = File::Remove(*pTmpPath); // Remove temporary file after releasing _ResultSetEnumerator.
470                                         SysTryLog(NID_APP, !IsFailed(r), "Failed to remove temp file for result set: %ls", pTmpPath->GetPointer());
471                                 }
472
473                                 break;
474                         }
475                         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
476                         {
477                                 long long insertRowId = -1;
478                                 if (providerResult == true)
479                                 {
480                                         pInsertRowId = dynamic_cast< String* >(pResultList->GetAt(2)); // result list[2]
481                                         SysTryCatch(NID_APP, pInsertRowId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid result");
482
483                                         LongLong::Parse(*pInsertRowId, insertRowId);
484                                 }
485
486                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, static_cast <RequestId>(reqId),
487                                                 *pProviderId, *pDataId, null, insertRowId, providerResult, pErrorMsg);
488                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
489                                 pSqlDataControlEvent->Fire(*pEventArg);
490                                 break;
491                         }
492                         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
493                         {
494                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, static_cast <RequestId>(reqId),
495                                                 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
496                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
497                                 pSqlDataControlEvent->Fire(*pEventArg);
498                                 break;
499                         }
500                         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
501                         {
502                                 pEventArg = new (std::nothrow) _SqlDataControlEventArg(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, static_cast <RequestId>(reqId),
503                                                 *pProviderId, *pDataId, null, -1, providerResult, pErrorMsg);
504                                 SysTryCatch(NID_APP, pEventArg != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
505                                 pSqlDataControlEvent->Fire(*pEventArg);
506                                 break;
507                         }
508                         default:
509                                 break;
510                         }
511
512                         pResultList->RemoveAll(true);
513                         delete pResultList;
514                         delete pProviderId;
515                 }
516         }
517
518         p = appsvc_get_data(pArg->GetBundle(), OSP_K_REQUEST_ID);
519         SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
520         launchReqId = atoi(p);
521
522         return E_SUCCESS;
523
524 CATCH:
525         if (pResultList)
526         {
527                 pResultList->RemoveAll(true);
528                 delete pResultList;
529         }
530         delete pProviderId;
531
532         return r;
533 }
534
535 result
536 _SqlDataControlImpl::Insert(const String& dataId, const IMap& insertMap, RequestId& reqId)
537 {
538         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
539                         "The INSERT query is not permitted by DataControl provider.");
540
541         int columnCount = 0;
542         int id = 0;
543         int i = 0;
544         int uniqueId = -1;
545         File request;
546         IMapEnumerator* pMapEnum = null;
547         _PackageManagerImpl* pPkgMgrImpl = null;
548         unique_ptr< String > pProviderAppId(null);
549         result r = E_SUCCESS;
550         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl INSERT");
551
552         columnCount = insertMap.GetCount();
553         SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
554
555         ArrayList* pArgList = new (std::nothrow) ArrayList();
556         pArgList->Construct();
557
558         pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
559         long long argSize = dataId.GetLength() * sizeof(wchar_t);
560
561         pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: inserted column count
562         SysLog(NID_APP, "[DC_CALLER_SEND] inserted column count: %d", columnCount);
563
564 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
565         String tmpPath(_DATACONTROL_REQUEST_DIR);
566         tmpPath.Append(App::GetInstance()->GetAppId());
567
568         _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
569         SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
570         uniqueId = pDcMgr->GetUniqueId();
571
572         tmpPath.Append(uniqueId);
573         SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
574
575         r = request.Construct(tmpPath, L"w+", true);
576         SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
577                         GetErrorMessage(r), tmpPath.GetPointer());
578
579         pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path 
580 #endif
581
582         pMapEnum = const_cast< IMap* >(&insertMap)->GetMapEnumeratorN();
583         while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
584         {
585 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
586                 int length = 0;
587                 String* pColumn = null;
588                 String* pValue = null;
589                 unique_ptr< char[] > pData(null);
590
591                 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
592                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
593                                 "[E_INVALID_ARG] The object is not String class.");
594
595                 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
596                 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
597                                 GetErrorMessage(GetLastResult()));
598
599                 length = strlen(pData.get());
600                 r = request.Write(&length, sizeof(int)); // data length
601                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
602
603                 r = request.Write(pData.get(), length); // data
604                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
605
606                 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
607                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
608                                 "[E_INVALID_ARG] The object is not String class.");
609
610                 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
611                 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
612                                 GetErrorMessage(GetLastResult()));
613
614                 length = strlen(pData.get());
615                 r = request.Write(&length, sizeof(int)); // data length
616                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
617
618                 r = request.Write(pData.get(), length); // data
619                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
620
621                 argSize += pColumn->GetLength() * sizeof(wchar_t);
622                 argSize += pValue->GetLength() * sizeof(wchar_t);
623 #else
624                 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
625                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
626                                 "[E_INVALID_ARG] The object is not String class.");
627
628                 pArgList->Add(*(new String(*pColumn)));
629                 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
630
631                 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
632                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
633                                 "[E_INVALID_ARG] The object is not String class.");
634
635                 pArgList->Add(*(new String(*pValue)));
636                 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
637
638                 argSize += pColumn->GetLength() * sizeof(wchar_t);
639                 argSize += pValue->GetLength() * sizeof(wchar_t);
640                 i++;
641 #endif
642         }
643         SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
644                         "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
645
646 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
647         request.Flush();
648 #endif
649 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_3
650         pPkgMgrImpl = _PackageManagerImpl::GetInstance();
651         pProviderAppId.reset(pPkgMgrImpl->GetAppIdOfDataControlN(this->__providerId.GetPointer()));
652         SysTryCatch(NID_APP, pProviderAppId != null, r = E_SYSTEM, E_SYSTEM,
653                         "[E_SYSTEM] The method cannot proceed due to a severe system error.");
654         r = pDcMgr->AllowAccess(*(pProviderAppId.get()));
655         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
656 #endif
657
658         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_INSERT, pArgList, &id);
659         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
660
661         reqId = static_cast< RequestId >(id);
662
663         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, insertMap: 0x%x, req: %d", dataId.GetPointer(), &insertMap, reqId);
664
665         // fall through
666 CATCH:
667         pArgList->RemoveAll(true);
668         delete pArgList;
669         delete pMapEnum;
670
671         return r;
672 }
673
674 result
675 _SqlDataControlImpl::Update(const String& dataId, const IMap& updateMap, const String* pWhere, RequestId& reqId)
676 {
677         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
678                         "The UPDATE query is not permitted by DataControl provider.");
679
680         int columnCount = 0;
681         int id = 0;
682         int i = 0;
683         int uniqueId = -1;
684         File request;
685         IMapEnumerator* pMapEnum = null;
686         _PackageManagerImpl* pPkgMgrImpl = null;
687         unique_ptr< String > pProviderAppId(null);
688         result r = E_SUCCESS;
689         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl UPDATE");
690
691         columnCount = updateMap.GetCount();
692         SysTryReturnResult(NID_APP, columnCount > 0, E_INVALID_ARG, "The specified insertMap parameter is empty.");
693
694         ArrayList* pArgList = new ArrayList();
695         pArgList->Construct();
696
697         pArgList->Add(new (std::nothrow) String(dataId)); // list[0]: data ID
698         long long argSize = dataId.GetLength() * sizeof(wchar_t);
699
700         pArgList->Add(new (std::nothrow) String(Integer::ToString(columnCount))); // list[1]: updated column count
701         SysLog(NID_APP, "[DC_CALLER_SEND] updated column count: %d", columnCount);
702
703 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
704         String tmpPath(_DATACONTROL_REQUEST_DIR);
705         tmpPath.Append(App::GetInstance()->GetAppId());
706
707         _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
708         SysTryCatch(NID_APP, pDcMgr, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to get _DataControlManager instance.");
709         uniqueId = pDcMgr->GetUniqueId();
710
711         tmpPath.Append(uniqueId);
712         SysLog(NID_APP, "[DC_CALLER_SEND] request: %ls", tmpPath.GetPointer());
713
714         r = request.Construct(tmpPath, L"w+", true);
715         SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to create request (%ls).",
716                         GetErrorMessage(r), tmpPath.GetPointer());
717
718         pArgList->Add(new (std::nothrow) String(tmpPath)); // list[2]: path 
719 #endif
720
721         pMapEnum = const_cast< IMap* >(&updateMap)->GetMapEnumeratorN();
722         while (pMapEnum->MoveNext() == E_SUCCESS) // list: column-value pairs
723         {
724 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
725                 int length = 0;
726                 String* pColumn = null;
727                 String* pValue = null;
728                 unique_ptr< char[] > pData(null);
729
730                 pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
731                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
732                                 "[E_INVALID_ARG] The object is not String class.");
733
734                 pData.reset(_StringConverter::CopyToCharArrayN(*pColumn));
735                 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
736                                 GetErrorMessage(GetLastResult()));
737
738                 length = strlen(pData.get());
739                 r = request.Write(&length, sizeof(int)); // data length
740                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
741
742                 r = request.Write(pData.get(), length); // data
743                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
744
745                 pValue = dynamic_cast< String* >(pMapEnum->GetValue());
746                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
747                                 "[E_INVALID_ARG] The object is not String class.");
748
749                 pData.reset(_StringConverter::CopyToCharArrayN(*pValue));
750                 SysTryCatch(NID_APP, pData != null, r = GetLastResult(), GetLastResult(), "[%s] Invalid request value",
751                                 GetErrorMessage(GetLastResult()));
752
753                 length = strlen(pData.get());
754                 r = request.Write(&length, sizeof(int)); // data length
755                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
756
757                 r = request.Write(pData.get(), length); // data
758                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] Failed to send request.", GetErrorMessage(r));
759
760                 argSize += pColumn->GetLength() * sizeof(wchar_t);
761                 argSize += pValue->GetLength() * sizeof(wchar_t);
762 #else
763                 String* pColumn = dynamic_cast< String* >(pMapEnum->GetKey());
764                 SysTryCatch(NID_APP, pColumn != null, r = E_INVALID_ARG, E_INVALID_ARG,
765                                 "[E_INVALID_ARG] The object is not String class.");
766
767                 pArgList->Add(*(new String(*pColumn)));
768                 SysLog(NID_APP, "[DC_CALLER_SEND] pColumn[%d]: %ls", i, pColumn->GetPointer());
769
770                 String* pValue = dynamic_cast< String* >(pMapEnum->GetValue());
771                 SysTryCatch(NID_APP, pValue != null, r = E_INVALID_ARG, E_INVALID_ARG,
772                                 "[E_INVALID_ARG] The object is not String class.");
773
774                 pArgList->Add(*(new String(*pValue)));
775                 SysLog(NID_APP, "[DC_CALLER_SEND] pValue[%d]: %ls", i, pValue->GetPointer());
776
777                 argSize += pColumn->GetLength() * sizeof(wchar_t);
778                 argSize += pValue->GetLength() * sizeof(wchar_t);
779                 i++;
780 #endif
781         }
782
783 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_2
784         request.Flush();
785 #endif
786
787         if (pWhere != null)     // list: where clause
788         {
789                 pArgList->Add(new (std::nothrow) String(*pWhere));
790                 argSize += pWhere->GetLength() * sizeof(wchar_t);
791                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
792         }
793         else
794         {
795                 pArgList->Add(new (std::nothrow) String(L"NULL"));
796         }
797         SysTryCatch(NID_APP, argSize <= _MAX_REQUEST_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
798                         "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
799
800 #ifdef DATACONTROL_PROTOCOL_VER_2_1_0_3
801         pPkgMgrImpl = _PackageManagerImpl::GetInstance();
802         pProviderAppId.reset(pPkgMgrImpl->GetAppIdOfDataControlN(this->__providerId.GetPointer()));
803         SysTryCatch(NID_APP, pProviderAppId != null, r = E_SYSTEM, E_SYSTEM,
804                         "[E_SYSTEM] The method cannot proceed due to a severe system error.");
805         r = pDcMgr->AllowAccess(*(pProviderAppId.get()));
806         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
807 #endif
808
809         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_UPDATE, pArgList, &id);
810         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
811
812         reqId = static_cast< RequestId >(id);
813
814         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, updateMap: 0x%x, pWhere: 0x%x, req: %d",
815                                 dataId.GetPointer(), &updateMap, pWhere, reqId);
816
817         // fall through
818 CATCH:
819         pArgList->RemoveAll(true);
820         delete pArgList;
821         delete pMapEnum;
822
823         return r;
824 }
825
826 result
827 _SqlDataControlImpl::Delete(const String& dataId, const String* pWhere, RequestId& reqId)
828 {
829         SysTryReturnResult(NID_APP, (__access & _DATACONTROL_ACCESS_WRITE) > 0, E_ILLEGAL_ACCESS,
830                         "The DELETE query is not permitted by DataControl provider.");
831
832         int id = 0;
833         result r = E_SUCCESS;
834         SysLog(NID_APP, "[DC_CALLER_SEND] SqlDataControl DELETE");
835
836         ArrayList* pArgList = new ArrayList();
837         pArgList->Construct();
838
839         pArgList->Add(*(new String(dataId))); // list(0): data ID
840         long long argSize = dataId.GetLength() * sizeof(wchar_t);
841
842         if (pWhere != null)     // list(1): where clause
843         {
844                 pArgList->Add(*(new String(*pWhere)));
845                 argSize += pWhere->GetLength() * sizeof(wchar_t);
846                 SysLog(NID_APP, "[DC_CALLER_SEND] pWhere: %ls", pWhere->GetPointer());
847         }
848         else
849         {
850                 pArgList->Add(*(new String(L"NULL")));
851         }
852         SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
853                         "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
854
855         r = StartSqlDataControl(_DATACONTROL_REQUEST_TYPE_SQL_DELETE, pArgList, &id);
856         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
857
858         reqId = static_cast< RequestId >(id);
859
860         SysLog(NID_APP, "[DC_CALLER_SEND] data: %ls, pWhere: 0x%x, req: %d", dataId.GetPointer(), pWhere, reqId);
861
862         // fall through
863 CATCH:
864         pArgList->RemoveAll(true);
865         delete pArgList;
866
867         return r;
868 }
869
870 result
871 _SqlDataControlImpl::SetSqlDataControlResponseListener(ISqlDataControlResponseListener* pListener)
872 {
873         result r = E_SUCCESS;
874
875         if (__pPreviousListener != null)
876         {
877                 r =  __pSqlDataControlEvent->RemoveListener(*__pPreviousListener);
878                 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "Remove listener failed.");
879                  __pPreviousListener = null;
880         }
881
882         if (pListener != null)
883         {
884                 r =  __pSqlDataControlEvent->AddListener(*pListener);
885                 if (IsFailed(r))
886                 {
887                         switch (r)
888                         {
889                         case E_OBJ_ALREADY_EXIST:
890                                 return E_SUCCESS;
891                         case E_INVALID_OPERATION:
892                                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] The thread setting the listener is worker thread.");
893                                 return E_SYSTEM;
894                         default:
895                                 SysLogException(NID_APP, r, "[%s] Propagating to caller...", GetErrorMessage(r));
896                                 return r;
897                         }
898                 }
899         }
900
901         __pPreviousListener = pListener;
902
903         return E_SUCCESS;
904 }
905
906 // private
907 SqlDataControl*
908 _SqlDataControlImpl::CreateSqlDataControl(const String& appId, const String& providerId, const String& access)
909 {
910         unique_ptr<SqlDataControl> pDc(new (std::nothrow) SqlDataControl);
911         SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
912
913         _SqlDataControlImpl* pDcImpl = _SqlDataControlImpl::GetInstance(*pDc);
914         pDcImpl->__appId = appId;
915         pDcImpl->__providerId = providerId;
916         unique_ptr<_SqlDataControlEvent> pSqlDataControlEvent(new (std::nothrow) _SqlDataControlEvent);
917         SysTryReturn(NID_IO, pSqlDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
918
919         pDcImpl->__pSqlDataControlEvent = pSqlDataControlEvent.release();
920
921         if (access == L"readonly")
922         {
923                 pDcImpl->__access = _DATACONTROL_ACCESS_READ;
924         }
925         else if (access == L"writeonly")
926         {
927                 pDcImpl->__access = _DATACONTROL_ACCESS_WRITE;
928         }
929         else if (access == L"readwrite")
930         {
931                 pDcImpl->__access = _DATACONTROL_ACCESS_READWRITE;
932         }
933         else
934         {
935                 pDcImpl->__access = _DATACONTROL_ACCESS_UNDEFINED;
936                 SysLog(NID_IO, "The accessibility of DataControl provider is invalid.");
937         }
938
939         return pDc.release();
940 }
941
942 }} // Tizen::App
943