2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <SecurityExceptions.h>
21 #include <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <PlatformException.h>
24 #include <MultiCallbackUserData.h>
26 #include "plugin_config.h"
27 #include "JSBluetoothHealthApplication.h"
28 #include "BluetoothHealthProfileHandler.h"
30 #include <TimeTracer.h>
33 using namespace WrtDeviceApis::Commons;
34 using namespace DeviceAPI::Common;
39 JSClassDefinition JSBluetoothHealthApplication::m_classInfo = {
41 kJSClassAttributeNone,
42 "BluetoothHealthApplication",
44 m_property, //StaticValues
45 m_function, //StaticFunctions
46 initialize, //Initialize
51 NULL, //DeleteProperty,
52 NULL, //GetPropertyNames,
53 NULL, //CallAsFunction,
54 NULL, //CallAsConstructor,
59 JSStaticValue JSBluetoothHealthApplication::m_property[] = {
60 { BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
61 { BLUETOOTH_HEALTH_APPLICATION_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_HEALTH_APPLICATION_ONCONNECT, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
66 JSStaticFunction JSBluetoothHealthApplication::m_function[] = {
67 { "unregister", unregister, kJSPropertyAttributeNone },
71 JSClassRef JSBluetoothHealthApplication::m_jsClassRef = JSClassCreate(JSBluetoothHealthApplication::getClassInfo());
73 const JSClassRef JSBluetoothHealthApplication::getClassRef()
76 m_jsClassRef = JSClassCreate(&m_classInfo);
81 const JSClassDefinition* JSBluetoothHealthApplication::getClassInfo()
86 JSObjectRef JSBluetoothHealthApplication::createJSObject(JSContextRef context, BluetoothHealthApplicationSharedPtr app)
88 BluetoothHealthApplicationHolderPtr holder = new BluetoothHealthApplicationHolder(app);
89 return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
92 BluetoothHealthApplicationSharedPtr JSBluetoothHealthApplication::toBluetoothHealthApplication(JSObjectRef appObj)
94 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(appObj));
98 void JSBluetoothHealthApplication::initialize(JSContextRef context, JSObjectRef object)
103 void JSBluetoothHealthApplication::finalize(JSObjectRef object)
105 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
107 JSObjectSetPrivate(object, NULL);
112 JSValueRef JSBluetoothHealthApplication::getProperty(JSContextRef context,
114 JSStringRef propertyName,
115 JSValueRef* exception)
120 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
122 throw TypeMismatchException("Private object is NULL");
125 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE)) {
126 return JSValueMakeNumber(context, priv->mApp->getDataType());
128 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_NAME)) {
129 return JSUtil::toJSValueRef(context, priv->mApp->getName());
131 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) {
132 LoggerD("get onconnect");
133 return priv->mApp->getOnConnect(context);
136 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_IS_REGISTERED)) {
137 return JSUtil::toJSValueRef(context, priv->mApp->getRegistrationState());
140 } catch (const BasePlatformException &err) {
141 LoggerW("Getting property is failed: " << err.getMessage().c_str());
147 bool JSBluetoothHealthApplication::setProperty(JSContextRef context,
149 JSStringRef propertyName,
151 JSValueRef* exception)
156 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
158 throw TypeMismatchException("Private object is NULL");
161 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) {
162 LoggerD("Set onconnect");
163 JSObjectRef onconnectObj = NULL;
164 if(!JSValueIsNull(context, value)) {
165 if(!JSValueIsObject(context, value)) {
166 throw TypeMismatchException("Value is not Object");
169 onconnectObj = JSUtil::JSValueToObject(context, value);
171 if(!JSObjectIsFunction(context, onconnectObj)) {
172 throw TypeMismatchException("Not function");
176 LoggerD("onconnect() is NULL");
179 return priv->mApp->setOnConnect(context, onconnectObj);
181 } catch (const BasePlatformException &err) {
182 JSWebAPIErrorFactory::postException(context, exception, err);
189 JSValueRef JSBluetoothHealthApplication::unregister(JSContextRef context,
191 JSObjectRef thisObject,
192 size_t argumentCount,
193 const JSValueRef arguments[],
194 JSValueRef* exception)
198 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
204 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(thisObject));
206 throw TypeMismatchException("Private object is NULL");
208 TIME_TRACER_ITEM_BEGIN("unregister::ACE", 1);
209 TIZEN_CHECK_ACCESS(context, exception, priv->mApp.get(), BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER);
210 TIME_TRACER_ITEM_END("unregister::ACE", 1);
212 ArgumentValidator validator(context, argumentCount, arguments);
213 JSObjectRef successCallback = validator.toFunction(0, true); // successCallback
214 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
217 MultiCallbackUserDataPtr callback(
218 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
220 LoggerW("Can't create MultiCallbackUserData");
223 callback->setCallback("success", successCallback);
224 callback->setCallback("error", errorCallback);
227 BluetoothHealthProfileHandler::getInstance()->unregisterApp(priv->mApp->getAppID(), callback);
228 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
230 return JSValueMakeUndefined(context);
231 } catch (const BasePlatformException &err) {
232 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
233 return JSWebAPIErrorFactory::postException(context, exception, err);
235 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
236 return JSWebAPIErrorFactory::postException(context, exception, err);