MIDIAccessManager.h (2924B)
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/. */ 6 7 #ifndef mozilla_dom_MIDIAccessManager_h 8 #define mozilla_dom_MIDIAccessManager_h 9 10 #include "mozilla/Observer.h" 11 #include "mozilla/dom/MIDITypes.h" 12 #include "nsPIDOMWindow.h" 13 14 namespace mozilla::dom { 15 16 class MIDIAccess; 17 class MIDIManagerChild; 18 struct MIDIOptions; 19 class MIDIPortChangeEvent; 20 class MIDIPortInfo; 21 class Promise; 22 23 /** 24 * MIDIAccessManager manages creation and lifetime of MIDIAccess objects for the 25 * process it lives in. It is in charge of dealing with permission requests, 26 * creating new MIDIAccess objects, and updating live MIDIAccess objects with 27 * new device listings. 28 * 29 * While a process/window can have many MIDIAccess objects, there is only one 30 * MIDIAccessManager for any one process. 31 */ 32 class MIDIAccessManager final { 33 public: 34 NS_INLINE_DECL_REFCOUNTING(MIDIAccessManager); 35 // Handles requests from Navigator for MIDI permissions and MIDIAccess 36 // creation. 37 already_AddRefed<Promise> RequestMIDIAccess(nsPIDOMWindowInner* aWindow, 38 const MIDIOptions& aOptions, 39 ErrorResult& aRv); 40 // Creates a new MIDIAccess object 41 void CreateMIDIAccess(nsPIDOMWindowInner* aWindow, bool aNeedsSysex, 42 Promise* aPromise); 43 // Getter for manager singleton 44 static MIDIAccessManager* Get(); 45 // True if manager singleton has been created 46 static bool IsRunning(); 47 // Send device connection updates to all known MIDIAccess objects. 48 void Update(const MIDIPortList& aPortList); 49 // Adds a device update observer (usually a MIDIAccess object) 50 bool AddObserver(Observer<MIDIPortList>* aObserver); 51 // Removes a device update observer (usually a MIDIAccess object) 52 void RemoveObserver(Observer<MIDIPortList>* aObserver); 53 // Requests the service to update the list of devices 54 void SendRefresh(); 55 56 private: 57 MIDIAccessManager(); 58 ~MIDIAccessManager(); 59 void StartActor(); 60 // True if object has received a device list from the MIDI platform service. 61 bool mHasPortList; 62 // List of known ports for the system. 63 MIDIPortList mPortList; 64 // Holds MIDIAccess objects until we've received the first list of devices 65 // from the MIDI Service. 66 nsTArray<RefPtr<MIDIAccess>> mAccessHolder; 67 // Device state update observers (usually MIDIAccess objects) 68 ObserverList<MIDIPortList> mChangeObservers; 69 // IPC Object for MIDIManager. Created on first MIDIAccess object creation, 70 // destroyed on last MIDIAccess object destruction. 71 RefPtr<MIDIManagerChild> mChild; 72 }; 73 74 } // namespace mozilla::dom 75 76 #endif // mozilla_dom_MIDIAccessManager_h