Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: Source/modules/webmidi/MIDIPort.cpp

Issue 1043863002: Web MIDI: final IDL updates to conform the latest WD for shipping (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: fix #5 Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/webmidi/MIDIPort.h ('k') | Source/modules/webmidi/MIDIPort.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 return setStates(m_state, ConnectionStateOpen)->accept(scriptState);
99 // fall through
100 case PortState::MIDIPortStateOpened: 109 case PortState::MIDIPortStateOpened:
101 return accept(scriptState); 110 // TODO(toyoshim): Remove PortState::MIDIPortStateOpened.
102 default:
103 ASSERT_NOT_REACHED(); 111 ASSERT_NOT_REACHED();
104 } 112 }
105 return reject(scriptState, InvalidStateError, "The port is in unknown state. "); 113 return reject(scriptState, InvalidStateError, "The port is in unknown state. ");
106 } 114 }
107 115
108 ScriptPromise MIDIPort::close(ScriptState* scriptState) 116 ScriptPromise MIDIPort::close(ScriptState* scriptState)
109 { 117 {
118 // TODO(toyoshim): Implement the latest close() algorithm.
110 switch (m_state) { 119 switch (m_state) {
111 case PortState::MIDIPortStateDisconnected: 120 case PortState::MIDIPortStateDisconnected:
112 return reject(scriptState, InvalidStateError, "The port has been disconn ected."); 121 return reject(scriptState, InvalidStateError, "The port has been disconn ected.");
122 case PortState::MIDIPortStateConnected:
123 // TODO(toyoshim): Add blink API to perform a real close operation.
124 return setStates(m_state, ConnectionStateClosed)->accept(scriptState);
113 case PortState::MIDIPortStateOpened: 125 case PortState::MIDIPortStateOpened:
114 // FIXME: Add blink API to perform a real close operation. 126 // 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(); 127 ASSERT_NOT_REACHED();
121 } 128 }
122 return reject(scriptState, InvalidStateError, "The port is in unknown state. "); 129 return reject(scriptState, InvalidStateError, "The port is in unknown state. ");
123 } 130 }
124 131
125 void MIDIPort::setState(PortState state) 132 void MIDIPort::setState(PortState state)
126 { 133 {
127 if (m_state == state) 134 setStates(state, m_connection);
128 return;
129 m_state = state;
130 dispatchEvent(MIDIConnectionEvent::create(this));
131 } 135 }
132 136
133 ExecutionContext* MIDIPort::executionContext() const 137 ExecutionContext* MIDIPort::executionContext() const
134 { 138 {
135 return m_access->executionContext(); 139 return m_access->executionContext();
136 } 140 }
137 141
138 DEFINE_TRACE(MIDIPort) 142 DEFINE_TRACE(MIDIPort)
139 { 143 {
140 visitor->trace(m_access); 144 visitor->trace(m_access);
141 RefCountedGarbageCollectedEventTargetWithInlineData<MIDIPort>::trace(visitor ); 145 RefCountedGarbageCollectedEventTargetWithInlineData<MIDIPort>::trace(visitor );
142 } 146 }
143 147
144 ScriptPromise MIDIPort::accept(ScriptState* scriptState) 148 ScriptPromise MIDIPort::accept(ScriptState* scriptState)
145 { 149 {
146 return ScriptPromise::cast(scriptState, toV8(this, scriptState->context()->G lobal(), scriptState->isolate())); 150 return ScriptPromise::cast(scriptState, toV8(this, scriptState->context()->G lobal(), scriptState->isolate()));
147 } 151 }
148 152
149 ScriptPromise MIDIPort::reject(ScriptState* scriptState, ExceptionCode ec, const String& message) 153 ScriptPromise MIDIPort::reject(ScriptState* scriptState, ExceptionCode ec, const String& message)
150 { 154 {
151 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(ec, message)); 155 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(ec, message));
152 } 156 }
153 157
158 // Note: setStates() dispatches a event and JavaScript may release this object
159 // inside the handler. Caller should consider to retain |this|.
160 MIDIPort* MIDIPort::setStates(PortState state, ConnectionState connection)
161 {
162 // Retain |this| since an event handler may release it inside setStates().
163 MIDIPort* self = this;
tkent 2015/03/31 01:26:10 Ah, MIDIPort is already on Oilpan. Then you need
yhirano 2015/03/31 02:23:37 But I think we should protect the wrapper in order
164
165 if (m_state == state && m_connection == connection)
166 return self;
167 m_state = state;
168 m_connection = connection;
169 dispatchEvent(MIDIConnectionEvent::create(this));
170 return self;
171 }
172
154 } // namespace blink 173 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/webmidi/MIDIPort.h ('k') | Source/modules/webmidi/MIDIPort.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698