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.
18 #include <SecurityExceptions.h>
21 #include <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <PlatformException.h>
24 #include <MultiCallbackUserData.h>
26 #include "plugin_config.h"
27 #include "JSBluetoothServiceHandler.h"
28 #include "BluetoothAdapter.h"
30 #include <TimeTracer.h>
33 using namespace WrtDeviceApis::Commons;
34 using namespace DeviceAPI::Common;
39 JSClassDefinition JSBluetoothServiceHandler::m_classInfo = {
41 kJSClassAttributeNone,
42 "BluetoothServiceHandler",
44 m_property, //StaticValues
45 m_function, //StaticFunctions
46 initialize, //Initialize
51 NULL, //DeleteProperty,
52 NULL, //GetPropertyNames,
53 NULL, //CallAsFunction,
54 NULL, //CallAsConstructor,
59 JSStaticValue JSBluetoothServiceHandler::m_property[] = {
60 { BLUETOOTH_SERVICE_HANDLER_UUID, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
61 { BLUETOOTH_SERVICE_HANDLER_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63 { BLUETOOTH_SERVICE_HANDLER_ONCONNECT, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
67 JSStaticFunction JSBluetoothServiceHandler::m_function[] = {
68 { BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER, unregister, kJSPropertyAttributeNone },
72 JSClassRef JSBluetoothServiceHandler::m_jsClassRef = JSClassCreate(JSBluetoothServiceHandler::getClassInfo());
74 const JSClassRef JSBluetoothServiceHandler::getClassRef()
77 m_jsClassRef = JSClassCreate(&m_classInfo);
82 const JSClassDefinition* JSBluetoothServiceHandler::getClassInfo()
87 JSObjectRef JSBluetoothServiceHandler::createJSObject(JSContextRef context, BluetoothServiceHandlerPtr service)
89 return JSObjectMake(context, getClassRef(), static_cast<void*>(service));
92 void JSBluetoothServiceHandler::initialize(JSContextRef context, JSObjectRef object)
97 void JSBluetoothServiceHandler::finalize(JSObjectRef object)
99 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
101 JSObjectSetPrivate(object, NULL);
106 JSValueRef JSBluetoothServiceHandler::getProperty(JSContextRef context,
108 JSStringRef propertyName,
109 JSValueRef* exception)
112 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
114 throw TypeMismatchException("Private object is NULL");
117 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_UUID)) {
118 return JSUtil::toJSValueRef(context, priv->getUUID());
120 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_NAME)) {
121 return JSUtil::toJSValueRef(context, priv->getName());
123 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED)) {
124 return JSUtil::toJSValueRef(context, priv->getConnectionState());
126 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_ONCONNECT)) {
127 return priv->getOnConnect(context);
129 } catch (const BasePlatformException &err) {
130 LoggerW("Getting property is failed: " << err.getMessage().c_str());
136 bool JSBluetoothServiceHandler::setProperty(JSContextRef context,
138 JSStringRef propertyName,
140 JSValueRef* exception)
143 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
145 throw TypeMismatchException("Private object is NULL");
148 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_ONCONNECT)) {
149 JSObjectRef object = NULL;
150 if(!JSValueIsNull(context, value)) {
151 if(!JSValueIsObject(context, value)) {
152 throw TypeMismatchException("Value is not Object");
155 object = JSUtil::JSValueToObject(context, value);
157 if(!JSObjectIsFunction(context, object)) {
158 throw TypeMismatchException("Not function");
162 LoggerD("onconnect() is NULL");
165 return priv->setOnConnect(context, object);
167 } catch (const BasePlatformException &err) {
168 JSWebAPIErrorFactory::postException(context, exception, err);
174 JSValueRef JSBluetoothServiceHandler::unregister(JSContextRef context,
176 JSObjectRef thisObject,
177 size_t argumentCount,
178 const JSValueRef arguments[],
179 JSValueRef* exception)
181 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
187 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(thisObject));
189 throw TypeMismatchException("Private object is NULL.");
193 TIME_TRACER_ITEM_BEGIN("unregister::ACE", 1);
194 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER);
195 TIME_TRACER_ITEM_END("unregister::ACE", 1);
198 ArgumentValidator validator(context, argumentCount, arguments);
199 JSObjectRef successCallback = validator.toFunction(0, true); // successCallback
200 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
203 MultiCallbackUserDataPtr callback(
204 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
206 LoggerW("Can't create MultiCallbackUserData");
209 callback->setCallback("success", successCallback);
210 callback->setCallback("error", errorCallback);
213 priv->unregister(callback);
214 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
216 return JSValueMakeUndefined(context);
217 } catch (const BasePlatformException &err) {
218 return JSWebAPIErrorFactory::postException(context, exception, err);
220 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothServiceHandler.unregister().");
221 return JSWebAPIErrorFactory::postException(context, exception, err);