Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / dom / midi / MIDIPort.h
blob76e039ab6556e107ce018706178e5eb790acdc7a
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 "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/MIDIAccess.h"
12 #include "mozilla/dom/MIDIPortChild.h"
13 #include "mozilla/dom/MIDIPortInterface.h"
15 struct JSContext;
17 namespace mozilla::dom {
19 class Promise;
20 class MIDIPortInfo;
21 class MIDIAccess;
22 class MIDIPortChangeEvent;
23 class MIDIPortChild;
24 class MIDIMessage;
26 /**
27 * Implementation of WebIDL DOM MIDIPort class. Handles all port representation
28 * and communication.
31 class MIDIPort : public DOMEventTargetHelper,
32 public MIDIAccessDestructionObserver {
33 public:
34 NS_DECL_ISUPPORTS_INHERITED
35 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MIDIPort,
36 DOMEventTargetHelper)
37 protected:
38 explicit MIDIPort(nsPIDOMWindowInner* aWindow);
39 bool Initialize(const MIDIPortInfo& aPortInfo, bool aSysexEnabled,
40 MIDIAccess* aMIDIAccessParent);
41 virtual ~MIDIPort();
43 public:
44 nsPIDOMWindowInner* GetParentObject() const { return GetOwner(); }
46 // Getters
47 void GetId(nsString& aRetVal) const;
48 void GetManufacturer(nsString& aRetVal) const;
49 void GetName(nsString& aRetVal) const;
50 void GetVersion(nsString& aRetVal) const;
51 MIDIPortType Type() const;
52 MIDIPortConnectionState Connection() const;
53 MIDIPortDeviceState State() const;
54 bool SysexEnabled() const;
56 already_AddRefed<Promise> Open(ErrorResult& aError);
57 already_AddRefed<Promise> Close(ErrorResult& aError);
59 // MIDIPorts observe the death of their parent MIDIAccess object, and delete
60 // their reference accordingly.
61 virtual void Notify(const void_t& aVoid) override;
63 void FireStateChangeEvent();
65 virtual void StateChange();
66 virtual void Receive(const nsTArray<MIDIMessage>& aMsg);
68 // This object holds a pointer to its corresponding IPC MIDIPortChild actor.
69 // If the IPC actor is deleted, it cleans itself up via this method.
70 void UnsetIPCPort();
72 IMPL_EVENT_HANDLER(statechange)
74 void DisconnectFromOwner() override;
75 const nsString& StableId();
77 protected:
78 // Helper class to ensure we always call DetachOwner when we drop the
79 // reference to the the port.
80 class PortHolder {
81 public:
82 void Init(already_AddRefed<MIDIPortChild> aArg) {
83 MOZ_ASSERT(!mInner);
84 mInner = aArg;
86 void Clear() {
87 if (mInner) {
88 mInner->DetachOwner();
89 mInner = nullptr;
92 ~PortHolder() { Clear(); }
93 MIDIPortChild* Get() const { return mInner; }
95 private:
96 RefPtr<MIDIPortChild> mInner;
99 // IPC Actor corresponding to this class.
100 PortHolder mPortHolder;
101 MIDIPortChild* Port() const { return mPortHolder.Get(); }
103 private:
104 void KeepAliveOnStatechange();
105 void DontKeepAliveOnStatechange();
107 // MIDIAccess object that created this MIDIPort object, which we need for
108 // firing port connection events. There is a chance this MIDIPort object can
109 // outlive its parent MIDIAccess object, so this is a weak reference that must
110 // be handled properly. It is set on construction of the MIDIPort object, and
111 // set to null when the parent MIDIAccess object is destroyed, which fires an
112 // notification we observe.
113 MIDIAccess* mMIDIAccessParent;
114 // Promise object generated on Open() call, that needs to be resolved once the
115 // platform specific Open() function has completed.
116 RefPtr<Promise> mOpeningPromise;
117 // Promise object generated on Close() call, that needs to be resolved once
118 // the platform specific Close() function has completed.
119 RefPtr<Promise> mClosingPromise;
120 // If true this object will be kept alive even without direct JS references
121 bool mKeepAlive;
124 } // namespace mozilla::dom
126 #endif // mozilla_dom_MIDIPort_h