Update change log and spec for wrt-plugins-tizen_0.4.13
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothManager.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
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <JSTizenExceptionFactory.h>
25 #include <JSTizenException.h>
26 #include <SecurityExceptions.h>
27 #include "BluetoothFactory.h"
28 #include "IBluetoothAdapterManager.h"
29 #include "BluetoothProperty.h"
30 #include "JSBluetoothManager.h"
31 #include "JSBluetoothAdapter.h"
32 #include "JSBluetoothClassDeviceMajor.h"
33 #include "JSBluetoothClassDeviceMinor.h"
34 #include "JSBluetoothClassDeviceService.h"
35 #include "plugin_config.h"
36 #include "JSUtil.h"
37
38 using namespace WrtDeviceApis::Commons;
39 using namespace DeviceAPI::Common;
40
41 namespace {
42         #define DEVICE_MAJOR "deviceMajor"
43         #define DEVICE_MINOR "deviceMinor"
44         #define DEVICE_SERVICE "deviceService"
45 }
46
47 namespace DeviceAPI {
48 namespace Bluetooth {
49
50 JSClassRef JSBluetoothManager::m_jsClassRef = NULL;
51
52 JSClassDefinition JSBluetoothManager::m_classInfo =
53 {
54         0,
55         kJSClassAttributeNone,
56         "BluetoothManager",
57         NULL,
58         NULL,
59         m_function,
60         initialize,
61         finalize,
62         NULL, 
63         NULL, 
64         NULL, 
65         NULL, 
66         NULL, 
67         NULL,
68         NULL,
69         NULL,
70         NULL
71 };
72
73
74 JSStaticFunction JSBluetoothManager::m_function[] =
75 {
76         { "getDefaultAdapter", getDefaultAdapter, kJSPropertyAttributeNone },
77         { 0, 0, 0 }
78 };
79
80 const JSClassRef JSBluetoothManager::getClassRef() 
81 {
82         if (!m_jsClassRef) 
83         {
84                 m_jsClassRef = JSClassCreate(&m_classInfo);
85         }
86         return m_jsClassRef;
87 }
88
89 JSValueRef JSBluetoothManager::getDefaultAdapter(JSContextRef context, JSObjectRef object,
90                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
91                 JSValueRef* exception) 
92 {
93         JSBluetoothManagerPriv* priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(thisObject));
94
95         LogDebug("OK"); 
96
97         Try
98         {
99                 if (priv == NULL)
100                 {
101                         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
102                 }
103                 
104                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
105                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_MANAGER_GET_DEFAULT_ADAPTER]);
106                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
107
108                 JSContextRef globalContext = priv->getContext();
109
110                 return JSBluetoothAdapter::createJSObject(globalContext);
111         }
112         Catch (WrtDeviceApis::Commons::SecurityException) 
113         {
114                 LogError("permission denied error");
115                 return JSTizenExceptionFactory::postException(context, exception, 
116                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
117
118         }       
119         Catch(WrtDeviceApis::Commons::UnsupportedException)
120         {
121                 LogError("UnkownException");
122                 return JSTizenExceptionFactory::postException(context, exception,
123                                 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
124         }
125         Catch(WrtDeviceApis::Commons::Exception) 
126         {
127                 LogError("UnkownException");
128                 return JSTizenExceptionFactory::postException(context, exception,
129                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
130         }
131 }
132
133 const JSClassDefinition* JSBluetoothManager::getClassInfo() 
134 {
135         return &m_classInfo;
136 }
137
138
139 void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object) 
140 {
141         JSBluetoothManagerPriv *priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
142         LogDebug("JSBluetoothManager::initialize");
143
144         if (priv != NULL)
145         {
146                 LogError("already exist");
147         }
148         else
149         {
150                 priv = new JSBluetoothManagerPriv(context);
151
152                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
153                 {
154                         LogError("Object can't store private data.");
155                         delete priv;
156                 }
157         }
158
159         setAttributesIntoJSObject(context, object);
160 }
161
162 void JSBluetoothManager::finalize(JSObjectRef object) 
163 {
164         JSBluetoothManagerPriv* priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
165         LogDebug("JSBluetoothManager::Finalrize");
166
167         if (priv != NULL)
168         {
169                 JSObjectSetPrivate(object, NULL);
170                 LogDebug("Deleting BluetoothManager");
171                 delete priv;
172         }
173 }
174
175 bool JSBluetoothManager::hasInstance(JSContextRef context, JSObjectRef constructor,
176                 JSValueRef possibleInstance, JSValueRef* exception) 
177 {
178         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
179 }
180
181 void JSBluetoothManager::setAttributesIntoJSObject(JSContextRef context, JSObjectRef object)
182 {
183         // deviceMajor
184         JSUtil::setProperty(context, object, DEVICE_MAJOR,
185                         JSBluetoothClassDeviceMajor::createJSObject(context), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete);
186
187         // deviceMinor
188         JSUtil::setProperty(context, object, DEVICE_MINOR,
189                         JSBluetoothClassDeviceMinor::createJSObject(context), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete);
190
191         // deviceService
192         JSUtil::setProperty(context, object, DEVICE_SERVICE,
193                         JSBluetoothClassDeviceService::createJSObject(context), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete);
194 }
195
196 }
197 }
198