Bug 1946184 - Fix computing the CSD margin right after calling HideWindowChrome(...
[gecko.git] / dom / midi / MIDIMessageEvent.cpp
blob793cb4eeca0547e61c125d1b6fb3a01ee58b1d97
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 "js/Realm.h"
8 #include "mozilla/dom/EventBinding.h"
9 #include "mozilla/dom/MIDIMessageEvent.h"
10 #include "mozilla/dom/MIDIMessageEventBinding.h"
11 #include "js/GCAPI.h"
12 #include "jsfriendapi.h"
13 #include "mozilla/FloatingPoint.h"
14 #include "mozilla/HoldDropJSObjects.h"
15 #include "mozilla/dom/Nullable.h"
16 #include "mozilla/dom/PrimitiveConversions.h"
17 #include "mozilla/dom/TypedArray.h"
18 #include "mozilla/dom/Performance.h"
20 namespace mozilla::dom {
22 NS_IMPL_CYCLE_COLLECTION_INHERITED_WITH_JS_MEMBERS(MIDIMessageEvent, Event, (),
23 (mData))
25 NS_IMPL_ADDREF_INHERITED(MIDIMessageEvent, Event)
26 NS_IMPL_RELEASE_INHERITED(MIDIMessageEvent, Event)
28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MIDIMessageEvent)
29 NS_INTERFACE_MAP_END_INHERITING(Event)
31 MIDIMessageEvent::MIDIMessageEvent(mozilla::dom::EventTarget* aOwner)
32 : Event(aOwner, nullptr, nullptr) {
33 mozilla::HoldJSObjects(this);
36 MIDIMessageEvent::~MIDIMessageEvent() { mozilla::DropJSObjects(this); }
38 JSObject* MIDIMessageEvent::WrapObjectInternal(
39 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
40 return MIDIMessageEvent_Binding::Wrap(aCx, this, aGivenProto);
43 MIDIMessageEvent* MIDIMessageEvent::AsMIDIMessageEvent() { return this; }
45 already_AddRefed<MIDIMessageEvent> MIDIMessageEvent::Constructor(
46 EventTarget* aOwner, const class TimeStamp& aReceivedTime,
47 const nsTArray<uint8_t>& aData) {
48 MOZ_ASSERT(aOwner);
49 RefPtr<MIDIMessageEvent> e = new MIDIMessageEvent(aOwner);
50 e->InitEvent(u"midimessage"_ns, false, false);
51 e->mEvent->mTimeStamp = aReceivedTime;
52 e->mRawData = aData.Clone();
53 e->SetTrusted(true);
54 return e.forget();
57 already_AddRefed<MIDIMessageEvent> MIDIMessageEvent::Constructor(
58 const GlobalObject& aGlobal, const nsAString& aType,
59 const MIDIMessageEventInit& aEventInitDict, ErrorResult& aRv) {
60 nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
61 RefPtr<MIDIMessageEvent> e = new MIDIMessageEvent(owner);
62 bool trusted = e->Init(owner);
63 e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
64 // Set data for event. Timestamp will always be set to Now() (default for
65 // event) using this constructor.
66 if (aEventInitDict.mData.WasPassed()) {
67 JSAutoRealm ar(aGlobal.Context(), aGlobal.Get());
68 JS::Rooted<JSObject*> data(aGlobal.Context(),
69 aEventInitDict.mData.Value().Obj());
70 e->mData = JS_NewUint8ArrayFromArray(aGlobal.Context(), data);
71 if (NS_WARN_IF(!e->mData)) {
72 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
73 return nullptr;
77 e->SetTrusted(trusted);
78 mozilla::HoldJSObjects(e.get());
79 return e.forget();
82 void MIDIMessageEvent::GetData(JSContext* cx,
83 JS::MutableHandle<JSObject*> aData,
84 ErrorResult& aRv) {
85 if (!mData) {
86 mData = Uint8Array::Create(cx, this, mRawData, aRv);
87 if (aRv.Failed()) {
88 return;
90 mRawData.Clear();
92 aData.set(mData);
95 } // namespace mozilla::dom