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
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "modules/webmidi/MIDIAccessInitializer.h"
7
8 #include "bindings/v8/ScriptPromise.h"
9 #include "bindings/v8/ScriptPromiseResolverWithContext.h"
10 #include "core/dom/DOMError.h"
11 #include "core/dom/Document.h"
12 #include "core/frame/Navigator.h"
13 #include "modules/webmidi/MIDIAccess.h"
14 #include "modules/webmidi/MIDIController.h"
15 #include "modules/webmidi/MIDIOptions.h"
16 #include "modules/webmidi/MIDIPort.h"
17
18 namespace WebCore {
19
MIDIAccessInitializer(ScriptState * scriptState,const MIDIOptions & options)20 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MIDIOptions& options)
21 : ScriptPromiseResolverWithContext(scriptState)
22 , m_options(options)
23 , m_sysexEnabled(false)
24 {
25 }
26
~MIDIAccessInitializer()27 MIDIAccessInitializer::~MIDIAccessInitializer()
28 {
29 // It is safe to cancel a request which is already finished or canceld.
30 Document* document = toDocument(executionContext());
31 ASSERT(document);
32 MIDIController* controller = MIDIController::from(document->frame());
33 if (controller)
34 controller->cancelSysexPermissionRequest(this);
35 }
36
start()37 ScriptPromise MIDIAccessInitializer::start()
38 {
39 ScriptPromise promise = this->promise();
40 m_accessor = MIDIAccessor::create(this);
41
42 if (!m_options.sysex) {
43 m_accessor->startSession();
44 return promise;
45 }
46 Document* document = toDocument(executionContext());
47 ASSERT(document);
48 MIDIController* controller = MIDIController::from(document->frame());
49 if (controller) {
50 controller->requestSysexPermission(this);
51 } else {
52 reject(DOMError::create("SecurityError"));
53 }
54 return promise;
55 }
56
didAddInputPort(const String & id,const String & manufacturer,const String & name,const String & version)57 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version)
58 {
59 ASSERT(m_accessor);
60 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MIDIPortTypeInput, version));
61 }
62
didAddOutputPort(const String & id,const String & manufacturer,const String & name,const String & version)63 void MIDIAccessInitializer::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
64 {
65 ASSERT(m_accessor);
66 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MIDIPortTypeOutput, version));
67 }
68
didStartSession(bool success,const String & error,const String & message)69 void MIDIAccessInitializer::didStartSession(bool success, const String& error, const String& message)
70 {
71 ASSERT(m_accessor);
72 if (success) {
73 resolve(MIDIAccess::create(m_accessor.release(), m_sysexEnabled, m_portDescriptors, executionContext()));
74 } else {
75 reject(DOMError::create(error, message));
76 }
77 }
78
setSysexEnabled(bool enable)79 void MIDIAccessInitializer::setSysexEnabled(bool enable)
80 {
81 m_sysexEnabled = enable;
82 if (enable)
83 m_accessor->startSession();
84 else
85 reject(DOMError::create("SecurityError"));
86 }
87
securityOrigin() const88 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const
89 {
90 return executionContext()->securityOrigin();
91 }
92
executionContext() const93 ExecutionContext* MIDIAccessInitializer::executionContext() const
94 {
95 return scriptState()->executionContext();
96 }
97
98 } // namespace WebCore
99