This source file includes following definitions.
- create
- m_state
- call
- request
- m_asyncRejectRunner
- setSysexEnabled
- didAddInputPort
- didAddOutputPort
- didStartSession
- didReceiveMIDIData
- sendMIDIData
- suspend
- resume
- stop
- hasPendingActivity
- permissionDenied
- startRequest
- resolve
- reject
- resolveNow
- rejectNow
- doPostAction
- trace
#include "config.h"
#include "modules/webmidi/MIDIAccess.h"
#include "bindings/v8/MIDIAccessResolver.h"
#include "bindings/v8/ScriptPromise.h"
#include "bindings/v8/ScriptPromiseResolver.h"
#include "bindings/v8/V8Binding.h"
#include "core/dom/DOMError.h"
#include "core/dom/Document.h"
#include "core/loader/DocumentLoadTiming.h"
#include "core/loader/DocumentLoader.h"
#include "modules/webmidi/MIDIConnectionEvent.h"
#include "modules/webmidi/MIDIController.h"
#include "modules/webmidi/MIDIOptions.h"
#include "modules/webmidi/MIDIPort.h"
#include "platform/AsyncMethodRunner.h"
namespace WebCore {
class MIDIAccess::PostAction : public ScriptFunction {
public:
static PassOwnPtr<MIDIAccess::PostAction> create(v8::Isolate* isolate, WeakPtr<MIDIAccess> owner, State state) { return adoptPtr(new PostAction(isolate, owner, state)); }
private:
PostAction(v8::Isolate* isolate, WeakPtr<MIDIAccess> owner, State state): ScriptFunction(isolate), m_owner(owner), m_state(state) { }
virtual ScriptValue call(ScriptValue value) OVERRIDE
{
if (!m_owner.get())
return value;
m_owner->doPostAction(m_state);
return value;
}
WeakPtr<MIDIAccess> m_owner;
State m_state;
};
ScriptPromise MIDIAccess::request(const MIDIOptions& options, ExecutionContext* context)
{
RefPtrWillBeRawPtr<MIDIAccess> midiAccess(adoptRefWillBeRefCountedGarbageCollected(new MIDIAccess(options, context)));
midiAccess->suspendIfNeeded();
toV8NoInline(midiAccess.get(), context);
return midiAccess->startRequest();
}
MIDIAccess::~MIDIAccess()
{
}
MIDIAccess::MIDIAccess(const MIDIOptions& options, ExecutionContext* context)
: ActiveDOMObject(context)
, m_state(Requesting)
, m_weakPtrFactory(this)
, m_options(options)
, m_sysexEnabled(false)
, m_asyncResolveRunner(this, &MIDIAccess::resolveNow)
, m_asyncRejectRunner(this, &MIDIAccess::rejectNow)
{
ScriptWrappable::init(this);
m_accessor = MIDIAccessor::create(this);
}
void MIDIAccess::setSysexEnabled(bool enable)
{
m_sysexEnabled = enable;
if (enable) {
m_accessor->startSession();
} else {
reject(DOMError::create("SecurityError"));
}
}
void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version)
{
ASSERT(isMainThread());
m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version));
}
void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
{
ASSERT(isMainThread());
unsigned portIndex = m_outputs.size();
m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version));
}
void MIDIAccess::didStartSession(bool success)
{
ASSERT(isMainThread());
if (success)
resolve();
else
reject(DOMError::create("InvalidStateError"));
}
void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp)
{
ASSERT(isMainThread());
if (m_state == Resolved && portIndex < m_inputs.size()) {
Document* document = toDocument(executionContext());
ASSERT(document);
double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp);
m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds);
}
}
void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds)
{
if (m_state == Resolved && portIndex < m_outputs.size() && data && length > 0) {
double timeStamp;
if (!timeStampInMilliseconds) {
timeStamp = 0;
} else {
Document* document = toDocument(executionContext());
ASSERT(document);
double documentStartTime = document->loader()->timing()->referenceMonotonicTime();
timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
}
m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
}
}
void MIDIAccess::suspend()
{
m_asyncResolveRunner.suspend();
m_asyncRejectRunner.suspend();
}
void MIDIAccess::resume()
{
m_asyncResolveRunner.resume();
m_asyncRejectRunner.resume();
}
void MIDIAccess::stop()
{
if (m_state == Stopped)
return;
m_error.clear();
m_accessor.clear();
m_asyncResolveRunner.stop();
m_asyncRejectRunner.stop();
m_weakPtrFactory.revokeAll();
if (m_state == Requesting) {
Document* document = toDocument(executionContext());
ASSERT(document);
MIDIController* controller = MIDIController::from(document->page());
ASSERT(controller);
controller->cancelSysexPermissionRequest(this);
}
m_state = Stopped;
}
bool MIDIAccess::hasPendingActivity() const
{
return m_state == Requesting;
}
void MIDIAccess::permissionDenied()
{
ASSERT(isMainThread());
reject(DOMError::create("SecurityError"));
}
ScriptPromise MIDIAccess::startRequest()
{
m_resolver = MIDIAccessResolver::create(ScriptPromiseResolver::create(executionContext()), toIsolate(executionContext()));
ScriptPromise promise = m_resolver->promise();
promise.then(PostAction::create(toIsolate(executionContext()), m_weakPtrFactory.createWeakPtr(), Resolved),
PostAction::create(toIsolate(executionContext()), m_weakPtrFactory.createWeakPtr(), Stopped));
if (!m_options.sysex) {
m_accessor->startSession();
return promise;
}
Document* document = toDocument(executionContext());
ASSERT(document);
MIDIController* controller = MIDIController::from(document->page());
if (controller) {
controller->requestSysexPermission(this);
} else {
reject(DOMError::create("SecurityError"));
}
return promise;
}
void MIDIAccess::resolve()
{
m_asyncResolveRunner.runAsync();
}
void MIDIAccess::reject(PassRefPtrWillBeRawPtr<DOMError> error)
{
m_error = error;
m_asyncRejectRunner.runAsync();
}
void MIDIAccess::resolveNow()
{
m_resolver->resolve(this, executionContext());
}
void MIDIAccess::rejectNow()
{
m_resolver->reject(m_error.get(), executionContext());
m_error.clear();
}
void MIDIAccess::doPostAction(State state)
{
ASSERT(m_state == Requesting);
ASSERT(state == Resolved || state == Stopped);
m_error.clear();
if (state == Stopped) {
m_accessor.clear();
}
m_weakPtrFactory.revokeAll();
m_state = state;
}
void MIDIAccess::trace(Visitor* visitor)
{
visitor->trace(m_inputs);
visitor->trace(m_outputs);
visitor->trace(m_error);
}
}