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 <JavaScriptCore/JavaScript.h>
22 #include "JSTimeDuration.h"
23 #include "TimeDuration.h"
24 #include <ArgumentValidator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include <PlatformException.h>
30 #ifdef IMPL_BACKWARD_COMPATIBLE
31 #include <CommonsJavaScript/Validator.h>
32 #include <Commons/Exception.h>
33 #endif // IMPL_BACKWARD_COMPATIBLE
35 using namespace DeviceAPI::Common;
40 struct TimeDurationHolder {
45 const char* CLASS_NAME = "TimeDuration";
47 const char* TIMEDURATION_ATTRIBUTE_API_LENGTH = "length";
48 const char* TIMEDURATION_ATTRIBUTE_API_UNIT = "unit";
50 const char* TIMEDURATION_FUNCTION_API_DIFFERENCE = "difference";
51 const char* TIMEDURATION_FUNCTION_API_EQUALS_TO = "equalsTo";
52 const char* TIMEDURATION_FUNCTION_API_LESS_THAN = "lessThan";
53 const char* TIMEDURATION_FUNCTION_API_GREATER_THAN = "greaterThan";
56 JSClassDefinition JSTimeDuration::m_classInfo = {
58 kJSClassAttributeNone,
61 JSTimeDuration::m_property,
62 JSTimeDuration::m_function,
63 JSTimeDuration::initialize,
64 JSTimeDuration::finalize,
68 NULL, //DeleteProperty,
69 NULL, //GetPropertyNames,
70 NULL, //CallAsFunction,
71 NULL, //CallAsConstructor,
76 JSStaticValue JSTimeDuration::m_property[] = {
77 { TIMEDURATION_ATTRIBUTE_API_LENGTH, getLength, setLength,
78 kJSPropertyAttributeDontDelete },
79 { TIMEDURATION_ATTRIBUTE_API_UNIT, getUnit, setUnit,
80 kJSPropertyAttributeDontDelete },
84 JSStaticFunction JSTimeDuration::m_function[] = {
85 {TIMEDURATION_FUNCTION_API_DIFFERENCE, difference, kJSPropertyAttributeNone },
86 {TIMEDURATION_FUNCTION_API_EQUALS_TO, equalsTo, kJSPropertyAttributeNone },
87 {TIMEDURATION_FUNCTION_API_LESS_THAN, lessThan, kJSPropertyAttributeNone },
88 {TIMEDURATION_FUNCTION_API_GREATER_THAN, greaterThan, kJSPropertyAttributeNone },
92 const JSClassDefinition* JSTimeDuration::getClassInfo()
97 JSClassRef JSTimeDuration::m_jsClassRef = JSClassCreate(JSTimeDuration::getClassInfo());
99 const JSClassRef JSTimeDuration::getClassRef()
102 m_jsClassRef = JSClassCreate(&m_classInfo);
107 void JSTimeDuration::initialize(JSContextRef context, JSObjectRef object)
109 LOGD("Entered initialize");
112 void JSTimeDuration::finalize(JSObjectRef object)
114 LOGD("Entered finalize");
115 TimeDurationHolder *priv = static_cast<TimeDurationHolder*>(JSObjectGetPrivate(object));
117 JSObjectSetPrivate(object, NULL);
122 TimeDurationPtr JSTimeDuration::getPrivateObject(JSContextRef context, JSValueRef value) {
124 if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
125 LOGE("Type mismatch");
126 throw TypeMismatchException("Type mismatch");
128 JSObjectRef object = JSUtil::JSValueToObject(context, value);
129 TimeDurationHolder* priv = static_cast<TimeDurationHolder*>(JSObjectGetPrivate(object));
130 if (!priv or !priv->ptr) {
131 LOGE("Priv is null");
132 throw UnknownException("Priv is null");
137 void JSTimeDuration::setPrivateObject(JSObjectRef object, TimeDurationPtr native) {
139 TimeDurationHolder* priv = static_cast<TimeDurationHolder*>(JSObjectGetPrivate(object));
141 LOGE("Priv is null");
142 throw UnknownException("Priv is null");
147 JSObjectRef JSTimeDuration::makeJSObject(JSContextRef context, TimeDurationPtr native) {
150 LOGE("Native is null");
151 throw UnknownException("Native is null");
153 TimeDurationHolder* priv = new(std::nothrow) TimeDurationHolder();
155 LOGE("Memory allocation for priv failed");
156 throw UnknownException("Memory allocation for priv failed");
160 JSObjectRef obj = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
164 JSValueRef JSTimeDuration::getLength(JSContextRef context,
166 JSStringRef propertyName,
167 JSValueRef* exception)
169 LOGD("Entered getLength()");
171 TimeDurationPtr priv = getPrivateObject(context, object);
172 return JSUtil::toJSValueRef(context, priv->getLength());
174 catch (const BasePlatformException &err) {
175 LOGE("Failed to get time duration length. %s : %s", err.getName().c_str(),
176 err.getMessage().c_str());
178 return JSValueMakeUndefined(context);
181 JSValueRef JSTimeDuration::getUnit(JSContextRef context,
183 JSStringRef propertyName,
184 JSValueRef* exception)
188 TimeDurationPtr priv = getPrivateObject(context, object);
189 return JSUtil::toJSValueRef(context, priv->getUnit());
191 catch (const BasePlatformException &err) {
192 LOGE("Failed to get time duration unit. %s : %s", err.getName().c_str(),
193 err.getMessage().c_str());
195 return JSValueMakeUndefined(context);
198 bool JSTimeDuration::setLength(JSContextRef context,
200 JSStringRef propertyName,
202 JSValueRef* exception)
204 LOGD("Entered setLength()");
206 TimeDurationPtr priv = getPrivateObject(context, object);
207 long long val = JSUtil::JSValueToLongLong(context, value);
208 priv->setLength(val);
210 catch (const BasePlatformException &err) {
211 LOGE("Failed to set time duration length. %s : %s", err.getName().c_str(),
212 err.getMessage().c_str());
217 bool JSTimeDuration::setUnit(JSContextRef context,
219 JSStringRef propertyName,
221 JSValueRef* exception)
225 TimeDurationPtr priv = getPrivateObject(context, object);
226 if (!JSValueIsNull(context, value)) {
227 std::string unit = JSUtil::JSValueToString(context, value);
228 if (unit != MSECS && unit != SECS && unit != MINS &&
229 unit != HOURS && unit != DAYS) {
230 LOGE("Invalid unit");
231 throw TypeMismatchException("Invalid unit");
236 catch (const BasePlatformException &err) {
237 LOGE("Failed to set time duration unit. %s : %s", err.getName().c_str(),
238 err.getMessage().c_str());
243 JSObjectRef JSTimeDuration::constructor(JSContextRef context,
244 JSObjectRef constructor,
245 size_t argumentCount,
246 const JSValueRef arguments[],
247 JSValueRef* exception)
249 LOGD("Entered constructor()");
250 ArgumentValidator validator(context, argumentCount, arguments);
251 JSObjectRef obj = JSObjectMake(context, getClassRef(), NULL);
253 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
254 JSObjectSetProperty(context, obj, ctorName, constructor, kJSPropertyAttributeReadOnly
255 | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
256 JSStringRelease(ctorName);
259 TimeDurationPtr priv = TimeDurationPtr(new (std::nothrow) TimeDuration());
261 LOGW("Failed to create TimeDurationPtr object.");
262 throw UnknownException("Failed to create TimeDurationPtr object.");
264 priv->setLength(validator.toLongLong(0, false, 0));
265 priv->setUnit(validator.toString(1, true, MSECS));
267 TimeDurationHolder *holder = new (std::nothrow) TimeDurationHolder();
269 LOGW("Failed to create TimeDurationHolder object.");
270 throw UnknownException("Failed to create TimeDurationHolder object.");
273 JSObjectSetPrivate(obj, static_cast<void*>(holder));
275 catch (const BasePlatformException &error) {
276 LOGE("%s : %s", error.getName().c_str(), error.getMessage().c_str());
279 LOGE("An error occured");
284 JSValueRef JSTimeDuration::difference(JSContextRef context,
285 JSObjectRef function,
286 JSObjectRef thisObject,
287 size_t argumentCount,
288 const JSValueRef arguments[],
289 JSValueRef * exception)
291 LOGD("Entered difference()");
293 TimeDurationPtr diff;
295 TimeDurationPtr priv = getPrivateObject(context, thisObject);
296 ArgumentValidator validator(context, argumentCount, arguments);
297 // TimeDuration other
298 JSObjectRef otherObj = validator.toObject(0, JSTimeDuration::getClassRef());
299 TimeDurationPtr other = getPrivateObject(context, otherObj);
301 return JSTimeDuration::makeJSObject(context,priv->difference(other));
303 catch (const BasePlatformException &err) {
304 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
305 return JSWebAPIErrorFactory::postException(context, exception, err);
308 UnknownException err("Unknown Error in tizen."
309 "TimeDuration.difference().");
310 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
311 return JSWebAPIErrorFactory::postException(context, exception, err);
315 JSValueRef JSTimeDuration::equalsTo(JSContextRef context,
316 JSObjectRef function,
317 JSObjectRef thisObject,
318 size_t argumentCount,
319 const JSValueRef arguments[],
320 JSValueRef * exception)
322 LOGD("Entered equalsTo()");
325 TimeDurationPtr priv = getPrivateObject(context, thisObject);
326 ArgumentValidator validator(context, argumentCount, arguments);
327 // TimeDuration other
328 JSObjectRef otherObj = validator.toObject(0, JSTimeDuration::getClassRef());
329 TimeDurationPtr other = getPrivateObject(context, otherObj);
331 return JSUtil::toJSValueRef(context, priv->equalsTo(other));
333 catch (const BasePlatformException &err) {
334 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
335 return JSWebAPIErrorFactory::postException(context, exception, err);
338 UnknownException err("Unknown Error in tizen.download.start().");
339 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
340 return JSWebAPIErrorFactory::postException(context, exception, err);
344 JSValueRef JSTimeDuration::lessThan(JSContextRef context,
345 JSObjectRef function,
346 JSObjectRef thisObject,
347 size_t argumentCount,
348 const JSValueRef arguments[],
349 JSValueRef * exception)
351 LOGD("Entered lessThan()");
354 TimeDurationPtr priv = getPrivateObject(context, thisObject);
355 ArgumentValidator validator(context, argumentCount, arguments);
356 // TimeDuration other
357 JSObjectRef otherObj = validator.toObject(0, JSTimeDuration::getClassRef());
358 TimeDurationPtr other = getPrivateObject(context, otherObj);
360 return JSUtil::toJSValueRef(context, priv->lessThan(other));
362 catch (const BasePlatformException &err) {
363 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
364 return JSWebAPIErrorFactory::postException(context, exception, err);
367 UnknownException err("Unknown Error in tizen.download.start().");
368 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
369 return JSWebAPIErrorFactory::postException(context, exception, err);
373 JSValueRef JSTimeDuration::greaterThan(JSContextRef context,
374 JSObjectRef function,
375 JSObjectRef thisObject,
376 size_t argumentCount,
377 const JSValueRef arguments[],
378 JSValueRef * exception)
380 LOGD("Entered greaterThan()");
383 TimeDurationPtr priv = getPrivateObject(context, thisObject);
384 ArgumentValidator validator(context, argumentCount, arguments);
385 // TimeDuration other
386 JSObjectRef otherObj = validator.toObject(0, JSTimeDuration::getClassRef());
387 TimeDurationPtr other = getPrivateObject(context, otherObj);
389 return JSUtil::toJSValueRef(context, priv->greaterThan(other));
391 catch (const BasePlatformException &err) {
392 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
393 return JSWebAPIErrorFactory::postException(context, exception, err);
396 UnknownException err("Unknown Error in tizen.download.start().");
397 LOGE("%s : %s", err.getName().c_str(), err.getMessage().c_str());
398 return JSWebAPIErrorFactory::postException(context, exception, err);
402 #ifdef IMPL_BACKWARD_COMPATIBLE
403 JSObjectRef JSTimeDuration::createJSObject(JSContextRef context,
404 const DurationProperties &durations)
406 DurationPropertiesPtr durationProps(new DurationProperties(durations));
407 TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps);
410 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
412 return JSObjectMake(context, getClassRef(), priv);
415 JSObjectRef JSTimeDuration::createJSObject(JSContextRef context,
416 const DurationPropertiesPtr duration)
418 TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, duration);
420 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null.");
422 return JSObjectMake(context, getClassRef(), priv);
424 #endif // IMPL_BACKWARD_COMPATIBLE