Chromium Code Reviews| 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/Navigator.h" | 13 #include "core/frame/Navigator.h" |
| 13 #include "modules/webmidi/MIDIAccess.h" | 14 #include "modules/webmidi/MIDIAccess.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 #include "modules/webmidi/MIDIPort.h" |
| 17 #include "platform/UserGestureIndicator.h" | |
| 18 #include "platform/mojo/MojoHelper.h" | |
| 19 #include "public/platform/ServiceRegistry.h" | |
| 20 #include "third_party/WebKit/public/platform/modules/permissions/permission.mojo m-blink.h" | |
| 17 | 21 |
| 18 namespace blink { | 22 namespace blink { |
| 19 | 23 |
| 20 using PortState = WebMIDIAccessorClient::MIDIPortState; | 24 using PortState = WebMIDIAccessorClient::MIDIPortState; |
| 21 | 25 |
| 26 using mojom::blink::PermissionName; | |
| 27 using mojom::blink::PermissionStatus; | |
| 28 | |
| 22 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID IOptions& options) | 29 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID IOptions& options) |
| 23 : ScriptPromiseResolver(scriptState) | 30 : ScriptPromiseResolver(scriptState) |
| 24 , m_options(options) | 31 , m_options(options) |
| 25 , m_hasBeenDisposed(false) | |
| 26 , m_permissionResolved(false) | |
| 27 { | 32 { |
| 28 } | 33 } |
| 29 | 34 |
| 30 MIDIAccessInitializer::~MIDIAccessInitializer() | |
| 31 { | |
| 32 dispose(); | |
| 33 } | |
| 34 | |
| 35 void MIDIAccessInitializer::contextDestroyed() | 35 void MIDIAccessInitializer::contextDestroyed() |
| 36 { | 36 { |
| 37 dispose(); | 37 m_permissionService.reset(); |
| 38 LifecycleObserver::contextDestroyed(); | 38 LifecycleObserver::contextDestroyed(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 void MIDIAccessInitializer::dispose() | |
| 42 { | |
| 43 if (m_hasBeenDisposed) | |
| 44 return; | |
| 45 | |
| 46 if (!getExecutionContext()) | |
| 47 return; | |
| 48 | |
| 49 if (!m_permissionResolved) { | |
| 50 Document* document = toDocument(getExecutionContext()); | |
| 51 DCHECK(document); | |
| 52 if (MIDIController* controller = MIDIController::from(document->frame()) ) | |
| 53 controller->cancelPermissionRequest(this); | |
| 54 m_permissionResolved = true; | |
| 55 } | |
| 56 | |
| 57 m_hasBeenDisposed = true; | |
| 58 } | |
| 59 | |
| 60 ScriptPromise MIDIAccessInitializer::start() | 41 ScriptPromise MIDIAccessInitializer::start() |
| 61 { | 42 { |
| 62 ScriptPromise promise = this->promise(); | 43 ScriptPromise promise = this->promise(); |
| 63 m_accessor = MIDIAccessor::create(this); | 44 m_accessor = MIDIAccessor::create(this); |
| 64 | 45 |
| 65 Document* document = toDocument(getExecutionContext()); | 46 Document* document = toDocument(getExecutionContext()); |
| 66 DCHECK(document); | 47 DCHECK(document); |
| 67 if (MIDIController* controller = MIDIController::from(document->frame())) | 48 |
| 68 controller->requestPermission(this, m_options); | 49 document->frame()->serviceRegistry()->connectToRemoteService(mojo::GetProxy( &m_permissionService)); |
| 69 else | 50 |
| 70 reject(DOMException::create(SecurityError)); | 51 bool requestSysEx = m_options.hasSysex() && m_options.sysex(); |
| 52 if (requestSysEx) { | |
| 53 Vector<PermissionName> permissions = { PermissionName::MIDI, PermissionN ame::MIDI_SYSEX }; | |
| 54 m_permissionService->RequestPermissions( | |
| 55 mojo::WTFArray<PermissionName>(std::move(permissions)), | |
| 56 getExecutionContext()->getSecurityOrigin()->toString(), | |
| 57 UserGestureIndicator::processingUserGesture(), | |
| 58 createBaseCallback(WTF::bind(&MIDIAccessInitializer::onPermissionsUp dated, wrapPersistent(this)))); | |
| 59 } else { | |
| 60 // TODO(toyoshim): Merge this MIDI only code path to the other one for | |
|
Takashi Toyoshima
2016/07/07 11:03:11
Here is TODO.
AwPermissionManagerTest.java's chang
| |
| 61 // MIDI and MIDI_SYSEX. This is a workaround to pass | |
| 62 // org.chromium.android_webview.test.AwPermissionManagerTest#testRequest Multiple | |
| 63 // because of RequestPermissions not implemented in AndroidWebView. | |
| 64 m_permissionService->RequestPermission( | |
| 65 PermissionName::MIDI, | |
| 66 getExecutionContext()->getSecurityOrigin()->toString(), | |
| 67 UserGestureIndicator::processingUserGesture(), | |
| 68 createBaseCallback(WTF::bind(&MIDIAccessInitializer::onPermissionUpd ated, wrapPersistent(this)))); | |
| 69 } | |
| 71 | 70 |
| 72 return promise; | 71 return promise; |
| 73 } | 72 } |
| 74 | 73 |
| 75 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu facturer, const String& name, const String& version, PortState state) | 74 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu facturer, const String& name, const String& version, PortState state) |
| 76 { | 75 { |
| 77 DCHECK(m_accessor); | 76 DCHECK(m_accessor); |
| 78 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty peInput, version, state)); | 77 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty peInput, version, state)); |
| 79 } | 78 } |
| 80 | 79 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 ec = AbortError; | 117 ec = AbortError; |
| 119 } else if (error == DOMException::getErrorName(InvalidStateError)) { | 118 } else if (error == DOMException::getErrorName(InvalidStateError)) { |
| 120 ec = InvalidStateError; | 119 ec = InvalidStateError; |
| 121 } else if (error == DOMException::getErrorName(NotSupportedError)) { | 120 } else if (error == DOMException::getErrorName(NotSupportedError)) { |
| 122 ec = NotSupportedError; | 121 ec = NotSupportedError; |
| 123 } | 122 } |
| 124 reject(DOMException::create(ec, message)); | 123 reject(DOMException::create(ec, message)); |
| 125 } | 124 } |
| 126 } | 125 } |
| 127 | 126 |
| 128 void MIDIAccessInitializer::resolvePermission(bool allowed) | |
| 129 { | |
| 130 m_permissionResolved = true; | |
| 131 if (allowed) | |
| 132 m_accessor->startSession(); | |
| 133 else | |
| 134 reject(DOMException::create(SecurityError)); | |
| 135 } | |
| 136 | |
| 137 SecurityOrigin* MIDIAccessInitializer::getSecurityOrigin() const | |
| 138 { | |
| 139 return getExecutionContext()->getSecurityOrigin(); | |
| 140 } | |
| 141 | |
| 142 ExecutionContext* MIDIAccessInitializer::getExecutionContext() const | 127 ExecutionContext* MIDIAccessInitializer::getExecutionContext() const |
| 143 { | 128 { |
| 144 return getScriptState()->getExecutionContext(); | 129 return getScriptState()->getExecutionContext(); |
| 145 } | 130 } |
| 146 | 131 |
| 132 void MIDIAccessInitializer::onPermissionsUpdated(mojo::WTFArray<PermissionStatus > statusArray) | |
| 133 { | |
| 134 bool allowed = true; | |
| 135 for (const auto status : statusArray.storage()) { | |
| 136 if (status != PermissionStatus::GRANTED) { | |
| 137 allowed = false; | |
| 138 break; | |
| 139 } | |
| 140 } | |
| 141 m_permissionService.reset(); | |
| 142 if (allowed) | |
| 143 m_accessor->startSession(); | |
| 144 else | |
| 145 reject(DOMException::create(SecurityError)); | |
| 146 | |
| 147 } | |
| 148 | |
| 149 void MIDIAccessInitializer::onPermissionUpdated(PermissionStatus status) | |
| 150 { | |
| 151 m_permissionService.reset(); | |
| 152 if (status == PermissionStatus::GRANTED) | |
| 153 m_accessor->startSession(); | |
| 154 else | |
| 155 reject(DOMException::create(SecurityError)); | |
| 156 } | |
| 157 | |
| 147 } // namespace blink | 158 } // namespace blink |
| OLD | NEW |