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 <PropertyBag.h>
27 #include "plugin_config.h"
28 #include "JSBluetoothManager.h"
29 #include "JSBluetoothClassDeviceMajor.h"
30 #include "JSBluetoothClassDeviceMinor.h"
31 #include "JSBluetoothClassDeviceService.h"
32 //#include "BluetoothManager.h"
33 #include "JSBluetoothAdapter.h"
34 #include "BluetoothAdapter.h"
36 #include <TimeTracer.h>
39 using namespace WrtDeviceApis::Commons;
40 using namespace DeviceAPI::Common;
45 JSClassDefinition JSBluetoothManager::m_classInfo = {
47 kJSClassAttributeNone,
50 m_property, //StaticValues
51 m_function, //StaticFunctions
52 initialize, //Initialize
57 NULL, //DeleteProperty,
58 NULL, //GetPropertyNames,
59 NULL, //CallAsFunction,
60 NULL, //CallAsConstructor,
65 JSStaticValue JSBluetoothManager::m_property[] = {
66 { BLUETOOTH_MANAGER_DEVICE_MAJOR, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete},
67 { BLUETOOTH_MANAGER_DEVICE_MINOR, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete},
68 { BLUETOOTH_MANAGER_DEVICE_SERVICE, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete},
72 JSStaticFunction JSBluetoothManager::m_function[] = {
73 { BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER, getDefaultAdapter, kJSPropertyAttributeNone },
77 JSClassRef JSBluetoothManager::m_jsClassRef = JSClassCreate(JSBluetoothManager::getClassInfo());
79 const JSClassRef JSBluetoothManager::getClassRef()
82 m_jsClassRef = JSClassCreate(&m_classInfo);
87 const JSClassDefinition* JSBluetoothManager::getClassInfo()
92 void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object)
94 if (!JSObjectGetPrivate(object)) {
95 PropertyBag *priv = new PropertyBag();
98 priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MAJOR,
99 JSBluetoothClassDeviceMajor::createJSObject(context));
102 priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MINOR,
103 JSBluetoothClassDeviceMinor::createJSObject(context));
106 priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_SERVICE,
107 JSBluetoothClassDeviceService::createJSObject(context));
109 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
110 LoggerW("Failed to set private data");
115 LoggerW("Failed to create private data");
119 LoggerW("Private data already exists");
123 void JSBluetoothManager::finalize(JSObjectRef object)
125 PropertyBag *priv = static_cast<PropertyBag*>(JSObjectGetPrivate(object));
127 JSObjectSetPrivate(object, NULL);
132 JSValueRef JSBluetoothManager::getReadOnlyProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) {
133 PropertyBag *priv = static_cast<PropertyBag*>(JSObjectGetPrivate(object));
135 LoggerW("There is no private data");
139 std::string name = JSUtil::JSStringToString(context, propertyName);
140 return priv->getProperty(context, propertyName);
143 JSValueRef JSBluetoothManager::getDefaultAdapter(JSContextRef context,
145 JSObjectRef thisObject,
146 size_t argumentCount,
147 const JSValueRef arguments[],
148 JSValueRef* exception)
151 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
154 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER);
155 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
158 if(!BluetoothAdapter::isBluetoothSupported()) {
159 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
162 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
163 return JSBluetoothAdapter::createJSObject(context);
165 } catch (const BasePlatformException &err) {
166 return JSWebAPIException::throwException(context, exception, err);
168 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothManager.getDefaultAdapter().");
169 return JSWebAPIException::throwException(context, exception, err);