| 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/MIDIPort.h" | 32 #include "modules/webmidi/MIDIPort.h" |
| 33 | 33 |
| 34 #include "bindings/core/v8/ScriptPromise.h" |
| 35 #include "core/dom/DOMError.h" |
| 34 #include "modules/webmidi/MIDIAccess.h" | 36 #include "modules/webmidi/MIDIAccess.h" |
| 35 #include "modules/webmidi/MIDIConnectionEvent.h" | 37 #include "modules/webmidi/MIDIConnectionEvent.h" |
| 36 | 38 |
| 37 namespace blink { | 39 namespace blink { |
| 38 | 40 |
| 39 MIDIPort::MIDIPort(MIDIAccess* access, const String& id, const String& manufactu
rer, const String& name, MIDIPortTypeCode type, const String& version, bool isAc
tive) | 41 using PortState = MIDIAccessor::MIDIPortState; |
| 42 |
| 43 MIDIPort::MIDIPort(MIDIAccess* access, const String& id, const String& manufactu
rer, const String& name, MIDIPortTypeCode type, const String& version, PortState
state) |
| 40 : m_id(id) | 44 : m_id(id) |
| 41 , m_manufacturer(manufacturer) | 45 , m_manufacturer(manufacturer) |
| 42 , m_name(name) | 46 , m_name(name) |
| 43 , m_type(type) | 47 , m_type(type) |
| 44 , m_version(version) | 48 , m_version(version) |
| 45 , m_access(access) | 49 , m_access(access) |
| 46 , m_isActive(isActive) | |
| 47 { | 50 { |
| 48 ASSERT(access); | 51 ASSERT(access); |
| 49 ASSERT(type == MIDIPortTypeInput || type == MIDIPortTypeOutput); | 52 ASSERT(type == MIDIPortTypeInput || type == MIDIPortTypeOutput); |
| 53 ASSERT(state == PortState::MIDIPortStateDisconnected |
| 54 || state == PortState::MIDIPortStateConnected |
| 55 || state == PortState::MIDIPortStateOpened); |
| 56 // FIXME: Remove following code once blink API has a real open and close |
| 57 // operations. |
| 58 if (state == PortState::MIDIPortStateOpened) |
| 59 state = PortState::MIDIPortStateConnected; |
| 60 m_state = state; |
| 50 } | 61 } |
| 51 | 62 |
| 52 String MIDIPort::state() const | 63 String MIDIPort::state() const |
| 53 { | 64 { |
| 54 // FIXME: Support complete MIDIPortState once Blink API is updated. | 65 switch (m_state) { |
| 55 return m_isActive ? "opened" : "disconnected"; | 66 case PortState::MIDIPortStateDisconnected: |
| 67 return "disconnected"; |
| 68 case PortState::MIDIPortStateConnected: |
| 69 return "connected"; |
| 70 case PortState::MIDIPortStateOpened: |
| 71 return "opened"; |
| 72 default: |
| 73 ASSERT_NOT_REACHED(); |
| 74 } |
| 75 return emptyString(); |
| 56 } | 76 } |
| 57 | 77 |
| 58 String MIDIPort::type() const | 78 String MIDIPort::type() const |
| 59 { | 79 { |
| 60 switch (m_type) { | 80 switch (m_type) { |
| 61 case MIDIPortTypeInput: | 81 case MIDIPortTypeInput: |
| 62 return "input"; | 82 return "input"; |
| 63 case MIDIPortTypeOutput: | 83 case MIDIPortTypeOutput: |
| 64 return "output"; | 84 return "output"; |
| 65 default: | 85 default: |
| 66 ASSERT_NOT_REACHED(); | 86 ASSERT_NOT_REACHED(); |
| 67 } | 87 } |
| 68 return emptyString(); | 88 return emptyString(); |
| 69 } | 89 } |
| 70 | 90 |
| 71 void MIDIPort::setActiveState(bool isActive) | 91 ScriptPromise MIDIPort::open(ScriptState* scriptState) |
| 72 { | 92 { |
| 73 m_isActive = isActive; | 93 switch (m_state) { |
| 94 case PortState::MIDIPortStateDisconnected: |
| 95 return reject(scriptState, "InvalidStateError", "The port has been disco
nnected."); |
| 96 case PortState::MIDIPortStateConnected: |
| 97 // FIXME: Add blink API to perform a real open operation. |
| 98 setState(PortState::MIDIPortStateOpened); |
| 99 // fall through |
| 100 case PortState::MIDIPortStateOpened: |
| 101 return accept(scriptState); |
| 102 default: |
| 103 ASSERT_NOT_REACHED(); |
| 104 } |
| 105 return reject(scriptState, "InvalidStateError", "The port is in unknown stat
e."); |
| 106 } |
| 107 |
| 108 ScriptPromise MIDIPort::close(ScriptState* scriptState) |
| 109 { |
| 110 switch (m_state) { |
| 111 case PortState::MIDIPortStateDisconnected: |
| 112 return reject(scriptState, "InvalidStateError", "The port has been disco
nnected."); |
| 113 case PortState::MIDIPortStateOpened: |
| 114 // FIXME: Add blink API to perform a real close operation. |
| 115 setState(PortState::MIDIPortStateConnected); |
| 116 // fall through |
| 117 case PortState::MIDIPortStateConnected: |
| 118 return accept(scriptState); |
| 119 default: |
| 120 ASSERT_NOT_REACHED(); |
| 121 } |
| 122 return reject(scriptState, "InvalidStateError", "The port is in unknown stat
e."); |
| 123 } |
| 124 |
| 125 void MIDIPort::setState(PortState state) |
| 126 { |
| 127 if (m_state == state) |
| 128 return; |
| 129 m_state = state; |
| 74 dispatchEvent(MIDIConnectionEvent::create(this)); | 130 dispatchEvent(MIDIConnectionEvent::create(this)); |
| 75 } | 131 } |
| 76 | 132 |
| 77 ExecutionContext* MIDIPort::executionContext() const | 133 ExecutionContext* MIDIPort::executionContext() const |
| 78 { | 134 { |
| 79 return m_access->executionContext(); | 135 return m_access->executionContext(); |
| 80 } | 136 } |
| 81 | 137 |
| 82 DEFINE_TRACE(MIDIPort) | 138 DEFINE_TRACE(MIDIPort) |
| 83 { | 139 { |
| 84 visitor->trace(m_access); | 140 visitor->trace(m_access); |
| 85 RefCountedGarbageCollectedEventTargetWithInlineData<MIDIPort>::trace(visitor
); | 141 RefCountedGarbageCollectedEventTargetWithInlineData<MIDIPort>::trace(visitor
); |
| 86 } | 142 } |
| 87 | 143 |
| 144 ScriptPromise MIDIPort::accept(ScriptState* scriptState) |
| 145 { |
| 146 return ScriptPromise::cast(scriptState, toV8(this, scriptState->context()->G
lobal(), scriptState->isolate())); |
| 147 } |
| 148 |
| 149 ScriptPromise MIDIPort::reject(ScriptState* scriptState, const String& name, con
st String& message) |
| 150 { |
| 151 return ScriptPromise::reject(scriptState, toV8(DOMError::create(name, messag
e), scriptState->context()->Global(), scriptState->isolate())); |
| 152 } |
| 153 |
| 154 |
| 88 } // namespace blink | 155 } // namespace blink |
| OLD | NEW |