2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <dpl/log/log.h>
18 #include <dpl/shared_ptr.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
21 #include "JSApplicationServiceData.h"
22 #include "ApplicationConverter.h"
26 namespace Application {
28 using namespace Api::Application;
29 using namespace WrtDeviceApis;
30 using namespace WrtDeviceApis::CommonsJavaScript;
34 // const char* APPLICATION_SERVICE_DATA_KEY = "key";
35 // const char* APPLICATION_SERVICE_DATA_VALUE = "value";
38 JSClassRef JSApplicationServiceData::m_classRef = NULL;
40 JSClassDefinition JSApplicationServiceData::m_classInfo =
43 kJSClassAttributeNone,
44 "ApplicationServiceData",
53 NULL, //DeleteProperty,
54 NULL, //getPropertyNames,
58 NULL, //ConvertToType,
61 JSStaticValue JSApplicationServiceData::m_property[] = {
62 { "key", getDataKey, setDataKey, kJSPropertyAttributeNone },
63 { "value", getDataValue, setDataValue, kJSPropertyAttributeNone },
67 JSStaticFunction JSApplicationServiceData::m_functions[] =
72 JSClassRef JSApplicationServiceData::getClassRef() {
74 m_classRef = JSClassCreate(&m_classInfo);
79 JSValueRef JSApplicationServiceData::createJSObject(JSContextRef context,
80 const std::string &key,
81 const std::vector<std::string> &value)
83 ApplicationServiceDataPtr privateData = ApplicationServiceDataPtr(new ApplicationServiceData());
85 privateData->setKey(key);
86 privateData->setValue(value);
87 JSApplicationServiceDataPriv *priv = new JSApplicationServiceDataPriv(context, privateData);
88 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
89 if (NULL == jsValueRef) {
90 LogError("object creation error");
91 return JSValueMakeUndefined(context);
96 JSObjectRef JSApplicationServiceData::constructor(JSContextRef context, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
98 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
100 if ((argumentCount == 2) && JSValueIsString(context, arguments[0]) && JSIsArrayValue(context, arguments[1])) {
101 return JSValueToObject(context, createJSObject(context, converter->toString(arguments[0]), converter->toVectorOfStrings(arguments[1])), exception);
108 void JSApplicationServiceData::initialize(JSContextRef context, JSObjectRef object)
110 //assert(NULL != JSObjectGetPrivate(object));
113 void JSApplicationServiceData::finalize(JSObjectRef object)
116 //delete (JSObjectGetPrivate(object));
119 bool JSApplicationServiceData::isObjectOfClass(JSContextRef context, JSValueRef value)
121 return JSValueIsObjectOfClass(context, value, getClassRef());
124 ApplicationServiceDataPtr JSApplicationServiceData::getPrivData(JSObjectRef object)
127 JSApplicationServiceDataPriv *priv = static_cast<JSApplicationServiceDataPriv*>(JSObjectGetPrivate(object));
129 ThrowMsg(Commons::NullPointerException, "Private object is null");
131 ApplicationServiceDataPtr result = priv->getObject();
133 ThrowMsg(Commons::NullPointerException, "Private object is null");
138 ApplicationServiceDataPtr JSApplicationServiceData::getApplicationServiceData(JSContextRef context, JSValueRef value)
140 if (!isObjectOfClass(context, value)) {
141 Throw(Commons::InvalidArgumentException);
143 JSObjectRef object = JSValueToObject(context, value, NULL);
145 Throw(Commons::InvalidArgumentException);
147 JSApplicationServiceDataPriv *priv = static_cast<JSApplicationServiceDataPriv*>(JSObjectGetPrivate(object));
149 Throw(Commons::NullPointerException);
151 return priv->getObject();
154 JSValueRef JSApplicationServiceData::getDataKey(JSContextRef context,
156 JSStringRef propertyName,
157 JSValueRef* exception)
161 CommonsJavaScript::Converter converter(context);
162 ApplicationServiceDataPtr privateData = getPrivData(object);
163 return converter.toJSValueRef(privateData->getKey());
165 Catch(Commons::Exception)
167 LogWarning("trying to get incorrect value");
169 return JSValueMakeUndefined(context);
172 bool JSApplicationServiceData::setDataKey(JSContextRef context,
174 JSStringRef propertyName,
176 JSValueRef* exception)
180 ApplicationServiceDataPtr privateData = getPrivData(object);
181 CommonsJavaScript::Converter converter(context);
182 privateData->setKey(converter.toString(key));
185 Catch(Commons::Exception)
187 LogWarning("trying to set incorrect value");
189 JSDOMExceptionFactory::TypeMismatchException.make(context, exception);
193 JSValueRef JSApplicationServiceData::getDataValue(JSContextRef context,
195 JSStringRef propertyName,
196 JSValueRef* exception)
200 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
201 ApplicationServiceDataPtr privateData = getPrivData(object);
203 return converter.toJSValueRef(privateData->getValue());
205 Catch(WrtDeviceApis::Commons::Exception)
207 LogWarning("trying to get incorrect value");
209 return JSValueMakeUndefined(context);
212 bool JSApplicationServiceData::setDataValue(JSContextRef context,
214 JSStringRef propertyName,
216 JSValueRef* exception)
220 ApplicationServiceDataPtr privateData = getPrivData(object);
221 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
222 privateData->setValue(converter.toVectorOfStrings(value));
225 Catch(WrtDeviceApis::Commons::Exception)
227 LogWarning("trying to set incorrect value");
229 JSDOMExceptionFactory::TypeMismatchException.make(context, exception);