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 "JSDeviceInfo.h"
25 using namespace WrtDeviceApis::CommonsJavaScript;
26 using namespace WrtDeviceApis::Commons;
27 using namespace Api::Systeminfo;
30 const char* DEVICE_IMEI_PROPERTY = "imei";
31 const char* DEVICE_MODEL_PROPERTY = "model";
32 const char* DEVICE_VERSION_PROPERTY = "version";
33 const char* DEVICE_VENDOR_PROPERTY = "vendor";
36 JSClassRef JSDeviceInfo::m_classRef = NULL;
38 JSClassDefinition JSDeviceInfo::m_classInfo = {
40 kJSClassAttributeNone,
58 JSStaticValue JSDeviceInfo::m_properties[] = {
59 { DEVICE_IMEI_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
60 { DEVICE_MODEL_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
61 { DEVICE_VERSION_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
62 { DEVICE_VENDOR_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
66 const JSClassRef JSDeviceInfo::getClassRef()
69 m_classRef = JSClassCreate(&m_classInfo);
74 const JSClassDefinition* JSDeviceInfo::getClassInfo()
79 void JSDeviceInfo::Initialize(JSContextRef context, JSObjectRef object)
83 void JSDeviceInfo::Finalize(JSObjectRef object)
86 JSDevicePriv* priv = static_cast<JSDevicePriv*>(JSObjectGetPrivate(object));
87 JSObjectSetPrivate(object, NULL);
88 LogDebug("Deleting DeviceInfo object");
92 bool JSDeviceInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
94 return JSUtils::hasProperty(m_properties, propertyName);
97 JSObjectRef JSDeviceInfo::createJSObject(JSContextRef context, const Api::Systeminfo::DeviceProperties &deviceInfo)
100 std::auto_ptr<Api::Systeminfo::DeviceProperties> accProps(new Api::Systeminfo::DeviceProperties(deviceInfo));
101 JSDevicePriv *priv = new JSDevicePriv(context, accProps.get());
103 return JSObjectMake(context, getClassRef(), priv);
106 JSValueRef JSDeviceInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
109 JSDevicePriv *priv = static_cast<JSDevicePriv*>(JSObjectGetPrivate(object));
111 LogError("Private object not set.");
112 return JSValueMakeUndefined(context);
117 Api::Systeminfo::DeviceProperties *deviceInfo = priv->getObject();
118 Converter convert(context);
120 if (JSStringIsEqualToUTF8CString(propertyName, DEVICE_IMEI_PROPERTY)) {
121 return convert.toJSValueRef(deviceInfo->imei);
122 } else if (JSStringIsEqualToUTF8CString(propertyName, DEVICE_MODEL_PROPERTY)) {
123 return convert.toJSValueRef(deviceInfo->model);
124 } else if (JSStringIsEqualToUTF8CString(propertyName, DEVICE_VERSION_PROPERTY)) {
125 return convert.toJSValueRef(deviceInfo->version);
126 } else if (JSStringIsEqualToUTF8CString(propertyName, DEVICE_VENDOR_PROPERTY)) {
127 return convert.toJSValueRef(deviceInfo->vendor);
132 LogError("Exception: " << _rethrown_exception.GetMessage());
134 return JSValueMakeUndefined(context);