diff options
Diffstat (limited to 'mobile_src/Tizen/JSTizen.cpp')
-rw-r--r-- | mobile_src/Tizen/JSTizen.cpp | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/mobile_src/Tizen/JSTizen.cpp b/mobile_src/Tizen/JSTizen.cpp new file mode 100644 index 0000000..7c8f6b5 --- /dev/null +++ b/mobile_src/Tizen/JSTizen.cpp @@ -0,0 +1,245 @@ +// +// Tizen Web Device API +// Copyright (c) 2012 Samsung Electronics Co., Ltd. +// +// 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. +// + +/** + * @file JSTizen.cpp + * @version 0.1 + * @brief + */ + +#include <vector> +#include <CommonsJavaScript/PrivateObject.h> +#include <CommonsJavaScript/Converter.h> +#include <Commons/WrtAccess/WrtAccess.h> +#include <CommonsJavaScript/JSDOMExceptionFactory.h> +#include "FilterTypes.h" +#include "AnyType.h" +#include "JSTizen.h" +#include "plugin_config.h" +#include <WidgetDB/WidgetDBMgr.h> +#include <WidgetDB/IWidgetDB.h> +#include <PluginManager/PluginManagerFactory.h> +#include <iostream> +#include <Logger.h> + +#define PLUGIN_NAME "tizen" + +namespace DeviceAPI {
namespace Tizen { + +using namespace WrtDeviceApis; +using namespace WrtDeviceApis::Commons; +using namespace DeviceAPI::Tizen; +using namespace WrtDeviceApis::PluginManager::Api; + +JSClassDefinition JSTizen::m_classInfo = +{ + 0, + kJSClassAttributeNone, + PLUGIN_NAME, + 0, + m_properties, + m_functions, + initialize, + finalize, + hasProperty, + getProperty, + setProperty, + NULL, //deleteProperty, + getPropertyNames, + NULL, + NULL, + hasInstance, + convertToType +}; + +JSStaticValue JSTizen::m_properties[] = { + { 0, 0, 0 } +}; + +JSStaticFunction JSTizen::m_functions[] = { + { 0, 0, 0 } +}; + +const JSClassDefinition* JSTizen::getClassInfo() +{ + return &m_classInfo; +} + +JSClassRef JSTizen::m_classRef = JSClassCreate(JSTizen::getClassInfo()); + +void JSTizen::initialize(JSContextRef context, JSObjectRef object) +{ + LoggerD("entered"); + TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object)); + JSObjectSetPrivate(object, NULL); + if (priv) { + delete priv; + } + + int widgetId = WrtAccessSingleton::Instance().getWidgetId(); + PluginOnDemandPrivPtr privObject( + new PluginOnDemandPriv( + PluginManagerFactory::getInstance().getPluginManager( + widgetId, + PLUGIN_NAME, + object, + context))); + + priv = new TizenPrivate(context, privObject); + if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) { + JSObjectSetPrivate(object, NULL); + delete priv; + } +} + +void JSTizen::finalize(JSObjectRef object) +{ + LoggerD("entered"); + TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object)); + JSObjectSetPrivate(object, NULL); + delete priv; +} + +JSClassRef JSTizen::getClassRef() +{ + if (!m_classRef) { + m_classRef = JSClassCreate(&m_classInfo); + } + return m_classRef; +} + +//bool JSTizen::deleteProperty(JSContextRef context, JSObjectRef object, +// JSStringRef propertyName, JSValueRef* exception) +//{ +// return true; +//} + +void JSTizen::getPropertyNames(JSContextRef context, JSObjectRef object, + JSPropertyNameAccumulatorRef propertyNames) +{ + Try + { + TizenPrivate* priv = + static_cast<TizenPrivate*>(JSObjectGetPrivate(object)); + if (!priv || !priv->getObject()) { + LoggerE("No private obejct"); + return; + } + PluginOnDemandPrivPtr privObj = priv->getObject(); + privObj->getPluginManager()->addPropertiesToList(propertyNames); + + } + Catch(Commons::Exception) + { + LoggerE("Error occured"); + } +} + +bool JSTizen::hasInstance(JSContextRef context, JSObjectRef constructor, + JSValueRef possibleInstance, JSValueRef* exception) +{ + return true; +} + +JSValueRef JSTizen::convertToType(JSContextRef context, JSObjectRef object, + JSType type, JSValueRef* exception) +{ + return JSValueMakeUndefined(context); +} + +bool JSTizen::hasProperty(JSContextRef context, + JSObjectRef object, JSStringRef propertyName) +{ + Try + { + TizenPrivate* priv = static_cast<TizenPrivate*>(JSObjectGetPrivate(object)); + + if (!priv || !priv->getObject()) { + LoggerE("No private obejct"); + return false; + } + + return priv->getObject()->getPluginManager()->hasChild( + CommonsJavaScript::Converter(context).toString(propertyName)); + } + Catch(WrtDeviceApis::Commons::Exception) + { + LoggerE("Error occured"); + } + + //let JSCore to handle this property + return false; +} + +JSValueRef JSTizen::getProperty(JSContextRef context, JSObjectRef object, + JSStringRef propertyName, JSValueRef* exception) +{ + Try + { + TizenPrivate* priv = + static_cast<TizenPrivate*>(JSObjectGetPrivate(object)); + if (!priv || !priv->getObject()) { + LoggerE("No private obejct"); + return false; + } + + return priv->getObject()->getPluginManager()->getProperty( + CommonsJavaScript::Converter(context).toString(propertyName)); + } + Catch(WrtDeviceApis::Commons::Exception) + { + LoggerE("Error occured"); + } + + /* During normal operation flow should not reach here, + but wrt may fail with loading plugin. */ + return JSValueMakeUndefined(context); +} + +bool JSTizen::setProperty(JSContextRef context, JSObjectRef object, + JSStringRef propertyName, JSValueRef value, JSValueRef* exception) +{ + Try + { + TizenPrivate* priv = + static_cast<TizenPrivate*>(JSObjectGetPrivate(object)); + if (!priv || !priv->getObject()) { + LoggerE("No private obejct"); + return false; + } + std::string strPropertyName = CommonsJavaScript::Converter(context).toString(propertyName); + if(!priv->getObject()->propertyInited(strPropertyName)) + { + priv->getObject()->getPluginManager()->setProperty( + strPropertyName, + value); + } + } + Catch(WrtDeviceApis::Commons::Exception) + { + LoggerE("Error occured"); + } + + //this code should not be reached. setProperty is not called when + //hasProperty returns false. Anyway, if we didn't set value then we + //return false. + return false; +} + +} // Tizen +} // DeviceAPI + |