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 #ifndef mozilla_dom_MIDIAccess_h
8 #define mozilla_dom_MIDIAccess_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/DOMEventTargetHelper.h"
12 #include "mozilla/Observer.h"
13 #include "mozilla/WeakPtr.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsWrapperCache.h"
24 class MIDIAccessManager
;
28 class MIDIPermissionRequest
;
30 class MIDIPortChangeEvent
;
36 * MIDIAccess is the DOM object that is handed to the user upon MIDI permissions
37 * being successfully granted. It manages access to MIDI ports, and fires events
38 * for device connection and disconnection.
40 * New MIDIAccess objects are created every time RequestMIDIAccess is called.
41 * MIDIAccess objects are managed via MIDIAccessManager.
43 class MIDIAccess final
: public DOMEventTargetHelper
,
44 public Observer
<MIDIPortList
> {
45 // Use the Permission Request class in MIDIAccessManager for creating
46 // MIDIAccess objects.
47 friend class MIDIPermissionRequest
;
48 friend class MIDIAccessManager
;
51 NS_DECL_ISUPPORTS_INHERITED
52 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MIDIAccess
,
55 virtual JSObject
* WrapObject(JSContext
* aCx
,
56 JS::Handle
<JSObject
*> aGivenProto
) override
;
58 // Return map of MIDI Input Ports
59 MIDIInputMap
* Inputs() const { return mInputMap
; }
61 // Return map of MIDI Output Ports
62 MIDIOutputMap
* Outputs() const { return mOutputMap
; }
64 // Returns true if sysex permissions were given
65 bool SysexEnabled() const { return mSysexEnabled
; }
67 // Observer implementation for receiving port connection updates
68 void Notify(const MIDIPortList
& aEvent
) override
;
70 // Fires DOM event on port connection/disconnection
71 void FireConnectionEvent(MIDIPort
* aPort
);
73 // Notify all MIDIPorts that were created by this MIDIAccess and are still
74 // alive, and detach from the MIDIAccessManager.
76 IMPL_EVENT_HANDLER(statechange
);
78 void DisconnectFromOwner() override
;
81 MIDIAccess(nsPIDOMWindowInner
* aWindow
, bool aSysexEnabled
,
82 Promise
* aAccessPromise
);
85 // On receiving a connection event from MIDIAccessManager, create a
86 // corresponding MIDIPort object if we don't already have one.
87 void MaybeCreateMIDIPort(const MIDIPortInfo
& aInfo
, ErrorResult
& aRv
);
89 // Stores all known MIDIInput Ports
90 RefPtr
<MIDIInputMap
> mInputMap
;
91 // Stores all known MIDIOutput Ports
92 RefPtr
<MIDIOutputMap
> mOutputMap
;
93 // True if user gave permissions for sysex usage to this object.
95 // Promise created by RequestMIDIAccess call, to be resolved after port
96 // populating is finished.
97 RefPtr
<Promise
> mAccessPromise
;
98 // True if shutdown process has started, so we don't try to add more ports.
103 } // namespace mozilla
105 #endif // mozilla_dom_MIDIAccess_h