26 #include <JavaScriptCore/JavaScript.h>
28 #include "jsbindings.h"
52 static std::map<std::string, std::string> g_PageHttpHeaders;
63 std::string _licenseServerUrl;
79 LOG_TRACE(
"setAAMPPlayerInstance (%p, id=%d)", aamp, sessionID);
90 std::multimap<AAMPEventType, void*>::iterator listenerIter;
118 std::multimap<AAMPEventType, void*>::iterator listenerIter;
150 *exception =
aamp_GetException(context, AAMPJS_MISSING_OBJECT,
"Can only call AAMP_JSController.closedCaptionEnabled on instances of AAMP_JSController");
151 return JSValueMakeUndefined(context);
154 LOG_ERROR_EX(
"AAMP_JSController.closedCaptionEnabled has been deprecated!!");
155 return JSValueMakeBoolean(context,
false);
176 *exception =
aamp_GetException(context, AAMPJS_MISSING_OBJECT,
"Can only call AAMP_JSController.closedCaptionEnabled on instances of AAMP_JSController");
178 else if (!JSValueIsBoolean(context, value))
180 *exception =
aamp_GetException(context, AAMPJS_INVALID_ARGUMENT,
"Failed to execute 'AAMP_JSController.closedCaptionEnabled' - value passed is not boolean");
184 LOG_ERROR_EX(
"AAMP_JSController.closedCaptionEnabled has been deprecated!!");
207 return JSValueMakeUndefined(context);
210 if (aampObj->_aampSessionID == 0)
212 return JSValueMakeUndefined(context);
215 return JSValueMakeNumber(context, aampObj->_aampSessionID);
226 {NULL, NULL, NULL, 0}
240 static JSValueRef
AAMPJSC_addEventListener(JSContextRef context, JSObjectRef
function, JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[], JSValueRef *exception)
247 *exception =
aamp_GetException(context, AAMPJS_MISSING_OBJECT,
"Can only call AAMP_JSController.addEventListener on instances of AAMP_JSController");
248 return JSValueMakeUndefined(context);
251 if (argumentCount >= 2)
254 JSObjectRef callbackFunc = JSValueToObject(context, arguments[1], NULL);
256 if ((callbackFunc != NULL) && JSObjectIsFunction(context, callbackFunc))
259 LOG_WARN_EX(
"eventType='%s', %d", type, eventType);
260 if ((eventType >= 0) && (eventType < AAMP_MAX_NUM_EVENTS))
267 LOG_ERROR_EX(
"callbackFunc = %p, JSObjectIsFunction(context, callbackFunc) is NULL", callbackFunc);
269 memset(errMsg,
'\0', 512);
270 snprintf(errMsg, 511,
"Failed to execute addEventListener() for event %s - parameter 2 is not a function", type);
271 *exception =
aamp_GetException(context, AAMPJS_INVALID_ARGUMENT, (
const char*)errMsg);
273 SAFE_DELETE_ARRAY(type);
277 *exception =
aamp_GetException(context, AAMPJS_INVALID_ARGUMENT,
"Failed to execute 'AAMP_JSController.addEventListener' - 2 arguments required");
280 return JSValueMakeUndefined(context);
294 static JSValueRef
AAMPJSC_removeEventListener(JSContextRef context, JSObjectRef
function, JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[], JSValueRef *exception)
298 JSObjectRef callbackFunc;
302 *exception =
aamp_GetException(context, AAMPJS_MISSING_OBJECT,
"Can only call AAMP_JSController.removeEventListener on instances of AAMP_JSController");
303 return JSValueMakeUndefined(context);
306 if (argumentCount >= 2)
309 JSObjectRef callbackFunc = JSValueToObject(context, arguments[1], NULL);
311 if ((callbackFunc != NULL) && JSObjectIsFunction(context, callbackFunc))
314 LOG_TRACE(
"eventType='%s', %d",type, eventType);
316 if ((eventType >= 0) && (eventType < AAMP_MAX_NUM_EVENTS))
323 LOG_ERROR_EX(
"callbackFunc = %p, JSObjectIsFunction(context, callbackFunc) is NULL", callbackFunc);
325 memset(errMsg,
'\0', 512);
326 snprintf(errMsg, 511,
"Failed to execute removeEventListener() for event %s - parameter 2 is not a function", type);
327 *exception =
aamp_GetException(context, AAMPJS_INVALID_ARGUMENT, (
const char*)errMsg);
329 SAFE_DELETE_ARRAY(type);
333 *exception =
aamp_GetException(context, AAMPJS_INVALID_ARGUMENT,
"Failed to execute 'AAMP_JSController.removeEventListener' - 2 arguments required");
336 return JSValueMakeUndefined(context);
350 static JSValueRef
AAMPJSC_setLicenseServerUrl(JSContextRef context, JSObjectRef
function, JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[], JSValueRef *exception)
357 *exception =
aamp_GetException(context, AAMPJS_MISSING_OBJECT,
"Can only call AAMP_JSController.setLicenseServerUrl on instances of AAMP_JSController");
358 return JSValueMakeUndefined(context);
361 if (argumentCount == 1)
366 aampObj->_licenseServerUrl = std::string(url);
367 if (aampObj->_aamp != NULL)
372 SAFE_DELETE_ARRAY(url);
376 *exception =
aamp_GetException(context, AAMPJS_INVALID_ARGUMENT,
"Failed to execute 'AAMP_JSController.setLicenseServerURL' - 1 argument required");
379 return JSValueMakeUndefined(context);
408 if (aampObj->_ctx != NULL)
410 if (aampObj->_listeners.size() > 0)
416 JSObjectSetPrivate(thisObj, NULL);
418 SAFE_DELETE(aampObj);
433 *exception =
aamp_GetException(context, AAMPJS_GENERIC_ERROR,
"Cannot create an object of AAMP_JSController");
443 kJSClassAttributeNone,
444 "__AAMPJSController__class",
468 g_PageHttpHeaders.clear();
469 if(
nullptr != headerJson &&
'\0' != headerJson[0])
471 LOG_WARN_EX(
"aamp_SetPageHttpHeaders headerJson=%s", headerJson);
472 cJSON *parentJsonObj = cJSON_Parse(headerJson);
473 cJSON *jsonObj =
nullptr;
474 if(
nullptr != parentJsonObj)
476 jsonObj = parentJsonObj->child;
479 while(
nullptr != jsonObj)
481 cJSON *child = jsonObj->child;
482 std::string key =
"";
483 std::string val =
"";
484 while(
nullptr != child )
486 if(strcmp (child->string,
"name") == 0)
488 key = std::string(child->valuestring);
490 else if(strcmp (child->string,
"value") == 0)
492 val = std::string(child->valuestring);
496 if(!key.empty() && key ==
"X-PRIVACY-SETTINGS")
499 g_PageHttpHeaders.insert(std::make_pair(key, val));
501 jsonObj = jsonObj->next;
504 cJSON_Delete(parentJsonObj);
513 LOG_WARN_EX(
"aamp_ApplyPageHttpHeaders aampObject=%p", aampObject);
514 if(NULL != aampObject)
527 LOG_WARN_EX(
"aamp_LoadJSController context=%p", context);
529 aampObj->_ctx = context;
530 aampObj->_aampSessionID = 0;
531 aampObj->_listeners.clear();
532 aampObj->_licenseServerUrl = std::string();
543 JSObjectRef classObj = JSObjectMake(context, classDef, aampObj);
544 JSObjectRef globalObj = JSContextGetGlobalObject(context);
545 JSStringRef str = JSStringCreateWithUTF8CString(
"AAMP_JSController");
546 JSObjectSetProperty(context, globalObj, str, classObj, kJSPropertyAttributeReadOnly, NULL);
547 JSClassRelease(classDef);
548 JSStringRelease(str);
559 LOG_WARN_EX(
"aamp_UnloadJSController context=%p", context);
564 JSObjectRef globalObj = JSContextGetGlobalObject(context);
565 JSStringRef str = JSStringCreateWithUTF8CString(
"AAMP_JSController");
566 JSValueRef aamp = JSObjectGetProperty(context, globalObj, str, NULL);
570 JSStringRelease(str);
574 JSObjectRef aampObj = JSValueToObject(context, aamp, NULL);
575 if (aampObj == NULL || JSObjectGetPrivate(aampObj) == NULL)
577 JSStringRelease(str);
585 JSObjectDeleteProperty(context, globalObj, str, NULL);
586 JSStringRelease(str);
588 LOG_TRACE(
"JSGarbageCollect(%p)", context);
591 JSGarbageCollect(context);