OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/webmidi/MIDIAccess.h" | 32 #include "modules/webmidi/MIDIAccess.h" |
33 | 33 |
34 #include "core/dom/DOMError.h" | 34 #include "core/dom/DOMError.h" |
35 #include "core/dom/Document.h" | 35 #include "core/dom/Document.h" |
36 #include "core/dom/ExceptionCode.h" | 36 #include "core/dom/ExceptionCode.h" |
37 #include "core/loader/DocumentLoadTiming.h" | 37 #include "core/loader/DocumentLoadTiming.h" |
38 #include "core/loader/DocumentLoader.h" | 38 #include "core/loader/DocumentLoader.h" |
39 #include "modules/webmidi/MIDIAccessPromise.h" | 39 #include "modules/webmidi/MIDIAccessPromise.h" |
40 #include "modules/webmidi/MIDIConnectionEvent.h" | 40 #include "modules/webmidi/MIDIConnectionEvent.h" |
| 41 #include "modules/webmidi/MIDIController.h" |
41 #include "modules/webmidi/MIDIInput.h" | 42 #include "modules/webmidi/MIDIInput.h" |
42 #include "modules/webmidi/MIDIOutput.h" | 43 #include "modules/webmidi/MIDIOutput.h" |
43 #include "modules/webmidi/MIDIPort.h" | 44 #include "modules/webmidi/MIDIPort.h" |
44 | 45 |
45 namespace WebCore { | 46 namespace WebCore { |
46 | 47 |
47 PassRefPtr<MIDIAccess> MIDIAccess::create(ScriptExecutionContext* context, MIDIA
ccessPromise* promise) | 48 PassRefPtr<MIDIAccess> MIDIAccess::create(ScriptExecutionContext* context, MIDIA
ccessPromise* promise) |
48 { | 49 { |
49 RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(context, promise))); | 50 RefPtr<MIDIAccess> midiAccess(adoptRef(new MIDIAccess(context, promise))); |
50 midiAccess->suspendIfNeeded(); | 51 midiAccess->suspendIfNeeded(); |
| 52 midiAccess->startRequest(); |
51 return midiAccess.release(); | 53 return midiAccess.release(); |
52 } | 54 } |
53 | 55 |
54 MIDIAccess::~MIDIAccess() | 56 MIDIAccess::~MIDIAccess() |
55 { | 57 { |
56 stop(); | 58 stop(); |
57 } | 59 } |
58 | 60 |
59 MIDIAccess::MIDIAccess(ScriptExecutionContext* context, MIDIAccessPromise* promi
se) | 61 MIDIAccess::MIDIAccess(ScriptExecutionContext* context, MIDIAccessPromise* promi
se) |
60 : ActiveDOMObject(context) | 62 : ActiveDOMObject(context) |
61 , m_promise(promise) | 63 , m_promise(promise) |
62 , m_hasAccess(false) | 64 , m_hasAccess(false) |
| 65 , m_enableSysEx(false) |
| 66 , m_requesting(false) |
63 { | 67 { |
64 ScriptWrappable::init(this); | 68 ScriptWrappable::init(this); |
65 m_accessor = MIDIAccessor::create(this); | 69 m_accessor = MIDIAccessor::create(this); |
66 m_accessor->requestAccess(promise->options()->sysex); | 70 } |
| 71 |
| 72 void MIDIAccess::enableSysEx(bool enable) |
| 73 { |
| 74 m_requesting = false; |
| 75 m_enableSysEx = enable; |
| 76 if (enable) |
| 77 m_accessor->startSession(); |
| 78 else |
| 79 permissionDenied(); |
67 } | 80 } |
68 | 81 |
69 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c
onst String& name, const String& version) | 82 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c
onst String& name, const String& version) |
70 { | 83 { |
71 ASSERT(isMainThread()); | 84 ASSERT(isMainThread()); |
72 | 85 |
| 86 // FIXME: Pass m_enableSysEx flag to filter system exclusive messages correc
tly. |
73 m_inputs.append(MIDIInput::create(scriptExecutionContext(), id, manufacturer
, name, version)); | 87 m_inputs.append(MIDIInput::create(scriptExecutionContext(), id, manufacturer
, name, version)); |
74 } | 88 } |
75 | 89 |
76 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer,
const String& name, const String& version) | 90 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer,
const String& name, const String& version) |
77 { | 91 { |
78 ASSERT(isMainThread()); | 92 ASSERT(isMainThread()); |
79 | 93 |
| 94 // FIXME: Pass m_enableSysEx flag to filter system exclusive messages correc
tly. |
80 m_outputs.append(MIDIOutput::create(scriptExecutionContext(), id, manufactur
er, name, version)); | 95 m_outputs.append(MIDIOutput::create(scriptExecutionContext(), id, manufactur
er, name, version)); |
81 } | 96 } |
82 | 97 |
83 void MIDIAccess::didAllowAccess() | 98 void MIDIAccess::didStartSession() |
84 { | 99 { |
85 ASSERT(isMainThread()); | 100 ASSERT(isMainThread()); |
86 | 101 |
87 m_hasAccess = true; | 102 m_hasAccess = true; |
88 m_promise->fulfill(); | 103 m_promise->fulfill(); |
89 } | 104 } |
90 | 105 |
91 void MIDIAccess::didBlockAccess() | |
92 { | |
93 ASSERT(isMainThread()); | |
94 | |
95 m_hasAccess = false; | |
96 RefPtr<DOMError> error = DOMError::create("SecurityError"); | |
97 m_promise->reject(error); | |
98 } | |
99 | |
100 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat
a, size_t length, double timeStamp) | 106 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat
a, size_t length, double timeStamp) |
101 { | 107 { |
102 ASSERT(isMainThread()); | 108 ASSERT(isMainThread()); |
103 | 109 |
104 if (m_hasAccess && portIndex < m_inputs.size()) { | 110 if (m_hasAccess && portIndex < m_inputs.size()) { |
105 // Convert from time in seconds which is based on the time coordinate sy
stem of monotonicallyIncreasingTime() | 111 // Convert from time in seconds which is based on the time coordinate sy
stem of monotonicallyIncreasingTime() |
106 // into time in milliseconds (a DOMHighResTimeStamp) according to the sa
me time coordinate system as performance.now(). | 112 // into time in milliseconds (a DOMHighResTimeStamp) according to the sa
me time coordinate system as performance.now(). |
107 // This is how timestamps are defined in the Web MIDI spec. | 113 // This is how timestamps are defined in the Web MIDI spec. |
108 Document* document = toDocument(scriptExecutionContext()); | 114 Document* document = toDocument(scriptExecutionContext()); |
109 ASSERT(document); | 115 ASSERT(document); |
110 | 116 |
111 double timeStampInMilliseconds = 1000 * document->loader()->timing()->mo
notonicTimeToZeroBasedDocumentTime(timeStamp); | 117 double timeStampInMilliseconds = 1000 * document->loader()->timing()->mo
notonicTimeToZeroBasedDocumentTime(timeStamp); |
112 | 118 |
113 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeSta
mpInMilliseconds); | 119 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeSta
mpInMilliseconds); |
114 } | 120 } |
115 } | 121 } |
116 | 122 |
117 void MIDIAccess::stop() | 123 void MIDIAccess::stop() |
118 { | 124 { |
119 m_hasAccess = false; | 125 m_hasAccess = false; |
| 126 if (!m_requesting) |
| 127 return; |
| 128 m_requesting = false; |
| 129 Document* document = toDocument(scriptExecutionContext()); |
| 130 ASSERT(document); |
| 131 MIDIController* controller = MIDIController::from(document->page()); |
| 132 ASSERT(controller); |
| 133 controller->cancelSysExPermissionRequest(this); |
120 } | 134 } |
121 | 135 |
| 136 void MIDIAccess::startRequest() |
| 137 { |
| 138 if (!m_promise->options()->sysex) { |
| 139 m_accessor->startSession(); |
| 140 return; |
| 141 } |
| 142 Document* document = toDocument(scriptExecutionContext()); |
| 143 ASSERT(document); |
| 144 MIDIController* controller = MIDIController::from(document->page()); |
| 145 if (controller) { |
| 146 m_requesting = true; |
| 147 controller->requestSysExPermission(this); |
| 148 } else { |
| 149 permissionDenied(); |
| 150 } |
| 151 } |
| 152 |
| 153 void MIDIAccess::permissionDenied() |
| 154 { |
| 155 ASSERT(isMainThread()); |
| 156 |
| 157 m_hasAccess = false; |
| 158 RefPtr<DOMError> error = DOMError::create("SecurityError"); |
| 159 m_promise->reject(error); |
| 160 } |
| 161 |
| 162 |
| 163 |
122 } // namespace WebCore | 164 } // namespace WebCore |
OLD | NEW |