/src/mozilla-central/dom/midi/MIDIPortParent.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "mozilla/dom/MIDIPortParent.h" |
8 | | #include "mozilla/dom/MIDIPlatformService.h" |
9 | | #include "nsContentUtils.h" |
10 | | |
11 | | // C++ file contents |
12 | | namespace mozilla { |
13 | | namespace dom { |
14 | | |
15 | | // Keep an internal ID that we can use for passing information about specific |
16 | | // MIDI ports back and forth to the Rust libraries. |
17 | | static uint32_t gId = 0; |
18 | | |
19 | | mozilla::ipc::IPCResult |
20 | | MIDIPortParent::RecvSend(nsTArray<MIDIMessage>&& aMsgs) |
21 | 0 | { |
22 | 0 | if (mConnectionState != MIDIPortConnectionState::Open) { |
23 | 0 | mMessageQueue.AppendElements(aMsgs); |
24 | 0 | if (MIDIPlatformService::IsRunning()) { |
25 | 0 | MIDIPlatformService::Get()->Open(this); |
26 | 0 | } |
27 | 0 | return IPC_OK(); |
28 | 0 | } |
29 | 0 | if (MIDIPlatformService::IsRunning()) { |
30 | 0 | MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId, aMsgs); |
31 | 0 | } |
32 | 0 | return IPC_OK(); |
33 | 0 | } |
34 | | |
35 | | mozilla::ipc::IPCResult |
36 | | MIDIPortParent::RecvOpen() |
37 | 0 | { |
38 | 0 | if (MIDIPlatformService::IsRunning()) { |
39 | 0 | MIDIPlatformService::Get()->Open(this); |
40 | 0 | } |
41 | 0 | return IPC_OK(); |
42 | 0 | } |
43 | | |
44 | | mozilla::ipc::IPCResult |
45 | | MIDIPortParent::RecvClose() |
46 | 0 | { |
47 | 0 | if (mConnectionState != MIDIPortConnectionState::Closed) { |
48 | 0 | if (MIDIPlatformService::IsRunning()) { |
49 | 0 | MIDIPlatformService::Get()->Close(this); |
50 | 0 | } |
51 | 0 | } |
52 | 0 | return IPC_OK(); |
53 | 0 | } |
54 | | |
55 | | mozilla::ipc::IPCResult |
56 | | MIDIPortParent::RecvClear() |
57 | 0 | { |
58 | 0 | if (MIDIPlatformService::IsRunning()) { |
59 | 0 | MIDIPlatformService::Get()->Clear(this); |
60 | 0 | } |
61 | 0 | return IPC_OK(); |
62 | 0 | } |
63 | | |
64 | | mozilla::ipc::IPCResult |
65 | | MIDIPortParent::RecvShutdown() |
66 | 0 | { |
67 | 0 | if (mShuttingDown) { |
68 | 0 | return IPC_OK(); |
69 | 0 | } |
70 | 0 | Teardown(); |
71 | 0 | Unused << Send__delete__(this); |
72 | 0 | return IPC_OK(); |
73 | 0 | } |
74 | | |
75 | | void |
76 | | MIDIPortParent::Teardown() |
77 | 0 | { |
78 | 0 | mMessageQueue.Clear(); |
79 | 0 | MIDIPortInterface::Shutdown(); |
80 | 0 | if (MIDIPlatformService::IsRunning()) { |
81 | 0 | MIDIPlatformService::Get()->RemovePort(this); |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | void |
86 | | MIDIPortParent::ActorDestroy(ActorDestroyReason) |
87 | 0 | { |
88 | 0 | } |
89 | | |
90 | | bool |
91 | | MIDIPortParent::SendUpdateStatus(const MIDIPortDeviceState& aDeviceState, |
92 | | const MIDIPortConnectionState& aConnectionState) |
93 | 0 | { |
94 | 0 | if (mShuttingDown) { |
95 | 0 | return true; |
96 | 0 | } |
97 | 0 | mDeviceState = aDeviceState; |
98 | 0 | mConnectionState = aConnectionState; |
99 | 0 | if (aConnectionState == MIDIPortConnectionState::Open && |
100 | 0 | aDeviceState == MIDIPortDeviceState::Disconnected) { |
101 | 0 | mConnectionState = MIDIPortConnectionState::Pending; |
102 | 0 | } else if (aConnectionState == MIDIPortConnectionState::Open && |
103 | 0 | aDeviceState == MIDIPortDeviceState::Connected && |
104 | 0 | !mMessageQueue.IsEmpty()) { |
105 | 0 | if (MIDIPlatformService::IsRunning()) { |
106 | 0 | MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId, mMessageQueue); |
107 | 0 | } |
108 | 0 | mMessageQueue.Clear(); |
109 | 0 | } |
110 | 0 | return PMIDIPortParent::SendUpdateStatus(static_cast<uint32_t>(mDeviceState), |
111 | 0 | static_cast<uint32_t>(mConnectionState)); |
112 | 0 | } |
113 | | |
114 | | MIDIPortParent::MIDIPortParent(const MIDIPortInfo& aPortInfo, |
115 | | const bool aSysexEnabled) : |
116 | | MIDIPortInterface(aPortInfo, aSysexEnabled), |
117 | | mInternalId(++gId) |
118 | 0 | { |
119 | 0 | MOZ_ASSERT(MIDIPlatformService::IsRunning(), |
120 | 0 | "Shouldn't be able to add MIDI port without MIDI service!"); |
121 | 0 | MIDIPlatformService::Get()->AddPort(this); |
122 | 0 | } |
123 | | |
124 | | } // namespace dom |
125 | | } // namespace mozilla |