1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "config.h" 6 #include "modules/webmidi/MIDIAccessInitializer.h" 7 8 #include "bindings/core/v8/ScriptPromise.h" 9 #include "bindings/core/v8/ScriptPromiseResolver.h" 10 #include "core/dom/DOMError.h" 11 #include "core/dom/Document.h" 12 #include "core/frame/Navigator.h" 13 #include "modules/webmidi/MIDIAccess.h" 14 #include "modules/webmidi/MIDIController.h" 15 #include "modules/webmidi/MIDIOptions.h" 16 #include "modules/webmidi/MIDIPort.h" 17 18 namespace blink { 19 20 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MIDIOptions& options) 21 : ScriptPromiseResolver(scriptState) 22 , m_requestSysex(false) 23 { 24 if (options.hasSysex()) 25 m_requestSysex = options.sysex(); 26 } 27 28 MIDIAccessInitializer::~MIDIAccessInitializer() 29 { 30 // It is safe to cancel a request which is already finished or canceld. 31 Document* document = toDocument(executionContext()); 32 ASSERT(document); 33 MIDIController* controller = MIDIController::from(document->frame()); 34 if (controller) 35 controller->cancelSysexPermissionRequest(this); 36 } 37 38 ScriptPromise MIDIAccessInitializer::start() 39 { 40 ScriptPromise promise = this->promise(); 41 m_accessor = MIDIAccessor::create(this); 42 43 if (!m_requestSysex) { 44 m_accessor->startSession(); 45 return promise; 46 } 47 Document* document = toDocument(executionContext()); 48 ASSERT(document); 49 MIDIController* controller = MIDIController::from(document->frame()); 50 if (controller) { 51 controller->requestSysexPermission(this); 52 } else { 53 reject(DOMError::create("SecurityError")); 54 } 55 return promise; 56 } 57 58 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version) 59 { 60 ASSERT(m_accessor); 61 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MIDIPortTypeInput, version)); 62 } 63 64 void MIDIAccessInitializer::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) 65 { 66 ASSERT(m_accessor); 67 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MIDIPortTypeOutput, version)); 68 } 69 70 void MIDIAccessInitializer::didStartSession(bool success, const String& error, const String& message) 71 { 72 ASSERT(m_accessor); 73 if (success) { 74 resolve(MIDIAccess::create(m_accessor.release(), m_requestSysex, m_portDescriptors, executionContext())); 75 } else { 76 reject(DOMError::create(error, message)); 77 } 78 } 79 80 void MIDIAccessInitializer::resolveSysexPermission(bool allowed) 81 { 82 if (allowed) 83 m_accessor->startSession(); 84 else 85 reject(DOMError::create("SecurityError")); 86 } 87 88 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const 89 { 90 return executionContext()->securityOrigin(); 91 } 92 93 ExecutionContext* MIDIAccessInitializer::executionContext() const 94 { 95 return scriptState()->executionContext(); 96 } 97 98 } // namespace blink 99