ffaee4f875dd19fb7851104911f0a7e8aac9f7a6
[framework/web/wrt-plugins-tizen.git] / src / Systeminfo / JSDeviceOrientationInfo.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 <memory>
19 #include "JSDeviceOrientationInfo.h"
20 #include <Logger.h>
21
22 namespace DeviceAPI {
23 namespace Systeminfo {
24 using namespace WrtDeviceApis::CommonsJavaScript;
25 using namespace WrtDeviceApis::Commons;
26
27 namespace {
28 const char* DEVICEORIENTATION_STATUS_PROPERTY = "status";
29 }
30
31 JSClassRef JSDeviceOrientationInfo::m_classRef = NULL;
32
33 JSClassDefinition JSDeviceOrientationInfo::m_classInfo = {
34     0,
35     kJSClassAttributeNone,
36     "deviceorientationinfo",
37     0,
38     m_properties,
39     NULL,
40     Initialize,
41     Finalize,
42     hasProperty,
43     getProperty,
44     NULL,
45     NULL,
46     NULL,
47     NULL,
48     NULL,
49     NULL,
50     NULL
51 };
52
53 JSStaticValue JSDeviceOrientationInfo::m_properties[] = {
54     { DEVICEORIENTATION_STATUS_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
55     { 0, 0, 0, 0 }
56 };
57
58 const JSClassRef JSDeviceOrientationInfo::getClassRef()
59 {
60     if (!m_classRef) {
61         m_classRef = JSClassCreate(&m_classInfo);
62     }
63     return m_classRef;
64 }
65
66 const JSClassDefinition* JSDeviceOrientationInfo::getClassInfo()
67 {
68     return &m_classInfo;
69 }
70
71 void JSDeviceOrientationInfo::Initialize(JSContextRef context, JSObjectRef object)
72 {
73 }
74
75 void JSDeviceOrientationInfo::Finalize(JSObjectRef object)
76 {
77     LoggerD("Entered");
78     JSDeviceOrientationPriv* priv = static_cast<JSDeviceOrientationPriv*>(JSObjectGetPrivate(object));
79     JSObjectSetPrivate(object, NULL);
80     LoggerD("Deleting deviceOrientationInfo object");
81     delete priv;
82 }
83
84 bool JSDeviceOrientationInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
85 {
86     return JSUtils::hasProperty(m_properties, propertyName);
87 }
88
89 JSObjectRef JSDeviceOrientationInfo::createJSObject(JSContextRef context, const DeviceOrientationPropertiesPtr deviceOrientationInfo)
90 {
91     JSDeviceOrientationPriv *priv = new JSDeviceOrientationPriv(context, deviceOrientationInfo);
92     return JSObjectMake(context, getClassRef(), priv);
93 }
94
95 JSValueRef JSDeviceOrientationInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
96 {
97     LoggerD("Enter");
98     JSDeviceOrientationPriv *priv = static_cast<JSDeviceOrientationPriv*>(JSObjectGetPrivate(object));
99     if (NULL == priv) {
100         LoggerE("Private object not set.");
101         return JSValueMakeUndefined(context);
102     }
103
104     Try
105     {
106         DeviceOrientationPropertiesPtr deviceOrientationInfo = priv->getObject();
107         Converter convert(context);
108
109         if (JSStringIsEqualToUTF8CString(propertyName, DEVICEORIENTATION_STATUS_PROPERTY)) {
110             return convert.toJSValueRef(deviceOrientationInfo->status);
111         }
112     }
113     Catch(Exception)
114     {
115         LoggerE("Exception: " << _rethrown_exception.GetMessage());
116     }
117     return JSValueMakeUndefined(context);
118 }
119 }
120 }