Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: content/browser/renderer_host/media/midi_host.cc

Issue 107513012: [WebMIDI] Introduce UsbMidi{Input, Output}Stream. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-midi-parser
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | media/media.gyp » ('j') | media/midi/midi_message_util.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "content/browser/renderer_host/media/midi_host.h" 5 #include "content/browser/renderer_host/media/midi_host.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/process/process.h" 10 #include "base/process/process.h"
(...skipping 17 matching lines...) Expand all
28 // The total number of bytes which we're allowed to send to the OS 28 // The total number of bytes which we're allowed to send to the OS
29 // before knowing that they have been successfully sent. 29 // before knowing that they have been successfully sent.
30 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. 30 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB.
31 31
32 // We keep track of the number of bytes successfully sent to 32 // We keep track of the number of bytes successfully sent to
33 // the hardware. Every once in a while we report back to the renderer 33 // the hardware. Every once in a while we report back to the renderer
34 // the number of bytes sent since the last report. This threshold determines 34 // the number of bytes sent since the last report. This threshold determines
35 // how many bytes will be sent before reporting back to the renderer. 35 // how many bytes will be sent before reporting back to the renderer.
36 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. 36 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB.
37 37
38 const uint8 kSysExMessage = 0xf0;
39 const uint8 kEndOfSysExMessage = 0xf7;
40
41 bool IsDataByte(uint8 data) { 38 bool IsDataByte(uint8 data) {
42 return (data & 0x80) == 0; 39 return (data & 0x80) == 0;
43 } 40 }
44 41
45 bool IsSystemRealTimeMessage(uint8 data) { 42 bool IsSystemRealTimeMessage(uint8 data) {
46 return 0xf8 <= data && data <= 0xff; 43 return 0xf8 <= data && data <= 0xff;
47 } 44 }
48 45
49 } // namespace 46 } // namespace
50 47
48 using media::kSystemExclusiveByte;
49 using media::kSystemExclusiveEndByte;
Takashi Toyoshima 2014/01/15 12:08:40 EOX or End of SysEx is an official name and I'm us
yhirano 2014/01/15 13:25:42 Done.
50
51 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager) 51 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager)
52 : renderer_process_id_(renderer_process_id), 52 : renderer_process_id_(renderer_process_id),
53 has_sys_ex_permission_(false), 53 has_sys_ex_permission_(false),
54 midi_manager_(midi_manager), 54 midi_manager_(midi_manager),
55 sent_bytes_in_flight_(0), 55 sent_bytes_in_flight_(0),
56 bytes_sent_since_last_acknowledgement_(0) { 56 bytes_sent_since_last_acknowledgement_(0) {
57 } 57 }
58 58
59 MIDIHost::~MIDIHost() { 59 MIDIHost::~MIDIHost() {
60 if (midi_manager_) 60 if (midi_manager_)
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 if (!midi_manager_) 112 if (!midi_manager_)
113 return; 113 return;
114 114
115 if (data.empty()) 115 if (data.empty())
116 return; 116 return;
117 117
118 // Blink running in a renderer checks permission to raise a SecurityError 118 // Blink running in a renderer checks permission to raise a SecurityError
119 // in JavaScript. The actual permission check for security purposes 119 // in JavaScript. The actual permission check for security purposes
120 // happens here in the browser process. 120 // happens here in the browser process.
121 if (!has_sys_ex_permission_ && 121 if (!has_sys_ex_permission_ &&
122 (std::find(data.begin(), data.end(), kSysExMessage) != data.end())) { 122 (std::find(data.begin(), data.end(), kSystemExclusiveByte)
123 != data.end())) {
123 RecordAction(base::UserMetricsAction("BadMessageTerminate_MIDI")); 124 RecordAction(base::UserMetricsAction("BadMessageTerminate_MIDI"));
124 BadMessageReceived(); 125 BadMessageReceived();
125 return; 126 return;
126 } 127 }
127 128
128 if (!IsValidWebMIDIData(data)) 129 if (!IsValidWebMIDIData(data))
129 return; 130 return;
130 131
131 base::AutoLock auto_lock(in_flight_lock_); 132 base::AutoLock auto_lock(in_flight_lock_);
132 // Sanity check that we won't send too much data. 133 // Sanity check that we won't send too much data.
(...skipping 22 matching lines...) Expand all
155 received_messages_queues_[port]->Add(data, length); 156 received_messages_queues_[port]->Add(data, length);
156 std::vector<uint8> message; 157 std::vector<uint8> message;
157 while (true) { 158 while (true) {
158 received_messages_queues_[port]->Get(&message); 159 received_messages_queues_[port]->Get(&message);
159 if (message.empty()) 160 if (message.empty())
160 break; 161 break;
161 162
162 // MIDI devices may send a system exclusive messages even if the renderer 163 // MIDI devices may send a system exclusive messages even if the renderer
163 // doesn't have a permission to receive it. Don't kill the renderer as 164 // doesn't have a permission to receive it. Don't kill the renderer as
164 // OnSendData() does. 165 // OnSendData() does.
165 if (message[0] == kSysExMessage && !has_sys_ex_permission_) 166 if (message[0] == kSystemExclusiveByte && !has_sys_ex_permission_)
166 continue; 167 continue;
167 168
168 // Send to the renderer. 169 // Send to the renderer.
169 Send(new MIDIMsg_DataReceived(port, message, timestamp)); 170 Send(new MIDIMsg_DataReceived(port, message, timestamp));
170 } 171 }
171 } 172 }
172 173
173 void MIDIHost::AccumulateMIDIBytesSent(size_t n) { 174 void MIDIHost::AccumulateMIDIBytesSent(size_t n) {
174 { 175 {
175 base::AutoLock auto_lock(in_flight_lock_); 176 base::AutoLock auto_lock(in_flight_lock_);
(...skipping 21 matching lines...) Expand all
197 const uint8 current = data[i]; 198 const uint8 current = data[i];
198 if (IsSystemRealTimeMessage(current)) 199 if (IsSystemRealTimeMessage(current))
199 continue; // Real time message can be placed at any point. 200 continue; // Real time message can be placed at any point.
200 if (waiting_data_length > 0) { 201 if (waiting_data_length > 0) {
201 if (!IsDataByte(current)) 202 if (!IsDataByte(current))
202 return false; // Error: |current| should have been data byte. 203 return false; // Error: |current| should have been data byte.
203 --waiting_data_length; 204 --waiting_data_length;
204 continue; // Found data byte as expected. 205 continue; // Found data byte as expected.
205 } 206 }
206 if (in_sysex) { 207 if (in_sysex) {
207 if (data[i] == kEndOfSysExMessage) 208 if (data[i] == kSystemExclusiveEndByte)
208 in_sysex = false; 209 in_sysex = false;
209 else if (!IsDataByte(current)) 210 else if (!IsDataByte(current))
210 return false; // Error: |current| should have been data byte. 211 return false; // Error: |current| should have been data byte.
211 continue; // Found data byte as expected. 212 continue; // Found data byte as expected.
212 } 213 }
213 if (current == kSysExMessage) { 214 if (current == kSystemExclusiveByte) {
214 in_sysex = true; 215 in_sysex = true;
215 continue; // Found SysEX 216 continue; // Found SysEX
216 } 217 }
217 waiting_data_length = media::GetMIDIMessageLength(current); 218 waiting_data_length = media::GetMIDIMessageLength(current);
218 if (waiting_data_length == 0) 219 if (waiting_data_length == 0)
219 return false; // Error: |current| should have been a valid status byte. 220 return false; // Error: |current| should have been a valid status byte.
220 --waiting_data_length; // Found status byte 221 --waiting_data_length; // Found status byte
221 } 222 }
222 return waiting_data_length == 0 && !in_sysex; 223 return waiting_data_length == 0 && !in_sysex;
223 } 224 }
224 225
225 } // namespace content 226 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | media/media.gyp » ('j') | media/midi/midi_message_util.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698