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

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

Issue 311773003: Decouple MIDIAccess initialization from MIDIAccess class. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 6 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
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 21 matching lines...) Expand all
32 #include "modules/webmidi/MIDIAccess.h" 32 #include "modules/webmidi/MIDIAccess.h"
33 33
34 #include "bindings/v8/ScriptFunction.h" 34 #include "bindings/v8/ScriptFunction.h"
35 #include "bindings/v8/ScriptPromise.h" 35 #include "bindings/v8/ScriptPromise.h"
36 #include "bindings/v8/ScriptPromiseResolverWithContext.h" 36 #include "bindings/v8/ScriptPromiseResolverWithContext.h"
37 #include "bindings/v8/V8Binding.h" 37 #include "bindings/v8/V8Binding.h"
38 #include "core/dom/DOMError.h" 38 #include "core/dom/DOMError.h"
39 #include "core/dom/Document.h" 39 #include "core/dom/Document.h"
40 #include "core/loader/DocumentLoadTiming.h" 40 #include "core/loader/DocumentLoadTiming.h"
41 #include "core/loader/DocumentLoader.h" 41 #include "core/loader/DocumentLoader.h"
42 #include "modules/webmidi/MIDIAccessInitializer.h"
42 #include "modules/webmidi/MIDIConnectionEvent.h" 43 #include "modules/webmidi/MIDIConnectionEvent.h"
43 #include "modules/webmidi/MIDIController.h" 44 #include "modules/webmidi/MIDIController.h"
44 #include "modules/webmidi/MIDIOptions.h" 45 #include "modules/webmidi/MIDIOptions.h"
45 #include "modules/webmidi/MIDIPort.h" 46 #include "modules/webmidi/MIDIPort.h"
46 #include "platform/AsyncMethodRunner.h" 47 #include "platform/AsyncMethodRunner.h"
47 48
48 namespace WebCore { 49 namespace WebCore {
49 50
50 class MIDIAccess::PostAction : public ScriptFunction {
51 public:
52 static PassOwnPtr<MIDIAccess::PostAction> create(v8::Isolate* isolate, WeakP tr<MIDIAccess> owner, State state) { return adoptPtr(new PostAction(isolate, own er, state)); }
53
54 private:
55 PostAction(v8::Isolate* isolate, WeakPtr<MIDIAccess> owner, State state): Sc riptFunction(isolate), m_owner(owner), m_state(state) { }
56 virtual ScriptValue call(ScriptValue value) OVERRIDE
57 {
58 if (!m_owner.get())
59 return value;
60 m_owner->doPostAction(m_state);
61 return value;
62 }
63
64 WeakPtr<MIDIAccess> m_owner;
65 State m_state;
66 };
67
68 ScriptPromise MIDIAccess::request(const MIDIOptions& options, ScriptState* scrip tState) 51 ScriptPromise MIDIAccess::request(const MIDIOptions& options, ScriptState* scrip tState)
69 { 52 {
70 RefPtrWillBeRawPtr<MIDIAccess> midiAccess(adoptRefWillBeRefCountedGarbageCol lected(new MIDIAccess(options, scriptState->executionContext()))); 53 RefPtrWillBeRawPtr<MIDIAccess> midiAccess(adoptRefWillBeRefCountedGarbageCol lected(new MIDIAccess(options, scriptState->executionContext())));
71 midiAccess->suspendIfNeeded(); 54 midiAccess->suspendIfNeeded();
72 // Create a wrapper to expose this object to the V8 GC so that 55 // Create a wrapper to expose this object to the V8 GC so that
73 // hasPendingActivity takes effect. 56 // hasPendingActivity takes effect.
74 toV8NoInline(midiAccess.get(), scriptState->context()->Global(), scriptState ->isolate()); 57 toV8NoInline(midiAccess.get(), scriptState->context()->Global(), scriptState ->isolate());
75 // Now this object is retained because m_state equals to Requesting. 58 // Now this object is retained because hasPending returns true.
76 return midiAccess->startRequest(scriptState); 59 return midiAccess->m_initializer->initialize(scriptState);
77 } 60 }
78 61
79 MIDIAccess::~MIDIAccess() 62 MIDIAccess::~MIDIAccess()
80 { 63 {
81 } 64 }
82 65
83 MIDIAccess::MIDIAccess(const MIDIOptions& options, ExecutionContext* context) 66 MIDIAccess::MIDIAccess(const MIDIOptions& options, ExecutionContext* context)
84 : ActiveDOMObject(context) 67 : ActiveDOMObject(context)
85 , m_state(Requesting) 68 , m_initializer(MIDIAccessInitializer::create(options, this))
86 , m_weakPtrFactory(this)
87 , m_options(options)
88 , m_sysexEnabled(false)
89 { 69 {
90 ScriptWrappable::init(this); 70 ScriptWrappable::init(this);
91 m_accessor = MIDIAccessor::create(this);
92 }
93
94 void MIDIAccess::setSysexEnabled(bool enable)
95 {
96 m_sysexEnabled = enable;
97 if (enable) {
98 m_accessor->startSession();
99 } else {
100 m_resolver->reject(DOMError::create("SecurityError"));
101 }
102 } 71 }
103 72
104 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c onst String& name, const String& version) 73 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c onst String& name, const String& version)
105 { 74 {
106 ASSERT(isMainThread()); 75 ASSERT(isMainThread());
107
108 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version)); 76 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version));
109 } 77 }
110 78
111 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) 79 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
112 { 80 {
113 ASSERT(isMainThread()); 81 ASSERT(isMainThread());
114
115 unsigned portIndex = m_outputs.size(); 82 unsigned portIndex = m_outputs.size();
116 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version)); 83 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version));
117 } 84 }
118 85
119 void MIDIAccess::didStartSession(bool success, const String& error, const String & message)
120 {
121 ASSERT(isMainThread());
122 if (success)
123 m_resolver->resolve(this);
124 else
125 m_resolver->reject(DOMError::create(error, message));
126 }
127
128 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat a, size_t length, double timeStamp) 86 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat a, size_t length, double timeStamp)
129 { 87 {
130 ASSERT(isMainThread()); 88 ASSERT(isMainThread());
89 if (portIndex >= m_inputs.size())
90 return;
131 91
132 if (m_state == Resolved && portIndex < m_inputs.size()) { 92 // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime()
133 // Convert from time in seconds which is based on the time coordinate sy stem of monotonicallyIncreasingTime() 93 // into time in milliseconds (a DOMHighResTimeStamp) according to the same t ime coordinate system as performance.now().
134 // into time in milliseconds (a DOMHighResTimeStamp) according to the sa me time coordinate system as performance.now(). 94 // This is how timestamps are defined in the Web MIDI spec.
135 // This is how timestamps are defined in the Web MIDI spec. 95 Document* document = toDocument(executionContext());
136 Document* document = toDocument(executionContext()); 96 ASSERT(document);
137 ASSERT(document);
138 97
139 double timeStampInMilliseconds = 1000 * document->loader()->timing()->mo notonicTimeToZeroBasedDocumentTime(timeStamp); 98 double timeStampInMilliseconds = 1000 * document->loader()->timing()->monoto nicTimeToZeroBasedDocumentTime(timeStamp);
140 99
141 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeSta mpInMilliseconds); 100 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampIn Milliseconds);
142 }
143 } 101 }
144 102
145 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, siz e_t length, double timeStampInMilliseconds) 103 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, siz e_t length, double timeStampInMilliseconds)
146 { 104 {
147 if (m_state == Resolved && portIndex < m_outputs.size() && data && length > 0) { 105 if (!data || !length || portIndex >= m_outputs.size())
148 // Convert from a time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now() 106 return;
149 // into a time in seconds which is based on the time coordinate system o f monotonicallyIncreasingTime(). 107 // Convert from a time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now()
150 double timeStamp; 108 // into a time in seconds which is based on the time coordinate system of mo notonicallyIncreasingTime().
109 double timeStamp;
151 110
152 if (!timeStampInMilliseconds) { 111 if (!timeStampInMilliseconds) {
153 // We treat a value of 0 (which is the default value) as special, me aning "now". 112 // We treat a value of 0 (which is the default value) as special, meanin g "now".
154 // We need to translate it exactly to 0 seconds. 113 // We need to translate it exactly to 0 seconds.
155 timeStamp = 0; 114 timeStamp = 0;
156 } else { 115 } else {
157 Document* document = toDocument(executionContext()); 116 Document* document = toDocument(executionContext());
158 ASSERT(document); 117 ASSERT(document);
159 double documentStartTime = document->loader()->timing()->referenceMo notonicTime(); 118 double documentStartTime = document->loader()->timing()->referenceMonoto nicTime();
160 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds; 119 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
161 } 120 }
162 121
163 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); 122 m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
164 }
165 } 123 }
166 124
167 void MIDIAccess::stop() 125 void MIDIAccess::stop()
168 { 126 {
169 if (m_state == Stopped)
170 return;
171 m_accessor.clear(); 127 m_accessor.clear();
172 m_weakPtrFactory.revokeAll(); 128 m_initializer->cancel();
173 if (m_state == Requesting) {
174 Document* document = toDocument(executionContext());
175 ASSERT(document);
176 MIDIController* controller = MIDIController::from(document->page());
177 ASSERT(controller);
178 controller->cancelSysexPermissionRequest(this);
179 }
180 m_state = Stopped;
181 } 129 }
182 130
183 bool MIDIAccess::hasPendingActivity() const 131 bool MIDIAccess::hasPendingActivity() const
184 { 132 {
185 return m_state == Requesting; 133 return m_initializer->hasPendingActivity();
186 } 134 }
187 135
188 void MIDIAccess::permissionDenied() 136 void MIDIAccess::initialize(PassOwnPtr<MIDIAccessor> accessor, bool sysexEnabled )
189 { 137 {
190 ASSERT(isMainThread()); 138 ASSERT(accessor);
191 m_resolver->reject(DOMError::create("SecurityError")); 139 m_accessor = accessor;
192 } 140 m_accessor->setClient(this);
193 141 m_sysexEnabled = sysexEnabled;
194 ScriptPromise MIDIAccess::startRequest(ScriptState* scriptState)
195 {
196 m_resolver = ScriptPromiseResolverWithContext::create(scriptState);
197 ScriptPromise promise = m_resolver->promise();
198 promise.then(PostAction::create(scriptState->isolate(), m_weakPtrFactory.cre ateWeakPtr(), Resolved),
199 PostAction::create(scriptState->isolate(), m_weakPtrFactory.createWeakPt r(), Stopped));
200
201 if (!m_options.sysex) {
202 m_accessor->startSession();
203 return promise;
204 }
205 Document* document = toDocument(executionContext());
206 ASSERT(document);
207 MIDIController* controller = MIDIController::from(document->page());
208 if (controller) {
209 controller->requestSysexPermission(this);
210 } else {
211 m_resolver->reject(DOMError::create("SecurityError"));
212 }
213 return promise;
214 }
215
216 void MIDIAccess::doPostAction(State state)
217 {
218 ASSERT(m_state == Requesting);
219 ASSERT(state == Resolved || state == Stopped);
220 if (state == Stopped) {
221 m_accessor.clear();
222 }
223 m_weakPtrFactory.revokeAll();
224 m_state = state;
225 } 142 }
226 143
227 void MIDIAccess::trace(Visitor* visitor) 144 void MIDIAccess::trace(Visitor* visitor)
228 { 145 {
229 visitor->trace(m_inputs); 146 visitor->trace(m_inputs);
230 visitor->trace(m_outputs); 147 visitor->trace(m_outputs);
231 EventTargetWithInlineData::trace(visitor); 148 EventTargetWithInlineData::trace(visitor);
232 } 149 }
233 150
234 } // namespace WebCore 151 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698