summaryrefslogtreecommitdiffstats
path: root/dom/media/eme/MediaEncryptedEvent.cpp
blob: bef98bcd5549c6b5d903b8b27e458abca19513b6 (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
/* -*- 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 "MediaEncryptedEvent.h"
#include "mozilla/dom/MediaEncryptedEventBinding.h"
#include "nsContentUtils.h"
#include "js/ArrayBuffer.h"
#include "jsfriendapi.h"
#include "nsINode.h"
#include "mozilla/dom/MediaKeys.h"
#include "mozilla/HoldDropJSObjects.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_CLASS(MediaEncryptedEvent)

NS_IMPL_ADDREF_INHERITED(MediaEncryptedEvent, Event)
NS_IMPL_RELEASE_INHERITED(MediaEncryptedEvent, Event)

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MediaEncryptedEvent, Event)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MediaEncryptedEvent, Event)
  NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mInitData)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MediaEncryptedEvent, Event)
  mozilla::DropJSObjects(tmp);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaEncryptedEvent)
NS_INTERFACE_MAP_END_INHERITING(Event)

MediaEncryptedEvent::MediaEncryptedEvent(EventTarget* aOwner)
    : Event(aOwner, nullptr, nullptr) {
  mozilla::HoldJSObjects(this);
}

MediaEncryptedEvent::~MediaEncryptedEvent() { mozilla::DropJSObjects(this); }

JSObject* MediaEncryptedEvent::WrapObjectInternal(
    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return MediaEncryptedEvent_Binding::Wrap(aCx, this, aGivenProto);
}

already_AddRefed<MediaEncryptedEvent> MediaEncryptedEvent::Constructor(
    EventTarget* aOwner) {
  RefPtr<MediaEncryptedEvent> e = new MediaEncryptedEvent(aOwner);
  e->InitEvent(u"encrypted"_ns, CanBubble::eNo, Cancelable::eNo);
  e->SetTrusted(true);
  return e.forget();
}

already_AddRefed<MediaEncryptedEvent> MediaEncryptedEvent::Constructor(
    EventTarget* aOwner, const nsAString& aInitDataType,
    const nsTArray<uint8_t>& aInitData) {
  RefPtr<MediaEncryptedEvent> e = new MediaEncryptedEvent(aOwner);
  e->InitEvent(u"encrypted"_ns, CanBubble::eNo, Cancelable::eNo);
  e->mInitDataType = aInitDataType;
  e->mRawInitData = aInitData.Clone();
  e->SetTrusted(true);
  return e.forget();
}

already_AddRefed<MediaEncryptedEvent> MediaEncryptedEvent::Constructor(
    const GlobalObject& aGlobal, const nsAString& aType,
    const MediaKeyNeededEventInit& aEventInitDict, ErrorResult& aRv) {
  nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
  RefPtr<MediaEncryptedEvent> e = new MediaEncryptedEvent(owner);
  bool trusted = e->Init(owner);
  e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
  e->mInitDataType = aEventInitDict.mInitDataType;
  if (!aEventInitDict.mInitData.IsNull()) {
    JS::Rooted<JSObject*> buffer(aGlobal.Context(),
                                 aEventInitDict.mInitData.Value().Obj());
    e->mInitData = JS::CopyArrayBuffer(aGlobal.Context(), buffer);
    if (!e->mInitData) {
      aRv.NoteJSContextException(aGlobal.Context());
      return nullptr;
    }
  }
  e->SetTrusted(trusted);
  return e.forget();
}

void MediaEncryptedEvent::GetInitDataType(nsString& aRetVal) const {
  aRetVal = mInitDataType;
}

void MediaEncryptedEvent::GetInitData(JSContext* cx,
                                      JS::MutableHandle<JSObject*> aData,
                                      ErrorResult& aRv) {
  if (mRawInitData.Length()) {
    mInitData = ArrayBuffer::Create(cx, this, mRawInitData, aRv);
    if (aRv.Failed()) {
      return;
    }
    mRawInitData.Clear();
  }
  aData.set(mInitData);
}

}  // namespace mozilla::dom