Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webmidi / MIDIAccess.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "modules/webmidi/MIDIAccess.h"
33
34 #include "core/dom/Document.h"
35 #include "core/loader/DocumentLoadTiming.h"
36 #include "core/loader/DocumentLoader.h"
37 #include "modules/webmidi/MIDIAccessInitializer.h"
38 #include "modules/webmidi/MIDIConnectionEvent.h"
39 #include "modules/webmidi/MIDIController.h"
40 #include "modules/webmidi/MIDIInput.h"
41 #include "modules/webmidi/MIDIInputMap.h"
42 #include "modules/webmidi/MIDIOutput.h"
43 #include "modules/webmidi/MIDIOutputMap.h"
44 #include "modules/webmidi/MIDIPort.h"
45 #include "platform/AsyncMethodRunner.h"
46 #include <v8.h>
47
48 namespace blink {
49
50 MIDIAccess::MIDIAccess(PassOwnPtr<MIDIAccessor> accessor, bool sysexEnabled, const Vector<MIDIAccessInitializer::PortDescriptor>& ports, ExecutionContext* executionContext)
51     : ActiveDOMObject(executionContext)
52     , m_accessor(accessor)
53     , m_sysexEnabled(sysexEnabled)
54 {
55     m_accessor->setClient(this);
56     for (size_t i = 0; i < ports.size(); ++i) {
57         const MIDIAccessInitializer::PortDescriptor& port = ports[i];
58         if (port.type == MIDIPort::MIDIPortTypeInput) {
59             m_inputs.append(MIDIInput::create(this, port.id, port.manufacturer, port.name, port.version, port.isActive));
60         } else {
61             m_outputs.append(MIDIOutput::create(this, m_outputs.size(), port.id, port.manufacturer, port.name, port.version, port.isActive));
62         }
63     }
64 }
65
66 MIDIAccess::~MIDIAccess()
67 {
68 }
69
70 MIDIInputMap* MIDIAccess::inputs() const
71 {
72     HeapHashMap<String, Member<MIDIInput> > inputs;
73     size_t inactiveCount = 0;
74     for (size_t i = 0; i < m_inputs.size(); ++i) {
75         MIDIInput* input = m_inputs[i];
76         if (input->isActive())
77             inputs.add(input->id(), input);
78         else
79             inactiveCount++;
80     }
81     if ((inputs.size() + inactiveCount) != m_inputs.size()) {
82         // There is id duplication that violates the spec.
83         inputs.clear();
84     }
85     return new MIDIInputMap(inputs);
86 }
87
88 MIDIOutputMap* MIDIAccess::outputs() const
89 {
90     HeapHashMap<String, Member<MIDIOutput> > outputs;
91     size_t inactiveCount = 0;
92     for (size_t i = 0; i < m_outputs.size(); ++i) {
93         MIDIOutput* output = m_outputs[i];
94         if (output->isActive())
95             outputs.add(output->id(), output);
96         else
97             inactiveCount++;
98     }
99     if ((outputs.size() + inactiveCount) != m_outputs.size()) {
100         // There is id duplication that violates the spec.
101         outputs.clear();
102     }
103     return new MIDIOutputMap(outputs);
104 }
105
106 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version, bool isActive)
107 {
108     ASSERT(isMainThread());
109     m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version, isActive));
110 }
111
112 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version, bool isActive)
113 {
114     ASSERT(isMainThread());
115     unsigned portIndex = m_outputs.size();
116     m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version, isActive));
117 }
118
119 void MIDIAccess::didSetInputPortState(unsigned portIndex, bool isActive)
120 {
121     ASSERT(isMainThread());
122     if (portIndex < m_inputs.size())
123         m_inputs[portIndex]->setActiveState(isActive);
124 }
125
126 void MIDIAccess::didSetOutputPortState(unsigned portIndex, bool isActive)
127 {
128     ASSERT(isMainThread());
129     if (portIndex < m_outputs.size())
130         m_outputs[portIndex]->setActiveState(isActive);
131 }
132
133 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp)
134 {
135     ASSERT(isMainThread());
136     if (portIndex >= m_inputs.size())
137         return;
138
139     // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime()
140     // into time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now().
141     // This is how timestamps are defined in the Web MIDI spec.
142     Document* document = toDocument(executionContext());
143     ASSERT(document);
144
145     double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp);
146
147     m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds);
148 }
149
150 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds)
151 {
152     if (!data || !length || portIndex >= m_outputs.size())
153         return;
154     // Convert from a time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now()
155     // into a time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime().
156     double timeStamp;
157
158     if (!timeStampInMilliseconds) {
159         // We treat a value of 0 (which is the default value) as special, meaning "now".
160         // We need to translate it exactly to 0 seconds.
161         timeStamp = 0;
162     } else {
163         Document* document = toDocument(executionContext());
164         ASSERT(document);
165         double documentStartTime = document->loader()->timing()->referenceMonotonicTime();
166         timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
167     }
168
169     m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
170 }
171
172 void MIDIAccess::stop()
173 {
174     m_accessor.clear();
175 }
176
177 void MIDIAccess::trace(Visitor* visitor)
178 {
179     visitor->trace(m_inputs);
180     visitor->trace(m_outputs);
181     EventTargetWithInlineData::trace(visitor);
182 }
183
184 } // namespace blink