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