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 std::string strUnit = convert.toDurationUnitString(convert.getDurationUnit(object));
119 ThrowMsg(InvalidArgumentException, "unit string is invald.");
120 return convert.toJSValueRef(strUnit);
122 } Catch (ConversionException) {
123 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
124 } Catch (InvalidArgumentException) {
125 LoggerE("Exception: " << _rethrown_exception.GetMessage());
126 } Catch (WrtDeviceApis::Commons::Exception) {
127 LoggerE("Exception: " << _rethrown_exception.GetMessage());
129 return JSValueMakeUndefined(context);
132 bool JSTimeDuration::setProperty(JSContextRef context, JSObjectRef object,
133 JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
138 TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(object));
139 if (!privateObject) {
140 LoggerE("Private object is not set.");
141 ThrowMsg(NullPointerException, "Private object not initialized");
144 DurationPropertiesPtr duration = privateObject->getObject();
145 TimeUtilConverter convert(context);
147 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_LENGTH)) {
148 duration->length = convert.toLongLong(value);
149 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_UNIT)) {
150 short unit = convert.toDurationUnit(convert.toString(value));
152 ThrowMsg(ConversionException, "property doesn't exist.");
153 duration->unit = unit;
158 } Catch (NullPointerException) {
159 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
160 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
161 } Catch (ConversionException) {
162 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
163 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
164 } Catch (InvalidArgumentException) {
165 LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage());
166 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
167 } Catch (WrtDeviceApis::Commons::Exception) {
168 LoggerE("Exception: " << _rethrown_exception.GetMessage());
169 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
174 JSObjectRef JSTimeDuration::createJSObject(JSContextRef context,
175 const DurationProperties &durations)
177 DurationPropertiesPtr durationProps(new DurationProperties(durations));
178 TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps);
181 ThrowMsg(NullPointerException, "Can not new an object");
183 return JSObjectMake(context, getClassRef(), priv);
186 JSObjectRef JSTimeDuration::createJSObject(JSContextRef context,
187 const DurationPropertiesPtr duration)
189 TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, duration);
191 ThrowMsg(NullPointerException, "Private object is null.");
193 return JSObjectMake(context, getClassRef(), priv);
196 JSObjectRef JSTimeDuration::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
198 TimeUtilConverter convert(ctx);
200 DurationProperties duration;
201 if (argumentCount == 0)
202 duration.length = convert.toLongLong(JSValueMakeUndefined(ctx));
204 duration.length = convert.toLongLong(arguments[0]);
206 if (argumentCount > 1) {
207 if (!JSValueIsUndefined(ctx, arguments[1]) && !JSValueIsNull(ctx, arguments[1])) {
208 std::string unit = convert.toString(arguments[1]);
209 duration.unit = convert.toDurationUnit(unit);
210 if (duration.unit == 0xff)
211 ThrowMsg(ConversionException, "Argument(unit) is invalid(wrong type unit)");
214 JSObjectRef timeDuration = createJSObject(ctx, duration);
216 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
217 JSObjectSetProperty(ctx, timeDuration, ctorName, constructor,
218 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
219 JSStringRelease(ctorName);
222 } Catch(NullPointerException) {
223 LoggerE("Exception: " << _rethrown_exception.GetMessage());
224 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
225 } Catch(WrtDeviceApis::Commons::UnknownException) {
226 LoggerE("Exception: " << _rethrown_exception.GetMessage());
227 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
228 } Catch(ConversionException) {
229 LoggerE("Exception: " << _rethrown_exception.GetMessage());
230 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
231 } Catch (InvalidArgumentException) {
232 LoggerE("Exception: " << _rethrown_exception.GetMessage());
233 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
234 } Catch (WrtDeviceApis::Commons::Exception) {
235 LoggerW("Trying to get incorrect value");
237 return JSWebAPIErrorFactory::postException(ctx, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
240 JSValueRef JSTimeDuration::diffTimeDuration(JSContextRef context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception, CompareType type) {
243 TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(thisObject));
244 if (!privateObject) {
245 LoggerE("Private object is not set.");
246 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
249 TimeUtilConverter converter(context);
251 DurationProperties first = converter.getDurationProperties(thisObject);
252 DurationProperties second;
253 if (argumentCount == 0)
254 second= converter.getDurationProperties(JSValueMakeUndefined(context));
256 second= converter.getDurationProperties(arguments[0]);
258 DurationProperties diff;
259 if (first.unit > second.unit) {
260 long long firstLength = converter.convertDurationLength(first, second.unit);
261 diff.unit = second.unit;
262 diff.length = firstLength - second.length;
264 long long secondLength = converter.convertDurationLength(second, first.unit);
265 diff.unit = first.unit;
266 diff.length = first.length - secondLength;
272 if (diff.length == 0)
273 return converter.toJSValueRef(true);
275 return converter.toJSValueRef(false);
280 return converter.toJSValueRef(true);
282 return converter.toJSValueRef(false);
287 return converter.toJSValueRef(true);
289 return converter.toJSValueRef(false);
293 return converter.makeDurationObject(converter.optimizedTimeDuration(diff));
297 JSValueRef JSTimeDuration::difference(JSContextRef context, JSObjectRef function,
298 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
301 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, DIFFERENCE);
302 } Catch(ConversionException) {
303 LoggerE("Exception: " << _rethrown_exception.GetMessage());
304 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
305 } Catch (InvalidArgumentException) {
306 LoggerE("Exception: " << _rethrown_exception.GetMessage());
307 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
308 } Catch (UnsupportedException) {
309 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
310 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR , "Not Support");
311 } Catch (PlatformException) {
312 LoggerE("Exception: " << _rethrown_exception.GetMessage());
313 } Catch (WrtDeviceApis::Commons::Exception) {
314 LoggerE("Exception: " << _rethrown_exception.GetMessage());
316 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
319 JSValueRef JSTimeDuration::equalsTo(JSContextRef context, JSObjectRef function,
320 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
323 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, EQUALSTO);
324 } Catch(ConversionException) {
325 LoggerE("Exception: " << _rethrown_exception.GetMessage());
326 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
327 } Catch (InvalidArgumentException) {
328 LoggerE("Exception: " << _rethrown_exception.GetMessage());
329 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
330 } Catch (UnsupportedException) {
331 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
332 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR , "Not Support");
333 } Catch (PlatformException) {
334 LoggerE("Exception: " << _rethrown_exception.GetMessage());
335 } Catch (WrtDeviceApis::Commons::Exception) {
336 LoggerE("Exception: " << _rethrown_exception.GetMessage());
338 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
341 JSValueRef JSTimeDuration::lessThan(JSContextRef context, JSObjectRef function,
342 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
345 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, LESSTHAN);
346 } Catch(ConversionException) {
347 LoggerE("Exception: " << _rethrown_exception.GetMessage());
348 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
349 } Catch (InvalidArgumentException) {
350 LoggerE("Exception: " << _rethrown_exception.GetMessage());
351 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
352 } Catch (UnsupportedException) {
353 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
354 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR , "Not Support");
355 } Catch (PlatformException) {
356 LoggerE("Exception: " << _rethrown_exception.GetMessage());
357 } Catch (WrtDeviceApis::Commons::Exception) {
358 LoggerE("Exception: " << _rethrown_exception.GetMessage());
360 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
363 JSValueRef JSTimeDuration::greaterThan(JSContextRef context, JSObjectRef function,
364 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
367 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, GREATERTHAN);
368 } Catch(ConversionException) {
369 LoggerE("Exception: " << _rethrown_exception.GetMessage());
370 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
371 } Catch (InvalidArgumentException) {
372 LoggerE("Exception: " << _rethrown_exception.GetMessage());
373 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
374 } Catch (UnsupportedException) {
375 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
376 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR , "Not Support");
377 } Catch (PlatformException) {
378 LoggerE("Exception: " << _rethrown_exception.GetMessage());
379 } Catch (WrtDeviceApis::Commons::Exception) {
380 LoggerE("Exception: " << _rethrown_exception.GetMessage());
382 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");