tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MIDIMessageEvent.cpp (3351B)


      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/. */
      6 
      7 #include "mozilla/dom/MIDIMessageEvent.h"
      8 
      9 #include "js/GCAPI.h"
     10 #include "js/Realm.h"
     11 #include "jsfriendapi.h"
     12 #include "mozilla/HoldDropJSObjects.h"
     13 #include "mozilla/dom/EventBinding.h"
     14 #include "mozilla/dom/MIDIMessageEventBinding.h"
     15 #include "mozilla/dom/Nullable.h"
     16 #include "mozilla/dom/Performance.h"
     17 #include "mozilla/dom/PrimitiveConversions.h"
     18 #include "mozilla/dom/TypedArray.h"
     19 
     20 namespace mozilla::dom {
     21 
     22 NS_IMPL_CYCLE_COLLECTION_INHERITED_WITH_JS_MEMBERS(MIDIMessageEvent, Event, (),
     23                                                   (mData))
     24 
     25 NS_IMPL_ADDREF_INHERITED(MIDIMessageEvent, Event)
     26 NS_IMPL_RELEASE_INHERITED(MIDIMessageEvent, Event)
     27 
     28 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MIDIMessageEvent)
     29 NS_INTERFACE_MAP_END_INHERITING(Event)
     30 
     31 MIDIMessageEvent::MIDIMessageEvent(mozilla::dom::EventTarget* aOwner)
     32    : Event(aOwner, nullptr, nullptr) {
     33  mozilla::HoldJSObjects(this);
     34 }
     35 
     36 MIDIMessageEvent::~MIDIMessageEvent() { mozilla::DropJSObjects(this); }
     37 
     38 JSObject* MIDIMessageEvent::WrapObjectInternal(
     39    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
     40  return MIDIMessageEvent_Binding::Wrap(aCx, this, aGivenProto);
     41 }
     42 
     43 MIDIMessageEvent* MIDIMessageEvent::AsMIDIMessageEvent() { return this; }
     44 
     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();
     55 }
     56 
     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;
     74    }
     75  }
     76 
     77  e->SetTrusted(trusted);
     78  mozilla::HoldJSObjects(e.get());
     79  return e.forget();
     80 }
     81 
     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;
     89    }
     90    mRawData.Clear();
     91  }
     92  aData.set(mData);
     93 }
     94 
     95 }  // namespace mozilla::dom