MIDIPort.h (3562B)
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_MIDIPort_h 8 #define mozilla_dom_MIDIPort_h 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/dom/MIDIAccess.h" 12 #include "mozilla/dom/MIDIPortChild.h" 13 #include "mozilla/dom/MIDIPortInterface.h" 14 15 struct JSContext; 16 17 namespace mozilla::dom { 18 19 class Promise; 20 class MIDIPortInfo; 21 class MIDIAccess; 22 class MIDIPortChangeEvent; 23 class MIDIPortChild; 24 class MIDIMessage; 25 26 /** 27 * Implementation of WebIDL DOM MIDIPort class. Handles all port representation 28 * and communication. 29 * 30 */ 31 class MIDIPort : public DOMEventTargetHelper { 32 public: 33 NS_DECL_ISUPPORTS_INHERITED 34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MIDIPort, 35 DOMEventTargetHelper) 36 protected: 37 explicit MIDIPort(nsPIDOMWindowInner* aWindow); 38 bool Initialize(const MIDIPortInfo& aPortInfo, bool aSysexEnabled, 39 MIDIAccess* aMIDIAccessParent); 40 virtual ~MIDIPort(); 41 42 public: 43 nsIGlobalObject* GetParentObject() const { return GetOwnerGlobal(); } 44 45 // Getters 46 void GetId(nsString& aRetVal) const; 47 void GetManufacturer(nsString& aRetVal) const; 48 void GetName(nsString& aRetVal) const; 49 void GetVersion(nsString& aRetVal) const; 50 MIDIPortType Type() const; 51 MIDIPortConnectionState Connection() const; 52 MIDIPortDeviceState State() const; 53 bool SysexEnabled() const; 54 55 already_AddRefed<Promise> Open(ErrorResult& aError); 56 already_AddRefed<Promise> Close(ErrorResult& aError); 57 58 void FireStateChangeEvent(); 59 60 virtual void StateChange(); 61 virtual void Receive(const nsTArray<MIDIMessage>& aMsg); 62 63 // This object holds a pointer to its corresponding IPC MIDIPortChild actor. 64 // If the IPC actor is deleted, it cleans itself up via this method. 65 void UnsetIPCPort(); 66 67 IMPL_EVENT_HANDLER(statechange) 68 69 void DisconnectFromOwner() override; 70 const nsString& StableId(); 71 72 protected: 73 // Helper class to ensure we always call DetachOwner when we drop the 74 // reference to the the port. 75 class PortHolder { 76 public: 77 void Init(already_AddRefed<MIDIPortChild> aArg) { 78 MOZ_ASSERT(!mInner); 79 mInner = aArg; 80 } 81 void Clear() { 82 if (mInner) { 83 mInner->DetachOwner(); 84 mInner = nullptr; 85 } 86 } 87 ~PortHolder() { Clear(); } 88 MIDIPortChild* Get() const { return mInner; } 89 90 private: 91 RefPtr<MIDIPortChild> mInner; 92 }; 93 94 // IPC Actor corresponding to this class. 95 PortHolder mPortHolder; 96 MIDIPortChild* Port() const { return mPortHolder.Get(); } 97 98 private: 99 void KeepAliveOnStatechange(); 100 void DontKeepAliveOnStatechange(); 101 102 // MIDIAccess object that created this MIDIPort object, which we need for 103 // firing port connection events. 104 RefPtr<MIDIAccess> mMIDIAccessParent; 105 // Promise object generated on Open() call, that needs to be resolved once the 106 // platform specific Open() function has completed. 107 RefPtr<Promise> mOpeningPromise; 108 // Promise object generated on Close() call, that needs to be resolved once 109 // the platform specific Close() function has completed. 110 RefPtr<Promise> mClosingPromise; 111 // If true this object will be kept alive even without direct JS references 112 bool mKeepAlive; 113 }; 114 115 } // namespace mozilla::dom 116 117 #endif // mozilla_dom_MIDIPort_h