Update change log and spec for wrt-plugins-tizen_0.4.25-1
[platform/framework/web/wrt-plugins-tizen.git] / src / Content / ContentListener.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <pthread.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <JSWebAPIError.h>
22 #include <JSUtil.h>
23 #include "ContentListener.h"
24 #include "ContentConverter.h"
25 #include <Logger.h>
26
27 using namespace DeviceAPI::Common;
28 using namespace WrtDeviceApis::Commons;
29 using namespace WrtDeviceApis::CommonsJavaScript;
30
31
32 namespace DeviceAPI {
33 namespace Content {
34
35 ContentListener::ContentListener(JSContextRef globalCtx, JSObjectRef object)
36 {
37         m_callback = new MultiCallbackUserData(globalCtx);
38
39     // onstarted
40     JSValueRef oncontentadded = JSUtil::getProperty(globalCtx , object, "oncontentadded");
41     if(!JSValueIsUndefined(globalCtx,oncontentadded)) {
42         m_callback->setCallback("oncontentadded", JSUtil::JSValueToObject(globalCtx, oncontentadded));
43     }
44     JSValueRef oncontentupdated = JSUtil::getProperty(globalCtx , object, "oncontentupdated");
45     if(!JSValueIsUndefined(globalCtx,oncontentupdated)) {
46         m_callback->setCallback("oncontentupdated", JSUtil::JSValueToObject(globalCtx, oncontentupdated));
47     }
48     JSValueRef oncontentremoved = JSUtil::getProperty(globalCtx , object, "oncontentremoved");
49     if(!JSValueIsUndefined(globalCtx,oncontentremoved)) {
50         m_callback->setCallback("oncontentremoved", JSUtil::JSValueToObject(globalCtx, oncontentremoved));
51     }
52 /*
53         // oncontentadded
54         JSValueRef oncontentadded = JSUtils::getJSPropertyOrUndefined(globalCtx, object, "oncontentadded");
55         if (validator.isNullOrUndefined(oncontentadded)) {
56                 oncontentadded = NULL;
57         }
58         else if (validator.isCallback(oncontentadded)) {
59                 JSObjectRef cbObj = JSValueToObject(globalCtx, oncontentadded, NULL);
60                 if(JSObjectIsFunction(globalCtx, cbObj)) {
61                         m_callback->setCallback("oncontentadded", cbObj);
62                 }
63                 else {
64                         ThrowMsg(ConversionException,"oncontentadded is not a callback function.");
65                 }
66         }
67         else{
68                 ThrowMsg(ConversionException, "oncontentadded is not available");
69         }
70
71         // oncontentupdated
72         JSValueRef oncontentupdated = JSUtils::getJSPropertyOrUndefined(globalCtx, object, "oncontentupdated");
73         if (validator.isNullOrUndefined(oncontentupdated)) {
74                 oncontentupdated = NULL;
75         }
76         else if (validator.isCallback(oncontentupdated)) {
77                 JSObjectRef cbObj = JSValueToObject(globalCtx, oncontentupdated, NULL);
78                 if(JSObjectIsFunction(globalCtx, cbObj)) {
79                         m_callback->setCallback("oncontentupdated", cbObj);
80                 }
81                 else {
82                         ThrowMsg(ConversionException,"oncontentupdated is not a callback function.");
83                 }
84         }
85         else{
86                 ThrowMsg(ConversionException, "oncontentupdated is not available");
87         }
88
89         // oncontentremoved
90         JSValueRef oncontentremoved = JSUtils::getJSPropertyOrUndefined(globalCtx, object, "oncontentremoved");
91         if (validator.isNullOrUndefined(oncontentremoved)) {
92                 oncontentremoved = NULL;
93         }
94         else if (validator.isCallback(oncontentremoved)) {
95                 JSObjectRef cbObj = JSValueToObject(globalCtx, oncontentremoved, NULL);
96                 if(JSObjectIsFunction(globalCtx, cbObj)) {
97                         m_callback->setCallback("oncontentremoved", cbObj);
98                 }
99                 else {
100                         ThrowMsg(ConversionException,"oncontentremoved is not a callback function.");
101                 }
102         }
103         else{
104                 ThrowMsg(ConversionException, "oncontentremoved is not available");
105         }
106 */
107 }
108
109 ContentListener::~ContentListener() {
110         if(m_callback != NULL){
111                 delete m_callback;
112                 m_callback = NULL;
113         }
114 }
115
116 void ContentListener::oncontentadded(MediacontentMediaPtr content)
117 {
118         JSContextRef context = m_callback->getContext();
119
120         MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
121         JSValueRef result = converter->toJSValueRefMedia(content);
122         m_callback->invokeCallback("oncontentadded", result);
123 }
124
125 void ContentListener::oncontentupdated(MediacontentMediaPtr content)
126 {
127         JSContextRef context = m_callback->getContext();
128
129         MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
130         JSValueRef result = converter->toJSValueRefMedia(content);
131         m_callback->invokeCallback("oncontentupdated", result);
132
133 }
134
135 void ContentListener::oncontentremoved(std::string id)
136 {
137         JSContextRef context = m_callback->getContext();
138
139         MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
140         m_callback->invokeCallback("oncontentremoved", converter->toJSValueRef(id));
141 }
142
143
144 } // Download
145 } // DeviceAPI