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
25 #include "JSSimpleCoordinates.h"
28 #include <dpl/shared_ptr.h>
30 #include <ArgumentValidator.h>
31 #include <JSWebAPIErrorFactory.h>
34 #define ATTRIBUTE_FILTER_CLASS_NAME "SimpleCoordinates"
36 #define ATTRIBUTE_FILTER_ATTR_LATITUDE "latitude"
37 #define ATTRIBUTE_FILTER_ATTR_LONGITUDE "longitude"
39 namespace DeviceAPI {
\rnamespace Tizen {
41 using namespace DeviceAPI::Common;
42 using namespace DeviceAPI::Tizen;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
46 JSClassRef JSSimpleCoordinates::m_classRef = NULL;
48 JSClassDefinition JSSimpleCoordinates::m_classInfo =
51 kJSClassAttributeNone,
52 ATTRIBUTE_FILTER_CLASS_NAME,
61 NULL, //DeleteProperty,
62 NULL, //getPropertyNames,
63 NULL, //CallAsFunction,
64 NULL, //CallAsConstructor,
66 NULL, //ConvertToType,
69 JSStaticValue JSSimpleCoordinates::m_property[] = {
70 { ATTRIBUTE_FILTER_ATTR_LATITUDE, getLatitude, setLatitude, kJSPropertyAttributeNone },
71 { ATTRIBUTE_FILTER_ATTR_LONGITUDE, getLongitude, setLongitude, kJSPropertyAttributeNone },
75 JSStaticFunction JSSimpleCoordinates::m_functions[] =
80 JSClassRef JSSimpleCoordinates::getClassRef() {
82 m_classRef = JSClassCreate(&m_classInfo);
87 bool JSSimpleCoordinates::isObjectOfClass(JSContextRef context, JSValueRef value)
89 return JSValueIsObjectOfClass(context, value, getClassRef());
92 SimpleCoordinatesPtr JSSimpleCoordinates::getSimpleCoordinates(JSContextRef context, JSValueRef value)
94 if (!isObjectOfClass(context, value))
95 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
97 JSObjectRef object = JSValueToObject(context, value, NULL);
99 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
103 Throw(WrtDeviceApis::Commons::NullPointerException);
105 return priv->getObject();
108 JSObjectRef JSSimpleCoordinates::createJSObject(JSContextRef context, SimpleCoordinatesPtr privateData)
110 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, privateData);
111 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
112 if (NULL == jsObjectRef)
114 LoggerE("object creation error");
120 JSObjectRef JSSimpleCoordinates::constructor(JSContextRef context,
121 JSObjectRef constructor,
122 size_t argumentCount,
123 const JSValueRef arguments[],
124 JSValueRef* exception)
128 ArgumentValidator validator(context, argumentCount, arguments);
135 latitude = validator.toDouble(0, false);
136 longitude = validator.toDouble(1, false);
138 catch(BasePlatformException &e)
140 return JSWebAPIErrorFactory::postException(context, exception, e);
143 SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
145 JSObjectRef jsobject;
149 jsobject = createJSObject(context, simpleCoordinates);
153 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
154 return JSWebAPIErrorFactory::postException(context, exception,
155 TypeMismatchException("Error occurred while creating object"));
161 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
163 if (!JSObjectGetPrivate(object))
165 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
166 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
167 if (!JSObjectSetPrivate(object, priv))
174 void JSSimpleCoordinates::Finalize(JSObjectRef object)
176 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
186 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
189 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
191 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
193 SimpleCoordinatesPtr result = priv->getObject();
195 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
200 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
202 JSStringRef propertyName,
203 JSValueRef* exception)
209 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
210 return JSUtil::toJSValueRef(context, simpleCoordinates->getLatitude());
212 catch(BasePlatformException &e)
214 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
217 return JSValueMakeUndefined(context);
220 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
222 JSStringRef propertyName,
224 JSValueRef* exception)
230 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
231 simpleCoordinates->setLatitude(JSUtil::JSValueToDouble(context, value));
233 catch(BasePlatformException &e)
235 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
241 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
243 JSStringRef propertyName,
244 JSValueRef* exception)
250 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
251 return JSUtil::toJSValueRef(context, simpleCoordinates->getLongitude());
253 catch(BasePlatformException &e)
255 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
258 return JSValueMakeUndefined(context);
261 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
263 JSStringRef propertyName,
265 JSValueRef* exception)
271 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
272 simpleCoordinates->setLongitude(JSUtil::JSValueToDouble(context, value));
274 catch(BasePlatformException &e)
276 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());