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.
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <JSTizenExceptionFactory.h>
25 #include <JSTizenException.h>
26 #include <SecurityExceptions.h>
27 #include "BluetoothFactory.h"
28 #include "IBluetoothAdapterManager.h"
29 #include "BluetoothProperty.h"
30 #include "JSBluetoothManager.h"
31 #include "JSBluetoothAdapter.h"
32 #include "JSBluetoothClassDeviceMajor.h"
33 #include "JSBluetoothClassDeviceMinor.h"
34 #include "JSBluetoothClassDeviceService.h"
35 #include "plugin_config.h"
37 using namespace WrtDeviceApis::Commons;
38 using namespace DeviceAPI::Common;
41 #define DEVICE_MAJOR "deviceMajor"
42 #define DEVICE_MINOR "deviceMinor"
43 #define DEVICE_SERVICE "deviceService"
49 JSClassRef JSBluetoothManager::m_jsClassRef = NULL;
50 JSObjectRef JSBluetoothManager::m_deviceMajor = NULL;
51 JSObjectRef JSBluetoothManager::m_deviceMinor = NULL;
52 JSObjectRef JSBluetoothManager::m_deviceService = NULL;
55 JSClassDefinition JSBluetoothManager::m_classInfo =
58 kJSClassAttributeNone,
77 JSStaticFunction JSBluetoothManager::m_function[] =
79 { "getDefaultAdapter", getDefaultAdapter, kJSPropertyAttributeNone },
84 JSStaticValue JSBluetoothManager::m_properties[] =
86 {DEVICE_MAJOR, getProperty, NULL, kJSPropertyAttributeReadOnly},
87 {DEVICE_MINOR, getProperty, NULL, kJSPropertyAttributeReadOnly},
88 {DEVICE_SERVICE, getProperty, NULL, kJSPropertyAttributeReadOnly},
94 const JSClassRef JSBluetoothManager::getClassRef()
98 m_jsClassRef = JSClassCreate(&m_classInfo);
103 JSValueRef JSBluetoothManager::getDefaultAdapter(JSContextRef context, JSObjectRef object,
104 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
105 JSValueRef* exception)
107 JSBluetoothManagerPriv* priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(thisObject));
115 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
118 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
119 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_MANAGER_GET_DEFAULT_ADAPTER]);
120 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
122 JSContextRef globalContext = priv->getContext();
124 return JSBluetoothAdapter::createJSObject(globalContext);
126 Catch (WrtDeviceApis::Commons::SecurityException)
128 LogError("permission denied error");
129 return JSTizenExceptionFactory::postException(context, exception,
130 JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");
133 Catch(WrtDeviceApis::Commons::UnsupportedException)
135 LogError("UnkownException");
136 return JSTizenExceptionFactory::postException(context, exception,
137 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
139 Catch(WrtDeviceApis::Commons::Exception)
141 LogError("UnkownException");
142 return JSTizenExceptionFactory::postException(context, exception,
143 JSTizenException::UNKNOWN_ERROR, "Unkown error");
148 JSValueRef JSBluetoothManager::getProperty(JSContextRef context,
150 JSStringRef propertyName,
151 JSValueRef* exception)
157 if(JSStringIsEqualToUTF8CString(propertyName, DEVICE_MAJOR))
160 m_deviceMajor = JSBluetoothClassDeviceMajor::createJSObject(context);
161 return m_deviceMajor;
163 else if(JSStringIsEqualToUTF8CString(propertyName, DEVICE_MINOR))
166 m_deviceMinor = JSBluetoothClassDeviceMinor::createJSObject(context);
167 return m_deviceMinor;
169 else if(JSStringIsEqualToUTF8CString(propertyName, DEVICE_SERVICE))
172 m_deviceService = JSBluetoothClassDeviceService::createJSObject(context);
173 return m_deviceService;
179 LogWarning("Exception "<<_rethrown_exception.GetMessage());
187 const JSClassDefinition* JSBluetoothManager::getClassInfo()
193 void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object)
195 JSBluetoothManagerPriv *priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
196 LogDebug("JSBluetoothManager::initialize");
200 LogError("already exist");
204 priv = new JSBluetoothManagerPriv(context);
206 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
208 LogError("Object can't store private data.");
214 void JSBluetoothManager::finalize(JSObjectRef object)
216 JSBluetoothManagerPriv* priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
217 LogDebug("JSBluetoothManager::Finalrize");
221 JSObjectSetPrivate(object, NULL);
222 LogDebug("Deleting BluetoothManager");
227 bool JSBluetoothManager::hasInstance(JSContextRef context, JSObjectRef constructor,
228 JSValueRef possibleInstance, JSValueRef* exception)
230 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());