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 <dpl/log/log.h>
23 #include <Commons/Exception.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <CommonsJavaScript/JSCallbackManager.h>
26 #include <JSTizenExceptionFactory.h>
27 #include <JSTizenException.h>
29 #include "JSTimeDuration.h"
30 #include "TimeUtilConverter.h"
36 using namespace DeviceAPI::Common;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
40 #define TIZEN_TIMEDURATION_LENGTH "length"
41 #define TIZEN_TIMEDURATION_UNIT "unit"
43 JSClassDefinition JSTimeDuration::m_classInfo = {
45 kJSClassAttributeNone,
54 setProperty, //SetProperty,
55 NULL, //DeleteProperty,
56 NULL, //GetPropertyNames,
57 NULL, //CallAsFunction,
58 constructor, //CallAsConstructor,
64 JSStaticFunction JSTimeDuration::m_function[] = {
65 {"difference", JSTimeDuration::difference, kJSPropertyAttributeNone},
66 {"equalsTo", JSTimeDuration::equalsTo, kJSPropertyAttributeNone},
67 {"lessThan", JSTimeDuration::lessThan, kJSPropertyAttributeNone},
68 {"greaterThan", JSTimeDuration::greaterThan, kJSPropertyAttributeNone},
71 JSStaticValue JSTimeDuration::m_property[] =
73 //TimdDurationProperties
74 {TIZEN_TIMEDURATION_LENGTH, getProperty, setProperty, kJSPropertyAttributeNone},
75 {TIZEN_TIMEDURATION_UNIT, getProperty, setProperty, kJSPropertyAttributeNone},
79 const JSClassRef JSTimeDuration::getClassRef()
82 m_jsClassRef = JSClassCreate(&m_classInfo);
87 const JSClassDefinition* JSTimeDuration::getClassInfo()
92 JSClassRef JSTimeDuration::m_jsClassRef = JSClassCreate(JSTimeDuration::getClassInfo());
94 void JSTimeDuration::initialize(JSContextRef context, JSObjectRef object)
96 LogDebug("entered Nothing to do.");
98 if (!JSObjectGetPrivate(object)) {
99 LogDebug("Private object not set... setting it.");
100 DurationProperties durations;
101 DurationPropertiesPtr durationProps(new DurationProperties(durations));
102 TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps);
104 if (!JSObjectSetPrivate(object, priv)) {
110 void JSTimeDuration::finalize(JSObjectRef object)
112 TimeDurationPrivObject* priv = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(object));
113 JSObjectSetPrivate(object, NULL);
114 LogDebug("Deleting TimeDuration");
118 JSValueRef JSTimeDuration::getProperty(JSContextRef context, JSObjectRef object,
119 JSStringRef propertyName, JSValueRef* exception)
124 TimeUtilConverter convert(context);
126 LogDebug("propertyName : " << convert.toString(propertyName));
127 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_LENGTH)) {
128 return convert.toJSValueRef(static_cast<double>(convert.getDurationLength(object)));
129 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_UNIT)) {
130 std::string strUnit = convert.toDurationUnitString(convert.getDurationUnit(object));
132 ThrowMsg(InvalidArgumentException, "unit string is invald.");
133 return convert.toJSValueRef(strUnit);
135 } Catch (ConversionException) {
136 LogError("ConversionException: " << _rethrown_exception.GetMessage());
137 } Catch (InvalidArgumentException) {
138 LogError("Exception: " << _rethrown_exception.GetMessage());
139 } Catch (WrtDeviceApis::Commons::Exception) {
140 LogError("Exception: " << _rethrown_exception.GetMessage());
142 return JSValueMakeUndefined(context);
145 bool JSTimeDuration::setProperty(JSContextRef context, JSObjectRef object,
146 JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
151 TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(object));
152 if (!privateObject) {
153 LogError("Private object is not set.");
154 ThrowMsg(NullPointerException, "Private object not initialized");
157 DurationPropertiesPtr duration = privateObject->getObject();
158 TimeUtilConverter convert(context);
160 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_LENGTH)) {
161 duration->length = convert.toLongLong(value);
162 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_TIMEDURATION_UNIT)) {
163 short unit = convert.toDurationUnit(convert.toString(value));
165 ThrowMsg(ConversionException, "property doesn't exist.");
166 duration->unit = unit;
171 } Catch (NullPointerException) {
172 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
173 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
174 } Catch (ConversionException) {
175 LogError("ConversionException: " << _rethrown_exception.GetMessage());
176 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
177 } Catch (InvalidArgumentException) {
178 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
179 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
180 } Catch (WrtDeviceApis::Commons::Exception) {
181 LogError("Exception: " << _rethrown_exception.GetMessage());
182 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
187 JSObjectRef JSTimeDuration::createJSObject(JSContextRef context,
188 const DurationProperties &durations)
190 DurationPropertiesPtr durationProps(new DurationProperties(durations));
191 TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps);
194 ThrowMsg(NullPointerException, "Can not new an object");
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 std::string unit = convert.toString(arguments[1]);
212 duration.unit = convert.toDurationUnit(unit);
213 if (duration.unit == 0xff)
214 ThrowMsg(ConversionException, "Argument(unit) is invalid(wrong type unit)");
217 return createJSObject(ctx, duration);
218 } Catch(NullPointerException) {
219 LogError("Exception: " << _rethrown_exception.GetMessage());
220 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
222 } Catch(UnknownException) {
223 LogError("Exception: " << _rethrown_exception.GetMessage());
224 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
226 } Catch(ConversionException) {
227 LogError("Exception: " << _rethrown_exception.GetMessage());
228 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
230 } Catch (InvalidArgumentException) {
231 LogError("Exception: " << _rethrown_exception.GetMessage());
232 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
234 } Catch (WrtDeviceApis::Commons::Exception) {
235 LogWarning("Trying to get incorrect value");
237 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
241 bool JSTimeDuration::hasInstance(JSContextRef context,
242 JSObjectRef constructor,
243 JSValueRef possibleInstance,
244 JSValueRef* exception)
246 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
249 JSValueRef JSTimeDuration::diffTimeDuration(JSContextRef context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception, CompareType type) {
252 TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(thisObject));
253 if (!privateObject) {
254 LogError("Private object is not set.");
255 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
258 TimeUtilConverter converter(context);
260 DurationProperties first = converter.getDurationPropertis(thisObject);
261 DurationProperties second;
262 if (argumentCount == 0)
263 second= converter.getDurationPropertis(JSValueMakeUndefined(context));
265 second= converter.getDurationPropertis(arguments[0]);
267 DurationProperties diff;
268 if (first.unit > second.unit) {
269 long long firstLength = converter.convertDurationLength(first, second.unit);
270 diff.unit = second.unit;
271 diff.length = firstLength - second.length;
273 long long secondLength = converter.convertDurationLength(second, first.unit);
274 diff.unit = first.unit;
275 diff.length = first.length - secondLength;
281 if (diff.length == 0)
282 return converter.toJSValueRef(true);
284 return converter.toJSValueRef(false);
289 return converter.toJSValueRef(true);
291 return converter.toJSValueRef(false);
296 return converter.toJSValueRef(true);
298 return converter.toJSValueRef(false);
302 return converter.makeDurationObject(diff);
306 JSValueRef JSTimeDuration::difference(JSContextRef context, JSObjectRef function,
307 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
310 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, DIFFERENCE);
311 } Catch(ConversionException) {
312 LogError("Exception: " << _rethrown_exception.GetMessage());
313 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
314 } Catch (InvalidArgumentException) {
315 LogError("Exception: " << _rethrown_exception.GetMessage());
316 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
317 } Catch (UnsupportedException) {
318 LogError("JSTimeUtil::hasInstance NotSupportedException");
319 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR , "Not Support");
320 } Catch (PlatformException) {
321 LogError("Exception: " << _rethrown_exception.GetMessage());
322 } Catch (WrtDeviceApis::Commons::Exception) {
323 LogError("Exception: " << _rethrown_exception.GetMessage());
325 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
328 JSValueRef JSTimeDuration::equalsTo(JSContextRef context, JSObjectRef function,
329 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
332 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, EQUALSTO);
333 } Catch(ConversionException) {
334 LogError("Exception: " << _rethrown_exception.GetMessage());
335 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
336 } Catch (InvalidArgumentException) {
337 LogError("Exception: " << _rethrown_exception.GetMessage());
338 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
339 } Catch (UnsupportedException) {
340 LogError("JSTimeUtil::hasInstance NotSupportedException");
341 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR , "Not Support");
342 } Catch (PlatformException) {
343 LogError("Exception: " << _rethrown_exception.GetMessage());
344 } Catch (WrtDeviceApis::Commons::Exception) {
345 LogError("Exception: " << _rethrown_exception.GetMessage());
347 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
350 JSValueRef JSTimeDuration::lessThan(JSContextRef context, JSObjectRef function,
351 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
354 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, LESSTHAN);
355 } Catch(ConversionException) {
356 LogError("Exception: " << _rethrown_exception.GetMessage());
357 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
358 } Catch (InvalidArgumentException) {
359 LogError("Exception: " << _rethrown_exception.GetMessage());
360 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
361 } Catch (UnsupportedException) {
362 LogError("JSTimeUtil::hasInstance NotSupportedException");
363 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR , "Not Support");
364 } Catch (PlatformException) {
365 LogError("Exception: " << _rethrown_exception.GetMessage());
366 } Catch (WrtDeviceApis::Commons::Exception) {
367 LogError("Exception: " << _rethrown_exception.GetMessage());
369 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
372 JSValueRef JSTimeDuration::greaterThan(JSContextRef context, JSObjectRef function,
373 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) {
376 return diffTimeDuration(context, thisObject, argumentCount, arguments, exception, GREATERTHAN);
377 } Catch(ConversionException) {
378 LogError("Exception: " << _rethrown_exception.GetMessage());
379 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
380 } Catch (InvalidArgumentException) {
381 LogError("Exception: " << _rethrown_exception.GetMessage());
382 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
383 } Catch (UnsupportedException) {
384 LogError("JSTimeUtil::hasInstance NotSupportedException");
385 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR , "Not Support");
386 } Catch (PlatformException) {
387 LogError("Exception: " << _rethrown_exception.GetMessage());
388 } Catch (WrtDeviceApis::Commons::Exception) {
389 LogError("Exception: " << _rethrown_exception.GetMessage());
391 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");