2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <Commons/Exception.h>
21 #include "SqlDataControlConsumer.h"
22 #include <appsvc/appsvc.h>
31 #include <security-server.h>
33 #include "DataControlAsyncCallbackManager.h"
34 #include "DataControlCallback.h"
35 #include "DataControlPendingEvent.h"
38 namespace DataControl {
40 SQLDataControlConsumer::SQLDataControlConsumer(std::string& provId, std::string& dataId, std::string& type)
46 m_providerId = provId;
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);
52 m_currentAppId = getCurrentApplicationId();
54 DataControlAsyncCallbackManagerSingleton::Instance().setDataControlGC((void*)this, false);
57 SQLDataControlConsumer::~SQLDataControlConsumer()
60 DataControlAsyncCallbackManagerSingleton::Instance().setDataControlGC((void*)this, true);
63 DPL::Mutex SQLDataControlConsumer::m_mutex;
65 bool SQLDataControlConsumer::checkReqIdUniqueness(unsigned int reqId)
68 std::stringstream ssReqId;
71 reqIdStr = ssReqId.str();
72 return DataControlAsyncCallbackManagerSingleton::Instance().checkReqIdUnique(reqIdStr);
76 std::string SQLDataControlConsumer::getDataId()
81 std::string SQLDataControlConsumer::getProviderId()
86 std::string SQLDataControlConsumer::getType()
94 void SQLDataControlConsumer::saveArrayToFile(std::string filename, RowData *rowData)
96 std::fstream insertUpdateFile;
97 std::map<std::string, std::string>::iterator it;
99 insertUpdateFile.open(filename.c_str(), std::ios::out | std::ios::binary);
101 if (!insertUpdateFile.is_open())
103 LoggerD("fail open" << filename);
104 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "DataControl IPC Error");
107 for (it = rowData->m_Data.begin(); it != rowData->m_Data.end(); ++it)
109 strLength = it->first.size();
110 insertUpdateFile.write((const char*)&strLength, sizeof(int));
111 insertUpdateFile.write(it->first.c_str(), strLength);
113 strLength = it->second.size();
114 insertUpdateFile.write((const char*)&strLength, sizeof(int));
115 insertUpdateFile.write(it->second.c_str(), strLength);
118 insertUpdateFile.close();
123 void SQLDataControlConsumer::handleCommonErrorEvent(void* userData, unsigned int code, std::string msg)
125 if (userData == NULL)
127 LoggerD("userData null");
131 CommonPendingEvent* pEvent = (CommonPendingEvent*)userData;
135 if (dynamic_cast<EventInsertPendingEvent*>(pEvent) != NULL)
137 EventInsertPendingEvent* pendingInsertEvent = (EventInsertPendingEvent*)pEvent;
138 EventInsertPtr insertEvent = pendingInsertEvent->getEvent();
139 insertEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
140 insertEvent->setErrorMsg(msg);
142 WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::ManualAnswer(insertEvent);
145 else if (dynamic_cast<EventUpdatePendingEvent*>(pEvent) != NULL)
147 EventUpdatePendingEvent* pendingUpdateEvent = (EventUpdatePendingEvent*)pEvent;
148 EventUpdatePtr updateEvent = pendingUpdateEvent->getEvent();
149 updateEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
150 updateEvent->setErrorMsg(msg);
152 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::ManualAnswer(updateEvent);
154 else if (dynamic_cast<EventSelectPendingEvent*>(pEvent) != NULL)
156 EventSelectPendingEvent* pendingSelectEvent = (EventSelectPendingEvent*)pEvent;
157 EventSelectPtr selectEvent = pendingSelectEvent->getEvent();
158 selectEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
159 selectEvent->setErrorMsg(msg);
161 WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::ManualAnswer(selectEvent);
163 else if (dynamic_cast<EventDeletePendingEvent*>(pEvent) != NULL)
165 EventDeletePendingEvent* pendingDeleteEvent = (EventDeletePendingEvent*)pEvent;
166 EventDeletePtr deleteEvent = pendingDeleteEvent->getEvent();
167 deleteEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
168 deleteEvent->setErrorMsg(msg);
170 WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::ManualAnswer(deleteEvent);
173 catch (const WrtDeviceApis::Commons::Exception& ex)
175 LoggerE("Exception: " << ex.GetMessage());
180 void SQLDataControlConsumer::handlePendingEvent(const EventInsertPtr& event)
183 WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::ManualAnswer(event);
184 CommonPendingEvent* userData = NULL;
185 unsigned short currentJob = 0;
187 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
191 SendAppControlLaunchToProvider(userData, currentJob);
194 catch (const WrtDeviceApis::Commons::Exception& ex)
196 if (event.Get() == NULL)
198 LoggerD("event removed, invalid cb");
201 LoggerE("Exception: " << ex.GetMessage());
205 handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
210 void SQLDataControlConsumer::handlePendingEvent(const EventDeletePtr& event)
212 WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::ManualAnswer(event);
213 CommonPendingEvent* userData = NULL;
214 unsigned short currentJob = 0;
217 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
221 SendAppControlLaunchToProvider(userData, currentJob);
224 catch (const WrtDeviceApis::Commons::Exception& ex)
226 if (event.Get() == NULL)
228 LoggerD("event removed, invalid cb");
231 LoggerE("Exception: " << ex.GetMessage());
235 handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
241 void SQLDataControlConsumer::handlePendingEvent(const EventSelectPtr& event)
243 WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::ManualAnswer(event);
244 CommonPendingEvent* userData = NULL;
245 unsigned short currentJob = 0;
248 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
252 SendAppControlLaunchToProvider(userData, currentJob);
255 catch (const WrtDeviceApis::Commons::Exception& ex)
257 if (event.Get() == NULL)
259 LoggerD("event removed, invalid cb");
262 LoggerE("Exception: " << ex.GetMessage());
266 handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
274 void SQLDataControlConsumer::handlePendingEvent(const EventUpdatePtr& event)
276 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::ManualAnswer(event);
277 CommonPendingEvent* userData = NULL;
278 unsigned short currentJob = 0;
281 userData = (CommonPendingEvent*)DataControlAsyncCallbackManagerSingleton::Instance().getRemainingIpcOperation();
285 SendAppControlLaunchToProvider(userData, currentJob);
288 catch (const WrtDeviceApis::Commons::Exception& ex)
290 if (event.Get() == NULL)
292 LoggerD("event removed, invalid cb");
295 LoggerE("Exception: " << ex.GetMessage());
299 handleCommonErrorEvent(userData, ex.getCode(), ex.GetMessage());
306 void SQLDataControlConsumer::insertData(const EventInsertPtr& event)
308 WrtDeviceApis::Commons::EventRequestReceiver<EventInsert>::PostRequest(event);
311 void SQLDataControlConsumer::deleteData(const EventDeletePtr& event)
313 WrtDeviceApis::Commons::EventRequestReceiver<EventDelete>::PostRequest(event);
316 void SQLDataControlConsumer::selectData(const EventSelectPtr& event)
318 WrtDeviceApis::Commons::EventRequestReceiver<EventSelect>::PostRequest(event);
321 void SQLDataControlConsumer::updateData(const EventUpdatePtr& event)
323 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdate>::PostRequest(event);
326 void SQLDataControlConsumer::createResultDir()
329 memset(&info, 0, sizeof(struct stat));
331 int status = lstat(DATACONTROL_PROTOCOL_DIR_TOP, &info);
333 if (status != 0 && errno == ENOENT)
335 if (mkdir(DATACONTROL_PROTOCOL_DIR_TOP, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
337 LoggerD("make error");
338 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
342 status = lstat(DATACONTROL_PROTOCOL_DIR_MIDDLE, &info);
344 if (status != 0 && errno == ENOENT)
346 if (mkdir(DATACONTROL_PROTOCOL_DIR_MIDDLE, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
348 LoggerD("make error");
349 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
353 status = lstat(DATACONTROL_PROTOCOL_DIR, &info);
358 LoggerD("already exist");
361 else if (errno == ENOENT)
363 if (mkdir(DATACONTROL_PROTOCOL_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
365 LoggerD("make error");
366 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could not be created.");
370 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "result dir could be error during checking status");
373 void SQLDataControlConsumer::OnRequestReceived(const EventInsertPtr& event)
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;
385 if (checkReqIdUniqueness(reqId) == false)
387 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
390 if (rowData == NULL || rowData->m_Data.size() == 0)
392 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No insertion data");
395 saveArrayToFile(ipcFilename, rowData);
398 EventInsertPendingEvent* pendingEvent = new EventInsertPendingEvent((void*)this, m_dataId,
399 m_appId, m_providerId, m_currentAppId, event);
402 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
404 SendAppControlLaunchToProvider(pendingEvent, currentJob);
406 event->switchToManualAnswer();
407 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
411 catch (const WrtDeviceApis::Commons::Exception& ex)
413 event->setExceptionCode(ex.getCode());
414 event->setErrorMsg(ex.GetMessage());
415 LoggerE("Exception: " << ex.GetMessage());
420 void SQLDataControlConsumer::OnRequestReceived(const EventDeletePtr& event)
423 bundle* passData = NULL;
427 unsigned int reqId = event->getReqId();
428 std::string reqIdStr = convertIntToString(reqId);
429 unsigned short currentJob = 0;
431 if (checkReqIdUniqueness(reqId) == false)
433 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
436 EventDeletePendingEvent* pendingEvent = new EventDeletePendingEvent(this, m_dataId,
437 m_appId, m_providerId, m_currentAppId, event);
439 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
441 SendAppControlLaunchToProvider(pendingEvent, currentJob);
443 event->switchToManualAnswer();
444 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
447 catch (const WrtDeviceApis::Commons::Exception& ex)
449 LoggerE("Exception: " << ex.GetMessage());
450 event->setExceptionCode(ex.getCode());
451 event->setErrorMsg(ex.GetMessage());
456 bundle_free(passData);
461 void SQLDataControlConsumer::OnRequestReceived(const EventSelectPtr& event)
465 unsigned int reqId = event->getReqId();
466 std::string reqIdStr = convertIntToString(reqId);
467 unsigned short currentJob = 0;
469 if (checkReqIdUniqueness(reqId) == false)
471 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
474 EventSelectPendingEvent* pendingEvent = new EventSelectPendingEvent(this, m_dataId,
475 m_appId, m_providerId, m_currentAppId, event);
477 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
479 SendAppControlLaunchToProvider(pendingEvent, currentJob);
482 event->switchToManualAnswer();
483 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
485 catch (const WrtDeviceApis::Commons::Exception& ex)
487 LoggerE("Exception: " << ex.GetMessage());
488 event->setExceptionCode(ex.getCode());
489 event->setErrorMsg(ex.GetMessage());
494 void SQLDataControlConsumer::OnRequestReceived(const EventUpdatePtr& event)
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;
507 if (checkReqIdUniqueness(reqId) == false)
509 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
512 if (rowData == NULL || rowData->m_Data.size() == 0)
514 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "No update data");
517 saveArrayToFile(ipcFilename, rowData);
520 EventUpdatePendingEvent* pendingEvent = new EventUpdatePendingEvent(this, m_dataId,
521 m_appId, m_providerId, m_currentAppId, event);
523 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
525 SendAppControlLaunchToProvider(pendingEvent, currentJob);
527 event->switchToManualAnswer();
528 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(ssReqId.str(), (void*)pendingEvent);
531 catch (const WrtDeviceApis::Commons::Exception& ex)
533 LoggerE("Exception: " << ex.GetMessage());
534 event->setExceptionCode(ex.getCode());
535 event->setErrorMsg(ex.GetMessage());