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 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/webmidi/MIDIAccess.h" | 32 #include "modules/webmidi/MIDIAccess.h" |
33 | 33 |
34 #include "bindings/v8/MIDIAccessResolver.h" | |
35 #include "bindings/v8/ScriptPromise.h" | |
36 #include "bindings/v8/ScriptPromiseResolver.h" | |
37 #include "bindings/v8/V8Binding.h" | |
38 #include "core/dom/DOMError.h" | 34 #include "core/dom/DOMError.h" |
39 #include "core/dom/Document.h" | 35 #include "core/dom/Document.h" |
40 #include "core/loader/DocumentLoadTiming.h" | 36 #include "core/loader/DocumentLoadTiming.h" |
41 #include "core/loader/DocumentLoader.h" | 37 #include "core/loader/DocumentLoader.h" |
| 38 #include "modules/webmidi/MIDIAccessPromise.h" |
42 #include "modules/webmidi/MIDIConnectionEvent.h" | 39 #include "modules/webmidi/MIDIConnectionEvent.h" |
43 #include "modules/webmidi/MIDIController.h" | 40 #include "modules/webmidi/MIDIController.h" |
44 #include "modules/webmidi/MIDIOptions.h" | |
45 #include "modules/webmidi/MIDIPort.h" | 41 #include "modules/webmidi/MIDIPort.h" |
46 #include "platform/AsyncMethodRunner.h" | |
47 | 42 |
48 namespace WebCore { | 43 namespace WebCore { |
49 | 44 |
50 class MIDIAccess::PostAction : public ScriptFunction { | 45 PassRefPtrWillBeRawPtr<MIDIAccess> MIDIAccess::create(ExecutionContext* context,
MIDIAccessPromise* promise) |
51 public: | |
52 static PassOwnPtr<MIDIAccess::PostAction> create(v8::Isolate* isolate, WeakP
tr<MIDIAccess> owner, State state) { return adoptPtr(new PostAction(isolate, own
er, state)); } | |
53 | |
54 private: | |
55 PostAction(v8::Isolate* isolate, WeakPtr<MIDIAccess> owner, State state): Sc
riptFunction(isolate), m_owner(owner), m_state(state) { } | |
56 virtual ScriptValue call(ScriptValue value) OVERRIDE | |
57 { | |
58 if (!m_owner.get()) | |
59 return value; | |
60 m_owner->doPostAction(m_state); | |
61 return value; | |
62 } | |
63 | |
64 WeakPtr<MIDIAccess> m_owner; | |
65 State m_state; | |
66 }; | |
67 | |
68 ScriptPromise MIDIAccess::request(const MIDIOptions& options, ExecutionContext*
context) | |
69 { | 46 { |
70 RefPtrWillBeRawPtr<MIDIAccess> midiAccess(adoptRefWillBeRefCountedGarbageCol
lected(new MIDIAccess(options, context))); | 47 RefPtrWillBeRawPtr<MIDIAccess> midiAccess(adoptRefWillBeRefCountedGarbageCol
lected(new MIDIAccess(context, promise))); |
71 midiAccess->suspendIfNeeded(); | 48 midiAccess->suspendIfNeeded(); |
72 // Create a wrapper to expose this object to the V8 GC so that | 49 midiAccess->startRequest(); |
73 // hasPendingActivity takes effect. | 50 return midiAccess.release(); |
74 toV8NoInline(midiAccess.get(), context); | |
75 // Now this object is retained because m_state equals to Requesting. | |
76 return midiAccess->startRequest(); | |
77 } | 51 } |
78 | 52 |
79 MIDIAccess::~MIDIAccess() | 53 MIDIAccess::~MIDIAccess() |
80 { | 54 { |
| 55 stop(); |
81 } | 56 } |
82 | 57 |
83 MIDIAccess::MIDIAccess(const MIDIOptions& options, ExecutionContext* context) | 58 MIDIAccess::MIDIAccess(ExecutionContext* context, MIDIAccessPromise* promise) |
84 : ActiveDOMObject(context) | 59 : ActiveDOMObject(context) |
85 , m_state(Requesting) | 60 , m_promise(promise) |
86 , m_weakPtrFactory(this) | 61 , m_hasAccess(false) |
87 , m_options(options) | |
88 , m_sysExEnabled(false) | 62 , m_sysExEnabled(false) |
89 , m_asyncResolveRunner(this, &MIDIAccess::resolveNow) | 63 , m_requesting(false) |
90 , m_asyncRejectRunner(this, &MIDIAccess::rejectNow) | |
91 { | 64 { |
92 ScriptWrappable::init(this); | 65 ScriptWrappable::init(this); |
93 m_accessor = MIDIAccessor::create(this); | 66 m_accessor = MIDIAccessor::create(this); |
94 } | 67 } |
95 | 68 |
96 void MIDIAccess::setSysExEnabled(bool enable) | 69 void MIDIAccess::setSysExEnabled(bool enable) |
97 { | 70 { |
| 71 m_requesting = false; |
98 m_sysExEnabled = enable; | 72 m_sysExEnabled = enable; |
99 if (enable) { | 73 if (enable) |
100 m_accessor->startSession(); | 74 m_accessor->startSession(); |
101 } else { | 75 else |
102 reject(DOMError::create("SecurityError")); | 76 permissionDenied(); |
103 } | |
104 } | 77 } |
105 | 78 |
106 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c
onst String& name, const String& version) | 79 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c
onst String& name, const String& version) |
107 { | 80 { |
108 ASSERT(isMainThread()); | 81 ASSERT(isMainThread()); |
109 | 82 |
110 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version)); | 83 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version)); |
111 } | 84 } |
112 | 85 |
113 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer,
const String& name, const String& version) | 86 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer,
const String& name, const String& version) |
114 { | 87 { |
115 ASSERT(isMainThread()); | 88 ASSERT(isMainThread()); |
116 | 89 |
117 unsigned portIndex = m_outputs.size(); | 90 unsigned portIndex = m_outputs.size(); |
118 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name,
version)); | 91 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name,
version)); |
119 } | 92 } |
120 | 93 |
121 void MIDIAccess::didStartSession(bool success) | 94 void MIDIAccess::didStartSession(bool success) |
122 { | 95 { |
123 ASSERT(isMainThread()); | 96 ASSERT(isMainThread()); |
| 97 |
| 98 m_hasAccess = success; |
124 if (success) | 99 if (success) |
125 resolve(); | 100 m_promise->fulfill(); |
126 else | 101 else |
127 reject(DOMError::create("InvalidStateError")); | 102 m_promise->reject(DOMError::create("InvalidStateError")); |
128 } | 103 } |
129 | 104 |
130 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) |
131 { | 106 { |
132 ASSERT(isMainThread()); | 107 ASSERT(isMainThread()); |
133 | 108 |
134 if (m_state == Resolved && portIndex < m_inputs.size()) { | 109 if (m_hasAccess && portIndex < m_inputs.size()) { |
135 // 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() |
136 // 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(). |
137 // This is how timestamps are defined in the Web MIDI spec. | 112 // This is how timestamps are defined in the Web MIDI spec. |
138 Document* document = toDocument(executionContext()); | 113 Document* document = toDocument(executionContext()); |
139 ASSERT(document); | 114 ASSERT(document); |
140 | 115 |
141 double timeStampInMilliseconds = 1000 * document->loader()->timing()->mo
notonicTimeToZeroBasedDocumentTime(timeStamp); | 116 double timeStampInMilliseconds = 1000 * document->loader()->timing()->mo
notonicTimeToZeroBasedDocumentTime(timeStamp); |
142 | 117 |
143 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeSta
mpInMilliseconds); | 118 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeSta
mpInMilliseconds); |
144 } | 119 } |
145 } | 120 } |
146 | 121 |
147 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, siz
e_t length, double timeStampInMilliseconds) | 122 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, siz
e_t length, double timeStampInMilliseconds) |
148 { | 123 { |
149 if (m_state == Resolved && portIndex < m_outputs.size() && data && length >
0) { | 124 if (m_hasAccess && portIndex < m_outputs.size() && data && length > 0) { |
150 // Convert from a time in milliseconds (a DOMHighResTimeStamp) according
to the same time coordinate system as performance.now() | 125 // Convert from a time in milliseconds (a DOMHighResTimeStamp) according
to the same time coordinate system as performance.now() |
151 // into a time in seconds which is based on the time coordinate system o
f monotonicallyIncreasingTime(). | 126 // into a time in seconds which is based on the time coordinate system o
f monotonicallyIncreasingTime(). |
152 double timeStamp; | 127 double timeStamp; |
153 | 128 |
154 if (!timeStampInMilliseconds) { | 129 if (!timeStampInMilliseconds) { |
155 // We treat a value of 0 (which is the default value) as special, me
aning "now". | 130 // We treat a value of 0 (which is the default value) as special, me
aning "now". |
156 // We need to translate it exactly to 0 seconds. | 131 // We need to translate it exactly to 0 seconds. |
157 timeStamp = 0; | 132 timeStamp = 0; |
158 } else { | 133 } else { |
159 Document* document = toDocument(executionContext()); | 134 Document* document = toDocument(executionContext()); |
160 ASSERT(document); | 135 ASSERT(document); |
161 double documentStartTime = document->loader()->timing()->referenceMo
notonicTime(); | 136 double documentStartTime = document->loader()->timing()->referenceMo
notonicTime(); |
162 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds; | 137 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds; |
163 } | 138 } |
164 | 139 |
165 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); | 140 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); |
166 } | 141 } |
167 } | 142 } |
168 | 143 |
169 void MIDIAccess::suspend() | |
170 { | |
171 m_asyncResolveRunner.suspend(); | |
172 m_asyncRejectRunner.suspend(); | |
173 } | |
174 | |
175 void MIDIAccess::resume() | |
176 { | |
177 m_asyncResolveRunner.resume(); | |
178 m_asyncRejectRunner.resume(); | |
179 } | |
180 | |
181 void MIDIAccess::stop() | 144 void MIDIAccess::stop() |
182 { | 145 { |
183 if (m_state == Stopped) | 146 m_hasAccess = false; |
| 147 if (!m_requesting) |
184 return; | 148 return; |
185 m_error.clear(); | 149 m_requesting = false; |
186 m_accessor.clear(); | 150 Document* document = toDocument(executionContext()); |
187 m_asyncResolveRunner.stop(); | 151 ASSERT(document); |
188 m_asyncRejectRunner.stop(); | 152 MIDIController* controller = MIDIController::from(document->page()); |
189 m_weakPtrFactory.revokeAll(); | 153 ASSERT(controller); |
190 if (m_state == Requesting) { | 154 controller->cancelSysExPermissionRequest(this); |
191 Document* document = toDocument(executionContext()); | |
192 ASSERT(document); | |
193 MIDIController* controller = MIDIController::from(document->page()); | |
194 ASSERT(controller); | |
195 controller->cancelSysExPermissionRequest(this); | |
196 } | |
197 m_state = Stopped; | |
198 } | 155 } |
199 | 156 |
200 bool MIDIAccess::hasPendingActivity() const | 157 void MIDIAccess::startRequest() |
201 { | 158 { |
202 return m_state == Requesting; | 159 if (!m_promise->options()->sysex) { |
| 160 m_accessor->startSession(); |
| 161 return; |
| 162 } |
| 163 Document* document = toDocument(executionContext()); |
| 164 ASSERT(document); |
| 165 MIDIController* controller = MIDIController::from(document->page()); |
| 166 if (controller) { |
| 167 m_requesting = true; |
| 168 controller->requestSysExPermission(this); |
| 169 } else { |
| 170 permissionDenied(); |
| 171 } |
203 } | 172 } |
204 | 173 |
205 void MIDIAccess::permissionDenied() | 174 void MIDIAccess::permissionDenied() |
206 { | 175 { |
207 ASSERT(isMainThread()); | 176 ASSERT(isMainThread()); |
208 reject(DOMError::create("SecurityError")); | |
209 } | |
210 | 177 |
211 ScriptPromise MIDIAccess::startRequest() | 178 m_hasAccess = false; |
212 { | 179 m_promise->reject(DOMError::create("SecurityError")); |
213 m_resolver = MIDIAccessResolver::create(ScriptPromiseResolver::create(execut
ionContext()), toIsolate(executionContext())); | |
214 ScriptPromise promise = m_resolver->promise(); | |
215 promise.then(PostAction::create(toIsolate(executionContext()), m_weakPtrFact
ory.createWeakPtr(), Resolved), | |
216 PostAction::create(toIsolate(executionContext()), m_weakPtrFactory.creat
eWeakPtr(), Stopped)); | |
217 | |
218 if (!m_options.sysex) { | |
219 m_accessor->startSession(); | |
220 return promise; | |
221 } | |
222 Document* document = toDocument(executionContext()); | |
223 ASSERT(document); | |
224 MIDIController* controller = MIDIController::from(document->page()); | |
225 if (controller) { | |
226 controller->requestSysExPermission(this); | |
227 } else { | |
228 reject(DOMError::create("SecurityError")); | |
229 } | |
230 return promise; | |
231 } | |
232 | |
233 void MIDIAccess::resolve() | |
234 { | |
235 m_asyncResolveRunner.runAsync(); | |
236 } | |
237 | |
238 void MIDIAccess::reject(PassRefPtr<DOMError> error) | |
239 { | |
240 m_error = error; | |
241 m_asyncRejectRunner.runAsync(); | |
242 } | |
243 | |
244 void MIDIAccess::resolveNow() | |
245 { | |
246 m_resolver->resolve(this, executionContext()); | |
247 } | |
248 | |
249 void MIDIAccess::rejectNow() | |
250 { | |
251 m_resolver->reject(m_error.release().get(), executionContext()); | |
252 } | |
253 | |
254 void MIDIAccess::doPostAction(State state) | |
255 { | |
256 ASSERT(m_state == Requesting); | |
257 ASSERT(state == Resolved || state == Stopped); | |
258 m_error.clear(); | |
259 if (state == Stopped) { | |
260 m_accessor.clear(); | |
261 } | |
262 m_weakPtrFactory.revokeAll(); | |
263 m_state = state; | |
264 } | 180 } |
265 | 181 |
266 void MIDIAccess::trace(Visitor* visitor) | 182 void MIDIAccess::trace(Visitor* visitor) |
267 { | 183 { |
268 visitor->trace(m_inputs); | 184 visitor->trace(m_inputs); |
269 visitor->trace(m_outputs); | 185 visitor->trace(m_outputs); |
| 186 visitor->trace(m_promise); |
270 } | 187 } |
271 | 188 |
272 } // namespace WebCore | 189 } // namespace WebCore |
OLD | NEW |