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 void JSMediaServer::initialize(JSContextRef context, JSObjectRef object)
77 MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
80 MediaServerPtr mediaserver(new MediaServer());
81 priv = new MediaServerPrivObject( context, mediaserver);
82 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
84 LoggerE("Object can't store private data.");
89 LoggerD("JSMediaServer::initialize ");
92 void JSMediaServer::finalize(JSObjectRef object)
94 MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
95 JSObjectSetPrivate(object, NULL);
96 LoggerD("Deleting timeutil");
100 bool JSMediaServer::hasInstance(JSContextRef context,
101 JSObjectRef constructor,
102 JSValueRef possibleInstance,
103 JSValueRef* exception)
105 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
109 JSValueRef JSMediaServer::getProperty(JSContextRef context,
111 JSStringRef propertyName,
112 JSValueRef* exception)
115 // TODO try/catch block
116 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
117 if (NULL == privateObject)
119 LoggerE("private object is null");
120 // TODO post exception
123 if (JSStringIsEqualToUTF8CString(propertyName, "id")) {
124 JSStringRef jsStr = JSStringCreateWithUTF8CString("/com/intel/..."); // FIXME use proxy value
125 return JSValueMakeString(context, jsStr);
127 if (JSStringIsEqualToUTF8CString(propertyName, "friendlyName")) {
128 JSStringRef jsStr = JSStringCreateWithUTF8CString("FIXME mediaserver"); // FIXME use proxy value
129 return JSValueMakeString(context, jsStr);
131 if (JSStringIsEqualToUTF8CString(propertyName, "root")) {
132 JSStringRef jsStr = JSStringCreateWithUTF8CString("FIXME mediaserver root "); // FIXME use proxy value
133 return JSValueMakeFromJSONString(context, jsStr);
136 return JSValueMakeUndefined(context);
141 JSValueRef JSMediaServer::browse(JSContextRef context,
143 JSObjectRef thisObject,
144 size_t argumentCount,
145 const JSValueRef arguments[],
146 JSValueRef* exception)
149 // TODO try/catch block
150 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
151 if (NULL == privateObject)
153 LoggerE("private object is null");
154 // TODO post exception
157 LoggerD("Validating arguments: " << argumentCount);
159 ArgumentValidator validator(context, argumentCount, arguments);
160 std::string containerId = validator.toString(0);
161 std::string sortMode = validator.toString(1);
162 unsigned long count = validator.toULong(2);
163 unsigned long offset = validator.toULong(3);
165 BrowseFindCB *cbP = new BrowseFindCB();
166 cbP->context = privateObject->getContext();
167 cbP->successCB = validator.toFunction(4, true);
168 if (argumentCount > 5)
169 cbP->errorCB = validator.toFunction(5, true);
171 MediaServerPtr server(privateObject->getObject());
172 server->browse(containerId, sortMode, count, offset, cbP);
174 return JSValueMakeUndefined(context);
178 JSValueRef JSMediaServer::find(JSContextRef context,
180 JSObjectRef thisObject,
181 size_t argumentCount,
182 const JSValueRef arguments[],
183 JSValueRef* exception)
186 // TODO try/catch block
187 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
188 if (NULL == privateObject)
190 LoggerE("private object is null");
191 // TODO post exception
194 LoggerD("Validating arguments: " << argumentCount);
196 ArgumentValidator validator(context, argumentCount, arguments);
197 std::string containerId = validator.toString(0);
198 std::string searchFilter = validator.toString(1);
199 std::string sortMode = validator.toString(2);
200 unsigned long count = validator.toULong(3);
201 unsigned long offset = validator.toULong(4);
203 BrowseFindCB *cbP = new BrowseFindCB();
204 cbP->context = privateObject->getContext();
205 cbP->successCB = validator.toFunction(5, true);
206 if (argumentCount > 6)
207 cbP->errorCB = validator.toFunction(6, true);
209 MediaServerPtr server(privateObject->getObject());
210 server->find(containerId, searchFilter, sortMode, count, offset, cbP);
212 return JSValueMakeUndefined(context);