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>
32 namespace DataControl {
37 static const char OSP_V_CALLER_TYPE_OSP[] = "osp";
38 static const char OSP_V_LAUNCH_TYPE_LAUNCH[] = "launch";
39 static const char OSP_V_LAUNCH_TYPE_APPCONTROL[] = "appcontrol";
40 static const char OSP_V_LAUNCH_TYPE_DATACONTROL[] = "datacontrol";
41 static const char OSP_V_LAUNCH_TYPE_CONDTION[] = "condition";
42 static const char OSP_V_REQUEST_TYPE_SQL_QUERY[] = "sql_query";
43 static const char OSP_V_REQUEST_TYPE_SQL_INSERT[] = "sql_insert";
44 static const char OSP_V_REQUEST_TYPE_SQL_UPDATE[] = "sql_update";
45 static const char OSP_V_REQUEST_TYPE_SQL_DELETE[] = "sql_delete";
46 static const char OSP_V_REQUEST_TYPE_MAP_QEURY[] = "map_query";
47 static const char OSP_V_REQUEST_TYPE_MAP_INSERT[] = "map_insert";
48 static const char OSP_V_REQUEST_TYPE_MAP_UPDATE[] = "map_update";
49 static const char OSP_V_REQUEST_TYPE_MAP_DELETE[] = "map_delete";
50 static const char BUNDLE_KEY_WINDOW[] = "__APP_SVC_K_WIN_ID__";
53 #define OSP_K_CALLER_TYPE "__OSP_CALLER_TYPE__"
54 #define OSP_K_LAUNCH_TYPE "__OSP_LAUNCH_TYPE__"
55 #define OSP_K_ARG "__OSP_ARGS__"
56 #define OSP_K_COND "__OSP_COND_NAME__"
57 #define OSP_K_APPID "__OSP_APPID__"
58 #define OSP_K_REQUEST_ID "__OSP_REQUEST_ID__"
59 #define OSP_K_APPCONTROL_PROVIDER "__OSP_APPCONTROL_PROVIDER__"
60 #define OSP_K_APPCONTROL_OPERATION "__OSP_APPCONTROL_OPERATION__"
61 #define OSP_K_APPCONTROL_CATEGORY "__OSP_APPCONTROL_CATEGORY__"
62 #define OSP_K_APPCONTROL_MIME "__OSP_APPCONTROL_MIME__"
63 #define OSP_K_APPCONTROL_URI "__OSP_APPCONTROL_URI__"
64 #define OSP_K_DATACONTROL_PROVIDER "__OSP_DATACONTROL_PROVIDER__"
65 #define OSP_K_DATACONTROL_REQUEST_TYPE "__OSP_DATACONTROL_REQUEST_TYPE__"
67 #define RESULT_TRUE_FROM_OSP "1"
68 #define RESULT_FALSE_FROM_OSP "0"
70 static std::vector<std::string> getDataArrayFromBundle(bundle *b, std::string key)
72 std::vector<std::string> result;
77 array = appsvc_get_data_array(b, key.c_str(), &length);
79 if (array == NULL || length == 0)
81 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result data fail from datacontrol provider");
84 for (index = 0; index < length; index++)
86 result.push_back(array[index]);
93 static void MappedDataControlGetValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
97 EventGetValuePendingEvent *pendingEvent = NULL;
98 MappedDataControlConsumer *consumer = NULL;
99 EventGetValuePtr event;
107 LogDebug("data null, can not send result to JS Layer");
111 pendingEvent = (EventGetValuePendingEvent *)data;
112 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
113 event = pendingEvent->getEvent();
117 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
119 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
121 for (index = 0; index < result.size(); index++)
123 LogDebug(result[index]);
126 if (result.size() < 3)
128 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
131 // 0 : result true or false??
132 if (RESULT_TRUE_FROM_OSP != result[0])
135 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
138 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
141 std::istringstream str(result[2]);
147 for (index = 3; index < result.size(); index++)
149 event->addResultValue(result[index]);
155 catch (const WrtDeviceApis::Commons::Exception& ex)
157 LogError("Exception: " << ex.GetMessage());
158 event->setExceptionCode(ex.getCode());
159 event->setErrorMsg(ex.GetMessage());
161 consumer->handlePendingEvent(event);
170 static void MappedDataControlAddValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
175 EventAddValuePendingEvent *pendingEvent = NULL;
176 MappedDataControlConsumer *consumer = NULL;
177 EventAddValuePtr event;
183 LogDebug("data null, can not send result to JS Layer");
187 pendingEvent = (EventAddValuePendingEvent *)data;
188 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
189 event = pendingEvent->getEvent();
193 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
195 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
197 for (size_t index = 0; index < result.size(); index++)
199 LogDebug(result[index]);
202 if (result.size() < 2)
204 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
207 // 0 : result true or false??
208 if (RESULT_TRUE_FROM_OSP != result[0])
211 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
214 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
217 catch (const WrtDeviceApis::Commons::Exception& ex)
219 LogError("Exception: " << ex.GetMessage());
220 event->setExceptionCode(ex.getCode());
221 event->setErrorMsg(ex.GetMessage());
223 consumer->handlePendingEvent(event);
232 static void MappedDataControlRemoveValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
238 LogDebug("Data or Bundle error");
242 EventRemoveValuePendingEvent* pendingEvent = NULL;
243 MappedDataControlConsumer *consumer = NULL;
244 EventRemoveValuePtr event;
250 LogDebug("data null, can not send result to JS Layer");
254 pendingEvent = (EventRemoveValuePendingEvent *)data;
255 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
256 event = pendingEvent->getEvent();
260 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
262 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
264 for (size_t index = 0; index < result.size(); index++)
266 LogDebug(result[index]);
269 if (result.size() < 2)
271 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
274 // 0 : result true or false??
275 if (RESULT_TRUE_FROM_OSP != result[0])
278 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
281 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
285 catch (const WrtDeviceApis::Commons::Exception& ex)
287 LogError("Exception: " << ex.GetMessage());
288 event->setExceptionCode(ex.getCode());
289 event->setErrorMsg(ex.GetMessage());
291 consumer->handlePendingEvent(event);
299 static void MappedDataControlUpdateValueCallback(bundle* b, int request_code, appsvc_result_val res, void* data)
303 EventUpdateValuePendingEvent* pendingEvent = NULL;
304 MappedDataControlConsumer *consumer = NULL;
305 EventUpdateValuePtr event;
311 LogDebug("data null, can not send result to JS Layer");
315 pendingEvent = (EventUpdateValuePendingEvent *)data;
316 consumer = (MappedDataControlConsumer*)pendingEvent->getThisObject();
317 event = pendingEvent->getEvent();
321 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Get result fail from datacontrol provider");
323 std::vector<std::string> result = getDataArrayFromBundle(b, OSP_K_ARG);
325 for (size_t index = 0; index < result.size(); index++)
327 LogDebug(result[index]);
330 if (result.size() < 2)
332 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid result from datacontrol provider");
335 // 0 : result true or false??
336 if (RESULT_TRUE_FROM_OSP != result[0])
339 ThrowMsg(WrtDeviceApis::Commons::PlatformException, result[1]);
342 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
345 catch (const WrtDeviceApis::Commons::Exception& ex)
347 LogError("Exception: " << ex.GetMessage());
348 event->setExceptionCode(ex.getCode());
349 event->setErrorMsg(ex.GetMessage());
351 consumer->handlePendingEvent(event);
360 MappedDataControlConsumer::MappedDataControlConsumer()
371 MappedDataControlConsumer::~MappedDataControlConsumer()
376 DPL::Mutex MappedDataControlConsumer::m_mutex;
379 void MappedDataControlConsumer::setType(std::string& type)
381 if (type != MAP_DATA_CONTROL && type != SQL_DATA_CONTROL)
382 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "type mismatch");
387 void MappedDataControlConsumer::setProviderId(const std::string& id )
392 void MappedDataControlConsumer::setDataId(const std::string& id )
398 std::string MappedDataControlConsumer::getDataId()
403 std::string MappedDataControlConsumer::getProviderId()
408 std::string MappedDataControlConsumer::getType()
416 std::string MappedDataControlConsumer::getApplicationId(const std::string& provId)
418 std::string appIdStr = "";
422 const char *passId = provId.c_str();
424 if (m_appId.length() == 0)
427 LogDebug("need " << passId);
429 if( pkgmgr_datacontrol_get_info(passId, OSP_PKGINFO_MAP_TYPE, &appId, &access) < 0)
431 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "get appId error");
456 void MappedDataControlConsumer::addArrayToBundle(bundle* passData, std::vector<std::string>& array)
458 size_t arraySize = array.size();
462 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "size = 0");
465 const char** arr = NULL;
466 arr = (const char**)calloc(sizeof(char*), arraySize);
470 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "null");
473 for (size_t index = 0; index < arraySize; index++)
475 arr[index] = array[index].c_str();
479 bundle_add_str_array(passData, OSP_K_ARG, arr, arraySize);
487 void MappedDataControlConsumer::handlePendingEvent(const EventAddValuePtr& event)
489 WrtDeviceApis::Commons::EventRequestReceiver<EventAddValue>::ManualAnswer(event);
490 removeReqId(event->getReqId());
492 void MappedDataControlConsumer::handlePendingEvent(const EventRemoveValuePtr& event)
494 WrtDeviceApis::Commons::EventRequestReceiver<EventRemoveValue>::ManualAnswer(event);
495 removeReqId(event->getReqId());
497 void MappedDataControlConsumer::handlePendingEvent(const EventGetValuePtr& event)
499 WrtDeviceApis::Commons::EventRequestReceiver<EventGetValue>::ManualAnswer(event);
500 removeReqId(event->getReqId());
503 void MappedDataControlConsumer::handlePendingEvent(const EventUpdateValuePtr& event)
505 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdateValue>::ManualAnswer(event);
506 removeReqId(event->getReqId());
510 void MappedDataControlConsumer::addValue(const EventAddValuePtr& event)
512 WrtDeviceApis::Commons::EventRequestReceiver<EventAddValue>::PostRequest(event);
515 void MappedDataControlConsumer::removeValue(const EventRemoveValuePtr& event)
517 WrtDeviceApis::Commons::EventRequestReceiver<EventRemoveValue>::PostRequest(event);
520 void MappedDataControlConsumer::getValue(const EventGetValuePtr& event)
522 WrtDeviceApis::Commons::EventRequestReceiver<EventGetValue>::PostRequest(event);
525 void MappedDataControlConsumer::updateValue(const EventUpdateValuePtr& event)
527 WrtDeviceApis::Commons::EventRequestReceiver<EventUpdateValue>::PostRequest(event);
530 bool MappedDataControlConsumer::checkReqIdUniqueness(unsigned int reqId)
534 for (index = 0; index < m_currentReqIds.size(); index++)
536 if (m_currentReqIds[index] == reqId)
543 void MappedDataControlConsumer::removeReqId(unsigned int reqId)
547 std::vector<unsigned int>::iterator it, found;
549 for (it = m_currentReqIds.begin(); it != m_currentReqIds.end(); ++it)
560 DPL::Mutex::ScopedLock lock(&m_mutex);
561 m_currentReqIds.erase(found);
566 void MappedDataControlConsumer::OnRequestReceived(const EventAddValuePtr& event)
569 bundle* passData = NULL;
573 std::string dataId = getDataId();
574 unsigned int reqId = event->getReqId();
575 std::string key = event->getKey();
576 std::string value = event->getValue();
578 std::stringstream ss, ssreqtype;
579 std::string reqIdStr, reqtypestr;
580 std::vector<std::string> queryItem;
582 if (checkReqIdUniqueness(reqId) == false)
584 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
592 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_INSERT;
593 reqtypestr = ssreqtype.str();
596 // qi993y8s4e.DataControlProviderService
597 std::string appId = getApplicationId(m_providerId);
598 //.DataControlProviderService";
600 passData = bundle_create();
602 if (passData == NULL)
604 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
607 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
609 appsvc_set_appid(passData, appId.c_str());
611 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
612 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
613 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
614 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
615 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
617 queryItem.push_back(dataId); // dataid
618 queryItem.push_back(key); // key
619 queryItem.push_back(value); // value
621 addArrayToBundle(passData, queryItem);
624 // reqid sholud be known
625 EventAddValuePendingEvent* pendingEvent = new EventAddValuePendingEvent((void*)this, event);
626 int pid = appsvc_run_service(passData, reqId, MappedDataControlAddValueCallback, (void*)pendingEvent);
630 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Error");
633 event->switchToManualAnswer();
635 DPL::Mutex::ScopedLock lock(&m_mutex);
636 m_currentReqIds.push_back(reqId);
638 catch (const WrtDeviceApis::Commons::Exception& ex)
640 event->setExceptionCode(ex.getCode());
641 event->setErrorMsg(ex.GetMessage());
642 LogError("Exception: " << ex.GetMessage());
649 bundle_free(passData);
655 void MappedDataControlConsumer::OnRequestReceived(const EventRemoveValuePtr& event)
658 bundle* passData = NULL;
662 std::string dataId = getDataId();
663 std::vector<std::string> queryItem;
664 std::string key = event->getKey();
665 std::string value = event->getValue();
667 unsigned int reqId = event->getReqId();
668 std::stringstream ss, ssreqtype;
669 std::string reqIdStr, reqtypestr;
674 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_DELETE;
675 reqtypestr = ssreqtype.str();
677 if (checkReqIdUniqueness(reqId) == false)
679 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
683 std::string appId = getApplicationId(m_providerId);
684 passData = bundle_create();
686 if (passData == NULL)
688 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
691 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
692 appsvc_set_appid(passData, appId.c_str());
694 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
695 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
696 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
697 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
698 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
700 queryItem.push_back(dataId); // dataid
701 queryItem.push_back(key);
702 queryItem.push_back(value);
705 addArrayToBundle(passData, queryItem);
706 EventRemoveValuePendingEvent* pendingEvent = new EventRemoveValuePendingEvent(this, event);
708 int pid = appsvc_run_service(passData, reqId, MappedDataControlRemoveValueCallback, (void*)pendingEvent);
712 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
715 event->switchToManualAnswer();
717 DPL::Mutex::ScopedLock lock(&m_mutex);
718 m_currentReqIds.push_back(reqId);
720 catch (const WrtDeviceApis::Commons::Exception& ex)
722 LogError("Exception: " << ex.GetMessage());
723 event->setExceptionCode(ex.getCode());
724 event->setErrorMsg(ex.GetMessage());
729 bundle_free(passData);
737 void MappedDataControlConsumer::OnRequestReceived(const EventGetValuePtr& event)
740 bundle* passData = NULL;
744 std::string dataId = getDataId();
745 std::vector<std::string> queryItem;
747 unsigned int reqId = event->getReqId();
748 std::stringstream ss, ssreqtype;
749 std::string reqIdStr, reqtypestr;
750 std::string key = event->getKey();
752 if (checkReqIdUniqueness(reqId) == false)
754 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
761 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_QUERY;
762 reqtypestr = ssreqtype.str();
764 std::string appId = getApplicationId(m_providerId);
766 passData = bundle_create();
768 if (passData == NULL)
770 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
773 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
774 appsvc_set_appid(passData, appId.c_str());
776 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
777 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
778 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
779 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
780 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
782 queryItem.push_back(dataId); // dataid
783 queryItem.push_back(key);
784 queryItem.push_back("1");
785 queryItem.push_back("1");
787 addArrayToBundle(passData, queryItem);
788 EventGetValuePendingEvent* pendingEvent = new EventGetValuePendingEvent(this, event);
790 int pid = appsvc_run_service(passData, reqId, MappedDataControlGetValueCallback, (void*)pendingEvent);
794 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
797 event->switchToManualAnswer();
799 DPL::Mutex::ScopedLock lock(&m_mutex);
800 m_currentReqIds.push_back(reqId);
802 catch (const WrtDeviceApis::Commons::Exception& ex)
804 LogError("Exception: " << ex.GetMessage());
805 event->setExceptionCode(ex.getCode());
806 event->setErrorMsg(ex.GetMessage());
811 bundle_free(passData);
817 void MappedDataControlConsumer::OnRequestReceived(const EventUpdateValuePtr& event)
820 bundle* passData = NULL;
824 std::string dataId = getDataId();
825 std::vector<std::string> queryItem;
827 unsigned int reqId = event->getReqId();
828 std::stringstream ss, ssreqtype;
829 std::string reqIdStr, reqtypestr;
830 std::string key = event->getKey();
831 std::string oldValue = event->getOldValue();
832 std::string newValue = event->getNewValue();
834 if (checkReqIdUniqueness(reqId) == false)
836 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "Duplicated requested id");
839 std::string appId = getApplicationId(m_providerId);
840 passData = bundle_create();
842 if (passData == NULL)
844 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "ipc memory allocation fail");
847 appsvc_set_operation(passData, APPSVC_OPERATION_DEFAULT);
848 appsvc_set_appid(passData, appId.c_str());
853 ssreqtype << _DATACONTROL_REQUEST_TYPE_MAP_UPDATE;
854 reqtypestr = ssreqtype.str();
857 bundle_add(passData, OSP_K_REQUEST_ID, reqIdStr.c_str());
858 bundle_add(passData, OSP_K_CALLER_TYPE, OSP_V_CALLER_TYPE_OSP);
859 bundle_add(passData, OSP_K_LAUNCH_TYPE, OSP_V_LAUNCH_TYPE_DATACONTROL);
860 bundle_add(passData, OSP_K_DATACONTROL_REQUEST_TYPE, reqtypestr.c_str());
861 bundle_add(passData, OSP_K_DATACONTROL_PROVIDER, m_providerId.c_str());
863 queryItem.push_back(dataId); // dataid
864 queryItem.push_back(key);
865 queryItem.push_back(oldValue);
866 queryItem.push_back(newValue);
868 addArrayToBundle(passData, queryItem);
869 EventUpdateValuePendingEvent* pendingEvent = new EventUpdateValuePendingEvent(this, event);
871 int pid = appsvc_run_service(passData, reqId, MappedDataControlUpdateValueCallback, (void*)pendingEvent);
875 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "dataconstrol request fail.(can not launch)");
878 event->switchToManualAnswer();
880 DPL::Mutex::ScopedLock lock(&m_mutex);
881 m_currentReqIds.push_back(reqId);
883 catch (const WrtDeviceApis::Commons::Exception& ex)
885 LogError("Exception: " << ex.GetMessage());
886 event->setExceptionCode(ex.getCode());
887 event->setErrorMsg(ex.GetMessage());
892 bundle_free(passData);