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

content / test / data / browsing_data / media_license.js [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// EME creates session IDs dynamically, so we have no idea what it will be.
// As the tests only need to create a single session, keep track of the
// last session ID created.
var savedSessionId = 'UnknownSessionId';

function createPersistentSession() {
  // This function creates a persistent-license type session, and resolves
  // with the created session object on success.
  return navigator
      .requestMediaKeySystemAccess(
          'org.chromium.externalclearkey', [{
            initDataTypes: ['keyids'],
            audioCapabilities: [
              // Include a set of codecs that should cover all user agents.
              {contentType: 'audio/mp4; codecs="mp4a.40.2"'},
              {contentType: 'audio/webm; codecs="opus"'}
            ],
            persistentState: 'required',
            sessionTypes: ['persistent-license'],
          }])
      .then(function(access) {
        return access.createMediaKeys();
      })
      .then(function(mediaKeys) {
        return mediaKeys.createSession('persistent-license');
      });
}

function handleMessageEvent(e) {
  var session = e.target;
  var te = new TextEncoder();
  var license = te.encode(
      '{"keys":[{"kty":"oct","k":"tQ0bJVWb6b0KPL6KtZIy_A","kid":"LwVHf8JLtPrv2GUXFW2v_A"}],"type":"persistent-license"}');

  savedSessionId = session.sessionId;
  return session.update(license).then(() => true, () => false);
}

async function setMediaLicense() {
  var te = new TextEncoder();
  var initData = te.encode('{"kids":["LwVHf8JLtPrv2GUXFW2v_A"]}');

  try {
    const session = await createPersistentSession();
    // generateRequest() will trigger a 'message' event, which we need to
    // wait for in order to call update() which provides the license.
    const handled = new Promise((resolve, reject) => {
      session.addEventListener('message', (e) => {
        handleMessageEvent(e).then(resolve, reject)
      }, false);
    });
    await session.generateRequest('keyids', initData);
    await handled;
    return true;
  } catch {
    return false;
  }
}

async function hasMediaLicense() {
  try {
    const session = await createPersistentSession();
    const result = await session.load(savedSessionId);
    // |result| is a boolean, indicating if the session was loaded or not.
    return result;
  }catch {
    return false;
  }
}