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.
21 #include <Commons/Exception.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
25 #include <JSWebAPIErrorFactory.h>
26 #include "JSTimeDuration.h"
27 #include "TimeUtilConverter.h"
34 using namespace DeviceAPI::Common;
35 using namespace WrtDeviceApis::Commons;
36 using namespace WrtDeviceApis::CommonsJavaScript;
38 #define TIZEN_TIMEDURATION_LENGTH "length"
39 #define TIZEN_TIMEDURATION_UNIT "unit"
41 JSClassDefinition JSTimeDuration::m_classInfo = {
43 kJSClassAttributeNone,
52 setProperty, //SetProperty,
53 NULL, //DeleteProperty,
54 NULL, //GetPropertyNames,
55 NULL, //CallAsFunction,
56 NULL, //CallAsConstructor,
62 JSStaticFunction JSTimeDuration::m_function[] = {
63 {"difference", JSTimeDuration::difference, kJSPropertyAttributeNone},
64 {"equalsTo", JSTimeDuration::equalsTo, kJSPropertyAttributeNone},
65 {"lessThan", JSTimeDuration::lessThan, kJSPropertyAttributeNone},
66 {"greaterThan", JSTimeDuration::greaterThan, kJSPropertyAttributeNone},
69 JSStaticValue JSTimeDuration::m_property[] =
71 //TimdDurationProperties
72 {TIZEN_TIMEDURATION_LENGTH, getProperty, setProperty, kJSPropertyAttributeNone},
73 {TIZEN_TIMEDURATION_UNIT, getProperty, setProperty, kJSPropertyAttributeNone},
77 const JSClassRef JSTimeDuration::getClassRef()
80 m_jsClassRef = JSClassCreate(&m_classInfo);
85 const JSClassDefinition* JSTimeDuration::getClassInfo()
90 JSClassRef JSTimeDuration::m_jsClassRef = JSClassCreate(JSTimeDuration::getClassInfo());
92 void JSTimeDuration::initialize(JSContextRef context, JSObjectRef object)
94 LoggerD("entered Nothing to do.");
97 void JSTimeDuration::finalize(JSObjectRef object)
99 TimeDurationPrivObject* priv = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(object));
100 JSObjectSetPrivate(object, NULL);
101 LoggerD("Deleting TimeDuration");
105 JSValueRef JSTimeDuration::getProperty(JSContextRef context, JSObjectRef object,
106 JSStringRef propertyName, JSValueRef* exception)
111 TimeUtilConverter convert(context);
113 LoggerD("propertyName : " << convert.toString(propertyName));
114 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_LENGTH)) {
115 return convert.toJSValueRef(static_cast<double>(convert.getDurationLength(object)));
116 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_UNIT)) {
117 TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(object));
118 if (!privateObject) {
119 LoggerE("Private object is not set.");
120 ThrowMsg(ConversionException, "Private object not initialized");
123 DurationPropertiesPtr duration = privateObject->getObject();
124 std::string unitStr = duration->unitStr;
125 if (duration->unit != WRONG_UNIT)
126 duration->unitStr = convert.toDurationUnitString(duration->unit);
127 return convert.toJSValueRef(duration->unitStr);
129 } Catch (ConversionException) {
130 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
131 } Catch (InvalidArgumentException) {
132 LoggerE("Exception: " << _rethrown_exception.GetMessage());
133 } Catch (WrtDeviceApis::Commons::Exception) {
134 LoggerE("Exception: " << _rethrown_exception.GetMessage());
136 return JSValueMakeUndefined(context);
139 bool JSTimeDuration::setProperty(JSContextRef context, JSObjectRef object,
140 JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
145 TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(object));
146 if (!privateObject) {
147 LoggerE("Private object is not set.");
148 ThrowMsg(NullPointerException, "Private object not initialized");
151 DurationPropertiesPtr duration = privateObject->getObject();
152 TimeUtilConverter convert(context);
154 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_LENGTH)) {
155 duration->length = convert.toLongLong(value);
156 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_UNIT)) {
157 duration->unitStr = convert.toString(value);
158 duration->unit = convert.toDurationUnit(duration->unitStr);
159 if (duration->unit == WRONG_UNIT)
160 LoggerE("Wrong Unit");
165 } Catch (NullPointerException) {
166 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
167 } Catch (ConversionException) {
168 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
169 } Catch (InvalidArgumentException) {
170 LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage());
171 } Catch (WrtDeviceApis::Commons::Exception) {
172 LoggerE("Exception: " << _rethrown_exception.GetMessage());
177 JSObjectRef JSTimeDuration::createJSObject(JSContextRef context,
178 const DurationProperties &durations)
180 DurationPropertiesPtr durationProps(new DurationProperties(durations));
181 TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps);
184 ThrowMsg(NullPointerException, "Can not new an object");
186 return JSObjectMake(context, getClassRef(), priv);
189 JSObjectRef JSTimeDuration::createJSObject(JSContextRef context,
190 const DurationPropertiesPtr duration)
192 TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, duration);
194 ThrowMsg(NullPointerException, "Private object is null.");
196 return JSObjectMake(context, getClassRef(), priv);
199 JSObjectRef JSTimeDuration::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
201 TimeUtilConverter convert(ctx);
203 DurationProperties duration;
204 if (argumentCount == 0)
205 duration.length = convert.toLongLong(JSValueMakeUndefined(ctx));
207 duration.length = convert.toLongLong(arguments[0]);
209 if (argumentCount > 1) {
210 if (!JSValueIsUndefined(ctx, arguments[1]) && !JSValueIsNull(ctx, arguments[1])) {
211 duration.unitStr = convert.toString(arguments[1]);
212 duration.unit = convert.toDurationUnit(duration.unitStr);
213 if (duration.unit == WRONG_UNIT)
214 ThrowMsg(ConversionException, "Argument(unit) is invalid(wrong type unit)");
217 JSObjectRef timeDuration = createJSObject(ctx, duration);
219 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
220 JSObjectSetProperty(ctx, timeDuration, ctorName, constructor,
221 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
222 JSStringRelease(ctorName);
225 } Catch(NullPointerException) {
226 LoggerE("Exception: " << _rethrown_exception.GetMessage());
227 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
228 } Catch(WrtDeviceApis::Commons::UnknownException) {
229 LoggerE("Exception: " << _rethrown_exception.GetMessage());
230 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
231 } Catch(ConversionException) {
232 LoggerE("Exception: " << _rethrown_exception.GetMessage());
233 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
234 } Catch (InvalidArgumentException) {
235 LoggerE("Exception: " << _rethrown_exception.GetMessage());
236 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
237 } Catch (WrtDeviceApis::Commons::Exception) {
238 LoggerW("Trying to get incorrect value");
240 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
243 JSValueRef JSTimeDuration::diffTimeDuration(JSContextRef context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception, CompareType type) {
246 TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(thisObject));
247 if (!privateObject) {
248 LoggerE("Private object is not set.");
249 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
252 TimeUtilConverter converter(context);
254 DurationProperties first = converter.getDurationProperties(thisObject);
255 DurationProperties second;
256 if (argumentCount == 0)
257 second= converter.getDurationProperties(JSValueMakeUndefined(context));
259 second= converter.getDurationProperties(arguments[0]);
261 DurationProperties diff;
262 if (first.unit > second.unit) {
263 long long firstLength = converter.convertDurationLength(first, second.unit);
264 diff.unit = second.unit;
265 diff.length = firstLength - second.length;
267 long long secondLength = converter.convertDurationLength(second, first.unit);
268 diff.unit = first.unit;
269 diff.length = first.length - secondLength;
275 if (diff.length == 0)
276 return converter.toJSValueRef(true);
278 return converter.toJSValueRef(false);
283 return converter.toJSValueRef(true);
285 return converter.toJSValueRef(false);
290 return converter.toJSValueRef(true);
292 return converter.toJSValueRef(false);
296 return converter.makeDurationObject(converter.optimizedTimeDuration(diff));
300 JSValueRef JSTimeDuration::difference(JSContextRef context, JSObjectRef function,
301 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
304 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, DIFFERENCE);
305 } Catch(ConversionException) {
306 LoggerE("Exception: " << _rethrown_exception.GetMessage());
307 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
308 } Catch (InvalidArgumentException) {
309 LoggerE("Exception: " << _rethrown_exception.GetMessage());
310 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
311 } Catch (UnsupportedException) {
312 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
313 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR , "Not Support");
314 } Catch (PlatformException) {
315 LoggerE("Exception: " << _rethrown_exception.GetMessage());
316 } Catch (WrtDeviceApis::Commons::Exception) {
317 LoggerE("Exception: " << _rethrown_exception.GetMessage());
319 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
322 JSValueRef JSTimeDuration::equalsTo(JSContextRef context, JSObjectRef function,
323 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
326 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, EQUALSTO);
327 } Catch(ConversionException) {
328 LoggerE("Exception: " << _rethrown_exception.GetMessage());
329 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
330 } Catch (InvalidArgumentException) {
331 LoggerE("Exception: " << _rethrown_exception.GetMessage());
332 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
333 } Catch (UnsupportedException) {
334 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
335 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR , "Not Support");
336 } Catch (PlatformException) {
337 LoggerE("Exception: " << _rethrown_exception.GetMessage());
338 } Catch (WrtDeviceApis::Commons::Exception) {
339 LoggerE("Exception: " << _rethrown_exception.GetMessage());
341 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
344 JSValueRef JSTimeDuration::lessThan(JSContextRef context, JSObjectRef function,
345 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
348 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, LESSTHAN);
349 } Catch(ConversionException) {
350 LoggerE("Exception: " << _rethrown_exception.GetMessage());
351 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
352 } Catch (InvalidArgumentException) {
353 LoggerE("Exception: " << _rethrown_exception.GetMessage());
354 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
355 } Catch (UnsupportedException) {
356 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
357 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR , "Not Support");
358 } Catch (PlatformException) {
359 LoggerE("Exception: " << _rethrown_exception.GetMessage());
360 } Catch (WrtDeviceApis::Commons::Exception) {
361 LoggerE("Exception: " << _rethrown_exception.GetMessage());
363 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
366 JSValueRef JSTimeDuration::greaterThan(JSContextRef context, JSObjectRef function,
367 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
370 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, GREATERTHAN);
371 } Catch(ConversionException) {
372 LoggerE("Exception: " << _rethrown_exception.GetMessage());
373 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
374 } Catch (InvalidArgumentException) {
375 LoggerE("Exception: " << _rethrown_exception.GetMessage());
376 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
377 } Catch (UnsupportedException) {
378 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
379 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR , "Not Support");
380 } Catch (PlatformException) {
381 LoggerE("Exception: " << _rethrown_exception.GetMessage());
382 } Catch (WrtDeviceApis::Commons::Exception) {
383 LoggerE("Exception: " << _rethrown_exception.GetMessage());
385 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");