Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Systeminfo / JSCpuInfo.cpp
1 /*
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved 
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
19 #include <memory>
20 #include <dpl/log.h>
21 #include "JSCpuInfo.h"
22
23 namespace TizenApis {
24 namespace Tizen1_0 {
25 using namespace WrtDeviceApis::CommonsJavaScript;
26 using namespace WrtDeviceApis::Commons;
27 using namespace Api::Systeminfo;
28
29 namespace {
30 const char* CPU_LOAD_PROPERTY = "load";
31
32
33 JSClassRef JSCpuInfo::m_classRef = NULL;
34
35 JSClassDefinition JSCpuInfo::m_classInfo = {
36     0,
37     kJSClassAttributeNone,
38     "cpuinfo",
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 JSCpuInfo::m_properties[] = {
56     { CPU_LOAD_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
57     { 0, 0, 0, 0 }
58 };
59
60 const JSClassRef JSCpuInfo::getClassRef()
61 {
62     if (!m_classRef) {
63         m_classRef = JSClassCreate(&m_classInfo);
64     }
65     return m_classRef;
66 }
67
68 const JSClassDefinition* JSCpuInfo::getClassInfo()
69 {
70     return &m_classInfo;
71 }
72
73 void JSCpuInfo::Initialize(JSContextRef context, JSObjectRef object)
74 {
75 }
76
77 void JSCpuInfo::Finalize(JSObjectRef object)
78 {
79     LogDebug("Entered");
80     JSCpuPriv* priv = static_cast<JSCpuPriv*>(JSObjectGetPrivate(object));
81     JSObjectSetPrivate(object, NULL);
82     LogDebug("Deleting CpuInfo object");
83     delete priv;
84 }
85
86 bool JSCpuInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
87 {
88     return JSUtils::hasProperty(m_properties, propertyName);
89 }
90
91 JSObjectRef JSCpuInfo::createJSObject(JSContextRef context, const Api::Systeminfo::CpuProperties &cpuInfo)
92 {
93     LogDebug("Enter");
94     std::auto_ptr<Api::Systeminfo::CpuProperties> accProps(new Api::Systeminfo::CpuProperties(cpuInfo));
95     JSCpuPriv *priv = new JSCpuPriv(context, accProps.get());
96     accProps.release();
97     return JSObjectMake(context, getClassRef(), priv);
98 }
99
100 JSValueRef JSCpuInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
101 {
102     LogDebug("Enter");
103     JSCpuPriv *priv = static_cast<JSCpuPriv*>(JSObjectGetPrivate(object));
104     if (NULL == priv) {
105         LogError("Private object not set.");
106         return JSValueMakeUndefined(context);
107     }
108
109     Try
110     {
111         Api::Systeminfo::CpuProperties *cpuInfo = priv->getObject();
112         Converter convert(context);
113
114         if (JSStringIsEqualToUTF8CString(propertyName, CPU_LOAD_PROPERTY)) {
115             return convert.toJSValueRef(cpuInfo->load);
116         }
117     }
118     Catch(Exception)
119     {
120         LogError("Exception: " << _rethrown_exception.GetMessage());
121     }
122     return JSValueMakeUndefined(context);
123 }
124 }
125 }