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 17 matching lines...) Expand all Loading... |
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 "core/dom/DOMError.h" | 34 #include "core/dom/DOMError.h" |
35 #include "core/dom/Document.h" | 35 #include "core/dom/Document.h" |
36 #include "core/loader/DocumentLoadTiming.h" | 36 #include "core/loader/DocumentLoadTiming.h" |
37 #include "core/loader/DocumentLoader.h" | 37 #include "core/loader/DocumentLoader.h" |
38 #include "modules/webmidi/MIDIAccessPromise.h" | |
39 #include "modules/webmidi/MIDIConnectionEvent.h" | 38 #include "modules/webmidi/MIDIConnectionEvent.h" |
40 #include "modules/webmidi/MIDIController.h" | 39 #include "modules/webmidi/MIDIController.h" |
| 40 #include "modules/webmidi/MIDIOptions.h" |
41 #include "modules/webmidi/MIDIPort.h" | 41 #include "modules/webmidi/MIDIPort.h" |
| 42 #include "modules/webmidi/NavigatorWebMIDI.h" |
42 | 43 |
43 namespace WebCore { | 44 namespace WebCore { |
44 | 45 |
45 PassRefPtr<MIDIAccess> MIDIAccess::create(ExecutionContext* context, MIDIAccessP
romise* promise) | 46 PassRefPtr<MIDIAccess> MIDIAccess::create(const MIDIOptions& options, ExecutionC
ontext* context, NavigatorWebMIDI* navigator) |
46 { | 47 { |
47 RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(context, promise))); | 48 RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(options, context, navi
gator))); |
48 midiAccess->suspendIfNeeded(); | 49 midiAccess->suspendIfNeeded(); |
49 midiAccess->startRequest(); | |
50 return midiAccess.release(); | 50 return midiAccess.release(); |
51 } | 51 } |
52 | 52 |
53 MIDIAccess::~MIDIAccess() | 53 MIDIAccess::~MIDIAccess() |
54 { | 54 { |
55 stop(); | 55 stop(); |
56 } | 56 } |
57 | 57 |
58 MIDIAccess::MIDIAccess(ExecutionContext* context, MIDIAccessPromise* promise) | 58 MIDIAccess::MIDIAccess(const MIDIOptions& options, ExecutionContext* context, Na
vigatorWebMIDI* navigator) |
59 : ActiveDOMObject(context) | 59 : ActiveDOMObject(context) |
60 , m_promise(promise) | 60 , m_navigator(navigator) |
| 61 , m_options(options) |
61 , m_hasAccess(false) | 62 , m_hasAccess(false) |
62 , m_sysExEnabled(false) | 63 , m_sysExEnabled(false) |
63 , m_requesting(false) | 64 , m_requesting(false) |
64 { | 65 { |
65 ScriptWrappable::init(this); | 66 ScriptWrappable::init(this); |
66 m_accessor = MIDIAccessor::create(this); | 67 m_accessor = MIDIAccessor::create(this); |
67 } | 68 } |
68 | 69 |
69 void MIDIAccess::setSysExEnabled(bool enable) | 70 void MIDIAccess::setSysExEnabled(bool enable) |
70 { | 71 { |
(...skipping 19 matching lines...) Expand all Loading... |
90 unsigned portIndex = m_outputs.size(); | 91 unsigned portIndex = m_outputs.size(); |
91 m_outputs.append(MIDIOutput::create(this, portIndex, executionContext(), id,
manufacturer, name, version)); | 92 m_outputs.append(MIDIOutput::create(this, portIndex, executionContext(), id,
manufacturer, name, version)); |
92 } | 93 } |
93 | 94 |
94 void MIDIAccess::didStartSession(bool success) | 95 void MIDIAccess::didStartSession(bool success) |
95 { | 96 { |
96 ASSERT(isMainThread()); | 97 ASSERT(isMainThread()); |
97 | 98 |
98 m_hasAccess = success; | 99 m_hasAccess = success; |
99 if (success) | 100 if (success) |
100 m_promise->fulfill(); | 101 m_navigator->didFinishRequest(this); |
101 else | 102 else |
102 m_promise->reject(DOMError::create("InvalidStateError")); | 103 m_navigator->didFailRequest(this, DOMError::create("InvalidStateError"))
; |
103 } | 104 } |
104 | 105 |
105 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat
a, size_t length, double timeStamp) | 106 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat
a, size_t length, double timeStamp) |
106 { | 107 { |
107 ASSERT(isMainThread()); | 108 ASSERT(isMainThread()); |
108 | 109 |
109 if (m_hasAccess && portIndex < m_inputs.size()) { | 110 if (m_hasAccess && portIndex < m_inputs.size()) { |
110 // Convert from time in seconds which is based on the time coordinate sy
stem of monotonicallyIncreasingTime() | 111 // 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(). | 112 // 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. | 113 // This is how timestamps are defined in the Web MIDI spec. |
(...skipping 27 matching lines...) Expand all Loading... |
140 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); | 141 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); |
141 } | 142 } |
142 } | 143 } |
143 | 144 |
144 void MIDIAccess::stop() | 145 void MIDIAccess::stop() |
145 { | 146 { |
146 m_hasAccess = false; | 147 m_hasAccess = false; |
147 if (!m_requesting) | 148 if (!m_requesting) |
148 return; | 149 return; |
149 m_requesting = false; | 150 m_requesting = false; |
| 151 m_navigator->didFailRequest(this, DOMError::create("AbortError")); |
150 Document* document = toDocument(executionContext()); | 152 Document* document = toDocument(executionContext()); |
151 ASSERT(document); | 153 ASSERT(document); |
152 MIDIController* controller = MIDIController::from(document->page()); | 154 MIDIController* controller = MIDIController::from(document->page()); |
153 ASSERT(controller); | 155 ASSERT(controller); |
154 controller->cancelSysExPermissionRequest(this); | 156 controller->cancelSysExPermissionRequest(this); |
155 | 157 |
156 m_accessor.clear(); | 158 m_accessor.clear(); |
157 } | 159 } |
158 | 160 |
159 void MIDIAccess::startRequest() | 161 void MIDIAccess::startRequest() |
160 { | 162 { |
161 if (!m_promise->options()->sysex) { | 163 if (!m_options.sysex) { |
162 m_accessor->startSession(); | 164 m_accessor->startSession(); |
163 return; | 165 return; |
164 } | 166 } |
165 Document* document = toDocument(executionContext()); | 167 Document* document = toDocument(executionContext()); |
166 ASSERT(document); | 168 ASSERT(document); |
167 MIDIController* controller = MIDIController::from(document->page()); | 169 MIDIController* controller = MIDIController::from(document->page()); |
168 if (controller) { | 170 if (controller) { |
169 m_requesting = true; | 171 m_requesting = true; |
170 controller->requestSysExPermission(this); | 172 controller->requestSysExPermission(this); |
171 } else { | 173 } else { |
172 permissionDenied(); | 174 permissionDenied(); |
173 } | 175 } |
174 } | 176 } |
175 | 177 |
176 void MIDIAccess::permissionDenied() | 178 void MIDIAccess::permissionDenied() |
177 { | 179 { |
178 ASSERT(isMainThread()); | 180 ASSERT(isMainThread()); |
179 | 181 |
180 m_hasAccess = false; | 182 m_hasAccess = false; |
181 m_promise->reject(DOMError::create("SecurityError")); | 183 m_navigator->didFailRequest(this, DOMError::create("SecurityError")); |
182 } | 184 } |
183 | 185 |
184 } // namespace WebCore | 186 } // namespace WebCore |
OLD | NEW |