Update change log and spec for wrt-plugins-tizen_0.4.9
[platform/framework/web/wrt-plugins-tizen.git] / src / Application / JSApplicationControlData.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 #include <dpl/log/log.h>
20 #include <dpl/shared_ptr.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include <JSTizenExceptionFactory.h>
24 #include <JSTizenException.h>
25
26 #include "JSApplicationControlData.h"
27 #include "ApplicationConverter.h"
28 #include "ApplicationUtil.h"
29
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
33
34 namespace DeviceAPI {
35 namespace Application {
36
37 JSClassRef JSApplicationControlData::m_classRef = NULL;
38
39 JSClassDefinition JSApplicationControlData::m_classInfo =
40 {
41         0,
42         kJSClassAttributeNone,
43         TIZEN_INTERFACE_APPLICATION_CONTROL_DATA,
44         NULL,
45         m_property,
46         m_functions,
47         initialize,
48         finalize,
49         NULL, //hasProperty,
50         getProperty, //GetProperty,
51         setProperty, //SetProperty,
52         NULL, //DeleteProperty,
53         NULL, //getPropertyNames,
54         NULL,
55         constructor,
56         NULL,
57         NULL, //ConvertToType,
58 };
59
60 JSStaticValue JSApplicationControlData::m_property[] = {
61         { TIZEN_APPLICATION_CONTROL_DATA_KEY, getProperty, setProperty, kJSPropertyAttributeNone },
62         { TIZEN_APPLICATION_CONTROL_DATA_VALUE, getProperty, setProperty, kJSPropertyAttributeNone },
63         { 0, 0, 0, 0 }
64 };
65
66 JSStaticFunction JSApplicationControlData::m_functions[] =
67 {
68         { 0, 0, 0 }
69 };
70
71 JSClassRef JSApplicationControlData::getClassRef() {
72         if (!m_classRef) {
73                 m_classRef = JSClassCreate(&m_classInfo);
74         }
75         return m_classRef;
76 }
77
78 JSValueRef JSApplicationControlData::createJSObject(JSContextRef context,
79                 const std::string &key,
80                 const std::vector<std::string> &value)
81 {
82         ApplicationControlDataPtr privateData = ApplicationControlDataPtr(new ApplicationControlData());
83
84         privateData->setKey(key);
85         privateData->setValue(value);
86         JSApplicationControlDataPriv *priv = new JSApplicationControlDataPriv(context, privateData);
87         
88         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
89         if (NULL == jsValueRef) {
90                 LogError("object creation error");
91                 return JSValueMakeUndefined(context);
92         }
93         
94         return jsValueRef;
95 }
96
97 JSObjectRef JSApplicationControlData::constructor(JSContextRef context, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
98 {
99         if (argumentCount == 0) {
100                 LogError("Wrong parameters");
101                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
102                 return NULL;
103         }       
104
105         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
106         Try {
107                 std::string key;
108                 std::vector<std::string> value;
109                 
110                 if (argumentCount > 0) {
111                         key = converter->toString(arguments[0]);
112                 }
113                 if (argumentCount > 1) {
114                         if (JSIsArrayValue(context, arguments[1])) {
115                                 value = converter->toVectorOfStrings(arguments[1]);
116                         } else {
117                                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
118                                 return NULL;
119                         }
120                 }
121                 return JSValueToObject(context, createJSObject(context, key, value), exception);
122         } Catch (WrtDeviceApis::Commons::Exception) {
123                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
124                 return NULL;
125         }
126 }
127
128 void JSApplicationControlData::initialize(JSContextRef context, JSObjectRef object)
129 {
130         LogInfo(">> initialize");
131 }
132
133 void JSApplicationControlData::finalize(JSObjectRef object)
134 {
135     LogInfo(">> finalize");
136     JSApplicationControlDataPriv* priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
137     JSObjectSetPrivate(object, NULL);
138     LogDebug("Deleting ApplicationControl object");
139     delete priv;        
140 }
141
142 bool JSApplicationControlData::isObjectOfClass(JSContextRef context, JSValueRef value)
143 {
144         return JSValueIsObjectOfClass(context, value, getClassRef());
145 }
146
147 ApplicationControlDataPtr JSApplicationControlData::getPrivData(JSObjectRef object)
148 {
149         LogDebug("entered");
150         JSApplicationControlDataPriv *priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
151         if (!priv) {
152                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153         }
154         ApplicationControlDataPtr result = priv->getObject();
155         if (!result) {
156                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
157         }
158         return result;
159 }
160
161 ApplicationControlDataPtr JSApplicationControlData::getApplicationControlData(JSContextRef context, JSValueRef value)
162 {
163         if (!isObjectOfClass(context, value)) {
164                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
165         }
166         JSObjectRef object = JSValueToObject(context, value, NULL);
167         if (!object) {
168                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
169         }
170         JSApplicationControlDataPriv *priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
171         if (!priv) {
172                 Throw(WrtDeviceApis::Commons::NullPointerException);
173         }
174         return priv->getObject();
175 }
176
177
178 JSValueRef JSApplicationControlData::getProperty(JSContextRef context,
179                 JSObjectRef object,
180                 JSStringRef propertyName,
181                 JSValueRef* exception)
182 {
183         Try     {
184                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
185                 ApplicationControlDataPtr privateData = getPrivData(object);
186                 
187                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_KEY)) {
188                         return converter.toJSValueRef(privateData->getKey());
189                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_VALUE)) {
190                         return converter.toJSValueRef(privateData->getValue());
191                 } 
192         } Catch(WrtDeviceApis::Commons::Exception) {
193         LogError("Exception: " << _rethrown_exception.GetMessage());
194                 return JSDOMExceptionFactory::UnknownException.make(context, exception);
195     }
196         
197         return JSValueMakeUndefined(context);
198 }
199
200 bool JSApplicationControlData::setProperty(JSContextRef context,
201                         JSObjectRef object,
202                         JSStringRef propertyName,
203                         JSValueRef value,
204                         JSValueRef* exception)
205 {
206         Try     {
207                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
208                 ApplicationControlDataPtr privateData = getPrivData(object);
209                 
210                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_KEY)) {
211                         privateData->setKey(converter.toString(value));
212                         return true;
213                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_VALUE)) {
214                         privateData->setValue(converter.toVectorOfStrings(value));
215                         return true;                    
216                 } 
217         } Catch(WrtDeviceApis::Commons::Exception) {
218                 LogError("Exception: " << _rethrown_exception.GetMessage());
219         JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
220     }
221         
222         return false;
223 }
224
225 }
226 }