tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

playback-persistent-license-events.js (7105B)


      1 function runTest(config,qualifier) {
      2 
      3    var testname = testnamePrefix(qualifier, config.keysystem)
      4                                    + ', persistent-license, '
      5                                    + /video\/([^;]*)/.exec(config.videoType)[1]
      6                                    + ', playback, check events';
      7 
      8    var configuration = {   initDataTypes: [ config.initDataType ],
      9                            audioCapabilities: [ { contentType: config.audioType } ],
     10                            videoCapabilities: [ { contentType: config.videoType } ],
     11                            sessionTypes: [ 'persistent-license' ] };
     12 
     13 
     14    async_test(function(test) {
     15        var _video = config.video,
     16            _mediaKeys,
     17            _mediaKeySession,
     18            _mediaSource,
     19            _receivedTimeupdateEvent = false,
     20            _startedReleaseSequence = false,
     21            _events = [ ];
     22 
     23        function recordEventFunc(eventType) {
     24            return function() { _events.push(eventType); };
     25        }
     26 
     27        function recordEventFuncAndCheckExpirationForNaN(eventType) {
     28            return function() {
     29                _events.push(eventType);
     30                assert_equals(_mediaKeySession.expiration, NaN);
     31            };
     32        }
     33 
     34        function onFailure(error) {
     35            forceTestFailureFromPromise(test, error);
     36        }
     37 
     38        function onMessage(event) {
     39            assert_equals( event.target, _mediaKeySession );
     40            assert_true( event instanceof window.MediaKeyMessageEvent );
     41            assert_equals( event.type, 'message');
     42 
     43            if (!_startedReleaseSequence) {
     44                assert_in_array(event.messageType, ['license-request', 'individualization-request']);
     45            } else {
     46                assert_equals(event.messageType, 'license-release');
     47            }
     48 
     49            if (event.messageType !== 'individualization-request') {
     50                _events.push(event.messageType);
     51            }
     52 
     53            config.messagehandler(event.messageType, event.message ).then(function(response) {
     54                _events.push(event.messageType + '-response');
     55                return _mediaKeySession.update(response);
     56            }).then(test.step_func(function() {
     57                _events.push(event.messageType + '-response-resolved');
     58                if (event.messageType === 'license-release') {
     59                    test.step_timeout(function() {
     60                        checkEventSequence(_events, [
     61                            'generaterequest',
     62                            [    // potentially repeating
     63                                'license-request',
     64                                'license-request-response',
     65                                'license-request-response-resolved'
     66                            ],
     67                            'keystatuseschange-usablekey',
     68                            'playing',
     69                            'remove-resolved',
     70                            'keystatuseschange-allkeysreleased',
     71                            'license-release',
     72                            'license-release-response',
     73                            'closed-attribute-resolved',
     74                            'license-release-response-resolved',
     75                            'keystatuseschange-empty'
     76                        ]);
     77                        test.done();
     78                    }, 100);
     79                }
     80            })).catch(onFailure);
     81        }
     82 
     83        function onKeyStatusesChange(event) {
     84            assert_equals(event.target, _mediaKeySession);
     85            assert_true(event instanceof window.Event);
     86            assert_equals(event.type, 'keystatuseschange');
     87            var hasKeys = false,
     88                usableKey = false;    // true if any key usable.
     89            _mediaKeySession.keyStatuses.forEach(function(value, keyid) {
     90                assert_in_array(value, ['usable', 'released']);
     91                hasKeys = true;
     92                usableKey = usableKey || (value === 'usable');
     93            });
     94 
     95            if (!hasKeys) {
     96                _events.push('keystatuseschange-empty');
     97            } else if (usableKey) {
     98                _events.push('keystatuseschange-usablekey');
     99            } else {
    100                _events.push('keystatuseschange-allkeysreleased');
    101            }
    102        }
    103 
    104        function onEncrypted(event) {
    105            assert_equals(event.target, _video);
    106            assert_true(event instanceof window.MediaEncryptedEvent);
    107            assert_equals(event.type, 'encrypted');
    108 
    109            _mediaKeySession.generateRequest(   config.initData ? config.initDataType : event.initDataType,
    110                                                config.initData || event.initData ).then(recordEventFunc('generaterequest')
    111            ).catch(onFailure);
    112        }
    113 
    114        function onTimeupdate(event) {
    115            if ( _video.currentTime > ( config.duration || 1 ) && !_receivedTimeupdateEvent ) {
    116                _receivedTimeupdateEvent = true;
    117                _video.pause();
    118                _video.removeAttribute('src');
    119                _video.load();
    120 
    121                _startedReleaseSequence = true;
    122                _mediaKeySession.remove()
    123                    .then(recordEventFuncAndCheckExpirationForNaN('remove-resolved'))
    124                    .catch(onFailure);
    125            }
    126        }
    127 
    128        function onPlaying(event) {
    129            _events.push( 'playing' );
    130        }
    131 
    132        navigator.requestMediaKeySystemAccess(config.keysystem, [ configuration ]).then(function(access) {
    133            return access.createMediaKeys();
    134        }).then(function(mediaKeys) {
    135            _mediaKeys = mediaKeys;
    136            return _video.setMediaKeys(_mediaKeys);
    137        }).then(function() {
    138            waitForEventAndRunStep('encrypted', _video, onEncrypted, test);
    139            waitForEventAndRunStep('playing', _video, onPlaying, test);
    140            // Not using waitForEventAndRunStep() to avoid too many
    141            // EVENT(onTimeUpdate) logs.
    142            _video.addEventListener('timeupdate', onTimeupdate, true);
    143            _mediaKeySession = _mediaKeys.createSession( 'persistent-license' );
    144            waitForEventAndRunStep('keystatuseschange', _mediaKeySession, onKeyStatusesChange, test);
    145            waitForEventAndRunStep('message', _mediaKeySession, onMessage, test);
    146            _mediaKeySession.closed
    147                .then(recordEventFuncAndCheckExpirationForNaN('closed-attribute-resolved'))
    148                .catch(onFailure);
    149            return testmediasource(config);
    150        }).then(function(source) {
    151            _mediaSource = source;
    152            _video.src = URL.createObjectURL(_mediaSource);
    153            return source.done;
    154        }).then(function(){
    155            _video.play();
    156        }).catch(function (error) {
    157            if (error && error.name === 'NotSupportedError') {
    158                // If persistent license is not supported, skip test as
    159                // key systems are not required to support this session
    160                // type.
    161                test.done();
    162            } else {
    163                onFailure(error);
    164            }
    165        });
    166    }, testname);
    167 }