Update change log and spec for wrt-plugins-tizen_0.4.44
[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 <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <PlatformException.h>
24
25 #include "plugin_config.h"
26
27 #include "JSBluetoothClass.h"
28 #include "BluetoothAdapter.h"
29
30 #include <TimeTracer.h>
31 #include <Logger.h>
32
33 using namespace WrtDeviceApis::Commons;
34 using namespace DeviceAPI::Common;
35
36 namespace DeviceAPI {
37 namespace Bluetooth {
38
39 JSClassDefinition JSBluetoothClass::m_classInfo = {
40     0,
41     kJSClassAttributeNone,
42     "BluetoothClass",
43     NULL, //ParentClass
44     m_property, //StaticValues
45     m_function, //StaticFunctions
46     initialize, //Initialize
47     finalize, //Finalize
48     NULL, //HasProperty,
49     NULL, //GetProperty,
50     NULL, //SetProperty,
51     NULL, //DeleteProperty,
52     NULL, //GetPropertyNames,
53     NULL, //CallAsFunction,
54     NULL, //CallAsConstructor,
55     NULL, //HasInstance,
56     NULL //ConvertToType
57 };
58
59 JSStaticValue JSBluetoothClass::m_property[] = {
60     { BLUETOOTH_CLASS_MAJOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
61     { BLUETOOTH_CLASS_MINOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62     { BLUETOOTH_CLASS_SERVICES, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63     { 0, 0, 0, 0 }
64 };
65
66 JSStaticFunction JSBluetoothClass::m_function[] = {
67     { BLUETOOTH_CLASS_API_HAS_SERVICE, hasService, kJSPropertyAttributeNone },
68     { 0, 0, 0 }
69 };
70
71 JSClassRef JSBluetoothClass::m_jsClassRef = JSClassCreate(JSBluetoothClass::getClassInfo());
72
73 const JSClassRef JSBluetoothClass::getClassRef()
74 {
75     if (!m_jsClassRef) {
76         m_jsClassRef = JSClassCreate(&m_classInfo);
77     }
78     return m_jsClassRef;
79 }
80
81 const JSClassDefinition* JSBluetoothClass::getClassInfo()
82 {
83     return &m_classInfo;
84 }
85
86 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, BluetoothClassSharedPtr bluetoothClass)
87 {
88     BluetoothClassHolderPtr holder = new BluetoothClassHolder(bluetoothClass);
89     return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
90 }
91
92 void JSBluetoothClass::initialize(JSContextRef context, JSObjectRef object)
93 {
94     // do nothing
95 }
96
97 void JSBluetoothClass::finalize(JSObjectRef object)
98 {
99     BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
100     if (priv) {
101         JSObjectSetPrivate(object, NULL);
102         delete priv;
103     }
104 }
105
106 JSValueRef JSBluetoothClass::getProperty(JSContextRef context,
107         JSObjectRef object,
108         JSStringRef propertyName,
109         JSValueRef* exception)
110 {
111     try {
112         BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
113         if (!priv) {
114             throw TypeMismatchException("Private object is NULL");
115         }
116
117         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MAJOR)) {
118             return JSUtil::toJSValueRef(context, priv->mClass->getMajor());
119         }
120         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MINOR)) {
121             return JSUtil::toJSValueRef(context, priv->mClass->getMinor());
122         }
123         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_SERVICES)) {
124             return priv->mClass->getServices(context);
125         }
126     } catch (const BasePlatformException &err) {
127         LoggerW("Getting property is failed: " << err.getMessage().c_str());
128     }
129     
130     return NULL;
131 }
132
133 JSValueRef JSBluetoothClass::hasService(JSContextRef context,
134         JSObjectRef object,
135         JSObjectRef thisObject,
136         size_t argumentCount,
137         const JSValueRef arguments[],
138         JSValueRef* exception)
139 {
140     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
141     
142     // Access Check
143     TIME_TRACER_ITEM_BEGIN("hasService::ACE", 1);
144     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_CLASS_API_HAS_SERVICE);
145     TIME_TRACER_ITEM_END("hasService::ACE", 1);
146     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
147
148     try {
149         // Private Object
150         BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(thisObject));
151         if (!priv) {
152             throw TypeMismatchException("Private object is NULL.");
153         }
154     
155         ArgumentValidator validator(context, argumentCount, arguments);
156         unsigned long service = validator.toULong(0);  // uuid
157
158         JSValueRef result = JSUtil::toJSValueRef(context, priv->mClass->hasService(service));
159         TIME_TRACER_ITEM_END(__FUNCTION__, 1);
160         
161         return result;
162     } catch (const BasePlatformException &err) {
163         return JSWebAPIErrorFactory::postException(context, exception, err);
164     } catch (...) {
165         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothClass.hasService().");
166         return JSWebAPIErrorFactory::postException(context, exception, err);
167     }
168 }
169
170
171
172 } // Bluetooth
173 } // DeviceAPI