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

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

Powered by Google App Engine
This is Rietveld 408576698