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>
24 #include <JSTizenExceptionFactory.h>
25 #include <JSTizenException.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 = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
161 } Catch (ConversionException) {
162 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
163 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
164 } Catch (InvalidArgumentException) {
165 LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage());
166 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
167 } Catch (WrtDeviceApis::Commons::Exception) {
168 LoggerE("Exception: " << _rethrown_exception.GetMessage());
169 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::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::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
188 TimeUtilConverter convert(ctx);
190 DurationProperties duration;
191 if (argumentCount == 0)
192 duration.length = convert.toLongLong(JSValueMakeUndefined(ctx));
194 duration.length = convert.toLongLong(arguments[0]);
196 if (argumentCount > 1) {
197 if (!JSValueIsUndefined(ctx, arguments[1]) && !JSValueIsNull(ctx, arguments[1])) {
198 std::string unit = convert.toString(arguments[1]);
199 duration.unit = convert.toDurationUnit(unit);
200 if (duration.unit == 0xff)
201 ThrowMsg(ConversionException, "Argument(unit) is invalid(wrong type unit)");
204 JSObjectRef timeDuration = createJSObject(ctx, duration);
206 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
207 JSObjectSetProperty(ctx, timeDuration, ctorName, constructor,
208 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
209 JSStringRelease(ctorName);
212 } Catch(NullPointerException) {
213 LoggerE("Exception: " << _rethrown_exception.GetMessage());
214 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
217 } Catch(UnknownException) {
218 LoggerE("Exception: " << _rethrown_exception.GetMessage());
219 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
222 } Catch(ConversionException) {
223 LoggerE("Exception: " << _rethrown_exception.GetMessage());
224 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
227 } Catch (InvalidArgumentException) {
228 LoggerE("Exception: " << _rethrown_exception.GetMessage());
229 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
232 } Catch (WrtDeviceApis::Commons::Exception) {
233 LoggerW("Trying to get incorrect value");
235 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
241 JSValueRef JSTimeDuration::diffTimeDuration(JSContextRef context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception, CompareType type) {
244 TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(thisObject));
245 if (!privateObject) {
246 LoggerE("Private object is not set.");
247 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
250 TimeUtilConverter converter(context);
252 DurationProperties first = converter.getDurationPropertis(thisObject);
253 DurationProperties second;
254 if (argumentCount == 0)
255 second= converter.getDurationPropertis(JSValueMakeUndefined(context));
257 second= converter.getDurationPropertis(arguments[0]);
259 DurationProperties diff;
260 if (first.unit > second.unit) {
261 long long firstLength = converter.convertDurationLength(first, second.unit);
262 diff.unit = second.unit;
263 diff.length = firstLength - second.length;
265 long long secondLength = converter.convertDurationLength(second, first.unit);
266 diff.unit = first.unit;
267 diff.length = first.length - secondLength;
273 if (diff.length == 0)
274 return converter.toJSValueRef(true);
276 return converter.toJSValueRef(false);
281 return converter.toJSValueRef(true);
283 return converter.toJSValueRef(false);
288 return converter.toJSValueRef(true);
290 return converter.toJSValueRef(false);
294 return converter.makeDurationObject(diff);
298 JSValueRef JSTimeDuration::difference(JSContextRef context, JSObjectRef function,
299 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
302 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, DIFFERENCE);
303 } Catch(ConversionException) {
304 LoggerE("Exception: " << _rethrown_exception.GetMessage());
305 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
306 } Catch (InvalidArgumentException) {
307 LoggerE("Exception: " << _rethrown_exception.GetMessage());
308 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
309 } Catch (UnsupportedException) {
310 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
311 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR , "Not Support");
312 } Catch (PlatformException) {
313 LoggerE("Exception: " << _rethrown_exception.GetMessage());
314 } Catch (WrtDeviceApis::Commons::Exception) {
315 LoggerE("Exception: " << _rethrown_exception.GetMessage());
317 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
320 JSValueRef JSTimeDuration::equalsTo(JSContextRef context, JSObjectRef function,
321 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
324 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, EQUALSTO);
325 } Catch(ConversionException) {
326 LoggerE("Exception: " << _rethrown_exception.GetMessage());
327 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
328 } Catch (InvalidArgumentException) {
329 LoggerE("Exception: " << _rethrown_exception.GetMessage());
330 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
331 } Catch (UnsupportedException) {
332 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
333 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR , "Not Support");
334 } Catch (PlatformException) {
335 LoggerE("Exception: " << _rethrown_exception.GetMessage());
336 } Catch (WrtDeviceApis::Commons::Exception) {
337 LoggerE("Exception: " << _rethrown_exception.GetMessage());
339 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
342 JSValueRef JSTimeDuration::lessThan(JSContextRef context, JSObjectRef function,
343 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
346 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, LESSTHAN);
347 } Catch(ConversionException) {
348 LoggerE("Exception: " << _rethrown_exception.GetMessage());
349 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
350 } Catch (InvalidArgumentException) {
351 LoggerE("Exception: " << _rethrown_exception.GetMessage());
352 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
353 } Catch (UnsupportedException) {
354 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
355 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR , "Not Support");
356 } Catch (PlatformException) {
357 LoggerE("Exception: " << _rethrown_exception.GetMessage());
358 } Catch (WrtDeviceApis::Commons::Exception) {
359 LoggerE("Exception: " << _rethrown_exception.GetMessage());
361 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
364 JSValueRef JSTimeDuration::greaterThan(JSContextRef context, JSObjectRef function,
365 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
368 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, GREATERTHAN);
369 } Catch(ConversionException) {
370 LoggerE("Exception: " << _rethrown_exception.GetMessage());
371 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
372 } Catch (InvalidArgumentException) {
373 LoggerE("Exception: " << _rethrown_exception.GetMessage());
374 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
375 } Catch (UnsupportedException) {
376 LoggerE("JSTimeUtil::hasInstance NotSupportedException");
377 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR , "Not Support");
378 } Catch (PlatformException) {
379 LoggerE("Exception: " << _rethrown_exception.GetMessage());
380 } Catch (WrtDeviceApis::Commons::Exception) {
381 LoggerE("Exception: " << _rethrown_exception.GetMessage());
383 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");