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/MIDIInput.h"
8 #include "mozilla/dom/Document.h"
9 #include "mozilla/dom/MIDIPortChild.h"
10 #include "mozilla/dom/MIDIInputBinding.h"
11 #include "mozilla/dom/MIDIMessageEvent.h"
12 #include "mozilla/dom/MIDIMessageEventBinding.h"
13 #include "nsDOMNavigationTiming.h"
14 
15 namespace mozilla::dom {
16 
MIDIInput(nsPIDOMWindowInner * aWindow,MIDIAccess * aMIDIAccessParent)17 MIDIInput::MIDIInput(nsPIDOMWindowInner* aWindow, MIDIAccess* aMIDIAccessParent)
18     : MIDIPort(aWindow, aMIDIAccessParent) {}
19 
20 // static
Create(nsPIDOMWindowInner * aWindow,MIDIAccess * aMIDIAccessParent,const MIDIPortInfo & aPortInfo,const bool aSysexEnabled)21 MIDIInput* MIDIInput::Create(nsPIDOMWindowInner* aWindow,
22                              MIDIAccess* aMIDIAccessParent,
23                              const MIDIPortInfo& aPortInfo,
24                              const bool aSysexEnabled) {
25   MOZ_ASSERT(static_cast<MIDIPortType>(aPortInfo.type()) ==
26              MIDIPortType::Input);
27   auto port = new MIDIInput(aWindow, aMIDIAccessParent);
28   if (!port->Initialize(aPortInfo, aSysexEnabled)) {
29     return nullptr;
30   }
31   return port;
32 }
33 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)34 JSObject* MIDIInput::WrapObject(JSContext* aCx,
35                                 JS::Handle<JSObject*> aGivenProto) {
36   return MIDIInput_Binding::Wrap(aCx, this, aGivenProto);
37 }
38 
Receive(const nsTArray<MIDIMessage> & aMsgs)39 void MIDIInput::Receive(const nsTArray<MIDIMessage>& aMsgs) {
40   nsCOMPtr<Document> doc = GetOwner() ? GetOwner()->GetDoc() : nullptr;
41   if (!doc) {
42     NS_WARNING("No document available to send MIDIMessageEvent to!");
43     return;
44   }
45   for (auto& msg : aMsgs) {
46     RefPtr<MIDIMessageEvent> event(
47         MIDIMessageEvent::Constructor(this, msg.timestamp(), msg.data()));
48     DispatchTrustedEvent(event);
49   }
50 }
51 
GetOnmidimessage()52 EventHandlerNonNull* MIDIInput::GetOnmidimessage() {
53   return GetEventHandler(nsGkAtoms::onmidimessage);
54 }
55 
SetOnmidimessage(EventHandlerNonNull * aCallback)56 void MIDIInput::SetOnmidimessage(EventHandlerNonNull* aCallback) {
57   SetEventHandler(nsGkAtoms::onmidimessage, aCallback);
58   if (mPort->ConnectionState() != MIDIPortConnectionState::Open) {
59     mPort->SendOpen();
60   }
61 }
62 
63 }  // namespace mozilla::dom
64