1 #include "JSMediaServer.h"
2 #include "MediaServer.h"
5 #include <Commons/Exception.h>
6 #include <CommonsJavaScript/Utils.h>
7 #include <CommonsJavaScript/JSCallbackManager.h>
8 #include <JSWebAPIErrorFactory.h>
9 #include <ArgumentValidator.h>
10 #include <CommonsJavaScript/Converter.h>
14 #include <json-glib/json-gvariant.h>
18 namespace MediaServer {
21 using namespace DeviceAPI::Common;
22 using namespace WrtDeviceApis::Commons;
23 using namespace WrtDeviceApis::CommonsJavaScript;
25 JSClassDefinition JSMediaServer::m_classInfo = {
27 kJSClassAttributeNone,
37 NULL, //DeleteProperty,
38 NULL, //GetPropertyNames,
39 NULL, //CallAsFunction,
40 NULL, //CallAsConstructor,
45 JSStaticValue JSMediaServer::m_property[] =
47 {"id", getProperty, NULL, kJSPropertyAttributeReadOnly},
48 {"friendlyName", getProperty, NULL, kJSPropertyAttributeReadOnly},
49 {"root", getProperty, NULL, kJSPropertyAttributeReadOnly},
53 JSStaticFunction JSMediaServer::m_function[] = {
54 {"browse", JSMediaServer::browse, kJSPropertyAttributeNone},
55 {"find", JSMediaServer::find, kJSPropertyAttributeNone},
59 const JSClassRef JSMediaServer::getClassRef()
63 m_jsClassRef = JSClassCreate(&m_classInfo);
68 const JSClassDefinition* JSMediaServer::getClassInfo()
73 JSClassRef JSMediaServer::m_jsClassRef = JSClassCreate(JSMediaServer::getClassInfo());
75 JSObjectRef JSMediaServer::createJSObject(JSContextRef context) {
76 LoggerD("Entered JSMediaServer::createJSObject");
77 MediaServerPtr mediaserver(new MediaServer());
78 MediaServerPrivObject* priv = new MediaServerPrivObject(context, mediaserver);
79 return JSObjectMake(context, getClassRef(), priv);
82 JSObjectRef JSMediaServer::createJSObject(JSContextRef context, std::string path) {
83 LoggerD("Entered JSMediaServer::createJSObject : " << path.c_str());
84 MediaServer *ms = new MediaServer();
85 ms->setProxyPath(path);
86 MediaServerPtr mediaserver(ms);
87 MediaServerPrivObject* priv = new MediaServerPrivObject(context, mediaserver);
88 return JSObjectMake(context, getClassRef(), priv);
91 void JSMediaServer::initialize(JSContextRef context, JSObjectRef object)
93 LoggerD("Entered JSMediaServer::initialize");
94 MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
97 LoggerD("Create private object");
98 MediaServerPtr mediaserver(new MediaServer());
99 priv = new MediaServerPrivObject(context, mediaserver);
100 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
102 LoggerE("Object can't store private data.");
107 LoggerD("JSMediaServer::initialize done");
110 void JSMediaServer::finalize(JSObjectRef object)
112 MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
113 JSObjectSetPrivate(object, NULL);
114 LoggerD("JSMediaServer::finalize");
118 bool JSMediaServer::hasInstance(JSContextRef context,
119 JSObjectRef constructor,
120 JSValueRef possibleInstance,
121 JSValueRef* exception)
123 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
127 JSValueRef JSMediaServer::getProperty(JSContextRef context,
129 JSStringRef propertyName,
130 JSValueRef* exception)
132 LoggerD("Entered JSMediaServer::getProperty");
133 // TODO try/catch block
134 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
135 if (NULL == privateObject)
137 LoggerE("private object is null");
138 // TODO post exception
140 MediaServerPtr server(privateObject->getObject());
142 Converter converter(context);
143 std::string name = converter.toString(propertyName);
144 std::string strVal = server->getPropertyStringValue(name);
145 LoggerD("JSMediaServer::getProperty : " << name.c_str() << " : " << strVal.c_str());
146 JSStringRef jsStr = converter.toJSStringRef(strVal);
148 if (JSStringIsEqualToUTF8CString(propertyName, "id"))
149 return JSValueMakeString(context, jsStr);
150 else if (JSStringIsEqualToUTF8CString(propertyName, "friendlyName"))
151 return JSValueMakeString(context, jsStr);
152 else if (JSStringIsEqualToUTF8CString(propertyName, "root"))
153 return JSValueMakeFromJSONString(context, jsStr);
155 return JSValueMakeUndefined(context);
160 JSValueRef JSMediaServer::browse(JSContextRef context,
162 JSObjectRef thisObject,
163 size_t argumentCount,
164 const JSValueRef arguments[],
165 JSValueRef* exception)
167 LoggerD("Entered JSMediaServer::browse");
168 // TODO try/catch block
169 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
170 if (NULL == privateObject)
172 LoggerE("private object is null");
173 // TODO post exception
176 LoggerD("Validating arguments: " << argumentCount);
178 ArgumentValidator validator(context, argumentCount, arguments);
179 std::string containerId = validator.toString(0);
180 std::string sortMode = validator.toString(1);
181 unsigned long count = validator.toULong(2);
182 unsigned long offset = validator.toULong(3);
184 LoggerD("browse container " << containerId << " sort: " << sortMode
185 << " count:" << count << " offset:" << offset);
187 BrowseFindCB *cbP = new BrowseFindCB();
188 cbP->context = privateObject->getContext();
189 cbP->object = object;
190 cbP->thisObject = thisObject;
191 cbP->successCB = validator.toFunction(4, true);
192 JSValueProtect(cbP->context, cbP->successCB);
193 if (argumentCount > 5) {
194 cbP->errorCB = validator.toFunction(5, true);
195 JSValueProtect(cbP->context, cbP->errorCB);
200 MediaServerPtr server(privateObject->getObject());
201 server->browse(containerId, sortMode, count, offset, cbP);
203 return JSValueMakeUndefined(context);
207 JSValueRef JSMediaServer::find(JSContextRef context,
209 JSObjectRef thisObject,
210 size_t argumentCount,
211 const JSValueRef arguments[],
212 JSValueRef* exception)
214 LoggerD("Entered JSMediaServer::find");
215 // TODO try/catch block
216 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
217 if (NULL == privateObject)
219 LoggerE("private object is null");
220 // TODO post exception
223 LoggerD("Validating arguments: " << argumentCount);
225 ArgumentValidator validator(context, argumentCount, arguments);
226 std::string containerId = validator.toString(0);
227 std::string searchFilter = validator.toString(1);
228 std::string sortMode = validator.toString(2);
229 unsigned long count = validator.toULong(3);
230 unsigned long offset = validator.toULong(4);
232 LoggerD("find: " << searchFilter << " in container " << containerId << " sort: " << sortMode
233 << " count:" << count << " offset:" << offset);
235 BrowseFindCB *cbP = new BrowseFindCB();
236 cbP->context = privateObject->getContext();
237 cbP->object = object;
238 cbP->thisObject = thisObject;
239 cbP->successCB = validator.toFunction(5, true);
240 JSValueProtect(cbP->context, cbP->successCB);
241 if (argumentCount > 6) {
242 cbP->errorCB = validator.toFunction(6, true);
243 JSValueProtect(cbP->context, cbP->errorCB);
248 MediaServerPtr server(privateObject->getObject());
249 server->find(containerId, searchFilter, sortMode, count, offset, cbP);
251 return JSValueMakeUndefined(context);