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 JSStaticFunction JSMediaServer::m_function[] = {
46 {"browse", JSMediaServer::browse, kJSPropertyAttributeNone},
47 {"find", JSMediaServer::find, kJSPropertyAttributeNone},
51 const JSClassRef JSMediaServer::getClassRef()
55 m_jsClassRef = JSClassCreate(&m_classInfo);
60 const JSClassDefinition* JSMediaServer::getClassInfo()
65 JSClassRef JSMediaServer::m_jsClassRef = JSClassCreate(JSMediaServer::getClassInfo());
67 void JSMediaServer::initialize(JSContextRef context, JSObjectRef object)
69 MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
72 MediaServerPtr mediaserver(new MediaServer());
73 priv = new MediaServerPrivObject( context, mediaserver);
74 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
76 LoggerE("Object can't store private data.");
81 LoggerD("JSMediaServer::initialize ");
84 void JSMediaServer::finalize(JSObjectRef object)
86 MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
87 JSObjectSetPrivate(object, NULL);
88 LoggerD("Deleting timeutil");
92 bool JSMediaServer::hasInstance(JSContextRef context,
93 JSObjectRef constructor,
94 JSValueRef possibleInstance,
95 JSValueRef* exception)
97 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
101 JSValueRef JSMediaServer::browse(JSContextRef context,
103 JSObjectRef thisObject,
104 size_t argumentCount,
105 const JSValueRef arguments[],
106 JSValueRef* exception)
109 // TODO try/catch block
110 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
111 if (NULL == privateObject)
113 LoggerE("private object is null");
114 // TODO post exception
117 LoggerD("Validating arguments: " << argumentCount);
119 ArgumentValidator validator(context, argumentCount, arguments);
120 std::string containerId = validator.toString(0);
121 std::string sortMode = validator.toString(1);
122 unsigned long count = validator.toULong(2);
123 unsigned long offset = validator.toULong(3);
125 BrowseFindCB *cbP = new BrowseFindCB();
126 cbP->context = privateObject->getContext();
127 cbP->successCB = validator.toFunction(4, true);
128 if (argumentCount > 5)
129 cbP->errorCB = validator.toFunction(5, true);
131 MediaServerPtr server(privateObject->getObject());
132 server->browse(containerId, sortMode, count, offset, cbP);
134 return JSValueMakeUndefined(context);
138 JSValueRef JSMediaServer::find(JSContextRef context,
140 JSObjectRef thisObject,
141 size_t argumentCount,
142 const JSValueRef arguments[],
143 JSValueRef* exception)
146 // TODO try/catch block
147 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
148 if (NULL == privateObject)
150 LoggerE("private object is null");
151 // TODO post exception
154 LoggerD("Validating arguments: " << argumentCount);
156 ArgumentValidator validator(context, argumentCount, arguments);
157 std::string containerId = validator.toString(0);
158 std::string searchFilter = validator.toString(1);
159 std::string sortMode = validator.toString(2);
160 unsigned long count = validator.toULong(3);
161 unsigned long offset = validator.toULong(4);
163 BrowseFindCB *cbP = new BrowseFindCB();
164 cbP->context = privateObject->getContext();
165 cbP->successCB = validator.toFunction(5, true);
166 if (argumentCount > 6)
167 cbP->errorCB = validator.toFunction(6, true);
169 MediaServerPtr server(privateObject->getObject());
170 server->find(containerId, searchFilter, sortMode, count, offset, cbP);
172 return JSValueMakeUndefined(context);