1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/EventBinding.h"
8 #include "mozilla/dom/MIDIMessageEvent.h"
9 #include "mozilla/dom/MIDIMessageEventBinding.h"
11 #include "jsfriendapi.h"
12 #include "mozilla/FloatingPoint.h"
13 #include "mozilla/HoldDropJSObjects.h"
14 #include "mozilla/dom/Nullable.h"
15 #include "mozilla/dom/PrimitiveConversions.h"
16 #include "mozilla/dom/TypedArray.h"
17 #include "mozilla/dom/Performance.h"
19 namespace mozilla::dom
{
21 NS_IMPL_CYCLE_COLLECTION_CLASS(MIDIMessageEvent
)
23 NS_IMPL_ADDREF_INHERITED(MIDIMessageEvent
, Event
)
24 NS_IMPL_RELEASE_INHERITED(MIDIMessageEvent
, Event
)
26 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MIDIMessageEvent
, Event
)
27 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
29 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MIDIMessageEvent
, Event
)
30 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData
)
31 NS_IMPL_CYCLE_COLLECTION_TRACE_END
33 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MIDIMessageEvent
, Event
)
35 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
37 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MIDIMessageEvent
)
38 NS_INTERFACE_MAP_END_INHERITING(Event
)
40 MIDIMessageEvent::MIDIMessageEvent(mozilla::dom::EventTarget
* aOwner
)
41 : Event(aOwner
, nullptr, nullptr) {
42 mozilla::HoldJSObjects(this);
45 MIDIMessageEvent::~MIDIMessageEvent() { mozilla::DropJSObjects(this); }
47 JSObject
* MIDIMessageEvent::WrapObjectInternal(
48 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
49 return MIDIMessageEvent_Binding::Wrap(aCx
, this, aGivenProto
);
52 MIDIMessageEvent
* MIDIMessageEvent::AsMIDIMessageEvent() { return this; }
54 already_AddRefed
<MIDIMessageEvent
> MIDIMessageEvent::Constructor(
55 EventTarget
* aOwner
, const class TimeStamp
& aReceivedTime
,
56 const nsTArray
<uint8_t>& aData
) {
58 RefPtr
<MIDIMessageEvent
> e
= new MIDIMessageEvent(aOwner
);
59 e
->InitEvent(u
"midimessage"_ns
, false, false);
60 e
->mEvent
->mTimeStamp
= aReceivedTime
;
61 e
->mRawData
= aData
.Clone();
66 already_AddRefed
<MIDIMessageEvent
> MIDIMessageEvent::Constructor(
67 const GlobalObject
& aGlobal
, const nsAString
& aType
,
68 const MIDIMessageEventInit
& aEventInitDict
, ErrorResult
& aRv
) {
69 nsCOMPtr
<EventTarget
> owner
= do_QueryInterface(aGlobal
.GetAsSupports());
70 RefPtr
<MIDIMessageEvent
> e
= new MIDIMessageEvent(owner
);
71 bool trusted
= e
->Init(owner
);
72 e
->InitEvent(aType
, aEventInitDict
.mBubbles
, aEventInitDict
.mCancelable
);
73 // Set data for event. Timestamp will always be set to Now() (default for
74 // event) using this constructor.
75 const auto& a
= aEventInitDict
.mData
.Value();
77 e
->mData
= Uint8Array::Create(aGlobal
.Context(), owner
, a
.Length(), a
.Data());
78 if (NS_WARN_IF(!e
->mData
)) {
79 aRv
.Throw(NS_ERROR_OUT_OF_MEMORY
);
83 e
->SetTrusted(trusted
);
84 mozilla::HoldJSObjects(e
.get());
88 void MIDIMessageEvent::GetData(JSContext
* cx
,
89 JS::MutableHandle
<JSObject
*> aData
,
93 Uint8Array::Create(cx
, this, mRawData
.Length(), mRawData
.Elements());
95 aRv
.Throw(NS_ERROR_OUT_OF_MEMORY
);
103 } // namespace mozilla::dom