wrt-plugins-tizen_0.4.23
[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 #include <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <PlatformException.h>
25 #include <PropertyBag.h>
26
27 #include "plugin_config.h"
28 #include "JSBluetoothManager.h"
29 #include "JSBluetoothClassDeviceMajor.h"
30 #include "JSBluetoothClassDeviceMinor.h"
31 #include "JSBluetoothClassDeviceService.h"
32 //#include "BluetoothManager.h"
33 #include "JSBluetoothAdapter.h"
34 #include "BluetoothAdapter.h"
35
36 #include <TimeTracer.h>
37 #include <Logger.h>
38
39 using namespace WrtDeviceApis::Commons;
40 using namespace DeviceAPI::Common;
41
42 namespace DeviceAPI {
43 namespace Bluetooth {
44
45 JSClassDefinition JSBluetoothManager::m_classInfo = {
46     0,
47     kJSClassAttributeNone,
48     "BluetoothManager",
49     NULL, //ParentClass
50     m_property, //StaticValues
51     m_function, //StaticFunctions
52     initialize, //Initialize
53     finalize, //Finalize
54     NULL, //HasProperty,
55     NULL, //GetProperty,
56     NULL, //SetProperty,
57     NULL, //DeleteProperty,
58     NULL, //GetPropertyNames,
59     NULL, //CallAsFunction,
60     NULL, //CallAsConstructor,
61     NULL, //HasInstance,
62     NULL //ConvertToType
63 };
64
65 JSStaticValue JSBluetoothManager::m_property[] = {
66     { BLUETOOTH_MANAGER_DEVICE_MAJOR, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete},
67     { BLUETOOTH_MANAGER_DEVICE_MINOR, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete},
68     { BLUETOOTH_MANAGER_DEVICE_SERVICE, getReadOnlyProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete},       
69     { 0, 0, 0, 0 }
70 };
71
72 JSStaticFunction JSBluetoothManager::m_function[] = {
73     { BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER, getDefaultAdapter, kJSPropertyAttributeNone },
74     { 0, 0, 0 }
75 };
76
77 JSClassRef JSBluetoothManager::m_jsClassRef = JSClassCreate(JSBluetoothManager::getClassInfo());
78
79 const JSClassRef JSBluetoothManager::getClassRef()
80 {
81     if (!m_jsClassRef) {
82         m_jsClassRef = JSClassCreate(&m_classInfo);
83     }
84     return m_jsClassRef;
85 }
86
87 const JSClassDefinition* JSBluetoothManager::getClassInfo()
88 {
89     return &m_classInfo;
90 }
91
92 void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object)
93 {
94     if (!JSObjectGetPrivate(object)) {
95         PropertyBag *priv = new PropertyBag();    
96         if(priv) {
97             // deviceMajor
98             priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MAJOR,
99                     JSBluetoothClassDeviceMajor::createJSObject(context));
100
101             // deviceMinor
102             priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MINOR,
103                     JSBluetoothClassDeviceMinor::createJSObject(context));
104
105             // deviceService
106             priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_SERVICE,
107                     JSBluetoothClassDeviceService::createJSObject(context));
108
109             if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
110                 LoggerW("Failed to set private data");
111                 delete priv;
112             }            
113         }
114         else {
115             LoggerW("Failed to create private data");
116         }
117     }
118     else {
119         LoggerW("Private data already exists");
120     }
121 }
122
123 void JSBluetoothManager::finalize(JSObjectRef object)
124 {
125     PropertyBag *priv = static_cast<PropertyBag*>(JSObjectGetPrivate(object));
126     if (priv) {
127         JSObjectSetPrivate(object, NULL);
128         delete priv;
129     }
130 }
131
132 JSValueRef JSBluetoothManager::getReadOnlyProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) {
133     PropertyBag *priv = static_cast<PropertyBag*>(JSObjectGetPrivate(object));
134     if(!priv) {
135         LoggerW("There is no private data");
136         return NULL;
137     }
138
139     std::string name = JSUtil::JSStringToString(context, propertyName);
140     return priv->getProperty(context, propertyName);
141 }
142
143 JSValueRef JSBluetoothManager::getDefaultAdapter(JSContextRef context,
144         JSObjectRef object,
145         JSObjectRef thisObject,
146         size_t argumentCount,
147         const JSValueRef arguments[],
148         JSValueRef* exception)
149 {
150     LoggerD("Enter");
151     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
152
153     // Access Check
154     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER);
155     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
156
157     try {
158         if(!BluetoothAdapter::isBluetoothSupported()) {
159             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
160         }
161         
162         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
163         return JSBluetoothAdapter::createJSObject(context);
164
165     } catch (const BasePlatformException &err) {
166         return JSWebAPIException::throwException(context, exception, err);
167     } catch (...) {
168         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothManager.getDefaultAdapter().");
169         return JSWebAPIException::throwException(context, exception, err);
170     }
171 }
172
173
174 } // Bluetooth
175 } // DeviceAPI