664bf5ab914cfc19d458af5f9dd4c985558ad1a6
[framework/web/wrt-plugins-tizen.git] / src / Tizen / JSFeature.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 /**
19  * @file        JSFeature.cpp
20  * @author
21  * @version     0.1
22  * @brief
23  */
24
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/PrivateObject.h>
27 #include <CommonsJavaScript/Converter.h>
28 #include "JSFeature.h"
29 #include "JSFeatureParam.h"
30
31
32 namespace DeviceAPI {\rnamespace Tizen {
33
34 namespace {
35     const char* PLUGIN_NAME = "Feature";
36     const char* ATTRIBUTE_URI = "uri";
37     const char* ATTRIBUTE_REQUIRED = "required";
38     const char* ATTRIBUTE_PARAMS = "params";
39 }
40
41 JSClassDefinition JSFeature::m_classInfo =
42 {
43     0,
44     kJSClassAttributeNone,
45     PLUGIN_NAME,
46     0,
47     m_properties,
48     NULL, //m_functions,
49     initialize,
50     finalize,
51     NULL, //hasProperty,
52     NULL, //getProperty,
53     NULL, //setProperty,
54     NULL, //deleteProperty,
55     NULL, //getPropertyNames,
56     NULL,
57     NULL,
58     NULL, //hasInstance,
59     NULL  //convertToType
60 };
61
62 JSStaticValue JSFeature::m_properties[] = {
63     { ATTRIBUTE_URI, getProperty, NULL, kJSPropertyAttributeReadOnly },
64     { ATTRIBUTE_REQUIRED, getProperty, NULL, kJSPropertyAttributeReadOnly },
65     { ATTRIBUTE_PARAMS, getProperty, NULL, kJSPropertyAttributeReadOnly },
66     { 0, 0, 0, 0 }
67 };
68
69 const JSClassDefinition* JSFeature::getClassInfo()
70 {
71     return &m_classInfo;
72 }
73
74 JSClassRef JSFeature::m_classRef = JSClassCreate(JSFeature::getClassInfo());
75
76 void JSFeature::initialize(JSContextRef context, JSObjectRef object)
77 {
78     //LogDebug("entered");
79     //private object should be passed to JSObjectMake function.
80     //Nothing to do here.
81 }
82
83 void JSFeature::finalize(JSObjectRef object)
84 {
85     LogDebug("entered");
86     JSFeaturePrivateObject* priv =
87             static_cast<JSFeaturePrivateObject*>(JSObjectGetPrivate(object));
88     delete priv;
89 }
90
91 JSClassRef JSFeature::getClassRef()
92 {
93     if (!m_classRef) {
94         m_classRef = JSClassCreate(&m_classInfo);
95     }
96     return m_classRef;
97 }
98
99 JSValueRef JSFeature::getProperty(JSContextRef context, JSObjectRef object,
100         JSStringRef propertyName, JSValueRef* exception)
101 {
102     LogDebug("entered");
103     JSFeaturePrivateObject* priv =
104         static_cast<JSFeaturePrivateObject*>(JSObjectGetPrivate(object));
105     if (!priv) {
106         LogError("no private object");
107         return JSValueMakeUndefined(context);
108     }
109     Try
110     {
111         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
112         if (JSStringIsEqualToUTF8CString(propertyName, ATTRIBUTE_URI)) {
113             return converter.toJSValueRef(priv->getObject()->getName() );
114         }
115         else if (JSStringIsEqualToUTF8CString(propertyName, ATTRIBUTE_REQUIRED)) {
116             if (priv->getObject()->isRequestedByWidget()) {
117                 return converter.toJSValueRef(priv->getObject()->isRequired() );
118             }
119             return JSValueMakeNull(context);
120         }
121         else if (JSStringIsEqualToUTF8CString(propertyName, ATTRIBUTE_PARAMS)) {
122             if (priv->getObject()->isRequestedByWidget()) {
123                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
124                 if (jsResult) {
125                     WrtDeviceApis::WidgetDB::Api::IWidgetFeature::Params params =
126                         priv->getObject()->getParams();
127                     WrtDeviceApis::WidgetDB::Api::IWidgetFeature::ParamsIterator it =
128                         params.begin();
129                     size_t i = 0;
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);
135                         if(result == false)
136                                 continue;
137                     }
138                     return jsResult;
139                 }
140             }
141             return JSValueMakeNull(context);
142         }
143     }
144     Catch(WrtDeviceApis::Commons::Exception)
145     {
146         LogError("error during conversion");
147     }
148     return JSValueMakeUndefined(context);
149 }
150
151 } // Tizen
152 } // DeviceAPI
153