summaryrefslogtreecommitdiffstats
path: root/dom/midi/MIDIAccess.cpp
blob: 2ddb9b46940e4f7dc2ea3c3ac168bd7406662681 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -*- 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/. */

#include "mozilla/dom/MIDIAccess.h"
#include "mozilla/dom/MIDIAccessManager.h"
#include "mozilla/dom/MIDIPort.h"
#include "mozilla/dom/MIDIAccessBinding.h"
#include "mozilla/dom/MIDIConnectionEvent.h"
#include "mozilla/dom/MIDIOptionsBinding.h"
#include "mozilla/dom/MIDIOutputMapBinding.h"
#include "mozilla/dom/MIDIInputMapBinding.h"
#include "mozilla/dom/MIDIOutputMap.h"
#include "mozilla/dom/MIDIInputMap.h"
#include "mozilla/dom/MIDIOutput.h"
#include "mozilla/dom/MIDIInput.h"
#include "mozilla/dom/MIDITypes.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PContent.h"
#include "mozilla/dom/Document.h"
#include "nsPIDOMWindow.h"
#include "nsContentPermissionHelper.h"
#include "nsISupportsImpl.h"  // for MOZ_COUNT_CTOR, MOZ_COUNT_DTOR
#include "ipc/IPCMessageUtils.h"
#include "MIDILog.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_CLASS(MIDIAccess)
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MIDIAccess, DOMEventTargetHelper)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MIDIAccess,
                                                  DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInputMap)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOutputMap)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAccessPromise)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MIDIAccess,
                                                DOMEventTargetHelper)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mInputMap)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mOutputMap)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mAccessPromise)
  tmp->Shutdown();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MIDIAccess)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

