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_MIDIPlatformRunnables_h
8 #define mozilla_dom_MIDIPlatformRunnables_h
9 
10 #include "mozilla/dom/MIDITypes.h"
11 
12 namespace mozilla::dom {
13 
14 enum class MIDIPortConnectionState : uint8_t;
15 enum class MIDIPortDeviceState : uint8_t;
16 
17 class MIDIPortParent;
18 class MIDIMessage;
19 class MIDIPortInfo;
20 
21 /**
22  * Base class for runnables to be fired to the platform-specific MIDI service
23  * thread in PBackground.
24  */
25 class MIDIBackgroundRunnable : public Runnable {
26  public:
MIDIBackgroundRunnable(const char * aName)27   MIDIBackgroundRunnable(const char* aName) : Runnable(aName) {}
28   virtual ~MIDIBackgroundRunnable() = default;
29   NS_IMETHOD Run() override;
30   virtual void RunInternal() = 0;
31 };
32 
33 /**
34  * Runnable fired from platform-specific MIDI service thread to PBackground
35  * Thread whenever messages need to be sent to a MIDI device.
36  *
37  */
38 class ReceiveRunnable final : public MIDIBackgroundRunnable {
39  public:
ReceiveRunnable(const nsAString & aPortId,const nsTArray<MIDIMessage> & aMsgs)40   ReceiveRunnable(const nsAString& aPortId, const nsTArray<MIDIMessage>& aMsgs)
41       : MIDIBackgroundRunnable("ReceiveRunnable"),
42         mMsgs(aMsgs.Clone()),
43         mPortId(aPortId) {}
44   // Used in tests
ReceiveRunnable(const nsAString & aPortId,const MIDIMessage & aMsgs)45   ReceiveRunnable(const nsAString& aPortId, const MIDIMessage& aMsgs)
46       : MIDIBackgroundRunnable("ReceiveRunnable"), mPortId(aPortId) {
47     mMsgs.AppendElement(aMsgs);
48   }
49   ~ReceiveRunnable() = default;
50   void RunInternal() override;
51 
52  private:
53   nsTArray<MIDIMessage> mMsgs;
54   nsString mPortId;
55 };
56 
57 /**
58  * Runnable fired from platform-specific MIDI service thread to PBackground
59  * Thread whenever a device is connected.
60  *
61  */
62 class AddPortRunnable final : public MIDIBackgroundRunnable {
63  public:
AddPortRunnable(const MIDIPortInfo & aPortInfo)64   explicit AddPortRunnable(const MIDIPortInfo& aPortInfo)
65       : MIDIBackgroundRunnable("AddPortRunnable"), mPortInfo(aPortInfo) {}
66   ~AddPortRunnable() = default;
67   void RunInternal() override;
68 
69  private:
70   MIDIPortInfo mPortInfo;
71 };
72 
73 /**
74  * Runnable fired from platform-specific MIDI service thread to PBackground
75  * Thread whenever a device is disconnected.
76  *
77  */
78 class RemovePortRunnable final : public MIDIBackgroundRunnable {
79  public:
RemovePortRunnable(const MIDIPortInfo & aPortInfo)80   explicit RemovePortRunnable(const MIDIPortInfo& aPortInfo)
81       : MIDIBackgroundRunnable("RemovePortRunnable"), mPortInfo(aPortInfo) {}
82   ~RemovePortRunnable() = default;
83   void RunInternal() override;
84 
85  private:
86   MIDIPortInfo mPortInfo;
87 };
88 
89 /**
90  * Runnable used to delay calls to SendPortList, which is requires to make sure
91  * MIDIManager actor initialization happens correctly. Also used for testing.
92  *
93  */
94 class SendPortListRunnable final : public MIDIBackgroundRunnable {
95  public:
SendPortListRunnable()96   SendPortListRunnable() : MIDIBackgroundRunnable("SendPortListRunnable") {}
97   ~SendPortListRunnable() = default;
98   void RunInternal() override;
99 };
100 
101 /**
102  * Runnable fired from platform-specific MIDI service thread to PBackground
103  * Thread whenever a device is disconnected.
104  *
105  */
106 class SetStatusRunnable final : public MIDIBackgroundRunnable {
107  public:
SetStatusRunnable(MIDIPortParent * aPort,MIDIPortDeviceState aState,MIDIPortConnectionState aConnection)108   SetStatusRunnable(MIDIPortParent* aPort, MIDIPortDeviceState aState,
109                     MIDIPortConnectionState aConnection)
110       : MIDIBackgroundRunnable("SetStatusRunnable"),
111         mPort(aPort),
112         mState(aState),
113         mConnection(aConnection) {}
114   ~SetStatusRunnable() = default;
115   void RunInternal() override;
116 
117  private:
118   MIDIPortParent* mPort;
119   MIDIPortDeviceState mState;
120   MIDIPortConnectionState mConnection;
121 };
122 
123 }  // namespace mozilla::dom
124 
125 #endif  // mozilla_dom_MIDIPlatformRunnables_h
126