- add third_party src.
[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/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"
44
45 namespace WebCore {
46
47 PassRefPtr<MIDIAccess> MIDIAccess::create(ExecutionContext* context, MIDIAccessPromise* promise)
48 {
49     RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(context, promise)));
50     midiAccess->suspendIfNeeded();
51     midiAccess->startRequest();
52     return midiAccess.release();
53 }
54
55 MIDIAccess::~MIDIAccess()
56 {
57     stop();
58 }
59
60 MIDIAccess::MIDIAccess(ExecutionContext* context, MIDIAccessPromise* promise)
61     : ActiveDOMObject(context)
62     , m_promise(promise)
63     , m_hasAccess(false)
64     , m_sysExEnabled(false)
65     , m_requesting(false)
66 {
67     ScriptWrappable::init(this);
68     m_accessor = MIDIAccessor::create(this);
69 }
70
71 void MIDIAccess::setSysExEnabled(bool enable)
72 {
73     m_requesting = false;
74     m_sysExEnabled = enable;
75     if (enable)
76         m_accessor->startSession();
77     else
78         permissionDenied();
79 }
80
81 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version)
82 {
83     ASSERT(isMainThread());
84
85     m_inputs.append(MIDIInput::create(this, executionContext(), id, manufacturer, name, version));
86 }
87
88 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
89 {
90     ASSERT(isMainThread());
91
92     unsigned portIndex = m_outputs.size();
93     m_outputs.append(MIDIOutput::create(this, portIndex, executionContext(), id, manufacturer, name, version));
94 }
95
96 void MIDIAccess::didStartSession(bool success)
97 {
98     ASSERT(isMainThread());
99
100     m_hasAccess = success;
101     if (success)
102         m_promise->fulfill();
103     else
104         m_promise->reject(DOMError::create("InvalidStateError"));
105 }
106
107 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp)
108 {
109     ASSERT(isMainThread());
110
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());
116         ASSERT(document);
117
118         double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp);
119
120         m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds);
121     }
122 }
123
124 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds)
125 {
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().
129         double timeStamp;
130
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.
134             timeStamp = 0;
135         } else {
136             Document* document = toDocument(executionContext());
137             ASSERT(document);
138             double documentStartTime = document->loader()->timing()->referenceMonotonicTime();
139             timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
140         }
141
142         m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
143     }
144 }
145
146 void MIDIAccess::stop()
147 {
148     m_hasAccess = false;
149     if (!m_requesting)
150         return;
151     m_requesting = false;
152     Document* document = toDocument(executionContext());
153     ASSERT(document);
154     MIDIController* controller = MIDIController::from(document->page());
155     ASSERT(controller);
156     controller->cancelSysExPermissionRequest(this);
157
158     m_accessor.clear();
159 }
160
161 void MIDIAccess::startRequest()
162 {
163     if (!m_promise->options()->sysex) {
164         m_accessor->startSession();
165         return;
166     }
167     Document* document = toDocument(executionContext());
168     ASSERT(document);
169     MIDIController* controller = MIDIController::from(document->page());
170     if (controller) {
171         m_requesting = true;
172         controller->requestSysExPermission(this);
173     } else {
174         permissionDenied();
175     }
176 }
177
178 void MIDIAccess::permissionDenied()
179 {
180     ASSERT(isMainThread());
181
182     m_hasAccess = false;
183     m_promise->reject(DOMError::create("SecurityError"));
184 }
185
186 } // namespace WebCore