| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/webmidi/MIDIAccessInitializer.h" | 5 #include "modules/webmidi/MIDIAccessInitializer.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/frame/Navigator.h" | 12 #include "core/frame/Navigator.h" |
| 13 #include "modules/webmidi/MIDIAccess.h" | 13 #include "modules/webmidi/MIDIAccess.h" |
| 14 #include "modules/webmidi/MIDIController.h" | 14 #include "modules/webmidi/MIDIController.h" |
| 15 #include "modules/webmidi/MIDIOptions.h" | 15 #include "modules/webmidi/MIDIOptions.h" |
| 16 #include "modules/webmidi/MIDIPort.h" | 16 #include "modules/webmidi/MIDIPort.h" |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 | 19 |
| 20 using PortState = WebMIDIAccessorClient::MIDIPortState; | 20 using PortState = WebMIDIAccessorClient::MIDIPortState; |
| 21 | 21 |
| 22 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID
IOptions& options) | 22 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID
IOptions& options) |
| 23 : ScriptPromiseResolver(scriptState) | 23 : ScriptPromiseResolver(scriptState) |
| 24 , m_requestSysex(false) | 24 , m_options(options) |
| 25 , m_hasBeenDisposed(false) | 25 , m_hasBeenDisposed(false) |
| 26 , m_sysexPermissionResolved(false) | 26 , m_sysexPermissionResolved(false) |
| 27 { | 27 { |
| 28 if (options.hasSysex()) | |
| 29 m_requestSysex = options.sysex(); | |
| 30 } | 28 } |
| 31 | 29 |
| 32 MIDIAccessInitializer::~MIDIAccessInitializer() | 30 MIDIAccessInitializer::~MIDIAccessInitializer() |
| 33 { | 31 { |
| 34 dispose(); | 32 dispose(); |
| 35 } | 33 } |
| 36 | 34 |
| 37 void MIDIAccessInitializer::contextDestroyed() | 35 void MIDIAccessInitializer::contextDestroyed() |
| 38 { | 36 { |
| 39 dispose(); | 37 dispose(); |
| 40 LifecycleObserver::contextDestroyed(); | 38 LifecycleObserver::contextDestroyed(); |
| 41 } | 39 } |
| 42 | 40 |
| 43 void MIDIAccessInitializer::dispose() | 41 void MIDIAccessInitializer::dispose() |
| 44 { | 42 { |
| 45 if (m_hasBeenDisposed) | 43 if (m_hasBeenDisposed) |
| 46 return; | 44 return; |
| 47 | 45 |
| 48 if (!executionContext()) | 46 if (!executionContext()) |
| 49 return; | 47 return; |
| 50 | 48 |
| 51 if (!m_sysexPermissionResolved) { | 49 if (!m_sysexPermissionResolved) { |
| 52 Document* document = toDocument(executionContext()); | 50 Document* document = toDocument(executionContext()); |
| 53 ASSERT(document); | 51 ASSERT(document); |
| 54 if (MIDIController* controller = MIDIController::from(document->frame())
) | 52 if (MIDIController* controller = MIDIController::from(document->frame())
) |
| 55 controller->cancelSysexPermissionRequest(this); | 53 controller->cancelPermissionRequest(this); |
| 56 m_sysexPermissionResolved = true; | 54 m_sysexPermissionResolved = true; |
| 57 } | 55 } |
| 58 | 56 |
| 59 m_hasBeenDisposed = true; | 57 m_hasBeenDisposed = true; |
| 60 } | 58 } |
| 61 | 59 |
| 62 ScriptPromise MIDIAccessInitializer::start() | 60 ScriptPromise MIDIAccessInitializer::start() |
| 63 { | 61 { |
| 64 ScriptPromise promise = this->promise(); | 62 ScriptPromise promise = this->promise(); |
| 65 m_accessor = MIDIAccessor::create(this); | 63 m_accessor = MIDIAccessor::create(this); |
| 66 | 64 |
| 67 if (!m_requestSysex) { | |
| 68 m_accessor->startSession(); | |
| 69 return promise; | |
| 70 } | |
| 71 Document* document = toDocument(executionContext()); | 65 Document* document = toDocument(executionContext()); |
| 72 ASSERT(document); | 66 ASSERT(document); |
| 73 if (MIDIController* controller = MIDIController::from(document->frame())) | 67 if (MIDIController* controller = MIDIController::from(document->frame())) |
| 74 controller->requestSysexPermission(this); | 68 controller->requestPermission(this, m_options); |
| 75 else | 69 else |
| 76 reject(DOMException::create(SecurityError)); | 70 reject(DOMException::create(SecurityError)); |
| 77 | 71 |
| 78 return promise; | 72 return promise; |
| 79 } | 73 } |
| 80 | 74 |
| 81 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version, PortState state) | 75 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version, PortState state) |
| 82 { | 76 { |
| 83 ASSERT(m_accessor); | 77 ASSERT(m_accessor); |
| 84 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty
peInput, version, state)); | 78 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty
peInput, version, state)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 101 void MIDIAccessInitializer::didSetOutputPortState(unsigned portIndex, PortState
state) | 95 void MIDIAccessInitializer::didSetOutputPortState(unsigned portIndex, PortState
state) |
| 102 { | 96 { |
| 103 // See comments on didSetInputPortState(). | 97 // See comments on didSetInputPortState(). |
| 104 ASSERT_NOT_REACHED(); | 98 ASSERT_NOT_REACHED(); |
| 105 } | 99 } |
| 106 | 100 |
| 107 void MIDIAccessInitializer::didStartSession(bool success, const String& error, c
onst String& message) | 101 void MIDIAccessInitializer::didStartSession(bool success, const String& error, c
onst String& message) |
| 108 { | 102 { |
| 109 ASSERT(m_accessor); | 103 ASSERT(m_accessor); |
| 110 if (success) { | 104 if (success) { |
| 111 resolve(MIDIAccess::create(m_accessor.release(), m_requestSysex, m_portD
escriptors, executionContext())); | 105 resolve(MIDIAccess::create(m_accessor.release(), m_options.hasSysex(), m
_portDescriptors, executionContext())); |
| 112 } else { | 106 } else { |
| 113 // The spec says the name is one of | 107 // The spec says the name is one of |
| 114 // - SecurityError | 108 // - SecurityError |
| 115 // - AbortError | 109 // - AbortError |
| 116 // - InvalidStateError | 110 // - InvalidStateError |
| 117 // - NotSupportedError | 111 // - NotSupportedError |
| 118 // TODO(toyoshim): Do not rely on |error| string. Instead an enum | 112 // TODO(toyoshim): Do not rely on |error| string. Instead an enum |
| 119 // representing an ExceptionCode should be defined and deliverred. | 113 // representing an ExceptionCode should be defined and deliverred. |
| 120 ExceptionCode ec = InvalidStateError; | 114 ExceptionCode ec = InvalidStateError; |
| 121 if (error == DOMException::getErrorName(SecurityError)) { | 115 if (error == DOMException::getErrorName(SecurityError)) { |
| 122 ec = SecurityError; | 116 ec = SecurityError; |
| 123 } else if (error == DOMException::getErrorName(AbortError)) { | 117 } else if (error == DOMException::getErrorName(AbortError)) { |
| 124 ec = AbortError; | 118 ec = AbortError; |
| 125 } else if (error == DOMException::getErrorName(InvalidStateError)) { | 119 } else if (error == DOMException::getErrorName(InvalidStateError)) { |
| 126 ec = InvalidStateError; | 120 ec = InvalidStateError; |
| 127 } else if (error == DOMException::getErrorName(NotSupportedError)) { | 121 } else if (error == DOMException::getErrorName(NotSupportedError)) { |
| 128 ec = NotSupportedError; | 122 ec = NotSupportedError; |
| 129 } | 123 } |
| 130 reject(DOMException::create(ec, message)); | 124 reject(DOMException::create(ec, message)); |
| 131 } | 125 } |
| 132 } | 126 } |
| 133 | 127 |
| 134 void MIDIAccessInitializer::resolveSysexPermission(bool allowed) | 128 void MIDIAccessInitializer::resolvePermission(bool allowed) |
| 135 { | 129 { |
| 136 m_sysexPermissionResolved = true; | 130 m_sysexPermissionResolved = true; |
| 137 if (allowed) | 131 if (allowed) |
| 138 m_accessor->startSession(); | 132 m_accessor->startSession(); |
| 139 else | 133 else |
| 140 reject(DOMException::create(SecurityError)); | 134 reject(DOMException::create(SecurityError)); |
| 141 } | 135 } |
| 142 | 136 |
| 143 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const | 137 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const |
| 144 { | 138 { |
| 145 return executionContext()->securityOrigin(); | 139 return executionContext()->securityOrigin(); |
| 146 } | 140 } |
| 147 | 141 |
| 148 ExecutionContext* MIDIAccessInitializer::executionContext() const | 142 ExecutionContext* MIDIAccessInitializer::executionContext() const |
| 149 { | 143 { |
| 150 return scriptState()->executionContext(); | 144 return scriptState()->executionContext(); |
| 151 } | 145 } |
| 152 | 146 |
| 153 } // namespace blink | 147 } // namespace blink |
| OLD | NEW |