28546b1edae567f8bdca169474006425647036c5
[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
37 using namespace WrtDeviceApis::Commons;
38 using namespace DeviceAPI::Common;
39
40 namespace {
41         #define DEVICE_MAJOR "deviceMajor"
42         #define DEVICE_MINOR "deviceMinor"
43         #define DEVICE_SERVICE "deviceService"
44 }
45
46 namespace DeviceAPI {
47 namespace Bluetooth {
48
49 JSClassRef JSBluetoothManager::m_jsClassRef = NULL;
50 JSObjectRef JSBluetoothManager::m_deviceMajor = NULL;
51 JSObjectRef JSBluetoothManager::m_deviceMinor = NULL;
52 JSObjectRef JSBluetoothManager::m_deviceService = NULL;
53
54
55 JSClassDefinition JSBluetoothManager::m_classInfo =
56 {
57         0,
58         kJSClassAttributeNone,
59         "BluetoothManager",
60         NULL,
61         m_properties,
62         m_function,
63         initialize,
64         finalize,
65         NULL, 
66         NULL, 
67         NULL, 
68         NULL, 
69         NULL, 
70         NULL,
71         NULL,
72         NULL,
73         NULL
74 };
75
76
77 JSStaticFunction JSBluetoothManager::m_function[] =
78 {
79         { "getDefaultAdapter", getDefaultAdapter, kJSPropertyAttributeNone },
80         { 0, 0, 0 }
81 };
82
83
84 JSStaticValue JSBluetoothManager::m_properties[] = 
85 {
86         {DEVICE_MAJOR, getProperty, NULL, kJSPropertyAttributeReadOnly},
87         {DEVICE_MINOR, getProperty, NULL, kJSPropertyAttributeReadOnly},
88         {DEVICE_SERVICE, getProperty, NULL, kJSPropertyAttributeReadOnly},      
89         {0, 0, 0, 0}
90 };
91
92
93
94 const JSClassRef JSBluetoothManager::getClassRef() 
95 {
96         if (!m_jsClassRef) 
97         {
98                 m_jsClassRef = JSClassCreate(&m_classInfo);
99         }
100         return m_jsClassRef;
101 }
102
103 JSValueRef JSBluetoothManager::getDefaultAdapter(JSContextRef context, JSObjectRef object,
104                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
105                 JSValueRef* exception) 
106 {
107         JSBluetoothManagerPriv* priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(thisObject));
108
109         LogDebug("OK"); 
110
111         Try
112         {
113                 if (priv == NULL)
114                 {
115                         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
116                 }
117                 
118                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
119                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_MANAGER_GET_DEFAULT_ADAPTER]);
120                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
121
122                 JSContextRef globalContext = priv->getContext();
123
124                 return JSBluetoothAdapter::createJSObject(globalContext);
125         }
126         Catch (WrtDeviceApis::Commons::SecurityException) 
127         {
128                 LogError("permission denied error");
129                 return JSTizenExceptionFactory::postException(context, exception, 
130                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
131
132         }       
133         Catch(WrtDeviceApis::Commons::UnsupportedException)
134         {
135                 LogError("UnkownException");
136                 return JSTizenExceptionFactory::postException(context, exception,
137                                 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
138         }
139         Catch(WrtDeviceApis::Commons::Exception) 
140         {
141                 LogError("UnkownException");
142                 return JSTizenExceptionFactory::postException(context, exception,
143                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
144         }
145 }
146
147
148 JSValueRef JSBluetoothManager::getProperty(JSContextRef context,
149                                         JSObjectRef object,
150                                         JSStringRef propertyName,
151                                         JSValueRef* exception)
152 {
153         LogDebug("OK"); 
154         
155         Try
156         {
157                 if(JSStringIsEqualToUTF8CString(propertyName, DEVICE_MAJOR)) 
158                 {
159                         if(!m_deviceMajor)
160                                 m_deviceMajor = JSBluetoothClassDeviceMajor::createJSObject(context);
161                         return m_deviceMajor;
162                 }
163                 else if(JSStringIsEqualToUTF8CString(propertyName, DEVICE_MINOR)) 
164                 {
165                         if(!m_deviceMinor)
166                                 m_deviceMinor = JSBluetoothClassDeviceMinor::createJSObject(context);
167                         return m_deviceMinor;
168                 }
169                 else if(JSStringIsEqualToUTF8CString(propertyName, DEVICE_SERVICE)) 
170                 {
171                         if(!m_deviceService)
172                                 m_deviceService = JSBluetoothClassDeviceService::createJSObject(context);
173                         return m_deviceService;
174                 }
175
176         }
177         Catch (Exception)
178         {
179                 LogWarning("Exception "<<_rethrown_exception.GetMessage());
180         }
181
182         return NULL;
183 }
184
185
186
187 const JSClassDefinition* JSBluetoothManager::getClassInfo() 
188 {
189         return &m_classInfo;
190 }
191
192
193 void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object) 
194 {
195         JSBluetoothManagerPriv *priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
196         LogDebug("JSBluetoothManager::initialize");
197
198         if (priv != NULL)
199         {
200                 LogError("already exist");
201         }
202         else
203         {
204                 priv = new JSBluetoothManagerPriv(context);
205
206                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
207                 {
208                         LogError("Object can't store private data.");
209                         delete priv;
210                 }
211         }
212 }
213
214 void JSBluetoothManager::finalize(JSObjectRef object) 
215 {
216         JSBluetoothManagerPriv* priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
217         LogDebug("JSBluetoothManager::Finalrize");
218
219         if (priv != NULL)
220         {
221                 JSObjectSetPrivate(object, NULL);
222                 LogDebug("Deleting BluetoothManager");
223                 delete priv;
224         }
225 }
226
227 bool JSBluetoothManager::hasInstance(JSContextRef context, JSObjectRef constructor,
228                 JSValueRef possibleInstance, JSValueRef* exception) 
229 {
230         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
231 }
232 }
233 }
234