1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 1× 1× 1× 122× 122× 114× 114× 1× 122× 7× 115× 1× 114× 1× 133× 133× 1002× 9× 124× 1× 124× 1× 123× 1× 9× 9× 9× 9× 9× 7× 7× 7× 1× 1× 1× 1× 9× 1× 8× 1× 1× 7× 6× 5× 6× 1× 12× 12× 1× 3× 3× 1× 9× 9× 7× 7× 7× 3× 3× 3× 1× 9× 1× 8× 1× 1× 2× 2× 1× 1× 2× 2× 1× 4× 1× 3× 3× 1× 1× 1× 1× 1× 1× 1× 1× 1× 10× 10× 1× 1× | 'use strict'; var VASTError = require('../vast/VASTError'); var utilities = require('../../utils/utilityFunctions'); function VPAIDAdUnitWrapper(vpaidAdUnit, opts) { Iif (!(this instanceof VPAIDAdUnitWrapper)) { return new VPAIDAdUnitWrapper(vpaidAdUnit, opts); } sanityCheck(vpaidAdUnit, opts); this.options = utilities.extend({}, opts); this._adUnit = vpaidAdUnit; /*** Local Functions ***/ function sanityCheck(adUnit, opts) { if (!adUnit || !VPAIDAdUnitWrapper.checkVPAIDInterface(adUnit)) { throw new VASTError('on VPAIDAdUnitWrapper, the passed VPAID adUnit does not fully implement the VPAID interface'); } if (!utilities.isObject(opts)) { throw new VASTError("on VPAIDAdUnitWrapper, expected options hash but got '" + opts + "'"); } Iif (!("responseTimeout" in opts) || !utilities.isNumber(opts.responseTimeout) ){ throw new VASTError("on VPAIDAdUnitWrapper, expected responseTimeout in options"); } } } VPAIDAdUnitWrapper.checkVPAIDInterface = function checkVPAIDInterface(VPAIDAdUnit) { //NOTE: skipAd is not part of the method list because it only appears in VPAID 2.0 and we support VPAID 1.0 var VPAIDInterfaceMethods = [ 'handshakeVersion', 'initAd', 'startAd', 'stopAd', 'resizeAd', 'pauseAd', 'expandAd', 'collapseAd' ]; for (var i = 0, len = VPAIDInterfaceMethods.length; i < len; i++) { if (!VPAIDAdUnit || !utilities.isFunction(VPAIDAdUnit[VPAIDInterfaceMethods[i]])) { return false; } } return canSubscribeToEvents(VPAIDAdUnit) && canUnsubscribeFromEvents(VPAIDAdUnit); /*** Local Functions ***/ function canSubscribeToEvents(adUnit) { return utilities.isFunction(adUnit.subscribe) || utilities.isFunction(adUnit.addEventListener) || utilities.isFunction(adUnit.on); } function canUnsubscribeFromEvents(adUnit) { return utilities.isFunction(adUnit.unsubscribe) || utilities.isFunction(adUnit.removeEventListener) || utilities.isFunction(adUnit.off); } }; VPAIDAdUnitWrapper.prototype.adUnitAsyncCall = function () { var args = utilities.arrayLikeObjToArray(arguments); var method = args.shift(); var cb = args.pop(); var timeoutId; sanityCheck(method, cb, this._adUnit); args.push(wrapCallback()); this._adUnit[method].apply(this._adUnit, args); timeoutId = setTimeout(function () { timeoutId = null; cb(new VASTError("on VPAIDAdUnitWrapper, timeout while waiting for a response on call '" + method + "'")); cb = utilities.noop; }, this.options.responseTimeout); /*** Local functions ***/ function sanityCheck(method, cb, adUnit) { if (!utilities.isString(method) || !utilities.isFunction(adUnit[method])) { throw new VASTError("on VPAIDAdUnitWrapper.adUnitAsyncCall, invalid method name"); } if (!utilities.isFunction(cb)) { throw new VASTError("on VPAIDAdUnitWrapper.adUnitAsyncCall, missing callback"); } } function wrapCallback() { return function () { if (timeoutId) { clearTimeout(timeoutId); } cb.apply(this, arguments); }; } }; VPAIDAdUnitWrapper.prototype.on = function (evtName, handler) { var addEventListener = this._adUnit.addEventListener || this._adUnit.subscribe || this._adUnit.on; addEventListener.call(this._adUnit, evtName, handler); }; VPAIDAdUnitWrapper.prototype.off = function (evtName, handler) { var removeEventListener = this._adUnit.removeEventListener || this._adUnit.unsubscribe || this._adUnit.off; removeEventListener.call(this._adUnit, evtName, handler); }; VPAIDAdUnitWrapper.prototype.waitForEvent = function (evtName, cb, context) { var timeoutId; sanityCheck(evtName, cb); context = context || null; this.on(evtName, responseListener); timeoutId = setTimeout(function () { cb(new VASTError("on VPAIDAdUnitWrapper.waitForEvent, timeout while waiting for event '" + evtName + "'")); timeoutId = null; cb = utilities.noop; }, this.options.responseTimeout); /*** Local functions ***/ function sanityCheck(evtName, cb) { if (!utilities.isString(evtName)) { throw new VASTError("on VPAIDAdUnitWrapper.waitForEvent, missing evt name"); } if (!utilities.isFunction(cb)) { throw new VASTError("on VPAIDAdUnitWrapper.waitForEvent, missing callback"); } } function responseListener() { var args = utilities.arrayLikeObjToArray(arguments); if (timeoutId) { clearTimeout(timeoutId); timeoutId = null; } args.unshift(null); cb.apply(context, args); } }; // VPAID METHODS VPAIDAdUnitWrapper.prototype.handshakeVersion = function (version, cb) { this.adUnitAsyncCall('handshakeVersion', version, cb); }; /* jshint maxparams:6 */ VPAIDAdUnitWrapper.prototype.initAd = function (width, height, viewMode, desiredBitrate, adUnitData, cb) { this.waitForEvent('AdLoaded', cb); this._adUnit.initAd(width, height, viewMode, desiredBitrate, adUnitData); }; VPAIDAdUnitWrapper.prototype.resizeAd = function (width, height, viewMode, cb) { // NOTE: AdSizeChange event is only supported on VPAID 2.0 so for the moment we are not going to use it // and will assume that everything is fine after the async call this.adUnitAsyncCall('resizeAd', width, height, viewMode, cb); }; VPAIDAdUnitWrapper.prototype.startAd = function (cb) { this.waitForEvent('AdStarted', cb); this._adUnit.startAd(); }; VPAIDAdUnitWrapper.prototype.stopAd = function (cb) { this.waitForEvent('AdStopped', cb); this._adUnit.stopAd(); }; VPAIDAdUnitWrapper.prototype.pauseAd = function (cb) { this.waitForEvent('AdPaused', cb); this._adUnit.pauseAd(); }; VPAIDAdUnitWrapper.prototype.resumeAd = function (cb) { this.waitForEvent('AdPlaying', cb); this._adUnit.resumeAd(); }; VPAIDAdUnitWrapper.prototype.expandAd = function (cb) { this.waitForEvent('AdExpandedChange', cb); this._adUnit.expandAd(); }; VPAIDAdUnitWrapper.prototype.collapseAd = function (cb) { this.waitForEvent('AdExpandedChange', cb); this._adUnit.collapseAd(); }; VPAIDAdUnitWrapper.prototype.skipAd = function (cb) { this.waitForEvent('AdSkipped', cb); this._adUnit.skipAd(); }; //VPAID property getters [ 'adLinear', 'adWidth', 'adHeight', 'adExpanded', 'adSkippableState', 'adRemainingTime', 'adDuration', 'adVolume', 'adCompanions', 'adIcons' ].forEach(function (property) { var getterName = 'get' + utilities.capitalize(property); VPAIDAdUnitWrapper.prototype[getterName] = function (cb) { this.adUnitAsyncCall(getterName, cb); }; }); //VPAID property setters VPAIDAdUnitWrapper.prototype.setAdVolume = function(volume, cb){ this.adUnitAsyncCall('setAdVolume',volume, cb); }; module.exports = VPAIDAdUnitWrapper; |