Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothHealthApplication.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 "JSBluetoothHealthApplication.h"
28 #include "BluetoothHealthProfileHandler.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 JSBluetoothHealthApplication::m_classInfo = {
40     0,
41     kJSClassAttributeNone,
42     "BluetoothHealthApplication",
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 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 },
63     { 0, 0, 0, 0 }
64 };
65
66 JSStaticFunction JSBluetoothHealthApplication::m_function[] = {
67     { "unregister", unregister, kJSPropertyAttributeNone },
68     { 0, 0, 0 }
69 };
70
71 JSClassRef JSBluetoothHealthApplication::m_jsClassRef = JSClassCreate(JSBluetoothHealthApplication::getClassInfo());
72
73 const JSClassRef JSBluetoothHealthApplication::getClassRef()
74 {
75     if (!m_jsClassRef) {
76         m_jsClassRef = JSClassCreate(&m_classInfo);
77     }
78     return m_jsClassRef;
79 }
80
81 const JSClassDefinition* JSBluetoothHealthApplication::getClassInfo()
82 {
83     return &m_classInfo;
84 }
85
86 JSObjectRef JSBluetoothHealthApplication::createJSObject(JSContextRef context, BluetoothHealthApplicationSharedPtr app)
87 {
88     BluetoothHealthApplicationHolderPtr holder = new BluetoothHealthApplicationHolder(app);
89     return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
90 }
91
92 BluetoothHealthApplicationSharedPtr JSBluetoothHealthApplication::toBluetoothHealthApplication(JSObjectRef appObj)
93 {
94     BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(appObj));
95     return priv->mApp;
96 }
97
98 void JSBluetoothHealthApplication::initialize(JSContextRef context, JSObjectRef object)
99 {
100     // Do nothing
101 }
102
103 void JSBluetoothHealthApplication::finalize(JSObjectRef object)
104 {
105     BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
106     if (priv) {
107         JSObjectSetPrivate(object, NULL);
108         delete priv;
109     }
110 }
111
112 JSValueRef JSBluetoothHealthApplication::getProperty(JSContextRef context,
113         JSObjectRef object,
114         JSStringRef propertyName,
115         JSValueRef* exception)
116 {
117     LoggerD("Enter");
118
119     try {
120         BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
121         if (!priv) {
122             throw TypeMismatchException("Private object is NULL");
123         }
124
125         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_DATA_TYPE)) {
126             return JSValueMakeNumber(context, priv->mApp->getDataType());
127         }
128         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_NAME)) {
129             return JSUtil::toJSValueRef(context, priv->mApp->getName());
130         }
131         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_ONCONNECT)) {
132             LoggerD("get onconnect");
133             return priv->mApp->getOnConnect(context);
134         }        
135         /*
136         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_APPLICATION_IS_REGISTERED)) {
137             return JSUtil::toJSValueRef(context, priv->mApp->getRegistrationState());
138         }
139         */
140     } catch (const BasePlatformException &err) {
141         LoggerW("Getting property is failed: " << err.getMessage().c_str());
142     }
143
144     return NULL;
145 }
146
147 bool JSBluetoothHealthApplication::setProperty(JSContextRef context,
148         JSObjectRef object,
149         JSStringRef propertyName,
150         JSValueRef value,
151         JSValueRef* exception)
152 {
153     LoggerD("Enter");
154     
155     try {
156         BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(object));
157         if (!priv) {
158             throw TypeMismatchException("Private object is NULL");
159         }
160         
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");
167                 }
168
169                 onconnectObj = JSUtil::JSValueToObject(context, value);
170
171                 if(!JSObjectIsFunction(context, onconnectObj)) {
172                     throw TypeMismatchException("Not function");
173                 }
174             }
175             else {
176                 LoggerD("onconnect() is NULL");
177             }
178             
179             return priv->mApp->setOnConnect(context, onconnectObj);
180         }
181     } catch (const BasePlatformException &err) {
182         JSWebAPIErrorFactory::postException(context, exception, err);
183     }
184
185     return false;
186 }
187
188
189 JSValueRef JSBluetoothHealthApplication::unregister(JSContextRef context,
190     JSObjectRef object,
191     JSObjectRef thisObject,
192     size_t argumentCount,
193     const JSValueRef arguments[],
194     JSValueRef* exception)
195 {
196     LoggerD("Enter");
197
198     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
199     
200     // Access Check
201
202
203     try {
204         BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(thisObject));
205         if (!priv) {
206             throw TypeMismatchException("Private object is NULL");
207         }
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);
211
212         ArgumentValidator validator(context, argumentCount, arguments);
213         JSObjectRef successCallback = validator.toFunction(0, true);  // successCallback  
214         JSObjectRef errorCallback = validator.toFunction(1, true);  // errorCallback
215
216         // perform
217         MultiCallbackUserDataPtr callback(
218                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
219         if(!callback){
220             LoggerW("Can't create MultiCallbackUserData");
221         }
222         else {
223             callback->setCallback("success", successCallback);
224             callback->setCallback("error", errorCallback);
225         }      
226         
227         BluetoothHealthProfileHandler::getInstance()->unregisterApp(priv->mApp->getAppID(), callback);
228         TIME_TRACER_ITEM_END(__FUNCTION__, 1);
229         
230         return JSValueMakeUndefined(context);    
231     } catch (const BasePlatformException &err) {
232         TIME_TRACER_ITEM_END(__FUNCTION__, 1);    
233         return JSWebAPIErrorFactory::postException(context, exception, err);
234     } catch (...) {
235         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
236         return JSWebAPIErrorFactory::postException(context, exception, err);
237     }    
238 }
239
240
241 } // Bluetooth
242 } // DeviceAPI