3 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "JSPowerInfo.h"
25 using namespace WrtDeviceApis::CommonsJavaScript;
26 using namespace WrtDeviceApis::Commons;
27 using namespace Api::Systeminfo;
30 const char* POWER_LEVEL_PROPERTY = "level";
31 const char* POWER_ISCHARGING_PROPERTY = "isCharging";
34 JSClassRef JSPowerInfo::m_classRef = NULL;
36 JSClassDefinition JSPowerInfo::m_classInfo = {
38 kJSClassAttributeNone,
56 JSStaticValue JSPowerInfo::m_properties[] = {
57 { POWER_LEVEL_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
58 { POWER_ISCHARGING_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
62 const JSClassRef JSPowerInfo::getClassRef()
65 m_classRef = JSClassCreate(&m_classInfo);
70 const JSClassDefinition* JSPowerInfo::getClassInfo()
75 void JSPowerInfo::Initialize(JSContextRef context, JSObjectRef object)
79 void JSPowerInfo::Finalize(JSObjectRef object)
82 JSPowerPriv* priv = static_cast<JSPowerPriv*>(JSObjectGetPrivate(object));
83 JSObjectSetPrivate(object, NULL);
84 LogDebug("Deleting powerInfo object");
88 bool JSPowerInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
90 return JSUtils::hasProperty(m_properties, propertyName);
93 JSObjectRef JSPowerInfo::createJSObject(JSContextRef context, const Api::Systeminfo::PowerProperties &powerInfo)
95 std::auto_ptr<Api::Systeminfo::PowerProperties> accProps(new Api::Systeminfo::PowerProperties(powerInfo));
96 JSPowerPriv *priv = new JSPowerPriv(context, accProps.get());
98 return JSObjectMake(context, getClassRef(), priv);
101 JSValueRef JSPowerInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
104 JSPowerPriv *priv = static_cast<JSPowerPriv*>(JSObjectGetPrivate(object));
106 LogError("Private object not set.");
107 return JSValueMakeUndefined(context);
112 Api::Systeminfo::PowerProperties *powerInfo = priv->getObject();
113 Converter convert(context);
115 if (JSStringIsEqualToUTF8CString(propertyName, POWER_LEVEL_PROPERTY)) {
116 return convert.toJSValueRef(powerInfo->level);
117 } else if (JSStringIsEqualToUTF8CString(propertyName, POWER_ISCHARGING_PROPERTY)) {
118 return convert.toJSValueRef(powerInfo->isCharging);
123 LogError("Exception: " << _rethrown_exception.GetMessage());
125 return JSValueMakeUndefined(context);