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/MIDIInput.h"
42 #include "modules/webmidi/MIDIOutput.h"
43 #include "modules/webmidi/MIDIPort.h"
47 PassRefPtr<MIDIAccess> MIDIAccess::create(ExecutionContext* context, MIDIAccessPromise* promise)
49 RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(context, promise)));
50 midiAccess->suspendIfNeeded();
51 midiAccess->startRequest();
52 return midiAccess.release();
55 MIDIAccess::~MIDIAccess()
60 MIDIAccess::MIDIAccess(ExecutionContext* context, MIDIAccessPromise* promise)
61 : ActiveDOMObject(context)
64 , m_sysExEnabled(false)
67 ScriptWrappable::init(this);
68 m_accessor = MIDIAccessor::create(this);
71 void MIDIAccess::setSysExEnabled(bool enable)
74 m_sysExEnabled = enable;
76 m_accessor->startSession();
81 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version)
83 ASSERT(isMainThread());
85 m_inputs.append(MIDIInput::create(this, executionContext(), id, manufacturer, name, version));
88 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
90 ASSERT(isMainThread());
92 unsigned portIndex = m_outputs.size();
93 m_outputs.append(MIDIOutput::create(this, portIndex, executionContext(), id, manufacturer, name, version));
96 void MIDIAccess::didStartSession(bool success)
98 ASSERT(isMainThread());
100 m_hasAccess = success;
102 m_promise->fulfill();
104 m_promise->reject(DOMError::create("InvalidStateError"));
107 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp)
109 ASSERT(isMainThread());
111 if (m_hasAccess && portIndex < m_inputs.size()) {
112 // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime()
113 // into time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now().
114 // This is how timestamps are defined in the Web MIDI spec.
115 Document* document = toDocument(executionContext());
118 double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp);
120 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds);
124 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds)
126 if (m_hasAccess && portIndex < m_outputs.size() && data && length > 1) {
127 // Convert from a time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now()
128 // into a time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime().
131 if (!timeStampInMilliseconds) {
132 // We treat a value of 0 (which is the default value) as special, meaning "now".
133 // We need to translate it exactly to 0 seconds.
136 Document* document = toDocument(executionContext());
138 double documentStartTime = document->loader()->timing()->referenceMonotonicTime();
139 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
142 m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
146 void MIDIAccess::stop()
151 m_requesting = false;
152 Document* document = toDocument(executionContext());
154 MIDIController* controller = MIDIController::from(document->page());
156 controller->cancelSysExPermissionRequest(this);
161 void MIDIAccess::startRequest()
163 if (!m_promise->options()->sysex) {
164 m_accessor->startSession();
167 Document* document = toDocument(executionContext());
169 MIDIController* controller = MIDIController::from(document->page());
172 controller->requestSysExPermission(this);
178 void MIDIAccess::permissionDenied()
180 ASSERT(isMainThread());
183 m_promise->reject(DOMError::create("SecurityError"));
186 } // namespace WebCore