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