Update change log and spec for wrt-plugins-tizen_0.4.66
[framework/web/wrt-plugins-tizen.git] / src / DataControl / SqlDataControlConsumer.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <cassert>
19 #include <Commons/Exception.h>
20 #include "DataType.h"
21 #include "SqlDataControlConsumer.h"
22 #include <appsvc/appsvc.h>
23 #include <string>
24 #include <vector>
25 #include <aul/aul.h>
26 #include <sstream>
27 #include <app.h>
28 #include <app_info.h>
29 // To get ppid
30 #include <unistd.h>
31 #include <security-server.h>
32 #include <sstream>
33 #include "DataControlAsyncCallbackManager.h"
34 #include "DataControlCallback.h"
35 #include "DataControlPendingEvent.h"
36
37 namespace DeviceAPI {
38 namespace DataControl {
39
40 SQLDataControlConsumer::SQLDataControlConsumer(std::string& provId, std::string& dataId, std::string& type)
41 {
42         LoggerD("Enter");
43
44         m_type = type;
45         m_dataId = dataId;
46         m_providerId = provId;
47         
48         m_appId = getProviderApplicationId(OSP_PKGINFO_SQL_TYPE, provId);       
49         m_ProviderPkgId = getProviderPkgId(m_appId);
50         security_server_app_give_access(m_ProviderPkgId.c_str(), -1);
51
52         m_currentAppId = getCurrentApplicationId();
53         createResultDir();
54         DataControlAsyncCallbackManagerSingleton::Instance().setDataControlGC((void*)this, false);
55 }
56
57 SQLDataControlConsumer::~SQLDataControlConsumer() 
58 {
59         LoggerD("Enter");
60         DataControlAsyncCallbackManagerSingleton::Instance().setDataControlGC((void*)this, true);
61 }
62
63 DPL::Mutex SQLDataControlConsumer::m_mutex;
64
65 bool SQLDataControlConsumer::checkReqIdUniqueness(unsigned int reqId)
66 {
67         std::string reqIdStr;
68         std::stringstream ssReqId;
69
70         ssReqId << reqId;
71         reqIdStr = ssReqId.str();
72         return DataControlAsyncCallbackManagerSingleton::Instance().checkReqIdUnique(reqIdStr);
73 }
74
75
76 std::string SQLDataControlConsumer::getDataId()
77 {
78         return m_dataId;
79 }
80
81 std::string SQLDataControlConsumer::getProviderId()
82 {
83         return m_providerId;
84 }
85
86 std::string SQLDataControlConsumer::getType()
87 {
88         return m_type;
89 }
90
91
92
93
94 void SQLDataControlConsumer::saveArrayToFile(std::string filename, RowData *rowData)
95 {
96         std::fstream insertUpdateFile;
97         std::map<std::string, std::string>::iterator it;
98         int strLength = 0;
99         insertUpdateFile.open(filename.c_str(), std::ios::out | std::ios::binary);
100
101         if (!insertUpdateFile.is_open()) 
102         {
103                 LoggerD("fail open" << filename);
104                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "DataControl IPC Error");
105         }
106
107         for (it = rowData->m_Data.begin(); it != rowData->m_Data.end(); ++it)
108         {
109                 strLength = it->first.size();
110                 insertUpdateFile.write((const char*)&strLength, sizeof(int));
111                 insertUpdateFile.write(it->first.c_str(), strLength);
112
113                 strLength = it->second.size();
114                 insertUpdateFile.write((const char*)&strLength, sizeof(int));
115                 insertUpdateFile.write(it->second.c_str(), strLength);
116         }
117         
118         insertUpdateFile.close();               
119         
120 }
121
122
123 void SQLDataControlConsumer::handleCommonErrorEvent(void* userData, unsigned int code, std::string msg)
124 {
125         if (userData == NULL)
126         {
127                 LoggerD("userData null");
128                 return;
129         }
130         
131         CommonPendingEvent* pEvent = (CommonPendingEvent*)userData;
132         
133         try 
134         {
135                 if (dynamic_cast<EventInsertPendingEvent*>(pEvent) != NULL)
136                 {
137                         EventInsertPendingEvent* pendingInsertEvent = (EventInsertPendingEvent*)pEvent;
138                         EventInsertPtr insertEvent = pendingInsertEvent->getEvent();
139                         insertEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
140                         insertEvent->setErrorMsg(msg);          
141                 
142                         WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::ManualAnswer(insertEvent); 
143                         
144                 }
145                 else if (dynamic_cast<EventUpdatePendingEvent*>(pEvent) != NULL)
146                 {
147                         EventUpdatePendingEvent* pendingUpdateEvent = (EventUpdatePendingEvent*)pEvent;
148                         EventUpdatePtr updateEvent = pendingUpdateEvent->getEvent();
149                         updateEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
150                         updateEvent->setErrorMsg(msg);          
151                         
152                         WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::ManualAnswer(updateEvent); 
153                 }
154                 else if (dynamic_cast<EventSelectPendingEvent*>(pEvent) != NULL)
155                 {
156                         EventSelectPendingEvent* pendingSelectEvent = (EventSelectPendingEvent*)pEvent;
157                         EventSelectPtr selectEvent = pendingSelectEvent->getEvent();
158                         selectEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
159                         selectEvent->setErrorMsg(msg);
160                         
161                         WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::ManualAnswer(selectEvent); 
162                 }
163                 else if (dynamic_cast<EventDeletePendingEvent*>(pEvent) != NULL)
164                 {
165                         EventDeletePendingEvent* pendingDeleteEvent = (EventDeletePendingEvent*)pEvent;
166                         EventDeletePtr deleteEvent = pendingDeleteEvent->getEvent();
167                         deleteEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
168                         deleteEvent->setErrorMsg(msg);
169                         
170                         WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::ManualAnswer(deleteEvent); 
171                 }       
172         }
173         catch (const WrtDeviceApis::Commons::Exception& ex) 
174         {
175                 LoggerE("Exception: " << ex.GetMessage());
176         }
177
178 }
179
180 void SQLDataControlConsumer::handlePendingEvent(const EventInsertPtr& event)
181 {
182         LoggerD("OK");
183         WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::ManualAnswer(event); 
184         CommonPendingEvent* userData = NULL;
185         unsigned short currentJob = 0;
186         try {
187                 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
188
189                 if (userData)
190                 {
191                         SendAppControlLaunchToProvider(userData, currentJob);
192                 }
193         }
194         catch (const WrtDeviceApis::Commons::Exception& ex) 
195         {
196                 if (event.Get() == NULL)
197                 {
198                         LoggerD("event removed, invalid cb");
199                         return;
200                 }
201                 LoggerE("Exception: " << ex.GetMessage());
202
203                 if (userData)
204                 {
205                         handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
206                 }
207         }
208
209 }
210 void SQLDataControlConsumer::handlePendingEvent(const EventDeletePtr& event)
211 {
212         WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::ManualAnswer(event); 
213         CommonPendingEvent* userData = NULL;
214         unsigned short currentJob = 0;
215
216         try {
217                 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
218
219                 if (userData)
220                 {
221                         SendAppControlLaunchToProvider(userData, currentJob);
222                 }
223         }
224         catch (const WrtDeviceApis::Commons::Exception& ex) 
225         {
226                 if (event.Get() == NULL)
227                 {
228                         LoggerD("event removed, invalid cb");
229                         return;
230                 }
231                 LoggerE("Exception: " << ex.GetMessage());
232
233                 if (userData)
234                 {
235                         handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
236                 }
237         }
238
239
240 }
241 void SQLDataControlConsumer::handlePendingEvent(const EventSelectPtr& event)
242 {
243         WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::ManualAnswer(event);
244         CommonPendingEvent* userData = NULL;
245         unsigned short currentJob = 0;
246
247         try {
248                 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
249
250                 if (userData)
251                 {
252                         SendAppControlLaunchToProvider(userData, currentJob);
253                 }
254         }
255         catch (const WrtDeviceApis::Commons::Exception& ex) 
256         {
257                 if (event.Get() == NULL)
258                 {
259                         LoggerD("event removed, invalid cb");
260                         return;
261                 }
262                 LoggerE("Exception: " << ex.GetMessage());
263
264                 if (userData)
265                 {
266                         handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
267                 }
268
269         }
270         
271
272 }
273
274 void SQLDataControlConsumer::handlePendingEvent(const EventUpdatePtr& event)
275 {
276         WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::ManualAnswer(event); 
277         CommonPendingEvent* userData = NULL;
278         unsigned short currentJob = 0;
279
280         try {
281                 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
282
283                 if (userData)
284                 {
285                         SendAppControlLaunchToProvider(userData, currentJob);
286                 }
287         }
288         catch (const WrtDeviceApis::Commons::Exception& ex) 
289         {
290                 if (event.Get() == NULL)
291                 {
292                         LoggerD("event removed, invalid cb");
293                         return;
294                 }
295                 LoggerE("Exception: " << ex.GetMessage());
296
297                 if (userData)
298                 {
299                         handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
300                 }               
301         }
302
303 }
304
305
306 void SQLDataControlConsumer::insertData(const EventInsertPtr& event)
307 {
308         WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::PostRequest(event);
309 }
310
311 void SQLDataControlConsumer::deleteData(const EventDeletePtr& event)
312 {
313         WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::PostRequest(event);
314 }
315
316 void SQLDataControlConsumer::selectData(const EventSelectPtr& event)
317 {
318         WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::PostRequest(event);
319 }
320
321 void SQLDataControlConsumer::updateData(const EventUpdatePtr& event)
322 {
323         WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::PostRequest(event);
324 }
325
326 void SQLDataControlConsumer::createResultDir()
327 {
328     struct stat info;
329     memset(&info, 0, sizeof(struct stat));
330
331     int status = lstat(DATACONTROL_PROTOCOL_DIR_TOP, &info);
332
333         if (status != 0 && errno == ENOENT) 
334         {
335                 if (mkdir(DATACONTROL_PROTOCOL_DIR_TOP, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) 
336                 {
337                         LoggerD("make error");
338                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
339                 }
340         }
341
342     status = lstat(DATACONTROL_PROTOCOL_DIR_MIDDLE, &info);
343
344         if (status != 0 && errno == ENOENT) 
345         {
346                 if (mkdir(DATACONTROL_PROTOCOL_DIR_MIDDLE, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) 
347                 {
348                         LoggerD("make error");
349                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
350                 }
351         }
352         
353     status = lstat(DATACONTROL_PROTOCOL_DIR, &info);
354
355         // already exist
356         if (status == 0) 
357         {
358                 LoggerD("already exist");
359         return;
360     } 
361         else if (errno == ENOENT) 
362         {
363                 if (mkdir(DATACONTROL_PROTOCOL_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) 
364                 {
365                         LoggerD("make error");
366                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
367                 }
368                 return;
369     }
370     ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could be error during checking status");
371 }
372
373 void SQLDataControlConsumer::OnRequestReceived(const EventInsertPtr& event)
374 {
375         LoggerD("Enter");
376
377         try 
378         {
379                 RowData* rowData = event->getRowData();
380                 unsigned int reqId = event->getReqId();
381                 std::string ipcFilename = generateFileName(reqId, m_currentAppId);
382                 std::string reqIdStr = convertIntToString(reqId);
383                 unsigned short currentJob = 0;
384
385                 if (checkReqIdUniqueness(reqId) == false)
386                 {
387                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
388                 }
389                 
390                 if (rowData == NULL || rowData->m_Data.size() == 0)
391                 {
392                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No insertion data");
393                 }
394                 
395                 saveArrayToFile(ipcFilename, rowData);          
396                 delete rowData;
397                 
398                 EventInsertPendingEvent* pendingEvent = new EventInsertPendingEvent((void*)this, m_dataId,
399                         m_appId, m_providerId, m_currentAppId, event);
400                 
401                 
402                 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
403                 {
404                         SendAppControlLaunchToProvider(pendingEvent, currentJob);
405                 }
406                 event->switchToManualAnswer();
407                 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
408
409
410         }       
411         catch (const WrtDeviceApis::Commons::Exception& ex) 
412         {
413                 event->setExceptionCode(ex.getCode());
414                 event->setErrorMsg(ex.GetMessage());
415                 LoggerE("Exception: " << ex.GetMessage());
416                 
417                 
418         }
419 }
420 void SQLDataControlConsumer::OnRequestReceived(const EventDeletePtr& event)
421 {
422         LoggerD("Enter");
423         bundle* passData = NULL;
424         
425         try 
426         {
427                 unsigned int reqId = event->getReqId();
428                 std::string reqIdStr = convertIntToString(reqId);
429                 unsigned short currentJob = 0;
430
431                 if (checkReqIdUniqueness(reqId) == false)
432                 {
433                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
434                 }
435
436                 EventDeletePendingEvent* pendingEvent = new EventDeletePendingEvent(this, m_dataId,
437                         m_appId, m_providerId, m_currentAppId, event);
438                 
439                 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
440                 {
441                         SendAppControlLaunchToProvider(pendingEvent, currentJob);
442                 }
443                 event->switchToManualAnswer();
444                 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
445         }       
446         
447         catch (const WrtDeviceApis::Commons::Exception& ex) 
448         {
449                 LoggerE("Exception: " << ex.GetMessage());
450                 event->setExceptionCode(ex.getCode());
451                 event->setErrorMsg(ex.GetMessage());
452         }
453
454         if (passData)
455         {
456                 bundle_free(passData);
457                 passData = NULL;
458         }
459
460 }
461 void SQLDataControlConsumer::OnRequestReceived(const EventSelectPtr& event)
462 {
463         try 
464         {
465                 unsigned int reqId = event->getReqId();
466                 std::string reqIdStr = convertIntToString(reqId);
467                 unsigned short currentJob = 0;
468
469                 if (checkReqIdUniqueness(reqId) == false)
470                 {
471                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
472                 }
473
474                 EventSelectPendingEvent* pendingEvent = new EventSelectPendingEvent(this, m_dataId,
475                         m_appId, m_providerId, m_currentAppId, event);
476
477                 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
478                 {
479                         SendAppControlLaunchToProvider(pendingEvent, currentJob);
480                 }
481                 
482                 event->switchToManualAnswer();
483                 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
484         }
485         catch (const WrtDeviceApis::Commons::Exception& ex) 
486         {
487                 LoggerE("Exception: " << ex.GetMessage());
488                 event->setExceptionCode(ex.getCode());
489                 event->setErrorMsg(ex.GetMessage());
490         }
491
492 }
493
494 void SQLDataControlConsumer::OnRequestReceived(const EventUpdatePtr& event)
495 {
496         LoggerD("Enter");
497
498         try 
499         {
500                 unsigned int reqId = event->getReqId();
501                 RowData* rowData = event->getRowData();
502                 std::string ipcFilename = generateFileName(reqId, m_currentAppId);
503                 std::stringstream ssReqId;
504                 unsigned short currentJob = 0;
505                 ssReqId << reqId;
506
507                 if (checkReqIdUniqueness(reqId) == false)
508                 {
509                         ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
510                 }
511                 
512                 if (rowData == NULL || rowData->m_Data.size() == 0)
513                 {\r                      
514                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No update data");
515                 }
516
517                 saveArrayToFile(ipcFilename, rowData);
518                 delete rowData;
519
520                 EventUpdatePendingEvent* pendingEvent = new EventUpdatePendingEvent(this, m_dataId, 
521                         m_appId, m_providerId, m_currentAppId, event);
522                 
523                 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
524                 {
525                         SendAppControlLaunchToProvider(pendingEvent, currentJob);
526                 }
527                 event->switchToManualAnswer();
528                 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(ssReqId.str(), (void*)pendingEvent);
529
530         }
531         catch (const WrtDeviceApis::Commons::Exception& ex) 
532         {
533                 LoggerE("Exception: " << ex.GetMessage());
534                 event->setExceptionCode(ex.getCode());
535                 event->setErrorMsg(ex.GetMessage());
536         }
537
538 }
539 }
540 }
541
542