Bug 1824634 disconnect from GtkIMContext OnDestroyWindow r=masayuki
[gecko.git] / dom / midi / MIDIPort.h
blob68a3aa0f3405dc78ac92b10c0e633a604ad40d26
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"
18 struct JSContext;
20 namespace mozilla::dom {
22 class Promise;
23 class MIDIPortInfo;
24 class MIDIAccess;
25 class MIDIPortChangeEvent;
26 class MIDIPortChild;
27 class MIDIMessage;
29 /**
30 * Implementation of WebIDL DOM MIDIPort class. Handles all port representation
31 * and communication.
34 class MIDIPort : public DOMEventTargetHelper,
35 public MIDIAccessDestructionObserver {
36 public:
37 NS_DECL_ISUPPORTS_INHERITED
38 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MIDIPort,
39 DOMEventTargetHelper)
40 protected:
41 MIDIPort(nsPIDOMWindowInner* aWindow, MIDIAccess* aMIDIAccessParent);
42 bool Initialize(const MIDIPortInfo& aPortInfo, bool aSysexEnabled);
43 virtual ~MIDIPort();
45 public:
46 nsPIDOMWindowInner* GetParentObject() const { return GetOwner(); }
48 // Getters
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.
72 void UnsetIPCPort();
74 IMPL_EVENT_HANDLER(statechange)
76 void DisconnectFromOwner() override;
77 const nsString& StableId();
79 protected:
80 // Helper class to ensure we always call DetachOwner when we drop the
81 // reference to the the port.
82 class PortHolder {
83 public:
84 void Init(already_AddRefed<MIDIPortChild> aArg) {
85 MOZ_ASSERT(!mInner);
86 mInner = aArg;
88 void Clear() {
89 if (mInner) {
90 mInner->DetachOwner();
91 mInner = nullptr;
94 ~PortHolder() { Clear(); }
95 MIDIPortChild* Get() const { return mInner; }
97 private:
98 RefPtr<MIDIPortChild> mInner;
101 // IPC Actor corresponding to this class.
102 PortHolder mPortHolder;
103 MIDIPortChild* Port() const { return mPortHolder.Get(); }
105 private:
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
123 bool mKeepAlive;
126 } // namespace mozilla::dom
128 #endif // mozilla_dom_MIDIPort_h