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>
25 #include "plugin_config.h"
27 #include "JSBluetoothClass.h"
28 #include "BluetoothAdapter.h"
30 #include <TimeTracer.h>
33 using namespace WrtDeviceApis::Commons;
34 using namespace DeviceAPI::Common;
39 JSClassDefinition JSBluetoothClass::m_classInfo = {
41 kJSClassAttributeNone,
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 JSBluetoothClass::m_property[] = {
60 { BLUETOOTH_CLASS_MAJOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
61 { BLUETOOTH_CLASS_MINOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_CLASS_SERVICES, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
66 JSStaticFunction JSBluetoothClass::m_function[] = {
67 { BLUETOOTH_CLASS_API_HAS_SERVICE, hasService, kJSPropertyAttributeNone },
71 JSClassRef JSBluetoothClass::m_jsClassRef = JSClassCreate(JSBluetoothClass::getClassInfo());
73 const JSClassRef JSBluetoothClass::getClassRef()
76 m_jsClassRef = JSClassCreate(&m_classInfo);
81 const JSClassDefinition* JSBluetoothClass::getClassInfo()
86 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, BluetoothClassSharedPtr bluetoothClass)
88 BluetoothClassHolderPtr holder = new BluetoothClassHolder(bluetoothClass);
89 return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
92 void JSBluetoothClass::initialize(JSContextRef context, JSObjectRef object)
97 void JSBluetoothClass::finalize(JSObjectRef object)
99 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
101 JSObjectSetPrivate(object, NULL);
106 JSValueRef JSBluetoothClass::getProperty(JSContextRef context,
108 JSStringRef propertyName,
109 JSValueRef* exception)
112 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
114 throw TypeMismatchException("Private object is NULL");
117 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MAJOR)) {
118 return JSUtil::toJSValueRef(context, priv->mClass->getMajor());
120 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MINOR)) {
121 return JSUtil::toJSValueRef(context, priv->mClass->getMinor());
123 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_SERVICES)) {
124 return priv->mClass->getServices(context);
126 } catch (const BasePlatformException &err) {
127 LoggerW("Getting property is failed: " << err.getMessage().c_str());
133 JSValueRef JSBluetoothClass::hasService(JSContextRef context,
135 JSObjectRef thisObject,
136 size_t argumentCount,
137 const JSValueRef arguments[],
138 JSValueRef* exception)
140 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
143 TIME_TRACER_ITEM_BEGIN("hasService::ACE", 1);
144 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_CLASS_API_HAS_SERVICE);
145 TIME_TRACER_ITEM_END("hasService::ACE", 1);
146 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
150 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(thisObject));
152 throw TypeMismatchException("Private object is NULL.");
155 ArgumentValidator validator(context, argumentCount, arguments);
156 unsigned long service = validator.toULong(0); // uuid
158 JSValueRef result = JSUtil::toJSValueRef(context, priv->mClass->hasService(service));
159 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
162 } catch (const BasePlatformException &err) {
163 return JSWebAPIErrorFactory::postException(context, exception, err);
165 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothClass.hasService().");
166 return JSWebAPIErrorFactory::postException(context, exception, err);