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