summaryrefslogtreecommitdiffstats
path: root/dom/media/eme/MediaKeySession.cpp
blob: 8a3a01dd5c5a8f243620acbc87b78237399554e9 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/MediaKeySession.h"

#include <ctime>
#include <utility>

#include "GMPUtils.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/CDMProxy.h"
#include "mozilla/EMEUtils.h"
#include "mozilla/Encoding.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/KeyIdsInitDataBinding.h"
#include "mozilla/dom/MediaEncryptedEvent.h"
#include "mozilla/dom/MediaKeyError.h"
#include "mozilla/dom/MediaKeyMessageEvent.h"
#include "mozilla/dom/MediaKeyStatusMap.h"
#include "mozilla/dom/MediaKeySystemAccess.h"
#include "nsCycleCollectionParticipant.h"
#include "nsPrintfCString.h"
#include "psshparser/PsshParser.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaKeySession, DOMEventTargetHelper,
                                   mMediaKeyError, mKeys, mKeyStatusMap,
                                   mClosed)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaKeySession)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

NS_IMPL_ADDREF_INHERITED(MediaKeySession, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(MediaKeySession, DOMEventTargetHelper)

// Count of number of instances. Used to give each instance a
// unique token.
static uint32_t sMediaKeySessionNum = 0;

// Max length of keyId in EME "keyIds" or WebM init data format, as enforced
// by web platform tests.
static const uint32_t MAX_KEY_ID_LENGTH = 512;

// Max length of CENC PSSH init data tolerated, as enforced by web
// platform tests.
static const uint32_t MAX_CENC_INIT_DATA_LENGTH = 64 * 1024;

MediaKeySession::MediaKeySession(nsPIDOMWindowInner* aParent, MediaKeys* aKeys,
                                 const nsAString& aKeySystem,
                                 MediaKeySessionType aSessionType,
                                 bool aHardwareDecryption, ErrorResult& aRv)
    : DOMEventTargetHelper(aParent),
      mKeys(aKeys),
      mKeySystem(aKeySystem),
      mSessionType(aSessionType),
      mToken(sMediaKeySessionNum++),
      mIsClosed(false),
      mUninitialized(true),
      mKeyStatusMap(new MediaKeyStatusMap(aParent)),
      mExpiration(JS::GenericNaN()),
      mHardwareDecryption(aHardwareDecryption) {
  EME_LOG("MediaKeySession[%p,''] ctor", this);

  MOZ_ASSERT(aParent);
  if (aRv.Failed()) {
    return;
  }
  mClosed = MakePromise(aRv, "MediaKeys.createSession"_ns);
}

void MediaKeySession::SetSessionId(const nsAString& aSessionId) {
  EME_LOG("MediaKeySession[%p,'%s'] session Id set", this,
          NS_ConvertUTF16toUTF8(aSessionId).get());

  if (NS_WARN_IF(!mSessionId.IsEmpty())) {
    return;
  }
  mSessionId = aSessionId;
  mKeys->OnSessionIdReady(this);
}

MediaKeySession::~MediaKeySession() {
  EME_LOG("MediaKeySession[%p,'%s'] dtor", this,
          NS_ConvertUTF16toUTF8(mSessionId).get());
}

MediaKeyError* MediaKeySession::GetError() const { return mMediaKeyError; }

void MediaKeySession::GetSessionId(nsString& aSessionId) const {
  aSessionId = GetSessionId();
}

const nsString& MediaKeySession::GetSessionId() const { return mSessionId; }

JSObject* MediaKeySession::WrapObject(JSContext* aCx,
                                      JS::Handle<JSObject*> aGivenProto) {
  return MediaKeySession_Binding::Wrap(aCx, this, aGivenProto);
}

double MediaKeySession::Expiration() const { return mExpiration; }

Promise* MediaKeySession::Closed() const { return mClosed; }

void MediaKeySession::UpdateKeyStatusMap() {
  MOZ_ASSERT(!IsClosed());
  if (!mKeys->GetCDMProxy()) {
    return;
  }

  nsTArray<CDMCaps::KeyStatus> keyStatuses;
  {
    auto caps = mKeys->GetCDMProxy()->Capabilites().Lock();
    caps->GetKeyStatusesForSession(mSessionId, keyStatuses);
  }

  mKeyStatusMap->Update(keyStatuses);

  if (EME_LOG_ENABLED()) {
    nsAutoCString message(
        nsPrintfCString("MediaKeySession[%p,'%s'] key statuses change {", this,
                        NS_ConvertUTF16toUTF8(mSessionId).get()));
    for (const CDMCaps::KeyStatus& status : keyStatuses) {
      message.AppendPrintf(" (%s,%s)", ToHexString(status.mId).get(),
                           GetEnumString(status.mStatus).get());
    }
    message.AppendLiteral(" }");
    // Use %s so we aren't exposing random strings to printf interpolation.
    EME_LOG("%s", message.get());
  }
}

MediaKeyStatusMap* MediaKeySession::KeyStatuses() const {
  return mKeyStatusMap;
}

// The user agent MUST thoroughly validate the Initialization Data before
// passing it to the CDM. This includes verifying that the length and
// values of fields are reasonable, verifying that values are within
// reasonable limits, and stripping irrelevant, unsupported, or unknown
// data or fields. It is RECOMMENDED that user agents pre-parse, sanitize,
// and/or generate a fully sanitized version of the Initialization Data.
// If the Initialization Data format specified by initDataType supports
// multiple entries, the user agent SHOULD remove entries that are not
// needed by the CDM. The user agent MUST NOT re-order entries within
// the Initialization Data.
static bool ValidateInitData(const nsTArray<uint8_t>& aInitData,
                             const nsAString& aInitDataType) {
  if (aInitDataType.LowerCaseEqualsLiteral("webm")) {
    // WebM initData consists of a single keyId. Ensure it's of reasonable
    // length.
    return aInitData.Length() <= MAX_KEY_ID_LENGTH;
  } else if (aInitDataType.LowerCaseEqualsLiteral("cenc")) {
    // Limit initData to less than 64KB.
    if (aInitData.Length() > MAX_CENC_INIT_DATA_LENGTH) {
      return false;
    }
    std::vector<std::vector<uint8_t>> keyIds;
    return ParseCENCInitData(aInitData.Elements(), aInitData.Length(), keyIds);
  } else if (aInitDataType.LowerCaseEqualsLiteral("keyids")) {
    if (aInitData.Length() > MAX_KEY_ID_LENGTH) {
      return false;
    }
    // Ensure that init data matches the expected JSON format.
    mozilla::dom::KeyIdsInitData keyIds;
    nsString json;
    nsDependentCSubstring raw(
        reinterpret_cast<const char*>(aInitData.Elements()),
        aInitData.Length());
    if (NS_FAILED(UTF_8_ENCODING->DecodeWithBOMRemoval(raw, json))) {
      return false;
    }
    if (!keyIds.Init(json)) {
      return false;
    }
    if (keyIds.mKids.Length() == 0) {
      return false;
    }
    for (const auto& kid : keyIds.mKids) {
      if (kid.IsEmpty()) {
        return false;
      }
    }
  }
  return true;
}

// Generates a license request based on the initData. A message of type
// "license-request" or "individualization-request" will always be queued
// if the algorithm succeeds and the promise is resolved.
already_AddRefed<Promise> MediaKeySession::GenerateRequest(
    const nsAString& aInitDataType,
    const ArrayBufferViewOrArrayBuffer& aInitData, ErrorResult& aRv) {
  RefPtr<DetailedPromise> promise(
      MakePromise(aRv, "MediaKeySession.generateRequest"_ns));
  if (aRv.Failed()) {
    return nullptr;
  }

  // If this object is closed, return a promise rejected with an
  // InvalidStateError.
  if (IsClosed()) {
    EME_LOG("MediaKeySession[%p,'%s'] GenerateRequest() failed, closed", this,
            NS_ConvertUTF16toUTF8(mSessionId).get());
    promise->MaybeRejectWithInvalidStateError(
        "Session is closed in MediaKeySession.generateRequest()");
    return promise.forget();
  }

  // If this object's uninitialized value is false, return a promise rejected
  // with an InvalidStateError.
  if (!mUninitialized) {
    EME_LOG("MediaKeySession[%p,'%s'] GenerateRequest() failed, uninitialized",
            this, NS_ConvertUTF16toUTF8(mSessionId).get());
    promise->MaybeRejectWithInvalidStateError(
        "Session is already initialized in MediaKeySession.generateRequest()");
    return promise.forget();
  }

  // Let this object's uninitialized value be false.
  mUninitialized = false;

  // If initDataType is the empty string, return a promise rejected
  // with a newly created TypeError.
  if (aInitDataType.IsEmpty()) {
    promise->MaybeRejectWithTypeError(
        "Empty initDataType passed to MediaKeySession.generateRequest()");
    EME_LOG(
        "MediaKeySession[%p,'%s'] GenerateRequest() failed, empty initDataType",
        this, NS_ConvertUTF16toUTF8(mSessionId).get());
    return promise.forget();
  }

  // If initData is an empty array, return a promise rejected with
  // a newly created TypeError.
  nsTArray<uint8_t> data;
  CopyArrayBufferViewOrArrayBufferData(aInitData, data);
  if (data.IsEmpty()) {
    promise->MaybeRejectWithTypeError(
        "Empty initData passed to MediaKeySession.generateRequest()");
    EME_LOG("MediaKeySession[%p,'%s'] GenerateRequest() failed, empty initData",
            this, NS_ConvertUTF16toUTF8(mSessionId).get());
    return promise.forget();
  }

  // If the Key System implementation represented by this object's
  // cdm implementation value does not support initDataType as an
  // Initialization Data Type, return a promise rejected with a
  // NotSupportedError. String comparison is case-sensitive.
  if (!MediaKeySystemAccess::KeySystemSupportsInitDataType(
          mKeySystem, aInitDataType, mHardwareDecryption)) {
    promise->MaybeRejectWithNotSupportedError(
        "Unsupported initDataType passed to MediaKeySession.generateRequest()");
    EME_LOG(
        "MediaKeySession[%p,'%s'] GenerateRequest() failed, unsupported "
        "initDataType",
        this, NS_ConvertUTF16toUTF8(mSessionId).get());
    return promise.forget();
  }

  // Let init data be a copy of the contents of the initData parameter.
  // Note: Handled by the CopyArrayBufferViewOrArrayBufferData call above.

  // Let session type be this object's session type.

  // Let promise be a new promise.

  // Run the following steps in parallel:

  // If the init data is not valid for initDataType, reject promise with
  // a newly created TypeError.
  if (!ValidateInitData(data, aInitDataType)) {
    // If the preceding step failed, reject promise with a newly created
    // TypeError.
    promise->MaybeRejectWithTypeError(
        "initData sanitization failed in MediaKeySession.generateRequest()");
    EME_LOG(
        "MediaKeySession[%p,'%s'] GenerateRequest() initData sanitization "
        "failed",
        this, NS_ConvertUTF16toUTF8(mSessionId).get());
    return promise.forget();
  }

  // Let sanitized init data be a validated and sanitized version of init data.

  // If sanitized init data is empty, reject promise with a NotSupportedError.

  // Note: Remaining steps of generateRequest method continue in CDM.

  // Convert initData to hex for easier logging.
  // Note: CreateSession() std::move()s the data out of the array, so we have
  // to copy it here.
  nsAutoCString hexInitData(ToHexString(data));
  PromiseId pid = mKeys->StorePromise(promise);
  mKeys->ConnectPendingPromiseIdWithToken(pid, Token());
  mKeys->GetCDMProxy()->CreateSession(Token(), mSessionType, pid, aInitDataType,
                                      data);

  EME_LOG(
      "MediaKeySession[%p,'%s'] GenerateRequest() sent, "
      "promiseId=%d initData='%s' initDataType='%s'",
      this, NS_ConvertUTF16toUTF8(mSessionId).get(), pid, hexInitData.get(),
      NS_ConvertUTF16toUTF8(aInitDataType).get());

  return promise.forget();
}

already_AddRefed<Promise> MediaKeySession::Load(const nsAString& aSessionId,
                                                ErrorResult& aRv) {
  RefPtr<DetailedPromise> promise(MakePromise(aRv, "MediaKeySession.load"_ns));
  if (aRv.Failed()) {
    return nullptr;
  }

  // 1. If this object is closed, return a promise rejected with an
  // InvalidStateError.
  if (IsClosed()) {
    promise->MaybeRejectWithInvalidStateError(
        "Session is closed in MediaKeySession.load()");
    EME_LOG("MediaKeySession[%p,'%s'] Load() failed, closed", this,
            NS_ConvertUTF16toUTF8(aSessionId).get());
    return promise.forget();
  }

  // 2.If this object's uninitialized value is false, return a promise rejected
  // with an InvalidStateError.
  if (!mUninitialized) {
    promise->MaybeRejectWithInvalidStateError(
        "Session is already initialized in MediaKeySession.load()");
    EME_LOG("MediaKeySession[%p,'%s'] Load() failed, uninitialized", this,
            NS_ConvertUTF16toUTF8(aSessionId).get());
    return promise.forget();
  }

  // 3.Let this object's uninitialized value be false.
  mUninitialized = false;

  // 4. If sessionId is the empty string, return a promise rejected with a newly
  // created TypeError.
  if (aSessionId.IsEmpty()) {
    promise->MaybeRejectWithTypeError(
        "Trying to load a session with empty session ID");
    // "The sessionId parameter is empty."
    EME_LOG("MediaKeySession[%p,''] Load() failed, no sessionId", this);
    return promise.forget();
  }

  // 5. If the result of running the Is persistent session type? algorithm
  // on this object's session type is false, return a promise rejected with
  // a newly created TypeError.
  if (mSessionType == MediaKeySessionType::Temporary) {
    promise->MaybeRejectWithTypeError(
        "Trying to load() into a non-persistent session");
    EME_LOG(
        "MediaKeySession[%p,''] Load() failed, can't load in a non-persistent "
        "session",
        this);
    return promise.forget();
  }

  // Note: We don't support persistent sessions in any keysystem, so all calls
  // to Load() should reject with a TypeError in the preceding check. Omitting
  // implementing the rest of the specified MediaKeySession::Load() algorithm.

  // We now know the sessionId being loaded into this session. Remove the
  // session from its owning MediaKey's set of sessions awaiting a sessionId.
  RefPtr<MediaKeySession> session(mKeys->GetPendingSession(Token()));
  MOZ_ASSERT(session == this, "Session should be awaiting id on its own token");

  // Associate with the known sessionId.
  SetSessionId(aSessionId);

  PromiseId pid = mKeys->StorePromise(promise);
  mKeys->GetCDMProxy()->LoadSession(pid, mSessionType, aSessionId);

  EME_LOG("MediaKeySession[%p,'%s'] Load() sent to CDM, promiseId=%d", this,
          NS_ConvertUTF16toUTF8(mSessionId).get(), pid);

  return promise.forget();
}

already_AddRefed<Promise> MediaKeySession::Update(
    const ArrayBufferViewOrArrayBuffer& aResponse, ErrorResult& aRv) {
  RefPtr<DetailedPromise> promise(
      MakePromise(aRv, "MediaKeySession.update"_ns));
  if (aRv.Failed()) {
    return nullptr;
  }

  if (!IsCallable()) {
    // If this object's callable value is false, return a promise rejected
    // with a new DOMException whose name is InvalidStateError.
    EME_LOG(
        "MediaKeySession[%p,''] Update() called before sessionId set by CDM",
        this);
    promise->MaybeRejectWithInvalidStateError(
        "MediaKeySession.Update() called before sessionId set by CDM");
    return promise.forget();
  }

  nsTArray<uint8_t> data;
  if (IsClosed() || !mKeys->GetCDMProxy()) {
    promise->MaybeRejectWithInvalidStateError(
        "Session is closed or was not properly initialized");
    EME_LOG(
        "MediaKeySession[%p,'%s'] Update() failed, session is closed or was "
        "not properly initialised.",
        this, NS_ConvertUTF16toUTF8(mSessionId).get());
    return promise.forget();
  }
  CopyArrayBufferViewOrArrayBufferData(aResponse, data);
  if (data.IsEmpty()) {
    promise->MaybeRejectWithTypeError(
        "Empty response buffer passed to MediaKeySession.update()");
    EME_LOG("MediaKeySession[%p,'%s'] Update() failed, empty response buffer",
            this, NS_ConvertUTF16toUTF8(mSessionId).get());
    return promise.forget();
  }

  // Convert response to hex for easier logging.
  // Note: UpdateSession() std::move()s the data out of the array, so we have
  // to copy it here.
  nsAutoCString hexResponse(ToHexString(data));

  PromiseId pid = mKeys->StorePromise(promise);
  mKeys->GetCDMProxy()->UpdateSession(mSessionId, pid, data);

  EME_LOG(
      "MediaKeySession[%p,'%s'] Update() sent to CDM, "
      "promiseId=%d Response='%s'",
      this, NS_ConvertUTF16toUTF8(mSessionId).get(), pid, hexResponse.get());

  return promise.forget();
}

already_AddRefed<Promise> MediaKeySession::Close(ErrorResult& aRv) {
  RefPtr<DetailedPromise> promise(MakePromise(aRv, "MediaKeySession.close"_ns));
  if (aRv.Failed()) {
    return nullptr;
  }
  // 1. Let session be the associated MediaKeySession object.
  // 2. If session is closed, return a resolved promise.
  if (IsClosed()) {
    EME_LOG("MediaKeySession[%p,'%s'] Close() already closed", this,
            NS_ConvertUTF16toUTF8(mSessionId).get());
    promise->MaybeResolveWithUndefined();
    return promise.forget();
  }
  // 3. If session's callable value is false, return a promise rejected
  // with an InvalidStateError.
  if (!IsCallable()) {
    EME_LOG("MediaKeySession[%p,''] Close() called before sessionId set by CDM",
            this);
    promise->MaybeRejectWithInvalidStateError(
        "MediaKeySession.Close() called before sessionId set by CDM");
    return promise.forget();
  }
  if (!mKeys->GetCDMProxy()) {
    EME_LOG("MediaKeySession[%p,'%s'] Close() null CDMProxy", this,
            NS_ConvertUTF16toUTF8(mSessionId).get());
    promise->MaybeRejectWithInvalidStateError(
        "MediaKeySession.Close() lost reference to CDM");
    return promise.forget();
  }
  // 4. Let promise be a new promise.
  PromiseId pid = mKeys->StorePromise(promise);
  // 5. Run the following steps in parallel:
  // 5.1 Let cdm be the CDM instance represented by session's cdm instance
  // value. 5.2 Use cdm to close the session associated with session.
  mKeys->GetCDMProxy()->CloseSession(mSessionId, pid);

  EME_LOG("MediaKeySession[%p,'%s'] Close() sent to CDM, promiseId=%d", this,
          NS_ConvertUTF16toUTF8(mSessionId).get(), pid);

  // Session Closed algorithm is run when CDM causes us to run
  // OnSessionClosed().

  // 6. Return promise.
  return promise.forget();
}

void MediaKeySession::OnClosed() {
  if (IsClosed()) {
    return;
  }
  EME_LOG("MediaKeySession[%p,'%s'] session close operation complete.", this,
          NS_ConvertUTF16toUTF8(mSessionId).get());
  mIsClosed = true;
  mKeys->OnSessionClosed(this);
  mKeys = nullptr;
  mClosed->MaybeResolveWithUndefined();
}

bool MediaKeySession::IsClosed() const { return mIsClosed; }

already_AddRefed<Promise> MediaKeySession::Remove(ErrorResult& aRv) {
  RefPtr<DetailedPromise> promise(
      MakePromise(aRv, "MediaKeySession.remove"_ns));
  if (aRv.Failed()) {
    return nullptr;
  }
  if (!IsCallable()) {
    // If this object's callable value is false, return a promise rejected
    // with a new DOMException whose name is InvalidStateError.
    EME_LOG(
        "MediaKeySession[%p,''] Remove() called before sessionId set by CDM",
        this);
    promise->MaybeRejectWithInvalidStateError(
        "MediaKeySession.Remove() called before sessionId set by CDM");
    return promise.forget();
  }
  if (mSessionType != MediaKeySessionType::Persistent_license) {
    promise->MaybeRejectWithInvalidAccessError(
        "Calling MediaKeySession.remove() on non-persistent session");
    // "The operation is not supported on session type sessions."
    EME_LOG("MediaKeySession[%p,'%s'] Remove() failed, sesion not persisrtent.",
            this, NS_ConvertUTF16toUTF8(mSessionId).get());
    return promise.forget();
  }
  if (IsClosed() || !mKeys->GetCDMProxy()) {
    promise->MaybeRejectWithInvalidStateError(
        "MediaKeySession.remove() called but session is not active");
    // "The session is closed."
    EME_LOG("MediaKeySession[%p,'%s'] Remove() failed, already session closed.",
            this, NS_ConvertUTF16toUTF8(mSessionId).get());
    return promise.forget();
  }
  PromiseId pid = mKeys->StorePromise(promise);
  mKeys->GetCDMProxy()->RemoveSession(mSessionId, pid);
  EME_LOG("MediaKeySession[%p,'%s'] Remove() sent to CDM, promiseId=%d.", this,
          NS_ConvertUTF16toUTF8(mSessionId).get(), pid);

  return promise.forget();
}

void MediaKeySession::DispatchKeyMessage(MediaKeyMessageType aMessageType,
                                         const nsTArray<uint8_t>& aMessage) {
  if (EME_LOG_ENABLED()) {
    EME_LOG(
        "MediaKeySession[%p,'%s'] DispatchKeyMessage() type=%s message='%s'",
        this, NS_ConvertUTF16toUTF8(mSessionId).get(),
        GetEnumString(aMessageType).get(), ToHexString(aMessage).get());
  }

  RefPtr<MediaKeyMessageEvent> event(
      MediaKeyMessageEvent::Constructor(this, aMessageType, aMessage));
  RefPtr<AsyncEventDispatcher> asyncDispatcher =
      new AsyncEventDispatcher(this, event.forget());
  asyncDispatcher->PostDOMEvent();
}

void MediaKeySession::DispatchKeyError(uint32_t aSystemCode) {
  EME_LOG("MediaKeySession[%p,'%s'] DispatchKeyError() systemCode=%u.", this,
          NS_ConvertUTF16toUTF8(mSessionId).get(), aSystemCode);

  auto event = MakeRefPtr<MediaKeyError>(this, aSystemCode);
  RefPtr<AsyncEventDispatcher> asyncDispatcher =
      new AsyncEventDispatcher(this, event.forget());
  asyncDispatcher->PostDOMEvent();
}

void MediaKeySession::DispatchKeyStatusesChange() {
  if (IsClosed()) {
    return;
  }

  UpdateKeyStatusMap();

  RefPtr<AsyncEventDispatcher> asyncDispatcher =
      new AsyncEventDispatcher(this, u"keystatuseschange"_ns, CanBubble::eNo);
  asyncDispatcher->PostDOMEvent();
}

uint32_t MediaKeySession::Token() const { return mToken; }

already_AddRefed<DetailedPromise> MediaKeySession::MakePromise(
    ErrorResult& aRv, const nsACString& aName) {
  nsCOMPtr<nsIGlobalObject> global = GetParentObject();
  if (!global) {
    NS_WARNING("Passed non-global to MediaKeys ctor!");
    aRv.Throw(NS_ERROR_UNEXPECTED);
    return nullptr;
  }
  return DetailedPromise::Create(global, aRv, aName);
}

void MediaKeySession::SetExpiration(double aExpiration) {
  EME_LOG("MediaKeySession[%p,'%s'] SetExpiry(%.12lf) (%.2lf hours from now)",
          this, NS_ConvertUTF16toUTF8(mSessionId).get(), aExpiration,
          (aExpiration - 1000.0 * double(time(0))) / (1000.0 * 60 * 60));
  mExpiration = aExpiration;
}

EventHandlerNonNull* MediaKeySession::GetOnkeystatuseschange() {
  return GetEventHandler(nsGkAtoms::onkeystatuseschange);
}

void MediaKeySession::SetOnkeystatuseschange(EventHandlerNonNull* aCallback) {
  SetEventHandler(nsGkAtoms::onkeystatuseschange, aCallback);
}

EventHandlerNonNull* MediaKeySession::GetOnmessage() {
  return GetEventHandler(nsGkAtoms::onmessage);
}

void MediaKeySession::SetOnmessage(EventHandlerNonNull* aCallback) {
  SetEventHandler(nsGkAtoms::onmessage, aCallback);
}

nsString ToString(MediaKeySessionType aType) {
  return NS_ConvertUTF8toUTF16(GetEnumString(aType));
}

}  // namespace mozilla::dom