Update change log and spec for wrt-plugins-tizen_0.4.70
[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 <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                 object = JSUtil::JSValueToObject(context, value);
156
157                 if(!JSObjectIsFunction(context, object)) {
158                     throw TypeMismatchException("Not function");
159                 }
160             }
161             else {
162                 LoggerD("onconnect() is NULL");
163             }
164             
165             return priv->setOnConnect(context, object);
166         }
167     } catch (const BasePlatformException &err) {
168         JSWebAPIErrorFactory::postException(context, exception, err);
169     }
170
171     return false;
172 }
173
174 JSValueRef JSBluetoothServiceHandler::unregister(JSContextRef context,
175         JSObjectRef object,
176         JSObjectRef thisObject,
177         size_t argumentCount,
178         const JSValueRef arguments[],
179         JSValueRef* exception)
180 {
181     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
182     
183
184
185     try {
186         // Private Object
187         BluetoothServiceHandlerPtr priv = static_cast<BluetoothServiceHandlerPtr>(JSObjectGetPrivate(thisObject));
188         if (!priv) {
189             throw TypeMismatchException("Private object is NULL.");
190         }
191
192                 // Access Check
193                 TIME_TRACER_ITEM_BEGIN("unregister::ACE", 1);
194                 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SERVICE_HANDLER_API_UNREGISTER);         
195                 TIME_TRACER_ITEM_END("unregister::ACE", 1);
196
197
198         ArgumentValidator validator(context, argumentCount, arguments);        
199         JSObjectRef successCallback = validator.toFunction(0, true);  // successCallback  
200         JSObjectRef errorCallback = validator.toFunction(1, true);  // errorCallback
201
202         // perform
203         MultiCallbackUserDataPtr callback(
204                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
205         if(!callback){
206             LoggerW("Can't create MultiCallbackUserData");
207         }
208         else {
209             callback->setCallback("success", successCallback);
210             callback->setCallback("error", errorCallback);
211         }
212         
213         priv->unregister(callback);
214         TIME_TRACER_ITEM_END(__FUNCTION__, 1);
215         
216         return JSValueMakeUndefined(context);
217     } catch (const BasePlatformException &err) {
218         return JSWebAPIErrorFactory::postException(context, exception, err);
219     } catch (...) {
220         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothServiceHandler.unregister().");
221         return JSWebAPIErrorFactory::postException(context, exception, err);
222     }
223 }
224
225
226
227 } // Bluetooth
228 } // DeviceAPI