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 "MappedDataControlConsumer.h"
22 #include <appsvc/appsvc.h>
28 #include <package-manager.h>
29 #include <security-server.h>
31 #include "DataControlAsyncCallbackManager.h"
32 #include "DataControlCallback.h"
38 namespace DataControl {
40 MappedDataControlConsumer::MappedDataControlConsumer(std::string& provId, std::string& dataId, std::string& type)
47 m_providerId = provId;
49 m_appId = getProviderApplicationId(OSP_PKGINFO_MAP_TYPE, provId);
50 m_ProviderPkgId = getProviderPkgId(m_appId);
51 security_server_app_give_access(m_ProviderPkgId.c_str(), -1);
53 m_currentAppId = getCurrentApplicationId();
55 DataControlAsyncCallbackManagerSingleton::Instance().setDataControlGC((void*)this, false);
59 MappedDataControlConsumer::~MappedDataControlConsumer()
62 DataControlAsyncCallbackManagerSingleton::Instance().setDataControlGC((void*)this, true);
66 DPL::Mutex MappedDataControlConsumer::m_mutex;
70 std::string MappedDataControlConsumer::getDataId()
75 std::string MappedDataControlConsumer::getProviderId()
80 std::string MappedDataControlConsumer::getType()
85 void MappedDataControlConsumer::handleCommonErrorEvent(void* userData, unsigned int code, std::string msg)
89 LoggerD("userData null");
93 CommonPendingEvent* pEvent = (CommonPendingEvent*)userData;
97 if (dynamic_cast<EventAddValuePendingEvent*>(pEvent) != NULL)
99 EventAddValuePendingEvent* pendingAddValueEvent = (EventAddValuePendingEvent*)pEvent;
100 EventAddValuePtr addValueEvent = pendingAddValueEvent->getEvent();
101 addValueEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
102 addValueEvent->setErrorMsg(msg);
104 WrtDeviceApis::Commons::EventRequestReceiver<EventAddValue>::ManualAnswer(addValueEvent);
106 else if (dynamic_cast<EventUpdateValuePendingEvent*>(pEvent) != NULL)
108 EventUpdateValuePendingEvent* pendingUpdateValueEvent = (EventUpdateValuePendingEvent*)pEvent;
109 EventUpdateValuePtr updateValueEvent = pendingUpdateValueEvent->getEvent();
110 updateValueEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
111 updateValueEvent->setErrorMsg(msg);
113 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdateValue>::ManualAnswer(updateValueEvent);
115 else if (dynamic_cast<EventGetValuePendingEvent*>(pEvent) != NULL)
117 EventGetValuePendingEvent* pendingGetValueEvent = (EventGetValuePendingEvent*)pEvent;
118 EventGetValuePtr getValueEvent = pendingGetValueEvent->getEvent();
119 getValueEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
120 getValueEvent->setErrorMsg(msg);
122 WrtDeviceApis::Commons::EventRequestReceiver<EventGetValue>::ManualAnswer(getValueEvent);
124 else if (dynamic_cast<EventRemoveValuePendingEvent*>(pEvent) != NULL)
126 EventRemoveValuePendingEvent* pendingRemoveEvent = (EventRemoveValuePendingEvent*)pEvent;
127 EventRemoveValuePtr deleteEvent = pendingRemoveEvent->getEvent();
128 deleteEvent->setExceptionCode((WrtDeviceApis::Commons::ExceptionCodes::Enumeration)code);
129 deleteEvent->setErrorMsg(msg);
131 WrtDeviceApis::Commons::EventRequestReceiver<EventRemoveValue>::ManualAnswer(deleteEvent);
134 catch (const WrtDeviceApis::Commons::Exception& ex)
136 LoggerE("Exception: " << ex.GetMessage());
142 void MappedDataControlConsumer::handlePendingEvent(const EventAddValuePtr& event)
144 WrtDeviceApis::Commons::EventRequestReceiver<EventAddValue>::ManualAnswer(event);
147 void MappedDataControlConsumer::handlePendingEvent(const EventRemoveValuePtr& event)
149 WrtDeviceApis::Commons::EventRequestReceiver<EventRemoveValue>::ManualAnswer(event);
152 void MappedDataControlConsumer::handlePendingEvent(const EventGetValuePtr& event)
154 WrtDeviceApis::Commons::EventRequestReceiver<EventGetValue>::ManualAnswer(event);
157 void MappedDataControlConsumer::handlePendingEvent(const EventUpdateValuePtr& event)
159 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdateValue>::ManualAnswer(event);
163 void MappedDataControlConsumer::addValue(const EventAddValuePtr& event)
165 WrtDeviceApis::Commons::EventRequestReceiver<EventAddValue>::PostRequest(event);
168 void MappedDataControlConsumer::removeValue(const EventRemoveValuePtr& event)
170 WrtDeviceApis::Commons::EventRequestReceiver<EventRemoveValue>::PostRequest(event);
173 void MappedDataControlConsumer::getValue(const EventGetValuePtr& event)
175 WrtDeviceApis::Commons::EventRequestReceiver<EventGetValue>::PostRequest(event);
178 void MappedDataControlConsumer::updateValue(const EventUpdateValuePtr& event)
180 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdateValue>::PostRequest(event);
183 bool MappedDataControlConsumer::checkReqIdUniqueness(unsigned int reqId)
185 std::string reqIdStr;
186 std::stringstream ssReqId;
189 reqIdStr = ssReqId.str();
190 return DataControlAsyncCallbackManagerSingleton::Instance().checkReqIdUnique(reqIdStr);
193 void MappedDataControlConsumer::OnRequestReceived(const EventAddValuePtr& event)
199 unsigned short currentJob = 0;
200 unsigned int reqId = event->getReqId();
201 std::string reqIdStr = convertIntToString(reqId);
203 if (checkReqIdUniqueness(reqId) == false)
205 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
208 EventAddValuePendingEvent* pendingEvent = new EventAddValuePendingEvent(this, m_dataId, m_appId, m_providerId, m_currentAppId, event);
210 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
212 SendAppControlLaunchToProvider(pendingEvent, currentJob);
214 event->switchToManualAnswer();
215 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
217 catch (const WrtDeviceApis::Commons::Exception& ex)
219 event->setExceptionCode(ex.getCode());
220 event->setErrorMsg(ex.GetMessage());
221 LoggerE("Exception: " << ex.GetMessage());
227 void MappedDataControlConsumer::OnRequestReceived(const EventAddValuePtr& event)
230 bundle* passData = NULL;
234 std::string dataId = getDataId();
235 unsigned int reqId = event->getReqId();
236 std::string key = event->getKey();
237 std::string value = event->getValue();
239 std::stringstream ss, ssreqtype;
240 std::string reqIdStr, reqtypestr;
241 std::vector<std::string> queryItem;
243 if (checkReqIdUniqueness(reqId) == false)
245 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
253 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_INSERT;
254 reqtypestr = ssreqtype.str();
256 passData = bundle_create();
258 if (passData == NULL)
260 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
263 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
265 appsvc_set_appid(passData, m_appId.c_str());
267 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
268 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
269 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
270 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
271 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
272 bundle_add(passData, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_K_DATACONTROL_PROTOCOL_VERSION_VALUE); // version
274 queryItem.push_back(dataId); // dataid
275 queryItem.push_back(key); // key
276 queryItem.push_back(value); // value
278 addArrayToBundle(passData, queryItem);
281 // reqid sholud be known
282 EventAddValuePendingEvent* pendingEvent = new EventAddValuePendingEvent((void*)this, dataId, m_appId, m_providerId, "", event);
283 int pid = appsvc_run_service(passData, reqId, dataControlCommonCallback, (void*)pendingEvent);
287 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Error");
290 event->switchToManualAnswer();
292 DPL::Mutex::ScopedLock lock(&m_mutex);
294 catch (const WrtDeviceApis::Commons::Exception& ex)
296 event->setExceptionCode(ex.getCode());
297 event->setErrorMsg(ex.GetMessage());
298 LoggerE("Exception: " << ex.GetMessage());
305 bundle_free(passData);
312 void MappedDataControlConsumer::OnRequestReceived(const EventRemoveValuePtr& event)
318 unsigned short currentJob = 0;
319 unsigned int reqId = event->getReqId();
320 std::string reqIdStr = convertIntToString(reqId);
322 if (checkReqIdUniqueness(reqId) == false)
324 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
327 EventRemoveValuePendingEvent* pendingEvent = new EventRemoveValuePendingEvent(this, m_dataId, m_appId, m_providerId, m_currentAppId, event);
329 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
331 SendAppControlLaunchToProvider(pendingEvent, currentJob);
333 event->switchToManualAnswer();
334 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
336 catch (const WrtDeviceApis::Commons::Exception& ex)
338 event->setExceptionCode(ex.getCode());
339 event->setErrorMsg(ex.GetMessage());
340 LoggerE("Exception: " << ex.GetMessage());
344 void MappedDataControlConsumer::OnRequestReceived(const EventRemoveValuePtr& event)
347 bundle* passData = NULL;
351 std::string dataId = getDataId();
352 std::vector<std::string> queryItem;
353 std::string key = event->getKey();
354 std::string value = event->getValue();
356 unsigned int reqId = event->getReqId();
357 std::stringstream ss, ssreqtype;
358 std::string reqIdStr, reqtypestr;
363 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_DELETE;
364 reqtypestr = ssreqtype.str();
366 if (checkReqIdUniqueness(reqId) == false)
368 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
371 passData = bundle_create();
373 if (passData == NULL)
375 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
378 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
379 appsvc_set_appid(passData, m_appId.c_str());
381 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
382 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
383 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
384 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
385 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
386 bundle_add(passData, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_K_DATACONTROL_PROTOCOL_VERSION_VALUE); // version
389 queryItem.push_back(dataId); // dataid
390 queryItem.push_back(key);
391 queryItem.push_back(value);
394 addArrayToBundle(passData, queryItem);
395 EventRemoveValuePendingEvent* pendingEvent = new EventRemoveValuePendingEvent(this, dataId, m_appId, m_providerId, "", event);
397 int pid = appsvc_run_service(passData, reqId, dataControlCommonCallback, (void*)pendingEvent);
401 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
404 event->switchToManualAnswer();
406 DPL::Mutex::ScopedLock lock(&m_mutex);
408 catch (const WrtDeviceApis::Commons::Exception& ex)
410 LoggerE("Exception: " << ex.GetMessage());
411 event->setExceptionCode(ex.getCode());
412 event->setErrorMsg(ex.GetMessage());
417 bundle_free(passData);
424 void MappedDataControlConsumer::OnRequestReceived(const EventGetValuePtr& event)
430 unsigned short currentJob = 0;
431 unsigned int reqId = event->getReqId();
432 std::string reqIdStr = convertIntToString(reqId);
434 if (checkReqIdUniqueness(reqId) == false)
436 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
439 EventGetValuePendingEvent* pendingEvent = new EventGetValuePendingEvent(this, m_dataId, m_appId, m_providerId, m_currentAppId, event);
441 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
443 SendAppControlLaunchToProvider(pendingEvent, currentJob);
445 event->switchToManualAnswer();
446 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
448 catch (const WrtDeviceApis::Commons::Exception& ex)
450 event->setExceptionCode(ex.getCode());
451 event->setErrorMsg(ex.GetMessage());
452 LoggerE("Exception: " << ex.GetMessage());
459 void MappedDataControlConsumer::OnRequestReceived(const EventGetValuePtr& event)
462 bundle* passData = NULL;
466 std::string dataId = getDataId();
467 std::vector<std::string> queryItem;
469 unsigned int reqId = event->getReqId();
470 std::stringstream ss, ssreqtype;
471 std::string reqIdStr, reqtypestr;
472 std::string key = event->getKey();
474 if (checkReqIdUniqueness(reqId) == false)
476 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
483 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_QUERY;
484 reqtypestr = ssreqtype.str();
486 passData = bundle_create();
488 if (passData == NULL)
490 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
493 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
494 appsvc_set_appid(passData, m_appId.c_str());
496 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
497 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
498 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
499 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
500 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
501 bundle_add(passData, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_K_DATACONTROL_PROTOCOL_VERSION_VALUE); // version
505 queryItem.push_back(dataId); // dataid
506 queryItem.push_back(key);
507 queryItem.push_back("1");
508 queryItem.push_back("1");
510 addArrayToBundle(passData, queryItem);
511 EventGetValuePendingEvent* pendingEvent = new EventGetValuePendingEvent(this, dataId, m_appId, m_providerId, "", event);
513 int pid = appsvc_run_service(passData, reqId, dataControlCommonCallback, (void*)pendingEvent);
517 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
520 event->switchToManualAnswer();
522 DPL::Mutex::ScopedLock lock(&m_mutex);
524 catch (const WrtDeviceApis::Commons::Exception& ex)
526 LoggerE("Exception: " << ex.GetMessage());
527 event->setExceptionCode(ex.getCode());
528 event->setErrorMsg(ex.GetMessage());
533 bundle_free(passData);
540 void MappedDataControlConsumer::OnRequestReceived(const EventUpdateValuePtr& event)
546 unsigned short currentJob = 0;
547 unsigned int reqId = event->getReqId();
548 std::string reqIdStr = convertIntToString(reqId);
550 if (checkReqIdUniqueness(reqId) == false)
552 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
555 EventUpdateValuePendingEvent* pendingEvent = new EventUpdateValuePendingEvent(this, m_dataId, m_appId, m_providerId, m_currentAppId, event);
557 if (DataControlAsyncCallbackManagerSingleton::Instance().checkRequestIpcOperation())
559 SendAppControlLaunchToProvider(pendingEvent, currentJob);
561 event->switchToManualAnswer();
562 DataControlAsyncCallbackManagerSingleton::Instance().addSQLUserData(reqIdStr, (void*)pendingEvent);
564 catch (const WrtDeviceApis::Commons::Exception& ex)
566 event->setExceptionCode(ex.getCode());
567 event->setErrorMsg(ex.GetMessage());
568 LoggerE("Exception: " << ex.GetMessage());
573 void MappedDataControlConsumer::OnRequestReceived(const EventUpdateValuePtr& event)
576 bundle* passData = NULL;
580 std::string dataId = getDataId();
581 std::vector<std::string> queryItem;
583 unsigned int reqId = event->getReqId();
584 std::stringstream ss, ssreqtype;
585 std::string reqIdStr, reqtypestr;
586 std::string key = event->getKey();
587 std::string oldValue = event->getOldValue();
588 std::string newValue = event->getNewValue();
590 if (checkReqIdUniqueness(reqId) == false)
592 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
595 passData = bundle_create();
597 if (passData == NULL)
599 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
602 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
603 appsvc_set_appid(passData, m_appId.c_str());
608 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_UPDATE;
609 reqtypestr = ssreqtype.str();
612 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
613 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
614 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
615 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
616 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
617 bundle_add(passData, OSP_K_DATACONTROL_PROTOCOL_VERSION, OSP_K_DATACONTROL_PROTOCOL_VERSION_VALUE); // version
620 queryItem.push_back(dataId); // dataid
621 queryItem.push_back(key);
622 queryItem.push_back(oldValue);
623 queryItem.push_back(newValue);
625 addArrayToBundle(passData, queryItem);
626 EventUpdateValuePendingEvent* pendingEvent = new EventUpdateValuePendingEvent(this, dataId, m_appId, m_providerId, "", event);
628 int pid = appsvc_run_service(passData, reqId, dataControlCommonCallback, (void*)pendingEvent);
632 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
635 event->switchToManualAnswer();
637 DPL::Mutex::ScopedLock lock(&m_mutex);
639 catch (const WrtDeviceApis::Commons::Exception& ex)
641 LoggerE("Exception: " << ex.GetMessage());
642 event->setExceptionCode(ex.getCode());
643 event->setErrorMsg(ex.GetMessage());
648 bundle_free(passData);