Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / midi / MIDIAccess.h
blob7f9ce36d7d390448eb2d54a795e1fd320a803829
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 "mozilla/WeakPtr.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsWrapperCache.h"
17 struct JSContext;
19 namespace mozilla {
20 class ErrorResult;
22 namespace dom {
24 class MIDIAccessManager;
25 class MIDIInputMap;
26 struct MIDIOptions;
27 class MIDIOutputMap;
28 class MIDIPermissionRequest;
29 class MIDIPort;
30 class MIDIPortChangeEvent;
31 class MIDIPortInfo;
32 class MIDIPortList;
33 class Promise;
35 /**
36 * MIDIAccess is the DOM object that is handed to the user upon MIDI permissions
37 * being successfully granted. It manages access to MIDI ports, and fires events
38 * for device connection and disconnection.
40 * New MIDIAccess objects are created every time RequestMIDIAccess is called.
41 * MIDIAccess objects are managed via MIDIAccessManager.
43 class MIDIAccess final : public DOMEventTargetHelper,
44 public Observer<MIDIPortList> {
45 // Use the Permission Request class in MIDIAccessManager for creating
46 // MIDIAccess objects.
47 friend class MIDIPermissionRequest;
48 friend class MIDIAccessManager;
50 public:
51 NS_DECL_ISUPPORTS_INHERITED
52 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MIDIAccess,
53 DOMEventTargetHelper)
54 public:
55 virtual JSObject* WrapObject(JSContext* aCx,
56 JS::Handle<JSObject*> aGivenProto) override;
58 // Return map of MIDI Input Ports
59 MIDIInputMap* Inputs() const { return mInputMap; }
61 // Return map of MIDI Output Ports
62 MIDIOutputMap* Outputs() const { return mOutputMap; }
64 // Returns true if sysex permissions were given
65 bool SysexEnabled() const { return mSysexEnabled; }
67 // Observer implementation for receiving port connection updates
68 void Notify(const MIDIPortList& aEvent) override;
70 // Fires DOM event on port connection/disconnection
71 void FireConnectionEvent(MIDIPort* aPort);
73 // Notify all MIDIPorts that were created by this MIDIAccess and are still
74 // alive, and detach from the MIDIAccessManager.
75 void Shutdown();
76 IMPL_EVENT_HANDLER(statechange);
78 void DisconnectFromOwner() override;
80 private:
81 MIDIAccess(nsPIDOMWindowInner* aWindow, bool aSysexEnabled,
82 Promise* aAccessPromise);
83 ~MIDIAccess();
85 // On receiving a connection event from MIDIAccessManager, create a
86 // corresponding MIDIPort object if we don't already have one.
87 void MaybeCreateMIDIPort(const MIDIPortInfo& aInfo, ErrorResult& aRv);
89 // Stores all known MIDIInput Ports
90 RefPtr<MIDIInputMap> mInputMap;
91 // Stores all known MIDIOutput Ports
92 RefPtr<MIDIOutputMap> mOutputMap;
93 // True if user gave permissions for sysex usage to this object.
94 bool mSysexEnabled;
95 // Promise created by RequestMIDIAccess call, to be resolved after port
96 // populating is finished.
97 RefPtr<Promise> mAccessPromise;
98 // True if shutdown process has started, so we don't try to add more ports.
99 bool mHasShutdown;
102 } // namespace dom
103 } // namespace mozilla
105 #endif // mozilla_dom_MIDIAccess_h