wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothClass.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <PlatformException.h>
25
26 #include "plugin_config.h"
27
28 #include "JSBluetoothClass.h"
29 #include "BluetoothAdapter.h"
30
31 #include <TimeTracer.h>
32 #include <Logger.h>
33
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
36
37 namespace DeviceAPI {
38 namespace Bluetooth {
39
40 JSClassDefinition JSBluetoothClass::m_classInfo = {
41     0,
42     kJSClassAttributeNone,
43     "BluetoothClass",
44     NULL, //ParentClass
45     m_property, //StaticValues
46     m_function, //StaticFunctions
47     initialize, //Initialize
48     finalize, //Finalize
49     NULL, //HasProperty,
50     NULL, //GetProperty,
51     NULL, //SetProperty,
52     NULL, //DeleteProperty,
53     NULL, //GetPropertyNames,
54     NULL, //CallAsFunction,
55     NULL, //CallAsConstructor,
56     NULL, //HasInstance,
57     NULL //ConvertToType
58 };
59
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 },
64     { 0, 0, 0, 0 }
65 };
66
67 JSStaticFunction JSBluetoothClass::m_function[] = {
68     { BLUETOOTH_CLASS_API_HAS_SERVICE, hasService, kJSPropertyAttributeNone },
69     { 0, 0, 0 }
70 };
71
72 JSClassRef JSBluetoothClass::m_jsClassRef = JSClassCreate(JSBluetoothClass::getClassInfo());
73
74 const JSClassRef JSBluetoothClass::getClassRef()
75 {
76     if (!m_jsClassRef) {
77         m_jsClassRef = JSClassCreate(&m_classInfo);
78     }
79     return m_jsClassRef;
80 }
81
82 const JSClassDefinition* JSBluetoothClass::getClassInfo()
83 {
84     return &m_classInfo;
85 }
86
87 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, BluetoothClassSharedPtr bluetoothClass)
88 {
89     BluetoothClassHolderPtr holder = new BluetoothClassHolder(bluetoothClass);
90     return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
91 }
92
93 void JSBluetoothClass::initialize(JSContextRef context, JSObjectRef object)
94 {
95     LoggerD("Enter");
96     // do nothing
97 }
98
99 void JSBluetoothClass::finalize(JSObjectRef object)
100 {
101     BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
102     if (priv) {
103         JSObjectSetPrivate(object, NULL);
104         delete priv;
105     }
106 }
107
108 JSValueRef JSBluetoothClass::getProperty(JSContextRef context,
109         JSObjectRef object,
110         JSStringRef propertyName,
111         JSValueRef* exception)
112 {
113     try {
114         BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
115         if (!priv) {
116             throw TypeMismatchException("Private object is NULL");
117         }
118
119         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MAJOR)) {
120             return JSUtil::toJSValueRef(context, priv->mClass->getMajor());
121         }
122         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MINOR)) {
123             return JSUtil::toJSValueRef(context, priv->mClass->getMinor());
124         }
125         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_SERVICES)) {
126             return priv->mClass->getServices(context);
127         }
128     } catch (const BasePlatformException &err) {
129         LoggerW("Getting property is failed: " << err.getMessage().c_str());
130     }
131     
132     return NULL;
133 }
134
135 JSValueRef JSBluetoothClass::hasService(JSContextRef context,
136         JSObjectRef object,
137         JSObjectRef thisObject,
138         size_t argumentCount,
139         const JSValueRef arguments[],
140         JSValueRef* exception)
141 {
142     LoggerD("Enter");
143     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
144     
145     // Access Check
146     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_CLASS_API_HAS_SERVICE);
147     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
148
149     try {
150         // Check whether Bluetooth is supported or not
151         if(!BluetoothAdapter::isBluetoothSupported()) {
152             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
153         }
154     
155         // Private Object
156         BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(thisObject));
157         if (!priv) {
158             throw TypeMismatchException("Private object is NULL.");
159         }
160     
161         ArgumentValidator validator(context, argumentCount, arguments);
162         unsigned long service = validator.toULong(0);  // uuid
163         if(service > 65535) {
164             throw InvalidValuesException("Invalid value");
165         }
166         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
167         
168         return JSUtil::toJSValueRef(context, priv->mClass->hasService(service));
169     } catch (const BasePlatformException &err) {
170         return JSWebAPIException::throwException(context, exception, err);
171     } catch (...) {
172         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothClass.hasService().");
173         return JSWebAPIException::throwException(context, exception, err);
174     }
175 }
176
177
178
179 } // Bluetooth
180 } // DeviceAPI