| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/webmidi/MIDIAccessInitializer.h" | 6 #include "modules/webmidi/MIDIAccessInitializer.h" |
| 7 | 7 |
| 8 #include "bindings/v8/ScriptFunction.h" | 8 #include "bindings/v8/ScriptFunction.h" |
| 9 #include "bindings/v8/ScriptPromise.h" | 9 #include "bindings/v8/ScriptPromise.h" |
| 10 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | 10 #include "bindings/v8/ScriptPromiseResolverWithContext.h" |
| 11 #include "core/dom/DOMError.h" | 11 #include "core/dom/DOMError.h" |
| 12 #include "core/dom/Document.h" | 12 #include "core/dom/Document.h" |
| 13 #include "modules/webmidi/MIDIAccess.h" | 13 #include "modules/webmidi/MIDIAccess.h" |
| 14 #include "modules/webmidi/MIDIController.h" | 14 #include "modules/webmidi/MIDIController.h" |
| 15 #include "modules/webmidi/MIDIOptions.h" | 15 #include "modules/webmidi/MIDIOptions.h" |
| 16 #include "modules/webmidi/MIDIPort.h" |
| 16 | 17 |
| 17 namespace WebCore { | 18 namespace WebCore { |
| 18 | 19 |
| 19 class MIDIAccessInitializer::PostAction : public ScriptFunction { | 20 MIDIAccessInitializer::MIDIAccessInitializer(const MIDIOptions& options) |
| 20 public: | 21 : m_resolver(nullptr) |
| 21 static PassOwnPtr<MIDIAccessInitializer::PostAction> create(v8::Isolate* iso
late, WeakPtr<MIDIAccessInitializer> owner, State state) { return adoptPtr(new P
ostAction(isolate, owner, state)); } | 22 , m_options(options) |
| 22 | 23 , m_sysexEnabled(false) |
| 23 private: | 24 { |
| 24 PostAction(v8::Isolate* isolate, WeakPtr<MIDIAccessInitializer> owner, State
state): ScriptFunction(isolate), m_owner(owner), m_state(state) { } | 25 } |
| 25 virtual ScriptValue call(ScriptValue value) OVERRIDE | |
| 26 { | |
| 27 if (!m_owner.get()) | |
| 28 return value; | |
| 29 m_owner->doPostAction(m_state); | |
| 30 return value; | |
| 31 } | |
| 32 | |
| 33 WeakPtr<MIDIAccessInitializer> m_owner; | |
| 34 State m_state; | |
| 35 }; | |
| 36 | 26 |
| 37 MIDIAccessInitializer::~MIDIAccessInitializer() | 27 MIDIAccessInitializer::~MIDIAccessInitializer() |
| 38 { | 28 { |
| 39 ASSERT(m_state != Requesting); | 29 cancel(); |
| 40 } | 30 } |
| 41 | 31 |
| 42 MIDIAccessInitializer::MIDIAccessInitializer(const MIDIOptions& options, MIDIAcc
ess* access) | 32 void MIDIAccessInitializer::start(AsyncInitializerResolver<MIDIAccessInitializer
>* resolver) |
| 43 : m_state(Requesting) | |
| 44 , m_weakPtrFactory(this) | |
| 45 , m_options(options) | |
| 46 , m_sysexEnabled(false) | |
| 47 , m_access(access) | |
| 48 { | 33 { |
| 34 m_resolver = resolver; |
| 49 m_accessor = MIDIAccessor::create(this); | 35 m_accessor = MIDIAccessor::create(this); |
| 36 |
| 37 if (!m_options.sysex) { |
| 38 m_accessor->startSession(); |
| 39 return; |
| 40 } |
| 41 Document* document = toDocument(executionContext()); |
| 42 ASSERT(document); |
| 43 MIDIController* controller = MIDIController::from(document->page()); |
| 44 if (controller) { |
| 45 controller->requestSysexPermission(this); |
| 46 } else { |
| 47 m_resolver->reject(DOMError::create("SecurityError")); |
| 48 } |
| 50 } | 49 } |
| 51 | 50 |
| 52 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version) | 51 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version) |
| 53 { | 52 { |
| 54 m_access->didAddInputPort(id, manufacturer, name, version); | 53 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MI
DIPortTypeInput, version)); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void MIDIAccessInitializer::didAddOutputPort(const String& id, const String& man
ufacturer, const String& name, const String& version) | 56 void MIDIAccessInitializer::didAddOutputPort(const String& id, const String& man
ufacturer, const String& name, const String& version) |
| 58 { | 57 { |
| 59 m_access->didAddOutputPort(id, manufacturer, name, version); | 58 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MI
DIPortTypeOutput, version)); |
| 60 } | 59 } |
| 61 | 60 |
| 62 void MIDIAccessInitializer::didStartSession(bool success, const String& error, c
onst String& message) | 61 void MIDIAccessInitializer::didStartSession(bool success, const String& error, c
onst String& message) |
| 63 { | 62 { |
| 64 if (success) | 63 if (success) { |
| 65 m_resolver->resolve(m_access); | 64 m_resolver->resolve(MIDIAccess::create(m_accessor.release(), m_sysexEnab
led, m_portDescriptors, executionContext())); |
| 66 else | 65 } else { |
| 67 m_resolver->reject(DOMError::create(error, message)); | 66 m_resolver->reject(DOMError::create(error, message)); |
| 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 void MIDIAccessInitializer::setSysexEnabled(bool enable) | 70 void MIDIAccessInitializer::setSysexEnabled(bool enable) |
| 71 { | 71 { |
| 72 m_sysexEnabled = enable; | 72 m_sysexEnabled = enable; |
| 73 if (enable) { | 73 if (enable) { |
| 74 m_accessor->startSession(); | 74 m_accessor->startSession(); |
| 75 } else { | 75 } else { |
| 76 m_resolver->reject(DOMError::create("SecurityError")); | 76 m_resolver->reject(DOMError::create("SecurityError")); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const | 80 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const |
| 81 { | 81 { |
| 82 return m_access->executionContext()->securityOrigin(); | 82 return executionContext()->securityOrigin(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void MIDIAccessInitializer::cancel() | 85 void MIDIAccessInitializer::cancel() |
| 86 { | 86 { |
| 87 if (m_state != Requesting) | |
| 88 return; | |
| 89 m_accessor.clear(); | 87 m_accessor.clear(); |
| 90 m_weakPtrFactory.revokeAll(); | |
| 91 Document* document = toDocument(executionContext()); | 88 Document* document = toDocument(executionContext()); |
| 92 ASSERT(document); | 89 ASSERT(document); |
| 93 MIDIController* controller = MIDIController::from(document->page()); | 90 MIDIController* controller = MIDIController::from(document->page()); |
| 94 ASSERT(controller); | 91 ASSERT(controller); |
| 95 controller->cancelSysexPermissionRequest(this); | 92 controller->cancelSysexPermissionRequest(this); |
| 96 m_state = Stopped; | |
| 97 } | |
| 98 | |
| 99 ExecutionContext* MIDIAccessInitializer::executionContext() const | |
| 100 { | |
| 101 return m_access->executionContext(); | |
| 102 } | 93 } |
| 103 | 94 |
| 104 void MIDIAccessInitializer::permissionDenied() | 95 void MIDIAccessInitializer::permissionDenied() |
| 105 { | 96 { |
| 106 ASSERT(isMainThread()); | 97 ASSERT(isMainThread()); |
| 107 m_resolver->reject(DOMError::create("SecurityError")); | 98 m_resolver->reject(DOMError::create("SecurityError")); |
| 108 } | 99 } |
| 109 | 100 |
| 110 ScriptPromise MIDIAccessInitializer::initialize(ScriptState* scriptState) | 101 ExecutionContext* MIDIAccessInitializer::executionContext() const |
| 111 { | 102 { |
| 112 m_resolver = ScriptPromiseResolverWithContext::create(scriptState); | 103 ASSERT(m_resolver); |
| 113 ScriptPromise promise = m_resolver->promise(); | 104 return m_resolver->scriptState()->executionContext(); |
| 114 promise.then(PostAction::create(scriptState->isolate(), m_weakPtrFactory.cre
ateWeakPtr(), Resolved), | |
| 115 PostAction::create(scriptState->isolate(), m_weakPtrFactory.createWeakPt
r(), Stopped)); | |
| 116 | |
| 117 if (!m_options.sysex) { | |
| 118 m_accessor->startSession(); | |
| 119 return promise; | |
| 120 } | |
| 121 Document* document = toDocument(executionContext()); | |
| 122 ASSERT(document); | |
| 123 MIDIController* controller = MIDIController::from(document->page()); | |
| 124 if (controller) { | |
| 125 controller->requestSysexPermission(this); | |
| 126 } else { | |
| 127 m_resolver->reject(DOMError::create("SecurityError")); | |
| 128 } | |
| 129 return promise; | |
| 130 } | |
| 131 | |
| 132 void MIDIAccessInitializer::doPostAction(State state) | |
| 133 { | |
| 134 ASSERT(m_state == Requesting); | |
| 135 ASSERT(state == Resolved || state == Stopped); | |
| 136 if (state == Resolved) { | |
| 137 m_access->initialize(m_accessor.release(), m_sysexEnabled); | |
| 138 } | |
| 139 m_accessor.clear(); | |
| 140 m_weakPtrFactory.revokeAll(); | |
| 141 m_state = state; | |
| 142 } | 105 } |
| 143 | 106 |
| 144 } // namespace WebCore | 107 } // namespace WebCore |
| OLD | NEW |