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 * @file JSSimpleCoordinates.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSSimpleCoordinates class
26 #include <dpl/log/log.h>
27 #include <dpl/shared_ptr.h>
28 #include <CommonsJavaScript/Converter.h>
29 #include <CommonsJavaScript/Validator.h>
30 #include <JSTizenExceptionFactory.h>
31 #include <JSTizenException.h>
32 #include "SimpleCoordinates.h"
33 #include "FilterConverter.h"
34 #include "JSSimpleCoordinates.h"
36 #define ATTRIBUTE_FILTER_CLASS_NAME "SimpleCoordinates"
38 #define ATTRIBUTE_FILTER_ATTR_LATITUDE "latitude"
39 #define ATTRIBUTE_FILTER_ATTR_LONGITUDE "longitude"
41 namespace DeviceAPI {
\rnamespace Tizen {
43 using namespace DeviceAPI::Common;
44 using namespace DeviceAPI::Tizen;
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
48 JSClassRef JSSimpleCoordinates::m_classRef = NULL;
50 JSClassDefinition JSSimpleCoordinates::m_classInfo =
53 kJSClassAttributeNone,
54 ATTRIBUTE_FILTER_CLASS_NAME,
63 NULL, //DeleteProperty,
64 NULL, //getPropertyNames,
65 NULL, //CallAsFunction,
66 constructor, //CallAsConstructor,
68 NULL, //ConvertToType,
71 JSStaticValue JSSimpleCoordinates::m_property[] = {
72 { ATTRIBUTE_FILTER_ATTR_LATITUDE, getLatitude, setLatitude, kJSPropertyAttributeNone },
73 { ATTRIBUTE_FILTER_ATTR_LONGITUDE, getLongitude, setLongitude, kJSPropertyAttributeNone },
77 JSStaticFunction JSSimpleCoordinates::m_functions[] =
82 JSClassRef JSSimpleCoordinates::getClassRef() {
84 m_classRef = JSClassCreate(&m_classInfo);
89 bool JSSimpleCoordinates::isObjectOfClass(JSContextRef context, JSValueRef value)
91 return JSValueIsObjectOfClass(context, value, getClassRef());
94 SimpleCoordinatesPtr JSSimpleCoordinates::getSimpleCoordinates(JSContextRef context, JSValueRef value)
96 if (!isObjectOfClass(context, value)) {
97 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99 JSObjectRef object = JSValueToObject(context, value, NULL);
101 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
105 Throw(WrtDeviceApis::Commons::NullPointerException);
107 return priv->getObject();
110 JSObjectRef JSSimpleCoordinates::createJSObject(JSContextRef context, SimpleCoordinatesPtr privateData)
112 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, privateData);
113 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
114 if (NULL == jsObjectRef) {
115 LogError("object creation error");
121 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
123 if (!JSObjectGetPrivate(object))
125 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
126 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
127 if (!JSObjectSetPrivate(object, priv)) {
133 void JSSimpleCoordinates::Finalize(JSObjectRef object)
135 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
145 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
148 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
150 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152 SimpleCoordinatesPtr result = priv->getObject();
154 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
159 JSObjectRef JSSimpleCoordinates::constructor(JSContextRef context,
160 JSObjectRef constructor,
161 size_t argumentCount,
162 const JSValueRef arguments[],
163 JSValueRef* exception)
167 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
168 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
170 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(constructor));
172 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
174 JSContextRef gContext = priv->getContext();
176 // JSGlobalContextRef gContext = JSGlobalContextFactory::getInstance()->get();
178 BasicValidator validator = BasicValidatorFactory::getValidator(gContext, exception);
180 if (argumentCount < 2)
181 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
183 if (!JSValueIsNumber(gContext, arguments[0]))
184 ThrowMsg(InvalidArgumentException, "1st argument is not string.");
186 if (!JSValueIsNumber(gContext, arguments[1]))
187 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
189 } Catch(Exception ) {
190 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
191 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
195 FilterConverterFactory::ConverterType converter = FilterConverterFactory::getConverter(gContext);
201 latitude = converter->toDouble(arguments[0]);
203 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
204 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
209 longitude = converter->toDouble(arguments[1]);
211 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
212 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
216 SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
218 JSObjectRef jsobject;
221 jsobject = createJSObject(gContext, simpleCoordinates);
223 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
224 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
231 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
233 JSStringRef propertyName,
234 JSValueRef* exception)
239 FilterConverterFactory::ConverterType converter =
240 FilterConverterFactory::getConverter(context);
241 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
242 return converter->toJSValueRef(simpleCoordinates->getLatitude());
244 Catch(WrtDeviceApis::Commons::Exception)
246 LogWarning("trying to get incorrect value");
248 return JSValueMakeUndefined(context);
251 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
253 JSStringRef propertyName,
255 JSValueRef* exception)
259 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
260 FilterConverterFactory::ConverterType converter =
261 FilterConverterFactory::getConverter(context);
262 simpleCoordinates->setLatitude(converter->toDouble(value));
265 Catch(WrtDeviceApis::Commons::Exception)
267 LogWarning("trying to set incorrect value");
270 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
274 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
276 JSStringRef propertyName,
277 JSValueRef* exception)
282 FilterConverterFactory::ConverterType converter =
283 FilterConverterFactory::getConverter(context);
284 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
285 return converter->toJSValueRef(simpleCoordinates->getLongitude());
287 Catch(WrtDeviceApis::Commons::Exception)
289 LogWarning("trying to get incorrect value");
291 return JSValueMakeUndefined(context);
294 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
296 JSStringRef propertyName,
298 JSValueRef* exception)
302 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
303 FilterConverterFactory::ConverterType converter =
304 FilterConverterFactory::getConverter(context);
305 simpleCoordinates->setLongitude(converter->toDouble(value));
308 Catch(WrtDeviceApis::Commons::Exception)
310 LogWarning("trying to set incorrect value");
313 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");