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);
137 catch(BasePlatformException &e)
139 LoggerW("Failed to convert 1st parameter to double. Using default value.");
145 longitude = validator.toDouble(1, false);
147 catch(BasePlatformException &e)
149 LoggerW("Failed to convert 2nd parameter to double. Using default value.");
153 SimpleCoordinatesPtr simpleCoordinates(new SimpleCoordinates(latitude, longitude));
155 JSObjectRef jsobject = NULL;
159 jsobject = createJSObject(context, simpleCoordinates);
163 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
167 return JSObjectMake(context, NULL, NULL);
172 void JSSimpleCoordinates::Initialize(JSContextRef context, JSObjectRef object)
174 if (!JSObjectGetPrivate(object))
176 SimpleCoordinatesPtr coord(new SimpleCoordinates(0.0, 0.0));
177 JSSimpleCoordinatesPriv *priv = new JSSimpleCoordinatesPriv(context, SimpleCoordinatesPtr(coord));
178 if (!JSObjectSetPrivate(object, priv))
185 void JSSimpleCoordinates::Finalize(JSObjectRef object)
187 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
197 SimpleCoordinatesPtr JSSimpleCoordinates::getPrivData(JSObjectRef object)
200 JSSimpleCoordinatesPriv *priv = static_cast<JSSimpleCoordinatesPriv*>(JSObjectGetPrivate(object));
202 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
204 SimpleCoordinatesPtr result = priv->getObject();
206 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
211 JSValueRef JSSimpleCoordinates::getLatitude(JSContextRef context,
213 JSStringRef propertyName,
214 JSValueRef* exception)
220 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
221 return JSUtil::toJSValueRef(context, simpleCoordinates->getLatitude());
223 catch(BasePlatformException &e)
225 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
228 return JSValueMakeUndefined(context);
231 bool JSSimpleCoordinates::setLatitude(JSContextRef context,
233 JSStringRef propertyName,
235 JSValueRef* exception)
241 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
242 simpleCoordinates->setLatitude(JSUtil::JSValueToDouble(context, value));
244 catch(BasePlatformException &e)
246 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());
252 JSValueRef JSSimpleCoordinates::getLongitude(JSContextRef context,
254 JSStringRef propertyName,
255 JSValueRef* exception)
261 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
262 return JSUtil::toJSValueRef(context, simpleCoordinates->getLongitude());
264 catch(BasePlatformException &e)
266 LoggerW("trying to get incorrect value [" << e.getName() << "] " << e.getMessage());
269 return JSValueMakeUndefined(context);
272 bool JSSimpleCoordinates::setLongitude(JSContextRef context,
274 JSStringRef propertyName,
276 JSValueRef* exception)
282 SimpleCoordinatesPtr simpleCoordinates = getPrivData(object);
283 simpleCoordinates->setLongitude(JSUtil::JSValueToDouble(context, value));
285 catch(BasePlatformException &e)
287 LoggerW("trying to set incorrect value [" << e.getName() << "] " << e.getMessage());