tizen 2.3 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Systeminfo / JSSystemInfoPeripheral.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012-2013 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 "JSSystemInfoProperty.h"
19 #include "JSSystemInfoPeripheral.h"
20
21 #include <Logger.h>
22 #include <Export.h>
23 #include <PlatformException.h>
24 #include <JSUtil.h>
25
26 namespace DeviceAPI {
27 namespace SystemInfo {
28
29 struct SystemInfoPeripheralHolder {
30     SystemInfoPeripheralPtr ptr;
31 };
32
33 namespace {
34     const char* SYSTEMINFO_SYSTEMINFO_PERIPHERAL = "SystemInfoPeripheral";
35     const char* SYSTEMINFO_SYSTEMINFO_IS_VIDEO_OUTPUT_ON = "isVideoOutputOn";
36 }
37
38 JSClassDefinition JSSystemInfoPeripheral::m_classInfo = {
39         0, // current (and only) version is 0
40         kJSClassAttributeNone, //attributes
41         SYSTEMINFO_SYSTEMINFO_PERIPHERAL, //class name
42         JSSystemInfoProperty::getClassRef(), // parent class
43         m_property,
44         NULL, //m_function,
45         initialize,
46         finalize,
47         NULL, //hasProperty,
48         NULL, //getProperty,
49         NULL, //setProperty,
50         NULL, //deleteProperty,
51         NULL, //getPropertyNames,
52         NULL, //function,
53         NULL, //constructor,
54         NULL, //hasInstance,
55         NULL  //convertToType,
56 };
57
58 JSStaticValue JSSystemInfoPeripheral::m_property[] = {
59         { SYSTEMINFO_SYSTEMINFO_IS_VIDEO_OUTPUT_ON, getIsVideoOutputOn, NULL,
60                 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
61         { 0, 0, 0, 0 }
62 };
63
64 const JSClassDefinition* JSSystemInfoPeripheral::getClassInfo()
65 {
66     return &m_classInfo;
67 }
68
69 JSClassRef JSSystemInfoPeripheral::m_jsClassRef =
70         JSClassCreate(JSSystemInfoPeripheral::getClassInfo());
71
72 JSClassRef DLL_EXPORT JSSystemInfoPeripheral::getClassRef()
73 {
74     if (!m_jsClassRef) {
75         m_jsClassRef = JSClassCreate(&m_classInfo);
76     }
77     return m_jsClassRef;
78 }
79
80 SystemInfoPeripheralPtr JSSystemInfoPeripheral::getPrivateObject(JSContextRef context,
81         JSValueRef value)
82 {
83     if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
84         LOGE("Type mismatch");
85         throw Common::TypeMismatchException("Type mismatch");
86     }
87
88     JSObjectRef object = Common::JSUtil::JSValueToObject(context, value);
89     SystemInfoPeripheralHolder* priv =
90             static_cast<SystemInfoPeripheralHolder*>(JSObjectGetPrivate(object));
91     if (!priv) {
92         LOGE("Holder is null");
93         throw Common::UnknownException("Holder is null");
94     }
95     if (!priv->ptr) {
96         LOGE("Priv is null");
97         throw Common::UnknownException("Priv is null");
98     }
99     return priv->ptr;
100 }
101
102 void JSSystemInfoPeripheral::setPrivateObject(JSObjectRef object,
103         SystemInfoPeripheralPtr native)
104 {
105     SystemInfoPeripheralHolder* priv =
106             static_cast<SystemInfoPeripheralHolder*>(JSObjectGetPrivate(object));
107     if (!priv) {
108         LOGE("Holder is null");
109         throw Common::UnknownException("Holder is null");
110     }
111     priv->ptr = native;
112 }
113
114 JSObjectRef JSSystemInfoPeripheral::makeJSObject(JSContextRef context,
115         SystemInfoPropertyPtr native)
116 {
117     if (!native) {
118         LOGE("NULL pointer to SystemInfoPeripheral given");
119         throw Common::UnknownException("NULL pointer to SystemInfoPeripheral given");
120     }
121
122     SystemInfoPeripheralPtr peripheral =
123             std::dynamic_pointer_cast<SystemInfoPeripheral>(native);
124
125     if (!peripheral) {
126         LOGE("Invalid pointer to SystemInfoPeripheral given");
127         throw Common::UnknownException("Invalid pointer to SystemInfoPeripheral given");
128     }
129
130     SystemInfoPeripheralHolder* priv = new(std::nothrow) SystemInfoPeripheralHolder();
131     if (!priv) {
132         LOGE("Failed to allocate memory");
133         throw Common::UnknownException("Failed to allocate memory");
134     }
135     priv->ptr = peripheral;
136
137     JSObjectRef obj = JSObjectMake(context, getClassRef(),
138             static_cast<void*>(priv));
139     return obj;
140 }
141
142 void JSSystemInfoPeripheral::initialize(JSContextRef context, JSObjectRef object)
143 {
144     LOGD("Entered");
145 }
146
147 void JSSystemInfoPeripheral::finalize(JSObjectRef object)
148 {
149     LOGD("Entered");
150     SystemInfoPeripheralHolder* priv =
151             static_cast<SystemInfoPeripheralHolder*>(JSObjectGetPrivate(object));
152     if (priv) {
153         JSObjectSetPrivate(object, NULL);
154         delete priv;
155         priv = NULL;
156     }
157 }
158
159 JSValueRef JSSystemInfoPeripheral::getIsVideoOutputOn(JSContextRef context,
160         JSObjectRef object,
161         JSStringRef property_name,
162         JSValueRef* exception)
163 {
164     LOGD("Entered");
165     try {
166         SystemInfoPeripheralPtr priv = getPrivateObject(context, object);
167
168         return Common::JSUtil::toJSValueRef(context, priv->isVideoOutputOn());
169     }
170     catch (const Common::BasePlatformException &err) {
171         LOGE("Exception caught: name: %s, msg: %s",
172                 err.getName().c_str(), err.getMessage().c_str());
173     }
174     catch (...) {
175         LOGE("getIsVideoOutputOn fails");
176     }
177     return JSValueMakeUndefined(context);
178 }
179
180 } // SystemInfo
181 } // DeviceAPI