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 Vector<PermissionName> permissions; |
| 53 permissions.resize(requestSysEx ? 2 : 1); |
| 54 |
| 55 permissions[0] = PermissionName::MIDI; |
| 56 if (requestSysEx) |
| 57 permissions[1] = PermissionName::MIDI_SYSEX; |
| 58 |
| 59 m_permissionService->RequestPermissions( |
| 60 mojo::WTFArray<PermissionName>(std::move(permissions)), |
| 61 getExecutionContext()->getSecurityOrigin()->toString(), |
| 62 UserGestureIndicator::processingUserGesture(), |
| 63 convertToBaseCallback(WTF::bind(&MIDIAccessInitializer::onPermissionsUpd
ated, wrapPersistent(this)))); |
71 | 64 |
72 return promise; | 65 return promise; |
73 } | 66 } |
74 | 67 |
75 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version, PortState state) | 68 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version, PortState state) |
76 { | 69 { |
77 DCHECK(m_accessor); | 70 DCHECK(m_accessor); |
78 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty
peInput, version, state)); | 71 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::Ty
peInput, version, state)); |
79 } | 72 } |
80 | 73 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 ec = AbortError; | 111 ec = AbortError; |
119 } else if (error == DOMException::getErrorName(InvalidStateError)) { | 112 } else if (error == DOMException::getErrorName(InvalidStateError)) { |
120 ec = InvalidStateError; | 113 ec = InvalidStateError; |
121 } else if (error == DOMException::getErrorName(NotSupportedError)) { | 114 } else if (error == DOMException::getErrorName(NotSupportedError)) { |
122 ec = NotSupportedError; | 115 ec = NotSupportedError; |
123 } | 116 } |
124 reject(DOMException::create(ec, message)); | 117 reject(DOMException::create(ec, message)); |
125 } | 118 } |
126 } | 119 } |
127 | 120 |
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 | 121 ExecutionContext* MIDIAccessInitializer::getExecutionContext() const |
143 { | 122 { |
144 return getScriptState()->getExecutionContext(); | 123 return getScriptState()->getExecutionContext(); |
145 } | 124 } |
146 | 125 |
| 126 void MIDIAccessInitializer::onPermissionsUpdated(mojo::WTFArray<PermissionStatus
> statusArray) |
| 127 { |
| 128 bool allowed = true; |
| 129 for (const auto status : statusArray.storage()) { |
| 130 if (status != PermissionStatus::GRANTED) { |
| 131 allowed = false; |
| 132 break; |
| 133 } |
| 134 } |
| 135 m_permissionService.reset(); |
| 136 if (allowed) |
| 137 m_accessor->startSession(); |
| 138 else |
| 139 reject(DOMException::create(SecurityError)); |
| 140 |
| 141 } |
| 142 |
| 143 void MIDIAccessInitializer::onPermissionUpdated(PermissionStatus status) |
| 144 { |
| 145 m_permissionService.reset(); |
| 146 if (status == PermissionStatus::GRANTED) |
| 147 m_accessor->startSession(); |
| 148 else |
| 149 reject(DOMException::create(SecurityError)); |
| 150 } |
| 151 |
147 } // namespace blink | 152 } // namespace blink |
OLD | NEW |