Update change log and spec for wrt-plugins-tizen_0.4.44
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothHealthApplication.cpp
1 //\r
2 // Tizen Web Device API\r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.\r
4 //\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
8 //\r
9 // http://www.apache.org/licenses/LICENSE-2.0\r
10 //\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
16 //\r
17 \r
18 #include <SecurityExceptions.h>\r
19 \r
20 #include <JSUtil.h>\r
21 #include <ArgumentValidator.h>\r
22 #include <GlobalContextManager.h>\r
23 #include <PlatformException.h>\r
24 #include <MultiCallbackUserData.h>\r
25 \r
26 #include "plugin_config.h"\r
27 #include "JSBluetoothHealthApplication.h"\r
28 #include "BluetoothHealthProfileHandler.h"\r
29 \r
30 #include <TimeTracer.h>\r
31 #include <Logger.h>\r
32 \r
33 using namespace WrtDeviceApis::Commons;\r
34 using namespace DeviceAPI::Common;\r
35 \r
36 namespace DeviceAPI {\r
37 namespace Bluetooth {\r
38 \r
39 JSClassDefinition JSBluetoothHealthApplication::m_classInfo = {\r
40     0,\r
41     kJSClassAttributeNone,\r
42     "BluetoothHealthApplication",\r
43     NULL, //ParentClass\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
57 };\r
58 \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
64     { 0, 0, 0, 0 }\r
65 };\r
66 \r
67 JSStaticFunction JSBluetoothHealthApplication::m_function[] = {\r
68     { BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER, unregister, kJSPropertyAttributeNone },\r
69     { 0, 0, 0 }\r
70 };\r
71 \r
72 JSClassRef JSBluetoothHealthApplication::m_jsClassRef = JSClassCreate(JSBluetoothHealthApplication::getClassInfo());\r
73 \r
74 const JSClassRef JSBluetoothHealthApplication::getClassRef()\r
75 {\r
76     if (!m_jsClassRef) {\r
77         m_jsClassRef = JSClassCreate(&m_classInfo);\r
78     }\r
79     return m_jsClassRef;\r
80 }\r
81 \r
82 const JSClassDefinition* JSBluetoothHealthApplication::getClassInfo()\r
83 {\r
84     return &m_classInfo;\r
85 }\r
86 \r
87 JSObjectRef JSBluetoothHealthApplication::createJSObject(JSContextRef context, BluetoothHealthApplicationSharedPtr app)\r
88 {\r
89     BluetoothHealthApplicationHolderPtr holder = new BluetoothHealthApplicationHolder(app);\r
90     return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));\r
91 }\r
92 \r
93 BluetoothHealthApplicationSharedPtr JSBluetoothHealthApplication::toBluetoothHealthApplication(JSObjectRef appObj)\r
94 {\r
95     BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(appObj));\r
96     return priv->mApp;\r
97 }\r
98 \r
99 void JSBluetoothHealthApplication::initialize(JSContextRef context, JSObjectRef object)\r
100 {\r
101     // Do nothing\r
102 }\r
103 \r
104 void JSBluetoothHealthApplication::finalize(JSObjectRef object)\r
105 {\r
106     BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));\r
107     if (priv) {\r
108         JSObjectSetPrivate(object, NULL);\r
109         delete priv;\r
110     }\r
111 }\r
112 \r
113 JSValueRef JSBluetoothHealthApplication::getProperty(JSContextRef context,\r
114         JSObjectRef object,\r
115         JSStringRef propertyName,\r
116         JSValueRef* exception)\r
117 {\r
118     try {\r
119         BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));\r
120         if (!priv) {\r
121             throw TypeMismatchException("Private object is NULL");\r
122         }\r
123 \r
124         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE)) {\r
125             return JSValueMakeNumber(context, priv->mApp->getDataType());\r
126         }\r
127         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_NAME)) {\r
128             return JSUtil::toJSValueRef(context, priv->mApp->getName());\r
129         }\r
130         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) {\r
131             return JSUtil::toJSValueRef(context, priv->mApp->getOnConnect());\r
132         }        \r
133         /*\r
134         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_IS_REGISTERED)) {\r
135             return JSUtil::toJSValueRef(context, priv->mApp->getRegistrationState());\r
136         }\r
137         */\r
138     } catch (const BasePlatformException &err) {\r
139         LoggerW("Getting property is failed: " << err.getMessage().c_str());\r
140     }\r
141 \r
142     return NULL;\r
143 }\r
144 \r
145 bool JSBluetoothHealthApplication::setProperty(JSContextRef context,\r
146         JSObjectRef object,\r
147         JSStringRef propertyName,\r
148         JSValueRef value,\r
149         JSValueRef* exception)\r
150 {\r
151     try {\r
152         BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));\r
153         if (!priv) {\r
154             throw TypeMismatchException("Private object is NULL");\r
155         }\r
156         \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
162                 }\r
163 \r
164                 JSValueRef ex = NULL;\r
165                 object = JSValueToObject(context, value, &ex);\r
166                 if(ex){\r
167                     throw TypeMismatchException("Can't convert to Object");\r
168                 }                \r
169 \r
170                 if(!JSObjectIsFunction(context, object)) {\r
171                     throw TypeMismatchException("Not function");\r
172                 }\r
173             }\r
174             else {\r
175                 LoggerD("onconnect() is NULL");\r
176             }\r
177             \r
178             return priv->mApp->setOnConnect(context, object);\r
179         }\r
180     } catch (const BasePlatformException &err) {\r
181         JSWebAPIErrorFactory::postException(context, exception, err);\r
182     }\r
183 \r
184     return false;\r
185 }\r
186 \r
187 \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
194 {\r
195     LoggerD("Enter");\r
196 \r
197     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);\r
198     \r
199     // Access Check\r
200     /*\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
205     */\r
206 \r
207     try {\r
208         BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(thisObject));\r
209         if (!priv) {\r
210             throw TypeMismatchException("Private object is NULL");\r
211         }\r
212 \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
216 \r
217         // perform\r
218         MultiCallbackUserDataPtr callback(\r
219                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));\r
220         if(!callback){\r
221             LoggerW("Can't create MultiCallbackUserData");\r
222         }\r
223         else {\r
224             callback->setCallback("success", successCallback);\r
225             callback->setCallback("error", errorCallback);\r
226         }      \r
227         \r
228         BluetoothHealthProfileHandler::getInstance()->unregisterApp(priv->mApp->getAppID(), callback);\r
229         TIME_TRACER_ITEM_END(__FUNCTION__, 1);\r
230         \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
235     } catch (...) {\r
236         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");\r
237         return JSWebAPIErrorFactory::postException(context, exception, err);\r
238     }    \r
239 }\r
240 \r
241 \r
242 } // Bluetooth\r
243 } // DeviceAPI\r
244 \r