Update change log and spec for wrt-plugins-tizen_0.4.52
[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     { BLUETOOTH_HEALTH_APPLICATION_API_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                 JSValueRef ex = NULL;
170                 onconnectObj = JSValueToObject(context, value, &ex);
171                 if(ex){
172                     throw TypeMismatchException("Can't convert to Object");
173                 }                
174
175                 if(!JSObjectIsFunction(context, onconnectObj)) {
176                     throw TypeMismatchException("Not function");
177                 }
178             }
179             else {
180                 LoggerD("onconnect() is NULL");
181             }
182             
183             return priv->mApp->setOnConnect(context, onconnectObj);
184         }
185     } catch (const BasePlatformException &err) {
186         JSWebAPIErrorFactory::postException(context, exception, err);
187     }
188
189     return false;
190 }
191
192
193 JSValueRef JSBluetoothHealthApplication::unregister(JSContextRef context,
194     JSObjectRef object,
195     JSObjectRef thisObject,
196     size_t argumentCount,
197     const JSValueRef arguments[],
198     JSValueRef* exception)
199 {
200     LoggerD("Enter");
201
202     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
203     
204     // Access Check
205     TIME_TRACER_ITEM_BEGIN("unregister::ACE", 1);
206     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_HEALTH_APPLICATION_API_UNREGISTER);
207     TIME_TRACER_ITEM_END("unregister::ACE", 1);
208     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
209
210     try {
211         BluetoothHealthApplicationHolderPtr priv = static_cast<BluetoothHealthApplicationHolderPtr>(JSObjectGetPrivate(thisObject));
212         if (!priv) {
213             throw TypeMismatchException("Private object is NULL");
214         }
215
216         ArgumentValidator validator(context, argumentCount, arguments);
217         JSObjectRef successCallback = validator.toFunction(0, true);  // successCallback  
218         JSObjectRef errorCallback = validator.toFunction(1, true);  // errorCallback
219
220         // perform
221         MultiCallbackUserDataPtr callback(
222                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
223         if(!callback){
224             LoggerW("Can't create MultiCallbackUserData");
225         }
226         else {
227             callback->setCallback("success", successCallback);
228             callback->setCallback("error", errorCallback);
229         }      
230         
231         BluetoothHealthProfileHandler::getInstance()->unregisterApp(priv->mApp->getAppID(), callback);
232         TIME_TRACER_ITEM_END(__FUNCTION__, 1);
233         
234         return JSValueMakeUndefined(context);    
235     } catch (const BasePlatformException &err) {
236         TIME_TRACER_ITEM_END(__FUNCTION__, 1);    
237         return JSWebAPIErrorFactory::postException(context, exception, err);
238     } catch (...) {
239         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
240         return JSWebAPIErrorFactory::postException(context, exception, err);
241     }    
242 }
243
244
245 } // Bluetooth
246 } // DeviceAPI