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

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

Issue 77773003: Make WebMIDI use blink Promise. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: \ Created 7 years 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
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 13 matching lines...) Expand all
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/MIDIAccess.h" 32 #include "modules/webmidi/MIDIAccess.h"
33 33
34 #include "bindings/v8/ScriptPromise.h"
35 #include "bindings/v8/ScriptPromiseResolver.h"
34 #include "core/dom/DOMError.h" 36 #include "core/dom/DOMError.h"
35 #include "core/dom/Document.h" 37 #include "core/dom/Document.h"
36 #include "core/loader/DocumentLoadTiming.h" 38 #include "core/loader/DocumentLoadTiming.h"
37 #include "core/loader/DocumentLoader.h" 39 #include "core/loader/DocumentLoader.h"
38 #include "modules/webmidi/MIDIAccessPromise.h"
39 #include "modules/webmidi/MIDIConnectionEvent.h" 40 #include "modules/webmidi/MIDIConnectionEvent.h"
40 #include "modules/webmidi/MIDIController.h" 41 #include "modules/webmidi/MIDIController.h"
42 #include "modules/webmidi/MIDIOptions.h"
41 #include "modules/webmidi/MIDIPort.h" 43 #include "modules/webmidi/MIDIPort.h"
44 #include "modules/webmidi/NavigatorWebMIDI.h"
42 45
43 namespace WebCore { 46 namespace WebCore {
44 47
45 PassRefPtr<MIDIAccess> MIDIAccess::create(ExecutionContext* context, MIDIAccessP romise* promise) 48 PassRefPtr<MIDIAccess> MIDIAccess::create(const MIDIOptions& options, ExecutionC ontext* context, NavigatorWebMIDI* navigator)
46 { 49 {
47 RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(context, promise))); 50 RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(options, context, navi gator)));
48 midiAccess->suspendIfNeeded(); 51 midiAccess->suspendIfNeeded();
49 midiAccess->startRequest();
50 return midiAccess.release(); 52 return midiAccess.release();
51 } 53 }
52 54
53 MIDIAccess::~MIDIAccess() 55 MIDIAccess::~MIDIAccess()
54 { 56 {
55 stop(); 57 stop();
58 ASSERT(!m_navigator);
56 } 59 }
57 60
58 MIDIAccess::MIDIAccess(ExecutionContext* context, MIDIAccessPromise* promise) 61 MIDIAccess::MIDIAccess(const MIDIOptions& options, ExecutionContext* context, Na vigatorWebMIDI* navigator)
59 : ActiveDOMObject(context) 62 : ActiveDOMObject(context)
60 , m_promise(promise) 63 , m_navigator(navigator)
64 , m_requestState(context)
65 , m_options(options)
61 , m_hasAccess(false) 66 , m_hasAccess(false)
62 , m_sysExEnabled(false) 67 , m_sysExEnabled(false)
63 , m_requesting(false) 68 , m_requesting(false)
64 { 69 {
65 ScriptWrappable::init(this); 70 ScriptWrappable::init(this);
66 m_accessor = MIDIAccessor::create(this); 71 m_accessor = MIDIAccessor::create(this);
67 } 72 }
68 73
69 void MIDIAccess::setSysExEnabled(bool enable) 74 void MIDIAccess::setSysExEnabled(bool enable)
70 { 75 {
71 m_requesting = false;
72 m_sysExEnabled = enable; 76 m_sysExEnabled = enable;
73 if (enable) 77 if (enable)
74 m_accessor->startSession(); 78 m_accessor->startSession();
75 else 79 else
76 permissionDenied(); 80 reject(DOMError::create("SecurityError"));
81 // |this| can be deleted here.
77 } 82 }
78 83
79 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c onst String& name, const String& version) 84 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c onst String& name, const String& version)
80 { 85 {
81 ASSERT(isMainThread()); 86 ASSERT(isMainThread());
82 87
83 m_inputs.append(MIDIInput::create(this, executionContext(), id, manufacturer , name, version)); 88 m_inputs.append(MIDIInput::create(this, executionContext(), id, manufacturer , name, version));
84 } 89 }
85 90
86 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) 91 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
87 { 92 {
88 ASSERT(isMainThread()); 93 ASSERT(isMainThread());
89 94
90 unsigned portIndex = m_outputs.size(); 95 unsigned portIndex = m_outputs.size();
91 m_outputs.append(MIDIOutput::create(this, portIndex, executionContext(), id, manufacturer, name, version)); 96 m_outputs.append(MIDIOutput::create(this, portIndex, executionContext(), id, manufacturer, name, version));
92 } 97 }
93 98
94 void MIDIAccess::didStartSession(bool success) 99 void MIDIAccess::didStartSession(bool success)
95 { 100 {
96 ASSERT(isMainThread()); 101 ASSERT(isMainThread());
97 102 if (!m_requesting)
98 m_hasAccess = success; 103 return;
99 if (success) 104 if (success)
100 m_promise->fulfill(); 105 resolve();
101 else 106 else
102 m_promise->reject(DOMError::create("InvalidStateError")); 107 reject(DOMError::create("InvalidStateError"));
108 // |this| can be deleted here.
103 } 109 }
104 110
105 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat a, size_t length, double timeStamp) 111 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat a, size_t length, double timeStamp)
106 { 112 {
107 ASSERT(isMainThread()); 113 ASSERT(isMainThread());
108 114
109 if (m_hasAccess && portIndex < m_inputs.size()) { 115 if (m_hasAccess && portIndex < m_inputs.size()) {
110 // Convert from time in seconds which is based on the time coordinate sy stem of monotonicallyIncreasingTime() 116 // 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(). 117 // 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. 118 // This is how timestamps are defined in the Web MIDI spec.
(...skipping 24 matching lines...) Expand all
137 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds; 143 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
138 } 144 }
139 145
140 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); 146 m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
141 } 147 }
142 } 148 }
143 149
144 void MIDIAccess::stop() 150 void MIDIAccess::stop()
145 { 151 {
146 m_hasAccess = false; 152 m_hasAccess = false;
147 if (!m_requesting) 153 if (!m_requesting) {
154 // Since MIDIAccess is not pending, this object cannot be registered
155 // in |m_navigator|.
156 m_navigator = 0;
148 return; 157 return;
149 m_requesting = false; 158 }
150 Document* document = toDocument(executionContext()); 159 Document* document = toDocument(executionContext());
151 ASSERT(document); 160 ASSERT(document);
152 MIDIController* controller = MIDIController::from(document->page()); 161 MIDIController* controller = MIDIController::from(document->page());
153 ASSERT(controller); 162 ASSERT(controller);
154 controller->cancelSysExPermissionRequest(this); 163 controller->cancelSysExPermissionRequest(this);
155 164
156 m_accessor.clear(); 165 m_accessor.clear();
157 } 166 reject(DOMError::create("AbortError"));
158 167 // |this| can be deleted here.
159 void MIDIAccess::startRequest()
160 {
161 if (!m_promise->options()->sysex) {
162 m_accessor->startSession();
163 return;
164 }
165 Document* document = toDocument(executionContext());
166 ASSERT(document);
167 MIDIController* controller = MIDIController::from(document->page());
168 if (controller) {
169 m_requesting = true;
170 controller->requestSysExPermission(this);
171 } else {
172 permissionDenied();
173 }
174 } 168 }
175 169
176 void MIDIAccess::permissionDenied() 170 void MIDIAccess::permissionDenied()
177 { 171 {
178 ASSERT(isMainThread()); 172 ASSERT(isMainThread());
173 reject(DOMError::create("SecurityError"));
174 // |this| can be deleted here.
175 }
179 176
177 ScriptPromise MIDIAccess::startRequest()
178 {
179 ScriptPromise promise = ScriptPromise::createPending();
180 m_resolver = ScriptPromiseResolver::create(promise, executionContext());
181 m_requesting = true;
182 ASSERT(m_navigator);
183 m_navigator->registerPending(this);
184 if (!m_options.sysex) {
185 m_accessor->startSession();
186 return promise;
187 }
188 Document* document = toDocument(executionContext());
189 ASSERT(document);
190 MIDIController* controller = MIDIController::from(document->page());
191 if (controller)
192 controller->requestSysExPermission(this);
193 else
194 reject(DOMError::create("SecurityError"));
195 // |this| can be deleted here.
196 return promise;
197 }
198
199 void MIDIAccess::resolve()
200 {
201 if (!m_requesting)
202 return;
203 DOMRequestState::Scope scope(m_requestState);
204 m_hasAccess = true;
205 m_requesting = false;
206 // FIXME: Care about suspend / stop.
207 m_resolver->resolve(this, executionContext());
208 NavigatorWebMIDI* navigator = m_navigator;
209 m_navigator = 0;
210 if (navigator)
211 navigator->unregisterPending(this);
212 // |this| can be deleted here.
213 }
214
215 void MIDIAccess::reject(PassRefPtr<DOMError> error)
216 {
217 if (!m_requesting)
218 return;
219 DOMRequestState::Scope scope(m_requestState);
180 m_hasAccess = false; 220 m_hasAccess = false;
181 m_promise->reject(DOMError::create("SecurityError")); 221 m_requesting = false;
222 // FIXME: Care about suspend / stop.
223 m_resolver->reject(error, executionContext());
224 NavigatorWebMIDI* navigator = m_navigator;
225 m_navigator = 0;
226 if (navigator)
227 navigator->unregisterPending(this);
228 // |this| can be deleted here.
182 } 229 }
183 230
184 } // namespace WebCore 231 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698