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