| 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 "modules/webmidi/MIDIAccessInitializer.h" | 5 #include "modules/webmidi/MIDIAccessInitializer.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/frame/LocalFrame.h" | 12 #include "core/frame/LocalFrame.h" |
| 13 #include "core/frame/Navigator.h" | 13 #include "core/frame/Navigator.h" |
| 14 #include "modules/permissions/PermissionUtils.h" | 14 #include "modules/permissions/PermissionUtils.h" |
| 15 #include "modules/webmidi/MIDIAccess.h" | 15 #include "modules/webmidi/MIDIAccess.h" |
| 16 #include "modules/webmidi/MIDIOptions.h" | 16 #include "modules/webmidi/MIDIOptions.h" |
| 17 #include "modules/webmidi/MIDIPort.h" | 17 #include "modules/webmidi/MIDIPort.h" |
| 18 #include "platform/UserGestureIndicator.h" | 18 #include "platform/UserGestureIndicator.h" |
| 19 #include "platform/mojo/MojoHelper.h" | 19 #include "platform/mojo/MojoHelper.h" |
| 20 #include "public/platform/InterfaceProvider.h" | 20 #include "public/platform/InterfaceProvider.h" |
| 21 #include "public/platform/modules/permissions/permission.mojom-blink.h" | 21 #include "public/platform/modules/permissions/permission.mojom-blink.h" |
| 22 | 22 |
| 23 namespace blink { | 23 namespace blink { |
| 24 | 24 |
| 25 using PortState = WebMIDIAccessorClient::MIDIPortState; | 25 using PortState = WebMIDIAccessorClient::MIDIPortState; |
| 26 | 26 |
| 27 using midi::mojom::Result; |
| 27 using mojom::blink::PermissionStatus; | 28 using mojom::blink::PermissionStatus; |
| 28 | 29 |
| 29 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, | 30 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, |
| 30 const MIDIOptions& options) | 31 const MIDIOptions& options) |
| 31 : ScriptPromiseResolver(scriptState), m_options(options) {} | 32 : ScriptPromiseResolver(scriptState), m_options(options) {} |
| 32 | 33 |
| 33 void MIDIAccessInitializer::contextDestroyed() { | 34 void MIDIAccessInitializer::contextDestroyed() { |
| 34 m_permissionService.reset(); | 35 m_permissionService.reset(); |
| 35 LifecycleObserver::contextDestroyed(); | 36 LifecycleObserver::contextDestroyed(); |
| 36 } | 37 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // are delegated to MIDIAccess. See constructor of MIDIAccess. | 79 // are delegated to MIDIAccess. See constructor of MIDIAccess. |
| 79 NOTREACHED(); | 80 NOTREACHED(); |
| 80 } | 81 } |
| 81 | 82 |
| 82 void MIDIAccessInitializer::didSetOutputPortState(unsigned portIndex, | 83 void MIDIAccessInitializer::didSetOutputPortState(unsigned portIndex, |
| 83 PortState state) { | 84 PortState state) { |
| 84 // See comments on didSetInputPortState(). | 85 // See comments on didSetInputPortState(). |
| 85 NOTREACHED(); | 86 NOTREACHED(); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void MIDIAccessInitializer::didStartSession(bool success, | 89 void MIDIAccessInitializer::didStartSession(Result result) { |
| 89 const String& error, | |
| 90 const String& message) { | |
| 91 DCHECK(m_accessor); | 90 DCHECK(m_accessor); |
| 92 if (success) { | 91 // We would also have AbortError and SecurityError according to the spec. |
| 93 resolve(MIDIAccess::create(std::move(m_accessor), | 92 // SecurityError is handled in onPermission(s)Updated(). |
| 94 m_options.hasSysex() && m_options.sysex(), | 93 switch (result) { |
| 95 m_portDescriptors, getExecutionContext())); | 94 case Result::NOT_INITIALIZED: |
| 96 } else { | 95 break; |
| 97 // The spec says the name is one of | 96 case Result::OK: |
| 98 // - SecurityError | 97 return resolve(MIDIAccess::create( |
| 99 // - AbortError | 98 std::move(m_accessor), m_options.hasSysex() && m_options.sysex(), |
| 100 // - InvalidStateError | 99 m_portDescriptors, getExecutionContext())); |
| 101 // - NotSupportedError | 100 case Result::NOT_SUPPORTED: |
| 102 // TODO(toyoshim): Do not rely on |error| string. Instead an enum | 101 return reject(DOMException::create(NotSupportedError)); |
| 103 // representing an ExceptionCode should be defined and deliverred. | 102 case Result::INITIALIZATION_ERROR: |
| 104 ExceptionCode ec = InvalidStateError; | 103 return reject(DOMException::create( |
| 105 if (error == DOMException::getErrorName(SecurityError)) { | 104 InvalidStateError, "Platform dependent initialization failed.")); |
| 106 ec = SecurityError; | |
| 107 } else if (error == DOMException::getErrorName(AbortError)) { | |
| 108 ec = AbortError; | |
| 109 } else if (error == DOMException::getErrorName(InvalidStateError)) { | |
| 110 ec = InvalidStateError; | |
| 111 } else if (error == DOMException::getErrorName(NotSupportedError)) { | |
| 112 ec = NotSupportedError; | |
| 113 } | |
| 114 reject(DOMException::create(ec, message)); | |
| 115 } | 105 } |
| 106 NOTREACHED(); |
| 107 reject(DOMException::create(InvalidStateError, |
| 108 "Unknown internal error occurred.")); |
| 116 } | 109 } |
| 117 | 110 |
| 118 ExecutionContext* MIDIAccessInitializer::getExecutionContext() const { | 111 ExecutionContext* MIDIAccessInitializer::getExecutionContext() const { |
| 119 return getScriptState()->getExecutionContext(); | 112 return getScriptState()->getExecutionContext(); |
| 120 } | 113 } |
| 121 | 114 |
| 122 void MIDIAccessInitializer::onPermissionsUpdated(PermissionStatus status) { | 115 void MIDIAccessInitializer::onPermissionsUpdated(PermissionStatus status) { |
| 123 m_permissionService.reset(); | 116 m_permissionService.reset(); |
| 124 if (status == PermissionStatus::GRANTED) | 117 if (status == PermissionStatus::GRANTED) |
| 125 m_accessor->startSession(); | 118 m_accessor->startSession(); |
| 126 else | 119 else |
| 127 reject(DOMException::create(SecurityError)); | 120 reject(DOMException::create(SecurityError)); |
| 128 } | 121 } |
| 129 | 122 |
| 130 void MIDIAccessInitializer::onPermissionUpdated(PermissionStatus status) { | 123 void MIDIAccessInitializer::onPermissionUpdated(PermissionStatus status) { |
| 131 m_permissionService.reset(); | 124 m_permissionService.reset(); |
| 132 if (status == PermissionStatus::GRANTED) | 125 if (status == PermissionStatus::GRANTED) |
| 133 m_accessor->startSession(); | 126 m_accessor->startSession(); |
| 134 else | 127 else |
| 135 reject(DOMException::create(SecurityError)); | 128 reject(DOMException::create(SecurityError)); |
| 136 } | 129 } |
| 137 | 130 |
| 138 } // namespace blink | 131 } // namespace blink |
| OLD | NEW |