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 JSValueRef ex = NULL;
156 object = JSValueToObject(context, value, &ex);
158 throw TypeMismatchException("Can't convert to Object");
161 if(!JSObjectIsFunction(context, object)) {
162 throw TypeMismatchException("Not function");
166 LoggerD("onconnect() is NULL");
169 return priv->setOnConnect(context, object);
171 } catch (const BasePlatformException &err) {
172 JSWebAPIErrorFactory::postException(context, exception, err);
178 JSValueRef JSBluetoothServiceHandler::unregister(JSContextRef context,
180 JSObjectRef thisObject,
181 size_t argumentCount,
182 const JSValueRef arguments[],
183 JSValueRef* exception)
185 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
188 TIME_TRACER_ITEM_BEGIN("unregister::ACE", 1);
189 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER);
190 TIME_TRACER_ITEM_END("unregister::ACE", 1);
191 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
195 BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(thisObject));
197 throw TypeMismatchException("Private object is NULL.");
200 ArgumentValidator validator(context, argumentCount, arguments);
201 JSObjectRef successCallback = validator.toFunction(0, true); // successCallback
202 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
205 MultiCallbackUserDataPtr callback(
206 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
208 LoggerW("Can't create MultiCallbackUserData");
211 callback->setCallback("success", successCallback);
212 callback->setCallback("error", errorCallback);
215 priv->unregister(callback);
216 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
218 return JSValueMakeUndefined(context);
219 } catch (const BasePlatformException &err) {
220 return JSWebAPIErrorFactory::postException(context, exception, err);
222 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothServiceHandler.unregister().");
223 return JSWebAPIErrorFactory::postException(context, exception, err);