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_MIDIPort_h
8 #define mozilla_dom_MIDIPort_h
10 #include "nsWrapperCache.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/Observer.h"
13 #include "mozilla/DOMEventTargetHelper.h"
14 #include "mozilla/dom/MIDIAccess.h"
15 #include "mozilla/dom/MIDIPortChild.h"
16 #include "mozilla/dom/MIDIPortInterface.h"
20 namespace mozilla::dom
{
25 class MIDIPortChangeEvent
;
30 * Implementation of WebIDL DOM MIDIPort class. Handles all port representation
34 class MIDIPort
: public DOMEventTargetHelper
,
35 public MIDIAccessDestructionObserver
{
37 NS_DECL_ISUPPORTS_INHERITED
38 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MIDIPort
,
41 MIDIPort(nsPIDOMWindowInner
* aWindow
, MIDIAccess
* aMIDIAccessParent
);
42 bool Initialize(const MIDIPortInfo
& aPortInfo
, bool aSysexEnabled
);
46 nsPIDOMWindowInner
* GetParentObject() const { return GetOwner(); }
49 void GetId(nsString
& aRetVal
) const;
50 void GetManufacturer(nsString
& aRetVal
) const;
51 void GetName(nsString
& aRetVal
) const;
52 void GetVersion(nsString
& aRetVal
) const;
53 MIDIPortType
Type() const;
54 MIDIPortConnectionState
Connection() const;
55 MIDIPortDeviceState
State() const;
56 bool SysexEnabled() const;
58 already_AddRefed
<Promise
> Open(ErrorResult
& aError
);
59 already_AddRefed
<Promise
> Close(ErrorResult
& aError
);
61 // MIDIPorts observe the death of their parent MIDIAccess object, and delete
62 // their reference accordingly.
63 virtual void Notify(const void_t
& aVoid
) override
;
65 void FireStateChangeEvent();
67 virtual void StateChange();
68 virtual void Receive(const nsTArray
<MIDIMessage
>& aMsg
);
70 // This object holds a pointer to its corresponding IPC MIDIPortChild actor.
71 // If the IPC actor is deleted, it cleans itself up via this method.
74 IMPL_EVENT_HANDLER(statechange
)
76 void DisconnectFromOwner() override
;
77 const nsString
& StableId();
80 // Helper class to ensure we always call DetachOwner when we drop the
81 // reference to the the port.
84 void Init(already_AddRefed
<MIDIPortChild
> aArg
) {
90 mInner
->DetachOwner();
94 ~PortHolder() { Clear(); }
95 MIDIPortChild
* Get() const { return mInner
; }
98 RefPtr
<MIDIPortChild
> mInner
;
101 // IPC Actor corresponding to this class.
102 PortHolder mPortHolder
;
103 MIDIPortChild
* Port() const { return mPortHolder
.Get(); }
106 void KeepAliveOnStatechange();
107 void DontKeepAliveOnStatechange();
109 // MIDIAccess object that created this MIDIPort object, which we need for
110 // firing port connection events. There is a chance this MIDIPort object can
111 // outlive its parent MIDIAccess object, so this is a weak reference that must
112 // be handled properly. It is set on construction of the MIDIPort object, and
113 // set to null when the parent MIDIAccess object is destroyed, which fires an
114 // notification we observe.
115 MIDIAccess
* mMIDIAccessParent
;
116 // Promise object generated on Open() call, that needs to be resolved once the
117 // platform specific Open() function has completed.
118 RefPtr
<Promise
> mOpeningPromise
;
119 // Promise object generated on Close() call, that needs to be resolved once
120 // the platform specific Close() function has completed.
121 RefPtr
<Promise
> mClosingPromise
;
122 // If true this object will be kept alive even without direct JS references
126 } // namespace mozilla::dom
128 #endif // mozilla_dom_MIDIPort_h