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