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>
26 #include "plugin_config.h"
28 #include "JSBluetoothClass.h"
29 #include "BluetoothAdapter.h"
31 #include <TimeTracer.h>
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
40 JSClassDefinition JSBluetoothClass::m_classInfo = {
42 kJSClassAttributeNone,
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 JSBluetoothClass::m_property[] = {
61 { BLUETOOTH_CLASS_MAJOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_CLASS_MINOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63 { BLUETOOTH_CLASS_SERVICES, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
67 JSStaticFunction JSBluetoothClass::m_function[] = {
68 { BLUETOOTH_CLASS_API_HAS_SERVICE, hasService, kJSPropertyAttributeNone },
72 JSClassRef JSBluetoothClass::m_jsClassRef = JSClassCreate(JSBluetoothClass::getClassInfo());
74 const JSClassRef JSBluetoothClass::getClassRef()
77 m_jsClassRef = JSClassCreate(&m_classInfo);
82 const JSClassDefinition* JSBluetoothClass::getClassInfo()
87 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, BluetoothClassSharedPtr bluetoothClass)
89 BluetoothClassHolderPtr holder = new BluetoothClassHolder(bluetoothClass);
90 return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
93 void JSBluetoothClass::initialize(JSContextRef context, JSObjectRef object)
98 void JSBluetoothClass::finalize(JSObjectRef object)
100 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
102 JSObjectSetPrivate(object, NULL);
107 JSValueRef JSBluetoothClass::getProperty(JSContextRef context,
109 JSStringRef propertyName,
110 JSValueRef* exception)
113 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
115 throw TypeMismatchException("Private object is NULL");
118 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MAJOR)) {
119 return JSUtil::toJSValueRef(context, priv->mClass->getMajor());
121 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MINOR)) {
122 return JSUtil::toJSValueRef(context, priv->mClass->getMinor());
124 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_SERVICES)) {
125 return priv->mClass->getServices(context);
127 } catch (const BasePlatformException &err) {
128 LoggerW("Getting property is failed: " << err.getMessage().c_str());
134 JSValueRef JSBluetoothClass::hasService(JSContextRef context,
136 JSObjectRef thisObject,
137 size_t argumentCount,
138 const JSValueRef arguments[],
139 JSValueRef* exception)
141 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
144 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_CLASS_API_HAS_SERVICE);
145 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
148 // Check whether Bluetooth is supported or not
149 if(!BluetoothAdapter::isBluetoothSupported()) {
150 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
154 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(thisObject));
156 throw TypeMismatchException("Private object is NULL.");
159 ArgumentValidator validator(context, argumentCount, arguments);
160 unsigned long service = validator.toULong(0); // uuid
162 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
164 return JSUtil::toJSValueRef(context, priv->mClass->hasService(service));
165 } catch (const BasePlatformException &err) {
166 return JSWebAPIException::throwException(context, exception, err);
168 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothClass.hasService().");
169 return JSWebAPIException::throwException(context, exception, err);