OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 MIDIAccess::~MIDIAccess() | 55 MIDIAccess::~MIDIAccess() |
56 { | 56 { |
57 stop(); | 57 stop(); |
58 } | 58 } |
59 | 59 |
60 MIDIAccess::MIDIAccess(ScriptExecutionContext* context, MIDIAccessPromise* promi
se) | 60 MIDIAccess::MIDIAccess(ScriptExecutionContext* context, MIDIAccessPromise* promi
se) |
61 : ActiveDOMObject(context) | 61 : ActiveDOMObject(context) |
62 , m_promise(promise) | 62 , m_promise(promise) |
63 , m_hasAccess(false) | 63 , m_hasAccess(false) |
64 , m_enableSysEx(false) | 64 , m_sysExEnabled(false) |
65 , m_requesting(false) | 65 , m_requesting(false) |
66 { | 66 { |
67 ScriptWrappable::init(this); | 67 ScriptWrappable::init(this); |
68 m_accessor = MIDIAccessor::create(this); | 68 m_accessor = MIDIAccessor::create(this); |
69 } | 69 } |
70 | 70 |
71 void MIDIAccess::enableSysEx(bool enable) | 71 void MIDIAccess::setSysExEnabled(bool enable) |
72 { | 72 { |
73 m_requesting = false; | 73 m_requesting = false; |
74 m_enableSysEx = enable; | 74 m_sysExEnabled = enable; |
75 if (enable) | 75 if (enable) |
76 m_accessor->startSession(); | 76 m_accessor->startSession(); |
77 else | 77 else |
78 permissionDenied(); | 78 permissionDenied(); |
79 } | 79 } |
80 | 80 |
81 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c
onst String& name, const String& version) | 81 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c
onst String& name, const String& version) |
82 { | 82 { |
83 ASSERT(isMainThread()); | 83 ASSERT(isMainThread()); |
84 | 84 |
85 // FIXME: Pass m_enableSysEx flag to filter system exclusive messages correc
tly. | 85 // FIXME: Pass in |this| to create() method so we can filter system exclusiv
e messages correctly. |
86 m_inputs.append(MIDIInput::create(scriptExecutionContext(), id, manufacturer
, name, version)); | 86 m_inputs.append(MIDIInput::create(scriptExecutionContext(), id, manufacturer
, name, version)); |
87 } | 87 } |
88 | 88 |
89 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer,
const String& name, const String& version) | 89 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer,
const String& name, const String& version) |
90 { | 90 { |
91 ASSERT(isMainThread()); | 91 ASSERT(isMainThread()); |
92 | 92 |
93 // FIXME: Pass m_enableSysEx flag to filter system exclusive messages correc
tly. | 93 unsigned portIndex = m_outputs.size(); |
94 m_outputs.append(MIDIOutput::create(scriptExecutionContext(), id, manufactur
er, name, version)); | 94 m_outputs.append(MIDIOutput::create(this, portIndex, scriptExecutionContext(
), id, manufacturer, name, version)); |
95 } | 95 } |
96 | 96 |
97 void MIDIAccess::didStartSession() | 97 void MIDIAccess::didStartSession() |
98 { | 98 { |
99 ASSERT(isMainThread()); | 99 ASSERT(isMainThread()); |
100 | 100 |
101 m_hasAccess = true; | 101 m_hasAccess = true; |
102 m_promise->fulfill(); | 102 m_promise->fulfill(); |
103 } | 103 } |
104 | 104 |
105 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat
a, size_t length, double timeStamp) | 105 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat
a, size_t length, double timeStamp) |
106 { | 106 { |
107 ASSERT(isMainThread()); | 107 ASSERT(isMainThread()); |
108 | 108 |
109 if (m_hasAccess && portIndex < m_inputs.size()) { | 109 if (m_hasAccess && portIndex < m_inputs.size()) { |
110 // Convert from time in seconds which is based on the time coordinate sy
stem of monotonicallyIncreasingTime() | 110 // Convert from time in seconds which is based on the time coordinate sy
stem of monotonicallyIncreasingTime() |
111 // into time in milliseconds (a DOMHighResTimeStamp) according to the sa
me time coordinate system as performance.now(). | 111 // into time in milliseconds (a DOMHighResTimeStamp) according to the sa
me time coordinate system as performance.now(). |
112 // This is how timestamps are defined in the Web MIDI spec. | 112 // This is how timestamps are defined in the Web MIDI spec. |
113 Document* document = toDocument(scriptExecutionContext()); | 113 Document* document = toDocument(scriptExecutionContext()); |
114 ASSERT(document); | 114 ASSERT(document); |
115 | 115 |
116 double timeStampInMilliseconds = 1000 * document->loader()->timing()->mo
notonicTimeToZeroBasedDocumentTime(timeStamp); | 116 double timeStampInMilliseconds = 1000 * document->loader()->timing()->mo
notonicTimeToZeroBasedDocumentTime(timeStamp); |
117 | 117 |
118 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeSta
mpInMilliseconds); | 118 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeSta
mpInMilliseconds); |
119 } | 119 } |
120 } | 120 } |
121 | 121 |
| 122 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, siz
e_t length, double timeStampInMilliseconds) |
| 123 { |
| 124 if (m_hasAccess && portIndex < m_outputs.size() && data && length > 1) { |
| 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 o
f monotonicallyIncreasingTime(). |
| 127 double timeStamp; |
| 128 |
| 129 if (!timeStampInMilliseconds) { |
| 130 // We treat a value of 0 (which is the default value) as special, me
aning "now". |
| 131 // We need to translate it exactly to 0 seconds. |
| 132 timeStamp = 0; |
| 133 } else { |
| 134 Document* document = toDocument(scriptExecutionContext()); |
| 135 ASSERT(document); |
| 136 double documentStartTime = document->loader()->timing()->referenceMo
notonicTime(); |
| 137 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds; |
| 138 } |
| 139 |
| 140 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); |
| 141 } |
| 142 } |
| 143 |
122 void MIDIAccess::stop() | 144 void MIDIAccess::stop() |
123 { | 145 { |
124 m_hasAccess = false; | 146 m_hasAccess = false; |
125 if (!m_requesting) | 147 if (!m_requesting) |
126 return; | 148 return; |
127 m_requesting = false; | 149 m_requesting = false; |
128 Document* document = toDocument(scriptExecutionContext()); | 150 Document* document = toDocument(scriptExecutionContext()); |
129 ASSERT(document); | 151 ASSERT(document); |
130 MIDIController* controller = MIDIController::from(document->page()); | 152 MIDIController* controller = MIDIController::from(document->page()); |
131 ASSERT(controller); | 153 ASSERT(controller); |
132 controller->cancelSysExPermissionRequest(this); | 154 controller->cancelSysExPermissionRequest(this); |
| 155 |
| 156 m_accessor.clear(); |
133 } | 157 } |
134 | 158 |
135 void MIDIAccess::startRequest() | 159 void MIDIAccess::startRequest() |
136 { | 160 { |
137 if (!m_promise->options()->sysex) { | 161 if (!m_promise->options()->sysex) { |
138 m_accessor->startSession(); | 162 m_accessor->startSession(); |
139 return; | 163 return; |
140 } | 164 } |
141 Document* document = toDocument(scriptExecutionContext()); | 165 Document* document = toDocument(scriptExecutionContext()); |
142 ASSERT(document); | 166 ASSERT(document); |
143 MIDIController* controller = MIDIController::from(document->page()); | 167 MIDIController* controller = MIDIController::from(document->page()); |
144 if (controller) { | 168 if (controller) { |
145 m_requesting = true; | 169 m_requesting = true; |
146 controller->requestSysExPermission(this); | 170 controller->requestSysExPermission(this); |
147 } else { | 171 } else { |
148 permissionDenied(); | 172 permissionDenied(); |
149 } | 173 } |
150 } | 174 } |
151 | 175 |
152 void MIDIAccess::permissionDenied() | 176 void MIDIAccess::permissionDenied() |
153 { | 177 { |
154 ASSERT(isMainThread()); | 178 ASSERT(isMainThread()); |
155 | 179 |
156 m_hasAccess = false; | 180 m_hasAccess = false; |
157 RefPtr<DOMError> error = DOMError::create("SecurityError"); | 181 RefPtr<DOMError> error = DOMError::create("SecurityError"); |
158 m_promise->reject(error); | 182 m_promise->reject(error); |
159 } | 183 } |
160 | 184 |
161 | |
162 | |
163 } // namespace WebCore | 185 } // namespace WebCore |
OLD | NEW |