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