Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / midi / MIDIAccess.h
blob9b3a860f02bd35f46162b53ab89beafd98857f1e
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_MIDIAccess_h
8 #define mozilla_dom_MIDIAccess_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/DOMEventTargetHelper.h"
12 #include "mozilla/Observer.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsWrapperCache.h"
16 struct JSContext;
18 namespace mozilla {
19 class ErrorResult;
21 // Predeclare void_t here, as including IPCMessageUtils brings in windows.h and
22 // causes binding compilation problems.
23 struct void_t;
25 namespace dom {
27 class MIDIAccessManager;
28 class MIDIInputMap;
29 struct MIDIOptions;
30 class MIDIOutputMap;
31 class MIDIPermissionRequest;
32 class MIDIPort;
33 class MIDIPortChangeEvent;
34 class MIDIPortInfo;
35 class MIDIPortList;
36 class Promise;
38 using MIDIAccessDestructionObserver = Observer<void_t>;
40 /**
41 * MIDIAccess is the DOM object that is handed to the user upon MIDI permissions
42 * being successfully granted. It manages access to MIDI ports, and fires events
43 * for device connection and disconnection.
45 * New MIDIAccess objects are created every time RequestMIDIAccess is called.
46 * MIDIAccess objects are managed via MIDIAccessManager.
48 class MIDIAccess final : public DOMEventTargetHelper,
49 public Observer<MIDIPortList> {
50 // Use the Permission Request class in MIDIAccessManager for creating
51 // MIDIAccess objects.
52 friend class MIDIPermissionRequest;
53 friend class MIDIAccessManager;
55 public:
56 NS_DECL_ISUPPORTS_INHERITED
57 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MIDIAccess,
58 DOMEventTargetHelper)
59 public:
60 virtual JSObject* WrapObject(JSContext* aCx,
61 JS::Handle<JSObject*> aGivenProto) override;
63 // Return map of MIDI Input Ports
64 MIDIInputMap* Inputs() const { return mInputMap; }
66 // Return map of MIDI Output Ports
67 MIDIOutputMap* Outputs() const { return mOutputMap; }
69 // Returns true if sysex permissions were given
70 bool SysexEnabled() const { return mSysexEnabled; }
72 // Observer implementation for receiving port connection updates
73 void Notify(const MIDIPortList& aEvent) override;
75 // All MIDIPort objects observe destruction of the MIDIAccess object that
76 // created them, as the port object receives disconnection events which then
77 // must be passed up to the MIDIAccess object. If the Port object dies before
78 // the MIDIAccess object, it needs to be removed from the observer list.
79 void RemovePortListener(MIDIAccessDestructionObserver* aObs);
81 // Fires DOM event on port connection/disconnection
82 void FireConnectionEvent(MIDIPort* aPort);
84 // Notify all MIDIPorts that were created by this MIDIAccess and are still
85 // alive, and detach from the MIDIAccessManager.
86 void Shutdown();
87 IMPL_EVENT_HANDLER(statechange);
89 void DisconnectFromOwner() override;
91 private:
92 MIDIAccess(nsPIDOMWindowInner* aWindow, bool aSysexEnabled,
93 Promise* aAccessPromise);
94 ~MIDIAccess();
96 // On receiving a connection event from MIDIAccessManager, create a
97 // corresponding MIDIPort object if we don't already have one.
98 void MaybeCreateMIDIPort(const MIDIPortInfo& aInfo, ErrorResult& aRv);
100 // Stores all known MIDIInput Ports
101 RefPtr<MIDIInputMap> mInputMap;
102 // Stores all known MIDIOutput Ports
103 RefPtr<MIDIOutputMap> mOutputMap;
104 // List of MIDIPort observers that need to be updated on destruction.
105 ObserverList<void_t> mDestructionObservers;
106 // True if user gave permissions for sysex usage to this object.
107 bool mSysexEnabled;
108 // Promise created by RequestMIDIAccess call, to be resolved after port
109 // populating is finished.
110 RefPtr<Promise> mAccessPromise;
111 // True if shutdown process has started, so we don't try to add more ports.
112 bool mHasShutdown;
115 } // namespace dom
116 } // namespace mozilla
118 #endif // mozilla_dom_MIDIAccess_h