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" | |
13 #include "core/frame/Navigator.h" | 12 #include "core/frame/Navigator.h" |
14 #include "modules/webmidi/MIDIAccess.h" | 13 #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" | |
21 | 17 |
22 namespace blink { | 18 namespace blink { |
23 | 19 |
24 using PortState = WebMIDIAccessorClient::MIDIPortState; | 20 using PortState = WebMIDIAccessorClient::MIDIPortState; |
25 | 21 |
26 using mojom::blink::PermissionName; | |
27 using mojom::blink::PermissionStatus; | |
28 | |
29 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID
IOptions& options) | 22 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID
IOptions& options) |
30 : ScriptPromiseResolver(scriptState) | 23 : ScriptPromiseResolver(scriptState) |
31 , m_options(options) | 24 , m_options(options) |
| 25 , m_hasBeenDisposed(false) |
| 26 , m_permissionResolved(false) |
32 { | 27 { |
33 } | 28 } |
34 | 29 |
| 30 MIDIAccessInitializer::~MIDIAccessInitializer() |
| 31 { |
| 32 dispose(); |
| 33 } |
| 34 |
35 void MIDIAccessInitializer::contextDestroyed() | 35 void MIDIAccessInitializer::contextDestroyed() |
36 { | 36 { |
37 m_permissionService.reset(); | 37 dispose(); |
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 |
41 ScriptPromise MIDIAccessInitializer::start() | 60 ScriptPromise MIDIAccessInitializer::start() |
42 { | 61 { |
43 ScriptPromise promise = this->promise(); | 62 ScriptPromise promise = this->promise(); |
44 m_accessor = MIDIAccessor::create(this); | 63 m_accessor = MIDIAccessor::create(this); |
45 | 64 |
46 Document* document = toDocument(getExecutionContext()); | 65 Document* document = toDocument(getExecutionContext()); |
47 DCHECK(document); | 66 DCHECK(document); |
48 | 67 if (MIDIController* controller = MIDIController::from(document->frame())) |
49 document->frame()->serviceRegistry()->connectToRemoteService(mojo::GetProxy(
&m_permissionService)); | 68 controller->requestPermission(this, m_options); |
50 | 69 else |
51 bool requestSysEx = m_options.hasSysex() && m_options.sysex(); | 70 reject(DOMException::create(SecurityError)); |
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 | |
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 } | |
70 | 71 |
71 return promise; | 72 return promise; |
72 } | 73 } |
73 | 74 |
74 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version, PortState state) | 75 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version, PortState state) |
75 { | 76 { |
76 DCHECK(m_accessor); | 77 DCHECK(m_accessor); |
77 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty
peInput, version, state)); | 78 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty
peInput, version, state)); |
78 } | 79 } |
79 | 80 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 ec = AbortError; | 118 ec = AbortError; |
118 } else if (error == DOMException::getErrorName(InvalidStateError)) { | 119 } else if (error == DOMException::getErrorName(InvalidStateError)) { |
119 ec = InvalidStateError; | 120 ec = InvalidStateError; |
120 } else if (error == DOMException::getErrorName(NotSupportedError)) { | 121 } else if (error == DOMException::getErrorName(NotSupportedError)) { |
121 ec = NotSupportedError; | 122 ec = NotSupportedError; |
122 } | 123 } |
123 reject(DOMException::create(ec, message)); | 124 reject(DOMException::create(ec, message)); |
124 } | 125 } |
125 } | 126 } |
126 | 127 |
| 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 |
127 ExecutionContext* MIDIAccessInitializer::getExecutionContext() const | 142 ExecutionContext* MIDIAccessInitializer::getExecutionContext() const |
128 { | 143 { |
129 return getScriptState()->getExecutionContext(); | 144 return getScriptState()->getExecutionContext(); |
130 } | 145 } |
131 | 146 |
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 | |
158 } // namespace blink | 147 } // namespace blink |
OLD | NEW |