Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
|
Takashi Toyoshima
2016/07/01 12:44:51
Major logic changes are in this file.
| |
| 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 |
| 22 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID IOptions& options) | 26 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID IOptions& options) |
| 23 : ScriptPromiseResolver(scriptState) | 27 : ScriptPromiseResolver(scriptState) |
| 24 , m_options(options) | 28 , m_options(options) |
| 25 , m_hasBeenDisposed(false) | |
| 26 , m_permissionResolved(false) | |
| 27 { | 29 { |
| 28 } | 30 } |
| 29 | 31 |
| 30 MIDIAccessInitializer::~MIDIAccessInitializer() | |
| 31 { | |
| 32 dispose(); | |
| 33 } | |
| 34 | |
| 35 void MIDIAccessInitializer::contextDestroyed() | 32 void MIDIAccessInitializer::contextDestroyed() |
| 36 { | 33 { |
| 37 dispose(); | 34 m_permissionService.reset(); |
| 38 LifecycleObserver::contextDestroyed(); | 35 LifecycleObserver::contextDestroyed(); |
| 39 } | 36 } |
| 40 | 37 |
| 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() | 38 ScriptPromise MIDIAccessInitializer::start() |
| 61 { | 39 { |
| 62 ScriptPromise promise = this->promise(); | 40 ScriptPromise promise = this->promise(); |
| 63 m_accessor = MIDIAccessor::create(this); | 41 m_accessor = MIDIAccessor::create(this); |
| 64 | 42 |
| 65 Document* document = toDocument(getExecutionContext()); | 43 Document* document = toDocument(getExecutionContext()); |
| 66 DCHECK(document); | 44 DCHECK(document); |
| 67 if (MIDIController* controller = MIDIController::from(document->frame())) | 45 |
| 68 controller->requestPermission(this, m_options); | 46 document->frame()->serviceRegistry()->connectToRemoteService(mojo::GetProxy( &m_permissionService)); |
| 69 else | 47 |
| 70 reject(DOMException::create(SecurityError)); | 48 bool requestSysEx = m_options.hasSysex() && m_options.sysex(); |
| 49 mojo::WTFArray<mojom::blink::PermissionName> permissions = mojo::WTFArray<mo jom::blink::PermissionName>::New(requestSysEx ? 2 : 1); | |
| 50 permissions[0] = mojom::blink::PermissionName::MIDI; | |
| 51 if (requestSysEx) | |
| 52 permissions[1] = mojom::blink::PermissionName::MIDI_SYSEX; | |
| 53 m_permissionService->RequestPermissions( | |
| 54 permissions.PassStorage(), | |
|
Takashi Toyoshima
2016/07/01 12:44:51
I don't have enough confidence on this mojo:WTFArr
Sam McNally
2016/07/05 01:17:54
WTFArray is "move" constructible from a WTF::Vecto
Takashi Toyoshima
2016/07/05 08:03:18
Done.
| |
| 55 getExecutionContext()->getSecurityOrigin()->toString(), | |
| 56 UserGestureIndicator::processingUserGesture(), | |
| 57 createBaseCallback(WTF::bind(&MIDIAccessInitializer::onPermissionUpdated , wrapPersistent(this)))); | |
| 71 | 58 |
| 72 return promise; | 59 return promise; |
| 73 } | 60 } |
| 74 | 61 |
| 75 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu facturer, const String& name, const String& version, PortState state) | 62 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu facturer, const String& name, const String& version, PortState state) |
| 76 { | 63 { |
| 77 DCHECK(m_accessor); | 64 DCHECK(m_accessor); |
| 78 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty peInput, version, state)); | 65 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty peInput, version, state)); |
| 79 } | 66 } |
| 80 | 67 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 ec = AbortError; | 105 ec = AbortError; |
| 119 } else if (error == DOMException::getErrorName(InvalidStateError)) { | 106 } else if (error == DOMException::getErrorName(InvalidStateError)) { |
| 120 ec = InvalidStateError; | 107 ec = InvalidStateError; |
| 121 } else if (error == DOMException::getErrorName(NotSupportedError)) { | 108 } else if (error == DOMException::getErrorName(NotSupportedError)) { |
| 122 ec = NotSupportedError; | 109 ec = NotSupportedError; |
| 123 } | 110 } |
| 124 reject(DOMException::create(ec, message)); | 111 reject(DOMException::create(ec, message)); |
| 125 } | 112 } |
| 126 } | 113 } |
| 127 | 114 |
| 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 | 115 ExecutionContext* MIDIAccessInitializer::getExecutionContext() const |
| 143 { | 116 { |
| 144 return getScriptState()->getExecutionContext(); | 117 return getScriptState()->getExecutionContext(); |
| 145 } | 118 } |
| 146 | 119 |
| 120 void MIDIAccessInitializer::onPermissionUpdated(mojo::WTFArray<mojom::blink::Per missionStatus> status) | |
| 121 { | |
| 122 bool allowed = true; | |
| 123 for (size_t i = 0; i < status.size(); ++i) { | |
|
Sam McNally
2016/07/05 01:17:54
I think this would be nicer as a foreach loop. WTF
Takashi Toyoshima
2016/07/05 08:03:18
Done.
| |
| 124 if (status[i] != mojom::blink::PermissionStatus::GRANTED) { | |
| 125 allowed = false; | |
| 126 break; | |
| 127 } | |
| 128 } | |
| 129 m_permissionService.reset(); | |
| 130 if (allowed) | |
| 131 m_accessor->startSession(); | |
| 132 else | |
| 133 reject(DOMException::create(SecurityError)); | |
| 134 | |
| 135 } | |
| 136 | |
| 147 } // namespace blink | 137 } // namespace blink |
| OLD | NEW |