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