summaryrefslogtreecommitdiffstats
path: root/dom/midi/MIDIPlatformRunnables.h
blob: 15727a99ccc93c439f9809213c85f61ce5e6b50a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_MIDIPlatformRunnables_h
#define mozilla_dom_MIDIPlatformRunnables_h

#include "mozilla/dom/MIDITypes.h"

namespace mozilla::dom {

enum class MIDIPortConnectionState : uint8_t;
enum class MIDIPortDeviceState : uint8_t;

class MIDIPortParent;
class MIDIMessage;
class MIDIPortInfo;

/**
 * Base class for runnables to be fired to the platform-specific MIDI service
 * thread in PBackground.
 */
class MIDIBackgroundRunnable : public Runnable {
 public:
  MIDIBackgroundRunnable(const char* aName) : Runnable(aName) {}
  virtual ~MIDIBackgroundRunnable() = default;
  NS_IMETHOD Run() override;
  virtual void RunInternal() = 0;
};

/**
 * Runnable fired from platform-specific MIDI service thread to PBackground
 * Thread whenever messages need to be sent to a MIDI device.
 *
 */
class ReceiveRunnable final : public MIDIBackgroundRunnable {
 public:
  ReceiveRunnable(const nsAString& aPortId, const nsTArray<MIDIMessage>& aMsgs)
      : MIDIBackgroundRunnable("ReceiveRunnable"),
        mMsgs(aMsgs.Clone()),
        mPortId(aPortId) {}
  // Used in tests
  ReceiveRunnable(const nsAString& aPortId, const MIDIMessage& aMsgs)
      : MIDIBackgroundRunnable("ReceiveRunnable"), mPortId(aPortId) {
    mMsgs.AppendElement(aMsgs);
  }
  ~ReceiveRunnable() = default;
  void RunInternal() override;

 private:
  nsTArray<MIDIMessage> mMsgs;
  nsString mPortId;
};

/**
 * Runnable fired from platform-specific MIDI service thread to PBackground
 * Thread whenever a device is connected.
 *
 */
class AddPortRunnable final : public MIDIBackgroundRunnable {
 public:
  explicit AddPortRunnable(const MIDIPortInfo& aPortInfo)
      : MIDIBackgroundRunnable("AddPortRunnable"), mPortInfo(aPortInfo) {}
  ~AddPortRunnable() = default;
  void RunInternal() override;

 private:
  MIDIPortInfo mPortInfo;
};

/**
 * Runnable fired from platform-specific MIDI service thread to PBackground
 * Thread whenever a device is disconnected.
 *
 */
class RemovePortRunnable final : public MIDIBackgroundRunnable {
 public:
  explicit RemovePortRunnable(const MIDIPortInfo& aPortInfo)
      : MIDIBackgroundRunnable("RemovePortRunnable"), mPortInfo(aPortInfo) {}
  ~RemovePortRunnable() = default;
  void RunInternal() override;

 private:
  MIDIPortInfo mPortInfo;
};

/**
 * Runnable used to delay calls to SendPortList, which is requires to make sure
 * MIDIManager actor initialization happens correctly. Also used for testing.
 *
 */
class SendPortListRunnable final : public MIDIBackgroundRunnable {
 public:
  SendPortListRunnable() : MIDIBackgroundRunnable("SendPortListRunnable") {}
  ~SendPortListRunnable() = default;
  void RunInternal() override;
};

/**
 * Runnable fired from platform-specific MIDI service thread to PBackground
 * Thread whenever a device is disconnected.
 *
 */
class SetStatusRunnable final : public MIDIBackgroundRunnable {
 public:
  SetStatusRunnable(MIDIPortParent* aPort, MIDIPortDeviceState aState,
                    MIDIPortConnectionState aConnection)
      : MIDIBackgroundRunnable("SetStatusRunnable"),
        mPort(aPort),
        mState(aState),
        mConnection(aConnection) {}
  ~SetStatusRunnable() = default;
  void RunInternal() override;

 private:
  RefPtr<MIDIPortParent> mPort;
  MIDIPortDeviceState mState;
  MIDIPortConnectionState mConnection;
};

}  // namespace mozilla::dom

#endif  // mozilla_dom_MIDIPlatformRunnables_h