NS_IMPL_ADDREF_INHERITED(MIDIAccess, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(MIDIAccess, DOMEventTargetHelper)

MIDIAccess::MIDIAccess(nsPIDOMWindowInner* aWindow, bool aSysexEnabled,
                       Promise* aAccessPromise)
    : DOMEventTargetHelper(aWindow),
      mInputMap(new MIDIInputMap(aWindow)),
      mOutputMap(new MIDIOutputMap(aWindow)),
      mSysexEnabled(aSysexEnabled),
      mAccessPromise(aAccessPromise),
      mHasShutdown(false) {
  MOZ_ASSERT(aWindow);
  MOZ_ASSERT(aAccessPromise);
  KeepAliveIfHasListenersFor(nsGkAtoms::onstatechange);
}

MIDIAccess::~MIDIAccess() { Shutdown(); }

void MIDIAccess::Shutdown() {
  LOG("MIDIAccess::Shutdown");
  if (mHasShutdown) {
    return;
  }
  mDestructionObservers.Broadcast(void_t());
  if (MIDIAccessManager::IsRunning()) {
    MIDIAccessManager::Get()->RemoveObserver(this);
  }
  mHasShutdown = true;
}

void MIDIAccess::FireConnectionEvent(MIDIPort* aPort) {
  MOZ_ASSERT(aPort);
  MIDIConnectionEventInit init;
  init.mPort = aPort;
  nsAutoString id;
  aPort->GetId(id);
  ErrorResult rv;
  if (aPort->State() == MIDIPortDeviceState::Disconnected) {
    if (aPort->Type() == MIDIPortType::Input && mInputMap->Has(id)) {
      MIDIInputMap_Binding::MaplikeHelpers::Delete(mInputMap, aPort->StableId(),
                                                   rv);
      mInputMap->Remove(id);
    } else if (aPort->Type() == MIDIPortType::Output && mOutputMap->Has(id)) {
      MIDIOutputMap_Binding::MaplikeHelpers::Delete(mOutputMap,
                                                    aPort->StableId(), rv);
      mOutputMap->Remove(id);
    }
    // Check to make sure Has()/Delete() calls haven't failed.
    if (NS_WARN_IF(rv.Failed())) {
      LOG("Inconsistency during FireConnectionEvent");
      return;
    }
  } else {
    // If we receive an event from a port that is not in one of our port maps,
    // this means a port that was disconnected has been reconnected, with the
    // port owner holding the object during that time, and we should add that
    // port object to our maps again.
    if (aPort->Type() == MIDIPortType::Input && !mInputMap->Has(id)) {
      if (NS_WARN_IF(rv.Failed())) {
        LOG("Input port not found");
        return;
      }
      MIDIInputMap_Binding::MaplikeHelpers::Set(
          mInputMap, aPort->StableId(), *(static_cast<MIDIInput*>(aPort)), rv);
      if (NS_WARN_IF(rv.Failed())) {
        LOG("Map Set failed for input port");
        return;
      }
      mInputMap->Insert(id, aPort);
    } else if (aPort->Type() == MIDIPortType::Output && mOutputMap->Has(id)) {
      if (NS_WARN_IF(rv.Failed())) {
        LOG("Output port not found");
        return;
      }
      MIDIOutputMap_Binding::MaplikeHelpers::Set(
          mOutputMap, aPort->StableId(), *(static_cast<MIDIOutput*>(aPort)),
          rv);
      if (NS_WARN_IF(rv.Failed())) {
        LOG("Map set failed for output port");
        return;
      }
      mOutputMap->Insert(id, aPort);
    }
  }
  RefPtr<MIDIConnectionEvent> event =
      MIDIConnectionEvent::Constructor(this, u"statechange"_ns, init);
  DispatchTrustedEvent(event);
}

void MIDIAccess::MaybeCreateMIDIPort(const MIDIPortInfo& aInfo,
                                     ErrorResult& aRv) {
  nsAutoString id(aInfo.id());
  MIDIPortType type = static_cast<MIDIPortType>(aInfo.type());
  RefPtr<MIDIPort> port;
  if (type == MIDIPortType::Input) {
    if (mInputMap->Has(id) || NS_WARN_IF(aRv.Failed())) {
      // We already have the port in our map.
      return;
    }
    port = MIDIInput::Create(GetOwner(), this, aInfo, mSysexEnabled);
    if (NS_WARN_IF(!port)) {
      LOG("Couldn't create input port");
      aRv.Throw(NS_ERROR_FAILURE);
      return;
    }
    MIDIInputMap_Binding::MaplikeHelpers::Set(
        mInputMap, port->StableId(), *(static_cast<MIDIInput*>(port.get())),
        aRv);
    if (NS_WARN_IF(aRv.Failed())) {
      LOG("Coudld't set input port in map");
      return;
    }
    mInputMap->Insert(id, port);
  } else if (type == MIDIPortType::Output) {
    if (mOutputMap->Has(id) || NS_WARN_IF(aRv.Failed())) {
      // We already have the port in our map.
      return;
    }
    port = MIDIOutput::Create(GetOwner(), this, aInfo, mSysexEnabled);
    if (NS_WARN_IF(!port)) {
      LOG("Couldn't create output port");
      aRv.Throw(NS_ERROR_FAILURE);
      return;
    }
    MIDIOutputMap_Binding::MaplikeHelpers::Set(
        mOutputMap, port->StableId(), *(static_cast<MIDIOutput*>(port.get())),
        aRv);
    if (NS_WARN_IF(aRv.Failed())) {
      LOG("Coudld't set output port in map");
      return;
    }
    mOutputMap->Insert(id, port);
  } else {
    // If we hit this, then we have some port that is neither input nor output.
    // That is bad.
    MOZ_CRASH("We shouldn't be here!");
  }
  // Set up port to listen for destruction of this access object.
  mDestructionObservers.AddObserver(port);

  // If we haven't resolved the promise for handing the MIDIAccess object to
  // content, this means we're still populating the list of already connected
  // devices. Don't fire events yet.
  if (!mAccessPromise) {
    FireConnectionEvent(port);
  }
}

// For the MIDIAccess object, only worry about new connections, where we create
// MIDIPort objects. When a port is removed and the MIDIPortRemove event is
// received, that will be handled by the MIDIPort object itself, and it will
// request removal from MIDIAccess's maps.
void MIDIAccess::Notify(const MIDIPortList& aEvent) {
  LOG("MIDIAcess::Notify");
  if (!GetOwner()) {
    // Do nothing if we've already been disconnected from the document.
    return;
  }

  for (const auto& port : aEvent.ports()) {
    // Something went very wrong. Warn and return.
    ErrorResult rv;
    MaybeCreateMIDIPort(port, rv);
    if (rv.Failed()) {
      if (!mAccessPromise) {
        return;
      }
      mAccessPromise->MaybeReject(std::move(rv));
      mAccessPromise = nullptr;
    }
  }
  if (!mAccessPromise) {
    return;
  }
  mAccessPromise->MaybeResolve(this);
  mAccessPromise = nullptr;
}

JSObject* MIDIAccess::WrapObject(JSContext* aCx,
                                 JS::Handle<JSObject*> aGivenProto) {
  return MIDIAccess_Binding::Wrap(aCx, this, aGivenProto);
}

void MIDIAccess::RemovePortListener(MIDIAccessDestructionObserver* aObs) {
  mDestructionObservers.RemoveObserver(aObs);
}

void MIDIAccess::DisconnectFromOwner() {
  IgnoreKeepAliveIfHasListenersFor(nsGkAtoms::onstatechange);

  DOMEventTargetHelper::DisconnectFromOwner();
  MIDIAccessManager::Get()->SendRefresh();
}

}  // namespace mozilla::dom