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.
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <JSWebAPIError.h>
23 #include "ContentListener.h"
24 #include "ContentConverter.h"
27 using namespace DeviceAPI::Common;
28 using namespace WrtDeviceApis::Commons;
29 using namespace WrtDeviceApis::CommonsJavaScript;
35 ContentListener::ContentListener(JSContextRef globalCtx, JSObjectRef object)
37 m_callback = new MultiCallbackUserData(globalCtx);
40 JSValueRef oncontentadded = JSUtil::getProperty(globalCtx , object, "oncontentadded");
41 if(!JSValueIsUndefined(globalCtx,oncontentadded)) {
42 m_callback->setCallback("oncontentadded", JSUtil::JSValueToObject(globalCtx, oncontentadded));
44 JSValueRef oncontentupdated = JSUtil::getProperty(globalCtx , object, "oncontentupdated");
45 if(!JSValueIsUndefined(globalCtx,oncontentupdated)) {
46 m_callback->setCallback("oncontentupdated", JSUtil::JSValueToObject(globalCtx, oncontentupdated));
48 JSValueRef oncontentremoved = JSUtil::getProperty(globalCtx , object, "oncontentremoved");
49 if(!JSValueIsUndefined(globalCtx,oncontentremoved)) {
50 m_callback->setCallback("oncontentremoved", JSUtil::JSValueToObject(globalCtx, oncontentremoved));
54 JSValueRef oncontentadded = JSUtils::getJSPropertyOrUndefined(globalCtx, object, "oncontentadded");
55 if (validator.isNullOrUndefined(oncontentadded)) {
56 oncontentadded = NULL;
58 else if (validator.isCallback(oncontentadded)) {
59 JSObjectRef cbObj = JSValueToObject(globalCtx, oncontentadded, NULL);
60 if(JSObjectIsFunction(globalCtx, cbObj)) {
61 m_callback->setCallback("oncontentadded", cbObj);
64 ThrowMsg(ConversionException,"oncontentadded is not a callback function.");
68 ThrowMsg(ConversionException, "oncontentadded is not available");
72 JSValueRef oncontentupdated = JSUtils::getJSPropertyOrUndefined(globalCtx, object, "oncontentupdated");
73 if (validator.isNullOrUndefined(oncontentupdated)) {
74 oncontentupdated = NULL;
76 else if (validator.isCallback(oncontentupdated)) {
77 JSObjectRef cbObj = JSValueToObject(globalCtx, oncontentupdated, NULL);
78 if(JSObjectIsFunction(globalCtx, cbObj)) {
79 m_callback->setCallback("oncontentupdated", cbObj);
82 ThrowMsg(ConversionException,"oncontentupdated is not a callback function.");
86 ThrowMsg(ConversionException, "oncontentupdated is not available");
90 JSValueRef oncontentremoved = JSUtils::getJSPropertyOrUndefined(globalCtx, object, "oncontentremoved");
91 if (validator.isNullOrUndefined(oncontentremoved)) {
92 oncontentremoved = NULL;
94 else if (validator.isCallback(oncontentremoved)) {
95 JSObjectRef cbObj = JSValueToObject(globalCtx, oncontentremoved, NULL);
96 if(JSObjectIsFunction(globalCtx, cbObj)) {
97 m_callback->setCallback("oncontentremoved", cbObj);
100 ThrowMsg(ConversionException,"oncontentremoved is not a callback function.");
104 ThrowMsg(ConversionException, "oncontentremoved is not available");
109 ContentListener::~ContentListener() {
110 if(m_callback != NULL){
116 void ContentListener::oncontentadded(MediacontentMediaPtr content)
118 JSContextRef context = m_callback->getContext();
120 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
121 JSValueRef result = converter->toJSValueRefMedia(content);
122 m_callback->invokeCallback("oncontentadded", result);
125 void ContentListener::oncontentupdated(MediacontentMediaPtr content)
127 JSContextRef context = m_callback->getContext();
129 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
130 JSValueRef result = converter->toJSValueRefMedia(content);
131 m_callback->invokeCallback("oncontentupdated", result);
135 void ContentListener::oncontentremoved(std::string id)
137 JSContextRef context = m_callback->getContext();
139 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
140 m_callback->invokeCallback("oncontentremoved", converter->toJSValueRef(id));