Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / dom / midi / MIDIPortParent.cpp
blob1e97e5fa215265f04b2940f331656b0320665d4c
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 #include "mozilla/dom/MIDIPortParent.h"
8 #include "mozilla/dom/MIDIPlatformService.h"
9 #include "nsContentUtils.h"
11 // C++ file contents
12 namespace mozilla::dom {
14 // Keep an internal ID that we can use for passing information about specific
15 // MIDI ports back and forth to the Rust libraries.
16 static uint32_t gId = 0;
18 mozilla::ipc::IPCResult MIDIPortParent::RecvSend(
19 nsTArray<MIDIMessage>&& aMsgs) {
20 if (mConnectionState != MIDIPortConnectionState::Open) {
21 mMessageQueue.AppendElements(aMsgs);
22 if (MIDIPlatformService::IsRunning()) {
23 MIDIPlatformService::Get()->Open(this);
25 return IPC_OK();
27 if (MIDIPlatformService::IsRunning()) {
28 MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId, aMsgs);
30 return IPC_OK();
33 mozilla::ipc::IPCResult MIDIPortParent::RecvOpen() {
34 if (MIDIPlatformService::IsRunning() &&
35 mConnectionState == MIDIPortConnectionState::Closed) {
36 MIDIPlatformService::Get()->Open(this);
38 return IPC_OK();
41 mozilla::ipc::IPCResult MIDIPortParent::RecvClose() {
42 if (mConnectionState != MIDIPortConnectionState::Closed) {
43 if (MIDIPlatformService::IsRunning()) {
44 MIDIPlatformService::Get()->Close(this);
47 return IPC_OK();
50 mozilla::ipc::IPCResult MIDIPortParent::RecvClear() {
51 if (MIDIPlatformService::IsRunning()) {
52 MIDIPlatformService::Get()->Clear(this);
54 return IPC_OK();
57 mozilla::ipc::IPCResult MIDIPortParent::RecvShutdown() {
58 if (mShuttingDown) {
59 return IPC_OK();
61 Teardown();
62 Unused << Send__delete__(this);
63 return IPC_OK();
66 void MIDIPortParent::Teardown() {
67 mMessageQueue.Clear();
68 MIDIPortInterface::Shutdown();
69 if (MIDIPlatformService::IsRunning()) {
70 MIDIPlatformService::Get()->RemovePort(this);
74 void MIDIPortParent::ActorDestroy(ActorDestroyReason) {}
76 bool MIDIPortParent::SendUpdateStatus(
77 const MIDIPortDeviceState& aDeviceState,
78 const MIDIPortConnectionState& aConnectionState) {
79 if (mShuttingDown) {
80 return true;
82 mDeviceState = aDeviceState;
83 mConnectionState = aConnectionState;
84 if (aConnectionState == MIDIPortConnectionState::Open &&
85 aDeviceState == MIDIPortDeviceState::Disconnected) {
86 mConnectionState = MIDIPortConnectionState::Pending;
87 } else if (aConnectionState == MIDIPortConnectionState::Open &&
88 aDeviceState == MIDIPortDeviceState::Connected &&
89 !mMessageQueue.IsEmpty()) {
90 if (MIDIPlatformService::IsRunning()) {
91 MIDIPlatformService::Get()->QueueMessages(MIDIPortInterface::mId,
92 mMessageQueue);
94 mMessageQueue.Clear();
96 return PMIDIPortParent::SendUpdateStatus(
97 static_cast<uint32_t>(mDeviceState),
98 static_cast<uint32_t>(mConnectionState));
101 MIDIPortParent::MIDIPortParent(const MIDIPortInfo& aPortInfo,
102 const bool aSysexEnabled)
103 : MIDIPortInterface(aPortInfo, aSysexEnabled), mInternalId(++gId) {
104 MOZ_ASSERT(MIDIPlatformService::IsRunning(),
105 "Shouldn't be able to add MIDI port without MIDI service!");
106 MIDIPlatformService::Get()->AddPort(this);
109 } // namespace mozilla::dom