/* * * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "JSSysteminfo.h" #include "plugin_config.h" namespace TizenApis { namespace Tizen1_0 { using namespace std; using namespace DPL; using namespace WrtDeviceApis::CommonsJavaScript; using namespace WrtDeviceApis::Commons; using namespace Api::Systeminfo; using namespace TizenApis::Commons; JSClassDefinition JSSysteminfo::m_classInfo = { 0, kJSClassAttributeNone, "systeminfo", NULL, NULL, m_function, initialize, finalize, NULL, //hasProperty, NULL, //getProperty, NULL, //setProperty, NULL, //deleteProperty, NULL, //getPropertyNames, NULL, NULL, NULL, NULL }; JSStaticFunction JSSysteminfo::m_function[] = { { "isSupported", JSSysteminfo::isSupported, kJSPropertyAttributeNone }, { "getPropertyValue", JSSysteminfo::getPropertyValue, kJSPropertyAttributeNone }, { "addPropertyValueChangeListener", JSSysteminfo::addPropertyValueChangeListener, kJSPropertyAttributeNone }, { "removePropertyValueChangeListener", JSSysteminfo::removePropertyValueChangeListener, kJSPropertyAttributeNone }, { 0, 0, 0 } }; const JSClassRef JSSysteminfo::getClassRef() { if (!m_jsClassRef) { m_jsClassRef = JSClassCreate(&m_classInfo); } return m_jsClassRef; } const JSClassDefinition* JSSysteminfo::getClassInfo() { return &m_classInfo; } JSClassRef JSSysteminfo::m_jsClassRef = JSClassCreate(JSSysteminfo::getClassInfo()); void JSSysteminfo::initialize(JSContextRef context, JSObjectRef object) { JSSysteminfoPriv* priv = static_cast(JSObjectGetPrivate(object)); assert(!priv && "Invalid object creation."); ISysteminfoPtr Systeminfos(SysteminfoFactory::getInstance().getSysteminfos()); priv = new JSSysteminfoPriv(context, Systeminfos); if (!JSObjectSetPrivate(object, static_cast(priv))) { LogError("Object can't store private data."); delete priv; } LogDebug("JSSysteminfo::initialize "); } void JSSysteminfo::finalize(JSObjectRef object) { JSSysteminfoPriv* priv = static_cast(JSObjectGetPrivate(object)); JSObjectSetPrivate(object, NULL); LogDebug("Deleting gallery"); delete priv; } JSValueRef JSSysteminfo::isSupported(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { JSSysteminfoPriv *priv = static_cast(JSObjectGetPrivate(thisObject)); AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_IS_SUPPORTED); TIZEN_SYNC_ACCESS_HANDLER(status, context, exception); Converter converter(context); Validator check(context, exception); if (argumentCount < 1) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error"); } if (argumentCount > 0 && JSValueIsObject(context, arguments[0])) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error"); } Try { ISysteminfoPtr Systeminfos(priv->getObject()); bool retVal; retVal = Systeminfos->isPropertyValid(priv->getContext(), arguments[0]); return converter.toJSValueRef(retVal); } Catch(ConversionException) { LogError("Error on conversion"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error"); } return JSValueMakeUndefined(context); } JSValueRef JSSysteminfo::getPropertyValue(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { JSSysteminfoPriv *priv = static_cast(JSObjectGetPrivate(thisObject)); AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_GET_PROPERTY_VALUE); TIZEN_SYNC_ACCESS_HANDLER(status, context, exception); Converter converter(context); Validator check(context, exception); if (!priv) { LogError("private object is null"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error"); } if (argumentCount == 0 || argumentCount > 4) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error"); } if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !check.isCallback(arguments[1])) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error"); } if (argumentCount > 2) { if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !check.isCallback(arguments[2])) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error"); } } JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL; if (check.isCallback(arguments[1])) { onSuccessForCbm = arguments[1]; } if (argumentCount > 2) { if (check.isCallback(arguments[2])) { onErrorForCbm = arguments[2]; } } JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true)); Try { if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) { LogError("successCallback parameter is JSNull/JSUndefined"); Throw(InvalidArgumentException); } ISysteminfoPtr Systeminfos(priv->getObject()); EventGetSysteminfoPtr event(new EventGetSysteminfo()); event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0])); event->setPrivateData(StaticPointerCast(cbm)); SysteminfoListener& listener = SysteminfoListener::getInstance(); event->setForAsynchronousCall(&listener); JSObjectRef pendingOperation = makePendingOperation(context, event); Systeminfos->get(event); return pendingOperation; } Catch(PendingOperationException) { LogError("JSSysteminfo::get PendingOperationException"); cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter")); } Catch(ConversionException) { LogError("JSSysteminfo::get ConversionException"); cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter")); } Catch(InvalidArgumentException) { LogError("JSSysteminfo::get InvalidArgumentException"); cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter")); } Catch(WrtDeviceApis::Commons::Exception) { LogError("JSSysteminfo::get Exception"); cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter")); } return JSValueMakeUndefined(context); } JSValueRef JSSysteminfo::addPropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { LogDebug("enter"); JSValueRef property = NULL; WatchOption option; int failId = -1; JSSysteminfoPriv *priv = static_cast(JSObjectGetPrivate(thisObject)); AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_ADD_PROPERTY_VALUE_CHANGE_LISTENER); TIZEN_SYNC_ACCESS_HANDLER(status, context, exception); Converter converter(context); Validator check(context, exception); if (!priv) { LogError("private object is null"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error"); } if (argumentCount == 0 || argumentCount > 4) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error"); } if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !check.isCallback(arguments[1])) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error"); } if (argumentCount > 2) { if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !check.isCallback(arguments[2])) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error"); } } JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL; if (check.isCallback(arguments[1])) { onSuccessForCbm = arguments[1]; } if (argumentCount > 2) { if (check.isCallback(arguments[2])) { onErrorForCbm = arguments[2]; } } JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true)); Try { if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) { LogError("successCallback parameter is JSNull/JSUndefined"); Throw(InvalidArgumentException); } property = arguments[0]; if (argumentCount > 3 && JSValueIsObject(context, arguments[3])) { option.timeout = converter.toULong(JSUtils::getJSProperty(context, arguments[3], "timeout", exception)); option.highThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "highThreshold", exception)); option.lowThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "lowThreshold", exception)); } ISysteminfoPtr Systeminfos(priv->getObject()); EventWatchSysteminfoPtr event(new EventWatchSysteminfo); event->setWatchOption(option); event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), property)); event->setPrivateData(StaticPointerCast(cbm)); SysteminfoListener& listener = SysteminfoListener::getInstance(); event->setForAsynchronousCall(&listener); Systeminfos->watch(event); LogDebug("event->getId()" << event->getId()); return converter.toJSValueRef(event->getId()); } Catch(PendingOperationException) { LogError("JSSysteminfo::get PendingOperationException"); cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter")); } Catch(ConversionException) { LogError("JSSysteminfo::get ConversionException"); cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter")); } Catch(InvalidArgumentException) { LogError("JSSysteminfo::get InvalidArgumentException"); cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter")); } Catch(WrtDeviceApis::Commons::Exception) { LogError("JSSysteminfo::get Exception"); cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter")); } return JSValueMakeUndefined(context); } JSValueRef JSSysteminfo::removePropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { int id = 0; JSSysteminfoPriv *priv = static_cast(JSObjectGetPrivate(thisObject)); AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_REMOVE_PROPERTY_VALUE_CHANGE_LISTENER); TIZEN_SYNC_ACCESS_HANDLER(status, context, exception); Converter converter(context); if (argumentCount == 0) { return JSValueMakeUndefined(context); } else if (argumentCount != 1 || (argumentCount > 0 && JSValueIsObject(context, arguments[0]))) { LogError("wrong argument"); return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error"); } Try { if (!priv) { ThrowMsg(NullPointerException, "No private object"); } ISysteminfoPtr Systeminfos(priv->getObject()); if (argumentCount == 1) { id = static_cast(converter.toInt(arguments[0])); } LogDebug("clearWatch id = " << id ); Systeminfos->clearWatch(id); return JSValueMakeUndefined(context); } Catch (InvalidArgumentException) { LogError("JSSysteminfo::get InvalidArgumentException"); } Catch (WrtDeviceApis::Commons::Exception) { LogError("JSSysteminfo::get Exception"); } return JSValueMakeUndefined(context); } } }