DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
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 249 250 251 252 253 254 255
/* -*- 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 "MIDIPlatformService.h"
#include "TestMIDIPlatformService.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/PMIDIManagerParent.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/dom/MIDIPortParent.h"

using namespace mozilla;
using namespace mozilla::dom;

MIDIPlatformService::MIDIPlatformService()
    : mHasSentPortList(false),
      mMessageQueueMutex("MIDIPlatformServce::mMessageQueueMutex") {}

MIDIPlatformService::~MIDIPlatformService() {}

void MIDIPlatformService::CheckAndReceive(const nsAString& aPortId,
                                          const nsTArray<MIDIMessage>& aMsgs) {
  AssertIsOnBackgroundThread();
  for (auto& port : mPorts) {
    // TODO Clean this up when we split input/output port arrays
    if (port->MIDIPortInterface::Id() != aPortId ||
        port->Type() != MIDIPortType::Input ||
        port->ConnectionState() != MIDIPortConnectionState::Open) {
      continue;
    }
    if (!port->SysexEnabled()) {
      nsTArray<MIDIMessage> msgs;
      for (auto& msg : aMsgs) {
        if (!MIDIUtils::IsSysexMessage(msg)) {
          msgs.AppendElement(msg);
        }
      }
      Unused << port->SendReceive(msgs);
    } else {
      Unused << port->SendReceive(aMsgs);
    }
  }
}

void MIDIPlatformService::AddPort(MIDIPortParent* aPort) {
  MOZ_ASSERT(aPort);
  AssertIsOnBackgroundThread();
  mPorts.AppendElement(aPort);
}

void MIDIPlatformService::RemovePort(MIDIPortParent* aPort) {
  // This should only be called from the background thread, when a MIDIPort
  // actor has been destroyed.
  AssertIsOnBackgroundThread();
  MOZ_ASSERT(aPort);
  mPorts.RemoveElement(aPort);
  MaybeStop();
}

void MIDIPlatformService::BroadcastState(const MIDIPortInfo& aPortInfo,
                                         const MIDIPortDeviceState& aState) {
  AssertIsOnBackgroundThread();
  for (auto& p : mPorts) {
    if (p->MIDIPortInterface::Id() == aPortInfo.id() &&
        p->DeviceState() != aState) {
      p->SendUpdateStatus(aState, p->ConnectionState());
    }
  }
}

void MIDIPlatformService::QueueMessages(const nsAString& aId,
                                        nsTArray<MIDIMessage>& aMsgs) {
  AssertIsOnBackgroundThread();
  {
    MutexAutoLock lock(mMessageQueueMutex);
    MIDIMessageQueue* msgQueue = mMessageQueues.LookupOrAdd(aId);
    msgQueue->Add(aMsgs);
    ScheduleSend(aId);
  }
}

void MIDIPlatformService::SendPortList() {
  AssertIsOnBackgroundThread();
  mHasSentPortList = true;
  MIDIPortList l;
  for (auto& el : mPortInfo) {
    l.ports().AppendElement(el);
  }
  for (auto& mgr : mManagers) {
    Unused << mgr->SendMIDIPortListUpdate(l);
  }
}

void MIDIPlatformService::Clear(MIDIPortParent* aPort) {
  AssertIsOnBackgroundThread();
  MOZ_ASSERT(aPort);
  {
    MutexAutoLock lock(mMessageQueueMutex);
    MIDIMessageQueue* msgQueue =
        mMessageQueues.Get(aPort->MIDIPortInterface::Id());
    if (msgQueue) {
      msgQueue->Clear();
    }
  }
}

void MIDIPlatformService::AddPortInfo(MIDIPortInfo& aPortInfo) {
  AssertIsOnBackgroundThread();
  MOZ_ASSERT(XRE_IsParentProcess());

  mPortInfo.AppendElement(aPortInfo);

  // ORDER MATTERS HERE.
  //
  // When MIDI hardware is disconnected, all open MIDIPort objects revert to a
  // "pending" state, and they are removed from the port maps of MIDIAccess
  // objects. We need to send connection updates to all living ports first, THEN
  // we can send port list updates to all of the live MIDIAccess objects. We
  // have to go in this order because if a port object is still held live but is
  // disconnected, it needs to readd itself to its originating MIDIAccess
  // object. Running SendPortList first would cause MIDIAccess to create a new
  // MIDIPort object, which would conflict (i.e. old disconnected object != new
  // object in port map, which is against spec).
  for (auto& port : mPorts) {
    if (port->MIDIPortInterface::Id() == aPortInfo.id()) {
      port->SendUpdateStatus(MIDIPortDeviceState::Connected,
                             port->ConnectionState());
    }
  }
  if (mHasSentPortList) {
    SendPortList();
  }
}

void MIDIPlatformService::RemovePortInfo(MIDIPortInfo& aPortInfo) {
  AssertIsOnBackgroundThread();
  mPortInfo.RemoveElement(aPortInfo);
  BroadcastState(aPortInfo, MIDIPortDeviceState::Disconnected);
  if (mHasSentPortList) {
    SendPortList();
  }
}

StaticRefPtr<MIDIPlatformService> gMIDIPlatformService;

// static
bool MIDIPlatformService::IsRunning() {
  return gMIDIPlatformService != nullptr;
}

void MIDIPlatformService::Close(mozilla::dom::MIDIPortParent* aPort) {
  AssertIsOnBackgroundThread();
  {
    MutexAutoLock lock(mMessageQueueMutex);
    MIDIMessageQueue* msgQueue =
        mMessageQueues.Get(aPort->MIDIPortInterface::Id());
    if (msgQueue) {
      msgQueue->ClearAfterNow();
    }
  }
  // Send all messages before sending a close request
  ScheduleSend(aPort->MIDIPortInterface::Id());
  // TODO We should probably have the send function schedule closing
  ScheduleClose(aPort);
}

// static
MIDIPlatformService* MIDIPlatformService::Get() {
  // We should never touch the platform service in a child process.
  MOZ_ASSERT(XRE_IsParentProcess());
  AssertIsOnBackgroundThread();
  if (!IsRunning()) {
    ErrorResult rv;
    // Uncomment once we have an actual platform library to test.
    //
    // bool useTestService = false;
    // rv = Preferences::GetRootBranch()->GetBoolPref("midi.testing",
    // &useTestService);
    gMIDIPlatformService = new TestMIDIPlatformService();
    gMIDIPlatformService->Init();
  }
  return gMIDIPlatformService;
}

void MIDIPlatformService::MaybeStop() {
  AssertIsOnBackgroundThread();
  if (!IsRunning()) {
    // Service already stopped or never started. Exit.
    return;
  }
  // If we have any ports or managers left, we should still be alive.
  if (!mPorts.IsEmpty() || !mManagers.IsEmpty()) {
    return;
  }
  Stop();
  gMIDIPlatformService = nullptr;
}

void MIDIPlatformService::AddManager(MIDIManagerParent* aManager) {
  AssertIsOnBackgroundThread();
  mManagers.AppendElement(aManager);
  // Managers add themselves during construction. We have to wait for the
  // protocol construction to finish before we send them a port list. The
  // runnable calls SendPortList, which iterates through the live manager list,
  // so this saves us from having to worry about Manager pointer validity at
  // time of runnable execution.
  nsCOMPtr<nsIRunnable> r(new SendPortListRunnable());
  NS_DispatchToCurrentThread(r);
}

void MIDIPlatformService::RemoveManager(MIDIManagerParent* aManager) {
  AssertIsOnBackgroundThread();
  mManagers.RemoveElement(aManager);
  MaybeStop();
}

void MIDIPlatformService::UpdateStatus(
    const nsAString& aPortId, const MIDIPortDeviceState& aDeviceState,
    const MIDIPortConnectionState& aConnectionState) {
  AssertIsOnBackgroundThread();
  for (auto port : mPorts) {
    if (port->MIDIPortInterface::Id() == aPortId) {
      port->SendUpdateStatus(aDeviceState, aConnectionState);
    }
  }
}

void MIDIPlatformService::GetMessages(const nsAString& aPortId,
                                      nsTArray<MIDIMessage>& aMsgs) {
  // Can run on either background thread or platform specific IO Thread.
  {
    MutexAutoLock lock(mMessageQueueMutex);
    MIDIMessageQueue* msgQueue;
    if (!mMessageQueues.Get(aPortId, &msgQueue)) {
      return;
    }
    msgQueue->GetMessages(aMsgs);
  }
}

void MIDIPlatformService::GetMessagesBefore(const nsAString& aPortId,
                                            const TimeStamp& aTimeStamp,
                                            nsTArray<MIDIMessage>& aMsgs) {
  // Can run on either background thread or platform specific IO Thread.
  {
    MutexAutoLock lock(mMessageQueueMutex);
    MIDIMessageQueue* msgQueue;
    if (!mMessageQueues.Get(aPortId, &msgQueue)) {
      return;
    }
    msgQueue->GetMessagesBefore(aTimeStamp, aMsgs);
  }
}