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