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 22 matching lines...) Expand all Loading... |
33 | 33 |
34 #include "bindings/core/v8/ScriptPromise.h" | 34 #include "bindings/core/v8/ScriptPromise.h" |
35 #include "core/dom/DOMException.h" | 35 #include "core/dom/DOMException.h" |
36 #include "modules/webmidi/MIDIAccess.h" | 36 #include "modules/webmidi/MIDIAccess.h" |
37 #include "modules/webmidi/MIDIConnectionEvent.h" | 37 #include "modules/webmidi/MIDIConnectionEvent.h" |
38 | 38 |
39 namespace blink { | 39 namespace blink { |
40 | 40 |
41 using PortState = MIDIAccessor::MIDIPortState; | 41 using PortState = MIDIAccessor::MIDIPortState; |
42 | 42 |
43 MIDIPort::MIDIPort(MIDIAccess* access, const String& id, const String& manufactu
rer, const String& name, MIDIPortTypeCode type, const String& version, PortState
state) | 43 MIDIPort::MIDIPort(MIDIAccess* access, const String& id, const String& manufactu
rer, const String& name, TypeCode type, const String& version, PortState state) |
44 : m_id(id) | 44 : m_id(id) |
45 , m_manufacturer(manufacturer) | 45 , m_manufacturer(manufacturer) |
46 , m_name(name) | 46 , m_name(name) |
47 , m_type(type) | 47 , m_type(type) |
48 , m_version(version) | 48 , m_version(version) |
49 , m_access(access) | 49 , m_access(access) |
| 50 , m_connection(ConnectionStateClosed) |
50 { | 51 { |
51 ASSERT(access); | 52 ASSERT(access); |
52 ASSERT(type == MIDIPortTypeInput || type == MIDIPortTypeOutput); | 53 ASSERT(type == TypeInput || type == TypeOutput); |
53 ASSERT(state == PortState::MIDIPortStateDisconnected | 54 ASSERT(state == PortState::MIDIPortStateDisconnected |
54 || state == PortState::MIDIPortStateConnected | 55 || state == PortState::MIDIPortStateConnected |
55 || state == PortState::MIDIPortStateOpened); | 56 || state == PortState::MIDIPortStateOpened); |
56 // FIXME: Remove following code once blink API has a real open and close | 57 // TODO(toyoshim): Remove following code once blink API has a real open and |
57 // operations. | 58 // close operations. |
58 if (state == PortState::MIDIPortStateOpened) | 59 if (state == PortState::MIDIPortStateOpened) |
59 state = PortState::MIDIPortStateConnected; | 60 state = PortState::MIDIPortStateConnected; |
60 m_state = state; | 61 m_state = state; |
61 } | 62 } |
62 | 63 |
| 64 String MIDIPort::connection() const |
| 65 { |
| 66 switch (m_connection) { |
| 67 case ConnectionStateOpen: |
| 68 return "open"; |
| 69 case ConnectionStateClosed: |
| 70 return "closed"; |
| 71 case ConnectionStatePending: |
| 72 return "pending"; |
| 73 } |
| 74 return emptyString(); |
| 75 } |
| 76 |
63 String MIDIPort::state() const | 77 String MIDIPort::state() const |
64 { | 78 { |
65 switch (m_state) { | 79 switch (m_state) { |
66 case PortState::MIDIPortStateDisconnected: | 80 case PortState::MIDIPortStateDisconnected: |
67 return "disconnected"; | 81 return "disconnected"; |
68 case PortState::MIDIPortStateConnected: | 82 case PortState::MIDIPortStateConnected: |
| 83 case PortState::MIDIPortStateOpened: |
69 return "connected"; | 84 return "connected"; |
70 case PortState::MIDIPortStateOpened: | |
71 return "opened"; | |
72 default: | |
73 ASSERT_NOT_REACHED(); | |
74 } | 85 } |
75 return emptyString(); | 86 return emptyString(); |
76 } | 87 } |
77 | 88 |
78 String MIDIPort::type() const | 89 String MIDIPort::type() const |
79 { | 90 { |
80 switch (m_type) { | 91 switch (m_type) { |
81 case MIDIPortTypeInput: | 92 case TypeInput: |
82 return "input"; | 93 return "input"; |
83 case MIDIPortTypeOutput: | 94 case TypeOutput: |
84 return "output"; | 95 return "output"; |
85 default: | |
86 ASSERT_NOT_REACHED(); | |
87 } | 96 } |
88 return emptyString(); | 97 return emptyString(); |
89 } | 98 } |
90 | 99 |
91 ScriptPromise MIDIPort::open(ScriptState* scriptState) | 100 ScriptPromise MIDIPort::open(ScriptState* scriptState) |
92 { | 101 { |
| 102 // TODO(toyoshim): Implement the latest open() algorithm. |
93 switch (m_state) { | 103 switch (m_state) { |
94 case PortState::MIDIPortStateDisconnected: | 104 case PortState::MIDIPortStateDisconnected: |
95 return reject(scriptState, InvalidStateError, "The port has been disconn
ected."); | 105 return reject(scriptState, InvalidStateError, "The port has been disconn
ected."); |
96 case PortState::MIDIPortStateConnected: | 106 case PortState::MIDIPortStateConnected: |
97 // FIXME: Add blink API to perform a real open operation. | 107 // TODO(toyoshim): Add blink API to perform a real open operation. |
98 setState(PortState::MIDIPortStateOpened); | 108 setStates(m_state, ConnectionStateOpen); |
99 // fall through | 109 return accept(scriptState); |
100 case PortState::MIDIPortStateOpened: | 110 case PortState::MIDIPortStateOpened: |
101 return accept(scriptState); | 111 // TODO(toyoshim): Remove PortState::MIDIPortStateOpened. |
102 default: | |
103 ASSERT_NOT_REACHED(); | 112 ASSERT_NOT_REACHED(); |
104 } | 113 } |
105 return reject(scriptState, InvalidStateError, "The port is in unknown state.
"); | 114 return reject(scriptState, InvalidStateError, "The port is in unknown state.
"); |
106 } | 115 } |
107 | 116 |
108 ScriptPromise MIDIPort::close(ScriptState* scriptState) | 117 ScriptPromise MIDIPort::close(ScriptState* scriptState) |
109 { | 118 { |
| 119 // TODO(toyoshim): Implement the latest close() algorithm. |
110 switch (m_state) { | 120 switch (m_state) { |
111 case PortState::MIDIPortStateDisconnected: | 121 case PortState::MIDIPortStateDisconnected: |
112 return reject(scriptState, InvalidStateError, "The port has been disconn
ected."); | 122 return reject(scriptState, InvalidStateError, "The port has been disconn
ected."); |
| 123 case PortState::MIDIPortStateConnected: |
| 124 // TODO(toyoshim): Add blink API to perform a real close operation. |
| 125 setStates(m_state, ConnectionStateClosed); |
| 126 return accept(scriptState); |
113 case PortState::MIDIPortStateOpened: | 127 case PortState::MIDIPortStateOpened: |
114 // FIXME: Add blink API to perform a real close operation. | 128 // TODO(toyoshim): Remove PortState::MIDIPortStateOpened. |
115 setState(PortState::MIDIPortStateConnected); | |
116 // fall through | |
117 case PortState::MIDIPortStateConnected: | |
118 return accept(scriptState); | |
119 default: | |
120 ASSERT_NOT_REACHED(); | 129 ASSERT_NOT_REACHED(); |
121 } | 130 } |
122 return reject(scriptState, InvalidStateError, "The port is in unknown state.
"); | 131 return reject(scriptState, InvalidStateError, "The port is in unknown state.
"); |
123 } | 132 } |
124 | 133 |
125 void MIDIPort::setState(PortState state) | 134 void MIDIPort::setState(PortState state) |
126 { | 135 { |
127 if (m_state == state) | 136 setStates(state, m_connection); |
128 return; | |
129 m_state = state; | |
130 dispatchEvent(MIDIConnectionEvent::create(this)); | |
131 } | 137 } |
132 | 138 |
133 ExecutionContext* MIDIPort::executionContext() const | 139 ExecutionContext* MIDIPort::executionContext() const |
134 { | 140 { |
135 return m_access->executionContext(); | 141 return m_access->executionContext(); |
136 } | 142 } |
137 | 143 |
138 DEFINE_TRACE(MIDIPort) | 144 DEFINE_TRACE(MIDIPort) |
139 { | 145 { |
140 visitor->trace(m_access); | 146 visitor->trace(m_access); |
141 RefCountedGarbageCollectedEventTargetWithInlineData<MIDIPort>::trace(visitor
); | 147 RefCountedGarbageCollectedEventTargetWithInlineData<MIDIPort>::trace(visitor
); |
142 } | 148 } |
143 | 149 |
144 ScriptPromise MIDIPort::accept(ScriptState* scriptState) | 150 ScriptPromise MIDIPort::accept(ScriptState* scriptState) |
145 { | 151 { |
146 return ScriptPromise::cast(scriptState, toV8(this, scriptState->context()->G
lobal(), scriptState->isolate())); | 152 return ScriptPromise::cast(scriptState, toV8(this, scriptState->context()->G
lobal(), scriptState->isolate())); |
147 } | 153 } |
148 | 154 |
149 ScriptPromise MIDIPort::reject(ScriptState* scriptState, ExceptionCode ec, const
String& message) | 155 ScriptPromise MIDIPort::reject(ScriptState* scriptState, ExceptionCode ec, const
String& message) |
150 { | 156 { |
151 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(ec, message)); | 157 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(ec, message)); |
152 } | 158 } |
153 | 159 |
| 160 void MIDIPort::setStates(PortState state, ConnectionState connection) |
| 161 { |
| 162 if (m_state == state && m_connection == connection) |
| 163 return; |
| 164 m_state = state; |
| 165 m_connection = connection; |
| 166 dispatchEvent(MIDIConnectionEvent::create(this)); |
| 167 } |
| 168 |
154 } // namespace blink | 169 } // namespace blink |
OLD | NEW |