2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/PrivateObject.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include "JSFeature.h"
29 #include "JSFeatureParam.h"
32 namespace DeviceAPI {
\rnamespace Tizen {
35 const char* PLUGIN_NAME = "Feature";
36 const char* ATTRIBUTE_URI = "uri";
37 const char* ATTRIBUTE_REQUIRED = "required";
38 const char* ATTRIBUTE_PARAMS = "params";
41 JSClassDefinition JSFeature::m_classInfo =
44 kJSClassAttributeNone,
54 NULL, //deleteProperty,
55 NULL, //getPropertyNames,
62 JSStaticValue JSFeature::m_properties[] = {
63 { ATTRIBUTE_URI, getProperty, NULL, kJSPropertyAttributeReadOnly },
64 { ATTRIBUTE_REQUIRED, getProperty, NULL, kJSPropertyAttributeReadOnly },
65 { ATTRIBUTE_PARAMS, getProperty, NULL, kJSPropertyAttributeReadOnly },
69 const JSClassDefinition* JSFeature::getClassInfo()
74 JSClassRef JSFeature::m_classRef = JSClassCreate(JSFeature::getClassInfo());
76 void JSFeature::initialize(JSContextRef context, JSObjectRef object)
78 //LogDebug("entered");
79 //private object should be passed to JSObjectMake function.
83 void JSFeature::finalize(JSObjectRef object)
86 JSFeaturePrivateObject* priv =
87 static_cast<JSFeaturePrivateObject*>(JSObjectGetPrivate(object));
91 JSClassRef JSFeature::getClassRef()
94 m_classRef = JSClassCreate(&m_classInfo);
99 JSValueRef JSFeature::getProperty(JSContextRef context, JSObjectRef object,
100 JSStringRef propertyName, JSValueRef* exception)
103 JSFeaturePrivateObject* priv =
104 static_cast<JSFeaturePrivateObject*>(JSObjectGetPrivate(object));
106 LogError("no private object");
107 return JSValueMakeUndefined(context);
111 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
112 if (JSStringIsEqualToUTF8CString(propertyName, ATTRIBUTE_URI)) {
113 return converter.toJSValueRef(priv->getObject()->getName() );
115 else if (JSStringIsEqualToUTF8CString(propertyName, ATTRIBUTE_REQUIRED)) {
116 if (priv->getObject()->isRequestedByWidget()) {
117 return converter.toJSValueRef(priv->getObject()->isRequired() );
119 return JSValueMakeNull(context);
121 else if (JSStringIsEqualToUTF8CString(propertyName, ATTRIBUTE_PARAMS)) {
122 if (priv->getObject()->isRequestedByWidget()) {
123 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
125 WrtDeviceApis::WidgetDB::Api::IWidgetFeature::Params params =
126 priv->getObject()->getParams();
127 WrtDeviceApis::WidgetDB::Api::IWidgetFeature::ParamsIterator it =
130 for (; it != params.end(); ++it, ++i) {
131 std::pair<std::string, std::string> param = make_pair(it->first, it->second);
132 JSFeatureParamPrivObject *priv = new JSFeatureParamPrivObject(context, param);
133 JSObjectRef tempVal = JSObjectMake(context, JSFeatureParam::getClassRef(), priv);
134 bool result = JSSetArrayElement(context, jsResult, i, tempVal);
141 return JSValueMakeNull(context);
144 Catch(WrtDeviceApis::Commons::Exception)
146 LogError("error during conversion");
148 return JSValueMakeUndefined(context);