Chromium Code Reviews| 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/DOMRequestState.h" | |
| 35 #include "bindings/v8/DOMWrapperWorld.h" | |
| 36 #include "bindings/v8/ScriptPromise.h" | |
| 37 #include "bindings/v8/ScriptPromiseResolver.h" | |
| 38 #include "bindings/v8/V8Binding.h" | |
| 34 #include "core/dom/DOMError.h" | 39 #include "core/dom/DOMError.h" |
| 35 #include "core/dom/Document.h" | 40 #include "core/dom/Document.h" |
| 36 #include "core/loader/DocumentLoadTiming.h" | 41 #include "core/loader/DocumentLoadTiming.h" |
| 37 #include "core/loader/DocumentLoader.h" | 42 #include "core/loader/DocumentLoader.h" |
| 38 #include "modules/webmidi/MIDIAccessPromise.h" | |
| 39 #include "modules/webmidi/MIDIConnectionEvent.h" | 43 #include "modules/webmidi/MIDIConnectionEvent.h" |
| 40 #include "modules/webmidi/MIDIController.h" | 44 #include "modules/webmidi/MIDIController.h" |
| 45 #include "modules/webmidi/MIDIOptions.h" | |
| 41 #include "modules/webmidi/MIDIPort.h" | 46 #include "modules/webmidi/MIDIPort.h" |
| 47 #include "modules/webmidi/NavigatorWebMIDI.h" | |
| 42 | 48 |
| 43 namespace WebCore { | 49 namespace WebCore { |
| 44 | 50 |
| 45 PassRefPtrWillBeRawPtr<MIDIAccess> MIDIAccess::create(ExecutionContext* context, MIDIAccessPromise* promise) | 51 PassRefPtrWillBeRawPtr<MIDIAccess> MIDIAccess::create(const MIDIOptions& options , ExecutionContext* context) |
| 46 { | 52 { |
| 47 RefPtrWillBeRawPtr<MIDIAccess> midiAccess(adoptRefCountedWillBeRefCountedGar bageCollected(new MIDIAccess(context, promise))); | 53 RefPtrWillBeRawPtr<MIDIAccess> midiAccess(adoptRefCountedWillBeRefCountedGar bageCollected(new MIDIAccess(options, context))); |
| 48 midiAccess->suspendIfNeeded(); | 54 midiAccess->suspendIfNeeded(); |
| 49 midiAccess->startRequest(); | |
| 50 return midiAccess.release(); | 55 return midiAccess.release(); |
| 51 } | 56 } |
| 52 | 57 |
| 53 MIDIAccess::~MIDIAccess() | 58 MIDIAccess::~MIDIAccess() |
| 54 { | 59 { |
| 55 stop(); | 60 ASSERT(!m_requesting); |
| 56 } | 61 } |
| 57 | 62 |
| 58 MIDIAccess::MIDIAccess(ExecutionContext* context, MIDIAccessPromise* promise) | 63 MIDIAccess::MIDIAccess(const MIDIOptions& options, ExecutionContext* context) |
| 59 : ActiveDOMObject(context) | 64 : ActiveDOMObject(context) |
| 60 , m_promise(promise) | 65 , m_activeDOMObjectState(Active) |
| 66 , m_promiseState(Pending) | |
| 67 , m_options(options) | |
| 61 , m_hasAccess(false) | 68 , m_hasAccess(false) |
| 62 , m_sysExEnabled(false) | 69 , m_sysExEnabled(false) |
| 63 , m_requesting(false) | 70 , m_requesting(false) |
| 71 , m_resumeTimer(this, &MIDIAccess::resumeTimerFired) | |
| 64 { | 72 { |
| 65 ScriptWrappable::init(this); | 73 ScriptWrappable::init(this); |
| 66 m_accessor = MIDIAccessor::create(this); | 74 m_accessor = MIDIAccessor::create(this); |
| 67 } | 75 } |
| 68 | 76 |
| 69 void MIDIAccess::setSysExEnabled(bool enable) | 77 void MIDIAccess::setSysExEnabled(bool enable) |
| 70 { | 78 { |
| 71 m_requesting = false; | |
| 72 m_sysExEnabled = enable; | 79 m_sysExEnabled = enable; |
| 73 if (enable) | 80 if (enable) { |
| 74 m_accessor->startSession(); | 81 m_accessor->startSession(); |
| 75 else | 82 } else { |
| 76 permissionDenied(); | 83 reject(DOMError::create("SecurityError")); |
| 84 // |this| may be deleted here. | |
| 85 } | |
| 77 } | 86 } |
| 78 | 87 |
| 79 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c onst String& name, const String& version) | 88 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c onst String& name, const String& version) |
| 80 { | 89 { |
| 81 ASSERT(isMainThread()); | 90 ASSERT(isMainThread()); |
| 82 | 91 |
| 83 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version)); | 92 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version)); |
| 84 } | 93 } |
| 85 | 94 |
| 86 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) | 95 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) |
| 87 { | 96 { |
| 88 ASSERT(isMainThread()); | 97 ASSERT(isMainThread()); |
| 89 | 98 |
| 90 unsigned portIndex = m_outputs.size(); | 99 unsigned portIndex = m_outputs.size(); |
| 91 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version)); | 100 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version)); |
| 92 } | 101 } |
| 93 | 102 |
| 94 void MIDIAccess::didStartSession(bool success) | 103 void MIDIAccess::didStartSession(bool success) |
| 95 { | 104 { |
| 96 ASSERT(isMainThread()); | 105 ASSERT(isMainThread()); |
| 97 | 106 if (!m_requesting) |
| 98 m_hasAccess = success; | 107 return; |
| 99 if (success) | 108 if (success) |
| 100 m_promise->fulfill(); | 109 resolve(); |
| 101 else | 110 else |
| 102 m_promise->reject(DOMError::create("InvalidStateError")); | 111 reject(DOMError::create("InvalidStateError")); |
| 112 // |this| may be deleted here. | |
| 103 } | 113 } |
| 104 | 114 |
| 105 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat a, size_t length, double timeStamp) | 115 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat a, size_t length, double timeStamp) |
| 106 { | 116 { |
| 107 ASSERT(isMainThread()); | 117 ASSERT(isMainThread()); |
| 108 | 118 |
| 109 if (m_hasAccess && portIndex < m_inputs.size()) { | 119 if (m_hasAccess && portIndex < m_inputs.size()) { |
| 110 // Convert from time in seconds which is based on the time coordinate sy stem of monotonicallyIncreasingTime() | 120 // 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(). | 121 // 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. | 122 // This is how timestamps are defined in the Web MIDI spec. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 134 Document* document = toDocument(executionContext()); | 144 Document* document = toDocument(executionContext()); |
| 135 ASSERT(document); | 145 ASSERT(document); |
| 136 double documentStartTime = document->loader()->timing()->referenceMo notonicTime(); | 146 double documentStartTime = document->loader()->timing()->referenceMo notonicTime(); |
| 137 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds; | 147 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds; |
| 138 } | 148 } |
| 139 | 149 |
| 140 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); | 150 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); |
| 141 } | 151 } |
| 142 } | 152 } |
| 143 | 153 |
| 154 void MIDIAccess::suspend() | |
| 155 { | |
| 156 ASSERT(m_activeDOMObjectState == Active); | |
| 157 m_activeDOMObjectState = Suspended; | |
| 158 m_resumeTimer.stop(); | |
| 159 } | |
| 160 | |
| 161 void MIDIAccess::resume() | |
| 162 { | |
| 163 ASSERT(m_activeDOMObjectState == Suspended); | |
| 164 m_activeDOMObjectState = Active; | |
| 165 if (m_promiseState == Resolving || m_promiseState == Rejecting) | |
| 166 m_resumeTimer.startOneShot(0); | |
| 167 } | |
| 168 | |
| 144 void MIDIAccess::stop() | 169 void MIDIAccess::stop() |
| 145 { | 170 { |
| 171 RefPtrWillBeRawPtr<MIDIAccess> protect(this); | |
| 172 m_activeDOMObjectState = Stopped; | |
| 146 m_hasAccess = false; | 173 m_hasAccess = false; |
| 174 m_promiseState = ResolvedOrRejected; | |
| 175 m_error.clear(); | |
| 176 m_resumeTimer.stop(); | |
| 177 m_accessor.clear(); | |
| 147 if (!m_requesting) | 178 if (!m_requesting) |
| 148 return; | 179 return; |
| 149 m_requesting = false; | |
| 150 Document* document = toDocument(executionContext()); | 180 Document* document = toDocument(executionContext()); |
| 151 ASSERT(document); | 181 ASSERT(document); |
| 152 MIDIController* controller = MIDIController::from(document->page()); | 182 MIDIController* controller = MIDIController::from(document->page()); |
| 153 ASSERT(controller); | 183 ASSERT(controller); |
| 154 controller->cancelSysExPermissionRequest(this); | 184 controller->cancelSysExPermissionRequest(this); |
| 185 setRequesting(false); | |
| 155 } | 186 } |
| 156 | 187 |
| 157 void MIDIAccess::startRequest() | 188 void MIDIAccess::permissionDenied() |
| 158 { | 189 { |
| 159 if (!m_promise->options()->sysex) { | 190 ASSERT(isMainThread()); |
| 191 reject(DOMError::create("SecurityError")); | |
| 192 } | |
| 193 | |
| 194 void MIDIAccess::setRequesting(bool value) | |
| 195 { | |
| 196 ASSERT(m_requesting != value); | |
| 197 if (value) { | |
| 198 setPendingActivity(this); | |
| 199 m_requesting = true; | |
|
haraken
2014/02/26 11:30:39
Could you avoid using setPendingActivity/unsetPend
yhirano
2014/02/26 11:55:15
Because MIDIAccess is not exposed to the V8 GC at
haraken
2014/02/26 12:09:52
Then probably should we use ContextLifecycleObserv
yhirano
2014/02/26 12:16:37
MIDIAccess will be exposed to the GC, but it is no
yhirano
2014/02/26 12:37:12
Sorry, I was wrong. The wrapper will be created wh
haraken
2014/02/26 12:52:40
Let's discuss this issue offline tomorrow. I think
yhirano
2014/03/04 17:52:59
Done.
| |
| 200 } else { | |
| 201 m_requesting = false; | |
| 202 unsetPendingActivity(this); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 ScriptPromise MIDIAccess::startRequest() | |
| 207 { | |
| 208 ASSERT(!m_requesting); | |
| 209 ScriptPromise promise = ScriptPromise::createPending(); | |
| 210 m_resolver = MIDIAccessResolver::create(ScriptPromiseResolver::create(promis e, executionContext()), executionContext()); | |
| 211 setRequesting(true); | |
| 212 if (!m_options.sysex) { | |
| 160 m_accessor->startSession(); | 213 m_accessor->startSession(); |
| 161 return; | 214 return promise; |
| 162 } | 215 } |
| 163 Document* document = toDocument(executionContext()); | 216 Document* document = toDocument(executionContext()); |
| 164 ASSERT(document); | 217 ASSERT(document); |
| 165 MIDIController* controller = MIDIController::from(document->page()); | 218 MIDIController* controller = MIDIController::from(document->page()); |
| 166 if (controller) { | 219 if (controller) { |
| 167 m_requesting = true; | |
| 168 controller->requestSysExPermission(this); | 220 controller->requestSysExPermission(this); |
| 169 } else { | 221 } else { |
| 170 permissionDenied(); | 222 reject(DOMError::create("SecurityError")); |
| 223 // |this| may be deleted here. | |
| 224 } | |
| 225 return promise; | |
| 226 } | |
| 227 | |
| 228 void MIDIAccess::resolve() | |
| 229 { | |
| 230 if (m_promiseState != Pending || m_activeDOMObjectState == Stopped) | |
| 231 return; | |
| 232 if (m_activeDOMObjectState == Active) { | |
| 233 resolveNow(); | |
| 234 // |this| may be deleted here. | |
| 235 } else { | |
| 236 ASSERT(m_activeDOMObjectState == Suspended); | |
| 237 m_promiseState = Resolving; | |
| 171 } | 238 } |
| 172 } | 239 } |
| 173 | 240 |
| 174 void MIDIAccess::permissionDenied() | 241 void MIDIAccess::reject(PassRefPtr<DOMError> error) |
| 175 { | 242 { |
| 176 ASSERT(isMainThread()); | 243 if (m_promiseState != Pending || m_activeDOMObjectState == Stopped) |
| 244 return; | |
| 245 if (m_activeDOMObjectState == Active) { | |
| 246 rejectNow(error); | |
| 247 // |this| may be deleted here. | |
| 248 } else { | |
| 249 ASSERT(m_activeDOMObjectState == Suspended); | |
| 250 m_promiseState = Rejecting; | |
| 251 m_error = error; | |
| 252 } | |
| 253 } | |
| 177 | 254 |
| 255 void MIDIAccess::resolveNow() | |
| 256 { | |
| 257 if (!m_requesting) | |
| 258 return; | |
| 259 ASSERT(m_promiseState != ResolvedOrRejected); | |
| 260 ASSERT(m_activeDOMObjectState == Active); | |
| 261 | |
| 262 RefPtrWillBeRawPtr<MIDIAccess> protect(this); | |
| 263 m_promiseState = ResolvedOrRejected; | |
| 264 m_hasAccess = true; | |
| 265 setRequesting(false); | |
| 266 m_resolver->resolve(this); | |
| 267 } | |
| 268 | |
| 269 void MIDIAccess::rejectNow(PassRefPtr<DOMError> error) | |
| 270 { | |
| 271 if (!m_requesting) | |
| 272 return; | |
| 273 ASSERT(m_promiseState != ResolvedOrRejected); | |
| 274 ASSERT(m_activeDOMObjectState == Active); | |
| 275 | |
| 276 RefPtrWillBeRawPtr<MIDIAccess> protect(this); | |
| 277 m_promiseState = ResolvedOrRejected; | |
| 178 m_hasAccess = false; | 278 m_hasAccess = false; |
| 179 m_promise->reject(DOMError::create("SecurityError")); | 279 setRequesting(false); |
| 280 m_resolver->reject(error); | |
| 281 } | |
| 282 | |
| 283 void MIDIAccess::resumeTimerFired(Timer<MIDIAccess>*) | |
| 284 { | |
| 285 ASSERT(m_activeDOMObjectState == Active); | |
| 286 if (m_promiseState == Resolving) { | |
| 287 resolveNow(); | |
| 288 } else { | |
| 289 ASSERT(m_promiseState == Rejecting); | |
| 290 rejectNow(m_error.release()); | |
| 291 } | |
| 292 // |this| may be deleted here. | |
| 180 } | 293 } |
| 181 | 294 |
| 182 void MIDIAccess::trace(Visitor* visitor) | 295 void MIDIAccess::trace(Visitor* visitor) |
| 183 { | 296 { |
| 184 visitor->trace(m_inputs); | 297 visitor->trace(m_inputs); |
| 185 visitor->trace(m_outputs); | 298 visitor->trace(m_outputs); |
| 186 visitor->trace(m_promise); | |
| 187 } | 299 } |
| 188 | 300 |
| 189 } // namespace WebCore | 301 } // namespace WebCore |
| OLD | NEW |