Update change log and spec for wrt-plugins-tizen_0.4.25-1
[platform/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     // do nothing
96 }
97
98 void JSBluetoothClass::finalize(JSObjectRef object)
99 {
100     BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
101     if (priv) {
102         JSObjectSetPrivate(object, NULL);
103         delete priv;
104     }
105 }
106
107 JSValueRef JSBluetoothClass::getProperty(JSContextRef context,
108         JSObjectRef object,
109         JSStringRef propertyName,
110         JSValueRef* exception)
111 {
112     try {
113         BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
114         if (!priv) {
115             throw TypeMismatchException("Private object is NULL");
116         }
117
118         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MAJOR)) {
119             return JSUtil::toJSValueRef(context, priv->mClass->getMajor());
120         }
121         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MINOR)) {
122             return JSUtil::toJSValueRef(context, priv->mClass->getMinor());
123         }
124         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_SERVICES)) {
125             return priv->mClass->getServices(context);
126         }
127     } catch (const BasePlatformException &err) {
128         LoggerW("Getting property is failed: " << err.getMessage().c_str());
129     }
130     
131     return NULL;
132 }
133
134 JSValueRef JSBluetoothClass::hasService(JSContextRef context,
135         JSObjectRef object,
136         JSObjectRef thisObject,
137         size_t argumentCount,
138         const JSValueRef arguments[],
139         JSValueRef* exception)
140 {
141     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
142     
143     // Access Check
144     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_CLASS_API_HAS_SERVICE);
145     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
146
147     try {
148         // Check whether Bluetooth is supported or not
149         if(!BluetoothAdapter::isBluetoothSupported()) {
150             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
151         }
152     
153         // Private Object
154         BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(thisObject));
155         if (!priv) {
156             throw TypeMismatchException("Private object is NULL.");
157         }
158     
159         ArgumentValidator validator(context, argumentCount, arguments);
160         unsigned long service = validator.toULong(0);  // uuid
161
162         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
163         
164         return JSUtil::toJSValueRef(context, priv->mClass->hasService(service));
165     } catch (const BasePlatformException &err) {
166         return JSWebAPIException::throwException(context, exception, err);
167     } catch (...) {
168         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothClass.hasService().");
169         return JSWebAPIException::throwException(context, exception, err);
170     }
171 }
172
173
174
175 } // Bluetooth
176 } // DeviceAPI