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 <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <PlatformException.h>
25 #include <MultiCallbackUserData.h>
27 #include "plugin_config.h"
28 #include "JSBluetoothServiceHandler.h"
29 #include "BluetoothAdapter.h"
31 #include <TimeTracer.h>
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
40 JSClassDefinition JSBluetoothServiceHandler::m_classInfo = {
42 kJSClassAttributeNone,
43 "BluetoothServiceHandler",
45 m_property, //StaticValues
46 m_function, //StaticFunctions
47 initialize, //Initialize
52 NULL, //DeleteProperty,
53 NULL, //GetPropertyNames,
54 NULL, //CallAsFunction,
55 NULL, //CallAsConstructor,
60 JSStaticValue JSBluetoothServiceHandler::m_property[] = {
61 { BLUETOOTH_SERVICE_HANDLER_UUID, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_SERVICE_HANDLER_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63 { BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
64 { BLUETOOTH_SERVICE_HANDLER_ONCONNECT, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
68 JSStaticFunction JSBluetoothServiceHandler::m_function[] = {
69 { BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER, unregister, kJSPropertyAttributeNone },
73 JSClassRef JSBluetoothServiceHandler::m_jsClassRef = JSClassCreate(JSBluetoothServiceHandler::getClassInfo());
75 const JSClassRef JSBluetoothServiceHandler::getClassRef()
78 m_jsClassRef = JSClassCreate(&m_classInfo);
83 const JSClassDefinition* JSBluetoothServiceHandler::getClassInfo()
88 JSObjectRef JSBluetoothServiceHandler::createJSObject(JSContextRef context, BluetoothServiceHandlerPtr service)
90 return JSObjectMake(context, getClassRef(), static_cast<void*>(service));
93 void JSBluetoothServiceHandler::initialize(JSContextRef context, JSObjectRef object)
98 void JSBluetoothServiceHandler::finalize(JSObjectRef object)
100 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
102 JSObjectSetPrivate(object, NULL);
107 JSValueRef JSBluetoothServiceHandler::getProperty(JSContextRef context,
109 JSStringRef propertyName,
110 JSValueRef* exception)
113 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
115 throw TypeMismatchException("Private object is NULL");
118 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_UUID)) {
119 return JSUtil::toJSValueRef(context, priv->getUUID());
121 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_NAME)) {
122 return JSUtil::toJSValueRef(context, priv->getName());
124 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED)) {
125 return JSUtil::toJSValueRef(context, priv->getConnectionState());
127 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_ONCONNECT)) {
128 return priv->getOnConnect(context);
130 } catch (const BasePlatformException &err) {
131 LoggerW("Getting property is failed: " << err.getMessage().c_str());
137 bool JSBluetoothServiceHandler::setProperty(JSContextRef context,
139 JSStringRef propertyName,
141 JSValueRef* exception)
144 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
146 throw TypeMismatchException("Private object is NULL");
149 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_ONCONNECT)) {
150 JSObjectRef object = NULL;
151 if(!JSValueIsNull(context, value)) {
152 if(!JSValueIsObject(context, value)) {
153 throw TypeMismatchException("Value is not Object");
156 JSValueRef ex = NULL;
157 object = JSValueToObject(context, value, &ex);
159 throw TypeMismatchException("Can't convert to Object");
162 if(!JSObjectIsFunction(context, object)) {
163 throw TypeMismatchException("Not function");
167 LoggerD("onconnect() is NULL");
170 return priv->setOnConnect(context, object);
172 } catch (const BasePlatformException &err) {
173 JSWebAPIException::throwException(context, exception, err);
179 JSValueRef JSBluetoothServiceHandler::unregister(JSContextRef context,
181 JSObjectRef thisObject,
182 size_t argumentCount,
183 const JSValueRef arguments[],
184 JSValueRef* exception)
186 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
189 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER);
190 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
193 // Check whether Bluetooth is supported or not
194 if(!BluetoothAdapter::isBluetoothSupported()) {
195 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
199 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(thisObject));
201 throw TypeMismatchException("Private object is NULL.");
204 ArgumentValidator validator(context, argumentCount, arguments);
205 JSObjectRef successCallback = validator.toFunction(0, true); // successCallback
206 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
209 MultiCallbackUserDataPtr callback(
210 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
212 LoggerW("Can't create MultiCallbackUserData");
215 callback->setCallback("success", successCallback);
216 callback->setCallback("error", errorCallback);
219 priv->unregister(callback);
220 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
222 return JSValueMakeUndefined(context);
223 } catch (const BasePlatformException &err) {
224 return JSWebAPIException::throwException(context, exception, err);
226 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothServiceHandler.unregister().");
227 return JSWebAPIException::throwException(context, exception, err);