2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "modules/webmidi/MIDIAccess.h"
34 #include "core/dom/DOMError.h"
35 #include "core/dom/Document.h"
36 #include "core/loader/DocumentLoadTiming.h"
37 #include "core/loader/DocumentLoader.h"
38 #include "modules/webmidi/MIDIAccessPromise.h"
39 #include "modules/webmidi/MIDIConnectionEvent.h"
40 #include "modules/webmidi/MIDIController.h"
41 #include "modules/webmidi/MIDIPort.h"
45 PassRefPtr<MIDIAccess> MIDIAccess::create(ExecutionContext* context, MIDIAccessPromise* promise)
47 RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(context, promise)));
48 midiAccess->suspendIfNeeded();
49 midiAccess->startRequest();
50 return midiAccess.release();
53 MIDIAccess::~MIDIAccess()
58 MIDIAccess::MIDIAccess(ExecutionContext* context, MIDIAccessPromise* promise)
59 : ActiveDOMObject(context)
62 , m_sysExEnabled(false)
65 ScriptWrappable::init(this);
66 m_accessor = MIDIAccessor::create(this);
69 void MIDIAccess::setSysExEnabled(bool enable)
72 m_sysExEnabled = enable;
74 m_accessor->startSession();
79 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version)
81 ASSERT(isMainThread());
83 m_inputs.append(MIDIInput::create(this, executionContext(), id, manufacturer, name, version));
86 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
88 ASSERT(isMainThread());
90 unsigned portIndex = m_outputs.size();
91 m_outputs.append(MIDIOutput::create(this, portIndex, executionContext(), id, manufacturer, name, version));
94 void MIDIAccess::didStartSession(bool success)
96 ASSERT(isMainThread());
98 m_hasAccess = success;
100 m_promise->fulfill();
102 m_promise->reject(DOMError::create("InvalidStateError"));
105 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp)
107 ASSERT(isMainThread());
109 if (m_hasAccess && portIndex < m_inputs.size()) {
110 // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime()
111 // into time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now().
112 // This is how timestamps are defined in the Web MIDI spec.
113 Document* document = toDocument(executionContext());
116 double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp);
118 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds);
122 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds)
124 if (m_hasAccess && portIndex < m_outputs.size() && data && length > 0) {
125 // Convert from a time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now()
126 // into a time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime().
129 if (!timeStampInMilliseconds) {
130 // We treat a value of 0 (which is the default value) as special, meaning "now".
131 // We need to translate it exactly to 0 seconds.
134 Document* document = toDocument(executionContext());
136 double documentStartTime = document->loader()->timing()->referenceMonotonicTime();
137 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
140 m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
144 void MIDIAccess::stop()
149 m_requesting = false;
150 Document* document = toDocument(executionContext());
152 MIDIController* controller = MIDIController::from(document->page());
154 controller->cancelSysExPermissionRequest(this);
159 void MIDIAccess::startRequest()
161 if (!m_promise->options()->sysex) {
162 m_accessor->startSession();
165 Document* document = toDocument(executionContext());
167 MIDIController* controller = MIDIController::from(document->page());
170 controller->requestSysExPermission(this);
176 void MIDIAccess::permissionDenied()
178 ASSERT(isMainThread());
181 m_promise->reject(DOMError::create("SecurityError"));
184 } // namespace WebCore