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