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.
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>
26 #include "JSApplicationControlData.h"
27 #include "ApplicationConverter.h"
28 #include "ApplicationUtil.h"
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
35 namespace Application {
37 JSClassRef JSApplicationControlData::m_classRef = NULL;
39 JSClassDefinition JSApplicationControlData::m_classInfo =
42 kJSClassAttributeNone,
43 TIZEN_INTERFACE_APPLICATION_CONTROL_DATA,
50 getProperty, //GetProperty,
51 setProperty, //SetProperty,
52 NULL, //DeleteProperty,
53 NULL, //getPropertyNames,
57 NULL, //ConvertToType,
60 JSStaticValue JSApplicationControlData::m_property[] = {
61 { TIZEN_APPLICATION_CONTROL_DATA_KEY, getProperty, setProperty, kJSPropertyAttributeNone },
62 { TIZEN_APPLICATION_CONTROL_DATA_VALUE, getProperty, setProperty, kJSPropertyAttributeNone },
66 JSStaticFunction JSApplicationControlData::m_functions[] =
71 JSClassRef JSApplicationControlData::getClassRef() {
73 m_classRef = JSClassCreate(&m_classInfo);
78 JSValueRef JSApplicationControlData::createJSObject(JSContextRef context,
79 const std::string &key,
80 const std::vector<std::string> &value)
82 ApplicationControlDataPtr privateData = ApplicationControlDataPtr(new ApplicationControlData());
84 privateData->setKey(key);
85 privateData->setValue(value);
86 JSApplicationControlDataPriv *priv = new JSApplicationControlDataPriv(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);
97 JSObjectRef JSApplicationControlData::constructor(JSContextRef context, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
99 if (argumentCount == 0) {
100 LogError("Wrong parameters");
101 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
105 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
108 std::vector<std::string> value;
110 if (argumentCount > 0) {
111 key = converter->toString(arguments[0]);
113 if (argumentCount > 1) {
114 if (JSIsArrayValue(context, arguments[1])) {
115 value = converter->toVectorOfStrings(arguments[1]);
117 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
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");
128 void JSApplicationControlData::initialize(JSContextRef context, JSObjectRef object)
130 LogInfo(">> initialize");
133 void JSApplicationControlData::finalize(JSObjectRef object)
135 LogInfo(">> finalize");
136 JSApplicationControlDataPriv* priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
137 JSObjectSetPrivate(object, NULL);
138 LogDebug("Deleting ApplicationControl object");
142 bool JSApplicationControlData::isObjectOfClass(JSContextRef context, JSValueRef value)
144 return JSValueIsObjectOfClass(context, value, getClassRef());
147 ApplicationControlDataPtr JSApplicationControlData::getPrivData(JSObjectRef object)
150 JSApplicationControlDataPriv *priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
152 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154 ApplicationControlDataPtr result = priv->getObject();
156 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
161 ApplicationControlDataPtr JSApplicationControlData::getApplicationControlData(JSContextRef context, JSValueRef value)
163 if (!isObjectOfClass(context, value)) {
164 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
166 JSObjectRef object = JSValueToObject(context, value, NULL);
168 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
170 JSApplicationControlDataPriv *priv = static_cast<JSApplicationControlDataPriv*>(JSObjectGetPrivate(object));
172 Throw(WrtDeviceApis::Commons::NullPointerException);
174 return priv->getObject();
178 JSValueRef JSApplicationControlData::getProperty(JSContextRef context,
180 JSStringRef propertyName,
181 JSValueRef* exception)
184 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
185 ApplicationControlDataPtr privateData = getPrivData(object);
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());
192 } Catch(WrtDeviceApis::Commons::Exception) {
193 LogError("Exception: " << _rethrown_exception.GetMessage());
194 return JSDOMExceptionFactory::UnknownException.make(context, exception);
197 return JSValueMakeUndefined(context);
200 bool JSApplicationControlData::setProperty(JSContextRef context,
202 JSStringRef propertyName,
204 JSValueRef* exception)
207 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
208 ApplicationControlDataPtr privateData = getPrivData(object);
210 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_KEY)) {
211 privateData->setKey(converter.toString(value));
213 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA_VALUE)) {
214 privateData->setValue(converter.toVectorOfStrings(value));
217 } Catch(WrtDeviceApis::Commons::Exception) {
218 LogError("Exception: " << _rethrown_exception.GetMessage());
219 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");