2 // Tizen Web Device API
\r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
\r
5 // Licensed under the Apache License, Version 2.0 (the License);
\r
6 // you may not use this file except in compliance with the License.
\r
7 // You may obtain a copy of the License at
\r
9 // http://www.apache.org/licenses/LICENSE-2.0
\r
11 // Unless required by applicable law or agreed to in writing, software
\r
12 // distributed under the License is distributed on an "AS IS" BASIS,
\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 // See the License for the specific language governing permissions and
\r
15 // limitations under the License.
\r
18 #include <SecurityExceptions.h>
\r
21 #include <ArgumentValidator.h>
\r
22 #include <GlobalContextManager.h>
\r
23 #include <PlatformException.h>
\r
24 #include <MultiCallbackUserData.h>
\r
26 #include "plugin_config.h"
\r
27 #include "JSBluetoothHealthApplication.h"
\r
28 #include "BluetoothHealthProfileHandler.h"
\r
30 #include <TimeTracer.h>
\r
33 using namespace WrtDeviceApis::Commons;
\r
34 using namespace DeviceAPI::Common;
\r
36 namespace DeviceAPI {
\r
37 namespace Bluetooth {
\r
39 JSClassDefinition JSBluetoothHealthApplication::m_classInfo = {
\r
41 kJSClassAttributeNone,
\r
42 "BluetoothHealthApplication",
\r
44 m_property, //StaticValues
\r
45 m_function, //StaticFunctions
\r
46 initialize, //Initialize
\r
47 finalize, //Finalize
\r
48 NULL, //HasProperty,
\r
49 NULL, //GetProperty,
\r
50 NULL, //SetProperty,
\r
51 NULL, //DeleteProperty,
\r
52 NULL, //GetPropertyNames,
\r
53 NULL, //CallAsFunction,
\r
54 NULL, //CallAsConstructor,
\r
55 NULL, //HasInstance,
\r
56 NULL //ConvertToType
\r
59 JSStaticValue JSBluetoothHealthApplication::m_property[] = {
\r
60 { BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
\r
61 { BLUETOOTH_HEALTH_APPLICATION_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
\r
62 { BLUETOOTH_HEALTH_APPLICATION_ONCONNECT, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
\r
63 //{ BLUETOOTH_HEALTH_APPLICATION_IS_REGISTERED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
\r
67 JSStaticFunction JSBluetoothHealthApplication::m_function[] = {
\r
68 { BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER, unregister, kJSPropertyAttributeNone },
\r
72 JSClassRef JSBluetoothHealthApplication::m_jsClassRef = JSClassCreate(JSBluetoothHealthApplication::getClassInfo());
\r
74 const JSClassRef JSBluetoothHealthApplication::getClassRef()
\r
76 if (!m_jsClassRef) {
\r
77 m_jsClassRef = JSClassCreate(&m_classInfo);
\r
79 return m_jsClassRef;
\r
82 const JSClassDefinition* JSBluetoothHealthApplication::getClassInfo()
\r
84 return &m_classInfo;
\r
87 JSObjectRef JSBluetoothHealthApplication::createJSObject(JSContextRef context, BluetoothHealthApplicationSharedPtr app)
\r
89 BluetoothHealthApplicationHolderPtr holder = new BluetoothHealthApplicationHolder(app);
\r
90 return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
\r
93 BluetoothHealthApplicationSharedPtr JSBluetoothHealthApplication::toBluetoothHealthApplication(JSObjectRef appObj)
\r
95 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(appObj));
\r
99 void JSBluetoothHealthApplication::initialize(JSContextRef context, JSObjectRef object)
\r
104 void JSBluetoothHealthApplication::finalize(JSObjectRef object)
\r
106 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
\r
108 JSObjectSetPrivate(object, NULL);
\r
113 JSValueRef JSBluetoothHealthApplication::getProperty(JSContextRef context,
\r
114 JSObjectRef object,
\r
115 JSStringRef propertyName,
\r
116 JSValueRef* exception)
\r
119 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
\r
121 throw TypeMismatchException("Private object is NULL");
\r
124 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE)) {
\r
125 return JSValueMakeNumber(context, priv->mApp->getDataType());
\r
127 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_NAME)) {
\r
128 return JSUtil::toJSValueRef(context, priv->mApp->getName());
\r
130 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) {
\r
131 return JSUtil::toJSValueRef(context, priv->mApp->getOnConnect());
\r
134 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_IS_REGISTERED)) {
\r
135 return JSUtil::toJSValueRef(context, priv->mApp->getRegistrationState());
\r
138 } catch (const BasePlatformException &err) {
\r
139 LoggerW("Getting property is failed: " << err.getMessage().c_str());
\r
145 bool JSBluetoothHealthApplication::setProperty(JSContextRef context,
\r
146 JSObjectRef object,
\r
147 JSStringRef propertyName,
\r
149 JSValueRef* exception)
\r
152 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
\r
154 throw TypeMismatchException("Private object is NULL");
\r
157 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) {
\r
158 JSObjectRef object = NULL;
\r
159 if(!JSValueIsNull(context, value)) {
\r
160 if(!JSValueIsObject(context, value)) {
\r
161 throw TypeMismatchException("Value is not Object");
\r
164 JSValueRef ex = NULL;
\r
165 object = JSValueToObject(context, value, &ex);
\r
167 throw TypeMismatchException("Can't convert to Object");
\r
170 if(!JSObjectIsFunction(context, object)) {
\r
171 throw TypeMismatchException("Not function");
\r
175 LoggerD("onconnect() is NULL");
\r
178 return priv->mApp->setOnConnect(context, object);
\r
180 } catch (const BasePlatformException &err) {
\r
181 JSWebAPIErrorFactory::postException(context, exception, err);
\r
188 JSValueRef JSBluetoothHealthApplication::unregister(JSContextRef context,
\r
189 JSObjectRef object,
\r
190 JSObjectRef thisObject,
\r
191 size_t argumentCount,
\r
192 const JSValueRef arguments[],
\r
193 JSValueRef* exception)
\r
197 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
\r
201 TIME_TRACER_ITEM_BEGIN("unregister::ACE", 1);
\r
202 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER);
\r
203 TIME_TRACER_ITEM_END("unregister::ACE", 1);
\r
204 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
\r
208 BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(thisObject));
\r
210 throw TypeMismatchException("Private object is NULL");
\r
213 ArgumentValidator validator(context, argumentCount, arguments);
\r
214 JSObjectRef successCallback = validator.toFunction(0, true); // successCallback
\r
215 JSObjectRef errorCallback = validator.toFunction(1, true); // errorCallback
\r
218 MultiCallbackUserDataPtr callback(
\r
219 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
\r
221 LoggerW("Can't create MultiCallbackUserData");
\r
224 callback->setCallback("success", successCallback);
\r
225 callback->setCallback("error", errorCallback);
\r
228 BluetoothHealthProfileHandler::getInstance()->unregisterApp(priv->mApp->getAppID(), callback);
\r
229 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
\r
231 return JSValueMakeUndefined(context);
\r
232 } catch (const BasePlatformException &err) {
\r
233 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
\r
234 return JSWebAPIErrorFactory::postException(context, exception, err);
\r
236 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
\r
237 return JSWebAPIErrorFactory::postException(context, exception, err);
\r