Update change log and spec for wrt-plugins-tizen_0.4.25-1
[platform/framework/web/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothServiceHandler.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 #include <MultiCallbackUserData.h>
26
27 #include "plugin_config.h"
28 #include "JSBluetoothServiceHandler.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 JSBluetoothServiceHandler::m_classInfo = {
41     0,
42     kJSClassAttributeNone,
43     "BluetoothServiceHandler",
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 JSBluetoothServiceHandler::m_property[] = {
61     { BLUETOOTH_SERVICE_HANDLER_UUID, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62     { BLUETOOTH_SERVICE_HANDLER_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63     { BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
64     { BLUETOOTH_SERVICE_HANDLER_ONCONNECT, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
65     { 0, 0, 0, 0 }
66 };
67
68 JSStaticFunction JSBluetoothServiceHandler::m_function[] = {
69     { BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER, unregister, kJSPropertyAttributeNone },
70     { 0, 0, 0 }
71 };
72
73 JSClassRef JSBluetoothServiceHandler::m_jsClassRef = JSClassCreate(JSBluetoothServiceHandler::getClassInfo());
74
75 const JSClassRef JSBluetoothServiceHandler::getClassRef()
76 {
77     if (!m_jsClassRef) {
78         m_jsClassRef = JSClassCreate(&m_classInfo);
79     }
80     return m_jsClassRef;
81 }
82
83 const JSClassDefinition* JSBluetoothServiceHandler::getClassInfo()
84 {
85     return &m_classInfo;
86 }
87
88 JSObjectRef JSBluetoothServiceHandler::createJSObject(JSContextRef context, BluetoothServiceHandlerPtr service)
89 {
90     return JSObjectMake(context, getClassRef(), static_cast<void*>(service));
91 }
92
93 void JSBluetoothServiceHandler::initialize(JSContextRef context, JSObjectRef object)
94 {
95     // do nothing
96 }
97
98 void JSBluetoothServiceHandler::finalize(JSObjectRef object)
99 {
100     BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
101     if (priv) {
102         JSObjectSetPrivate(object, NULL);
103         delete priv;
104     }
105 }
106
107 JSValueRef JSBluetoothServiceHandler::getProperty(JSContextRef context,
108         JSObjectRef object,
109         JSStringRef propertyName,
110         JSValueRef* exception)
111 {
112     try {
113         BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
114         if (!priv) {
115             throw TypeMismatchException("Private object is NULL");
116         }
117
118         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_UUID)) {
119             return JSUtil::toJSValueRef(context, priv->getUUID());
120         }
121         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_NAME)) {
122             return JSUtil::toJSValueRef(context, priv->getName());
123         }
124         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_IS_CONNECTED)) {
125             return JSUtil::toJSValueRef(context, priv->getConnectionState());
126         }
127         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_ONCONNECT)) {
128             return priv->getOnConnect(context);
129         }
130     } catch (const BasePlatformException &err) {
131         LoggerW("Getting property is failed: " << err.getMessage().c_str());
132     }
133
134     return NULL;
135 }
136
137 bool JSBluetoothServiceHandler::setProperty(JSContextRef context,
138         JSObjectRef object,
139         JSStringRef propertyName,
140         JSValueRef value,
141         JSValueRef* exception)
142 {
143     try {
144         BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(object));
145         if (!priv) {
146             throw TypeMismatchException("Private object is NULL");
147         }
148         
149         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SERVICE_HANDLER_ONCONNECT)) {
150             JSObjectRef object = NULL;
151             if(!JSValueIsNull(context, value)) {
152                 if(!JSValueIsObject(context, value)) {
153                     throw TypeMismatchException("Value is not Object");
154                 }
155
156                 JSValueRef ex = NULL;
157                 object = JSValueToObject(context, value, &ex);
158                 if(ex){
159                     throw TypeMismatchException("Can't convert to Object");
160                 }                
161
162                 if(!JSObjectIsFunction(context, object)) {
163                     throw TypeMismatchException("Not function");
164                 }
165             }
166             else {
167                 LoggerD("onconnect() is NULL");
168             }
169             
170             return priv->setOnConnect(context, object);
171         }
172     } catch (const BasePlatformException &err) {
173         JSWebAPIException::throwException(context, exception, err);
174     }
175
176     return false;
177 }
178
179 JSValueRef JSBluetoothServiceHandler::unregister(JSContextRef context,
180         JSObjectRef object,
181         JSObjectRef thisObject,
182         size_t argumentCount,
183         const JSValueRef arguments[],
184         JSValueRef* exception)
185 {
186     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
187     
188     // Access Check
189     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER);
190     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
191
192     try {
193         // Check whether Bluetooth is supported or not
194         if(!BluetoothAdapter::isBluetoothSupported()) {
195             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
196         }
197     
198         // Private Object
199         BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(thisObject));
200         if (!priv) {
201             throw TypeMismatchException("Private object is NULL.");
202         }
203
204         ArgumentValidator validator(context, argumentCount, arguments);        
205         JSObjectRef successCallback = validator.toFunction(0, true);  // successCallback  
206         JSObjectRef errorCallback = validator.toFunction(1, true);  // errorCallback
207
208         // perform
209         MultiCallbackUserDataPtr callback(
210                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
211         if(!callback){
212             LoggerW("Can't create MultiCallbackUserData");
213         }
214         else {
215             callback->setCallback("success", successCallback);
216             callback->setCallback("error", errorCallback);
217         }
218         
219         priv->unregister(callback);
220         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
221         
222         return JSValueMakeUndefined(context);
223     } catch (const BasePlatformException &err) {
224         return JSWebAPIException::throwException(context, exception, err);
225     } catch (...) {
226         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothServiceHandler.unregister().");
227         return JSWebAPIException::throwException(context, exception, err);
228     }
229 }
230
231
232
233 } // Bluetooth
234 } // DeviceAPI