MediaKeyMessageEvent.cpp (3683B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/MediaKeyMessageEvent.h" 8 9 #include "js/ArrayBuffer.h" 10 #include "js/RootingAPI.h" 11 #include "jsfriendapi.h" 12 #include "mozilla/HoldDropJSObjects.h" 13 #include "mozilla/dom/MediaKeyMessageEventBinding.h" 14 #include "mozilla/dom/MediaKeys.h" 15 #include "mozilla/dom/Nullable.h" 16 #include "mozilla/dom/PrimitiveConversions.h" 17 #include "mozilla/dom/TypedArray.h" 18 #include "nsContentUtils.h" 19 20 namespace mozilla::dom { 21 22 NS_IMPL_CYCLE_COLLECTION_CLASS(MediaKeyMessageEvent) 23 24 NS_IMPL_ADDREF_INHERITED(MediaKeyMessageEvent, Event) 25 NS_IMPL_RELEASE_INHERITED(MediaKeyMessageEvent, Event) 26 27 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MediaKeyMessageEvent, Event) 28 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END 29 30 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MediaKeyMessageEvent, Event) 31 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mMessage) 32 NS_IMPL_CYCLE_COLLECTION_TRACE_END 33 34 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MediaKeyMessageEvent, Event) 35 mozilla::DropJSObjects(tmp); 36 NS_IMPL_CYCLE_COLLECTION_UNLINK_END 37 38 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaKeyMessageEvent) 39 NS_INTERFACE_MAP_END_INHERITING(Event) 40 41 MediaKeyMessageEvent::MediaKeyMessageEvent(EventTarget* aOwner) 42 : Event(aOwner, nullptr, nullptr), 43 mMessageType(static_cast<MediaKeyMessageType>(0)) { 44 mozilla::HoldJSObjects(this); 45 } 46 47 MediaKeyMessageEvent::~MediaKeyMessageEvent() { mozilla::DropJSObjects(this); } 48 49 MediaKeyMessageEvent* MediaKeyMessageEvent::AsMediaKeyMessageEvent() { 50 return this; 51 } 52 53 JSObject* MediaKeyMessageEvent::WrapObjectInternal( 54 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { 55 return MediaKeyMessageEvent_Binding::Wrap(aCx, this, aGivenProto); 56 } 57 58 already_AddRefed<MediaKeyMessageEvent> MediaKeyMessageEvent::Constructor( 59 EventTarget* aOwner, MediaKeyMessageType aMessageType, 60 const nsTArray<uint8_t>& aMessage) { 61 RefPtr<MediaKeyMessageEvent> e = new MediaKeyMessageEvent(aOwner); 62 e->InitEvent(u"message"_ns, false, false); 63 e->mMessageType = aMessageType; 64 e->mRawMessage = aMessage.Clone(); 65 e->SetTrusted(true); 66 return e.forget(); 67 } 68 69 already_AddRefed<MediaKeyMessageEvent> MediaKeyMessageEvent::Constructor( 70 const GlobalObject& aGlobal, const nsAString& aType, 71 const MediaKeyMessageEventInit& aEventInitDict, ErrorResult& aRv) { 72 nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports()); 73 RefPtr<MediaKeyMessageEvent> e = new MediaKeyMessageEvent(owner); 74 bool trusted = e->Init(owner); 75 e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable); 76 JS::Rooted<JSObject*> buffer(aGlobal.Context(), 77 aEventInitDict.mMessage.Obj()); 78 e->mMessage = JS::CopyArrayBuffer(aGlobal.Context(), buffer); 79 if (!e->mMessage) { 80 aRv.NoteJSContextException(aGlobal.Context()); 81 return nullptr; 82 } 83 e->mMessageType = aEventInitDict.mMessageType; 84 e->SetTrusted(trusted); 85 e->SetComposed(aEventInitDict.mComposed); 86 return e.forget(); 87 } 88 89 void MediaKeyMessageEvent::GetMessage(JSContext* cx, 90 JS::MutableHandle<JSObject*> aMessage, 91 ErrorResult& aRv) { 92 if (!mMessage) { 93 mMessage = ArrayBuffer::Create(cx, this, mRawMessage, aRv); 94 if (aRv.Failed()) { 95 return; 96 } 97 mRawMessage.Clear(); 98 } 99 aMessage.set(mMessage); 100 } 101 102 } // namespace mozilla::dom