[WebMIDI] Increment in-flight bytes in front of sending data.
[chromium-blink-merge.git] / content / browser / renderer_host / media / midi_host.cc
blobb6e26886db7902bbd0071a9bea3f86cc0f387b00
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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/media/midi_host.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/debug/trace_event.h"
10 #include "base/process/process.h"
11 #include "content/browser/browser_main_loop.h"
12 #include "content/browser/child_process_security_policy_impl.h"
13 #include "content/browser/media/media_internals.h"
14 #include "content/common/media/midi_messages.h"
15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/media_observer.h"
17 #include "content/public/browser/user_metrics.h"
18 #include "media/midi/midi_manager.h"
19 #include "media/midi/midi_message_queue.h"
20 #include "media/midi/midi_message_util.h"
22 using media::MidiManager;
23 using media::MidiPortInfoList;
25 namespace content {
26 namespace {
28 // The total number of bytes which we're allowed to send to the OS
29 // before knowing that they have been successfully sent.
30 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB.
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
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.
36 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB.
38 bool IsDataByte(uint8 data) {
39 return (data & 0x80) == 0;
42 bool IsSystemRealTimeMessage(uint8 data) {
43 return 0xf8 <= data && data <= 0xff;
46 } // namespace
48 using media::kSysExByte;
49 using media::kEndOfSysExByte;
51 MidiHost::MidiHost(int renderer_process_id, media::MidiManager* midi_manager)
52 : renderer_process_id_(renderer_process_id),
53 has_sys_ex_permission_(false),
54 midi_manager_(midi_manager),
55 sent_bytes_in_flight_(0),
56 bytes_sent_since_last_acknowledgement_(0) {
59 MidiHost::~MidiHost() {
60 if (midi_manager_)
61 midi_manager_->EndSession(this);
64 void MidiHost::OnDestruct() const {
65 BrowserThread::DeleteOnIOThread::Destruct(this);
68 // IPC Messages handler
69 bool MidiHost::OnMessageReceived(const IPC::Message& message,
70 bool* message_was_ok) {
71 bool handled = true;
72 IPC_BEGIN_MESSAGE_MAP_EX(MidiHost, message, *message_was_ok)
73 IPC_MESSAGE_HANDLER(MidiHostMsg_StartSession, OnStartSession)
74 IPC_MESSAGE_HANDLER(MidiHostMsg_SendData, OnSendData)
75 IPC_MESSAGE_UNHANDLED(handled = false)
76 IPC_END_MESSAGE_MAP_EX()
78 return handled;
81 void MidiHost::OnStartSession(int client_id) {
82 MidiPortInfoList input_ports;
83 MidiPortInfoList output_ports;
85 // Initialize devices and register to receive MIDI data.
86 bool success = false;
87 if (midi_manager_) {
88 success = midi_manager_->StartSession(this);
89 if (success) {
90 input_ports = midi_manager_->input_ports();
91 output_ports = midi_manager_->output_ports();
92 received_messages_queues_.clear();
93 received_messages_queues_.resize(input_ports.size());
94 // ChildSecurityPolicy is set just before OnStartSession by
95 // MidiDispatcherHost. So we can safely cache the policy.
96 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()->
97 CanSendMidiSysExMessage(renderer_process_id_);
101 Send(new MidiMsg_SessionStarted(
102 client_id,
103 success,
104 input_ports,
105 output_ports));
108 void MidiHost::OnSendData(uint32 port,
109 const std::vector<uint8>& data,
110 double timestamp) {
111 if (!midi_manager_)
112 return;
114 if (data.empty())
115 return;
117 // Blink running in a renderer checks permission to raise a SecurityError
118 // in JavaScript. The actual permission check for security purposes
119 // happens here in the browser process.
120 if (!has_sys_ex_permission_ &&
121 std::find(data.begin(), data.end(), kSysExByte) != data.end()) {
122 RecordAction(base::UserMetricsAction("BadMessageTerminate_MIDI"));
123 BadMessageReceived();
124 return;
127 if (!IsValidWebMIDIData(data))
128 return;
131 base::AutoLock auto_lock(in_flight_lock_);
132 // Sanity check that we won't send too much data.
133 // TODO(yukawa): Consider to send an error event back to the renderer
134 // after some future discussion in W3C.
135 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
136 return;
137 sent_bytes_in_flight_ += data.size();
139 midi_manager_->DispatchSendMidiData(this, port, data, timestamp);
142 void MidiHost::ReceiveMidiData(
143 uint32 port,
144 const uint8* data,
145 size_t length,
146 double timestamp) {
147 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData");
149 if (received_messages_queues_.size() <= port)
150 return;
152 // Lazy initialization
153 if (received_messages_queues_[port] == NULL)
154 received_messages_queues_[port] = new media::MidiMessageQueue(true);
156 received_messages_queues_[port]->Add(data, length);
157 std::vector<uint8> message;
158 while (true) {
159 received_messages_queues_[port]->Get(&message);
160 if (message.empty())
161 break;
163 // MIDI devices may send a system exclusive messages even if the renderer
164 // doesn't have a permission to receive it. Don't kill the renderer as
165 // OnSendData() does.
166 if (message[0] == kSysExByte && !has_sys_ex_permission_)
167 continue;
169 // Send to the renderer.
170 Send(new MidiMsg_DataReceived(port, message, timestamp));
174 void MidiHost::AccumulateMidiBytesSent(size_t n) {
176 base::AutoLock auto_lock(in_flight_lock_);
177 if (n <= sent_bytes_in_flight_)
178 sent_bytes_in_flight_ -= n;
181 if (bytes_sent_since_last_acknowledgement_ + n >=
182 bytes_sent_since_last_acknowledgement_)
183 bytes_sent_since_last_acknowledgement_ += n;
185 if (bytes_sent_since_last_acknowledgement_ >=
186 kAcknowledgementThresholdBytes) {
187 Send(new MidiMsg_AcknowledgeSentData(
188 bytes_sent_since_last_acknowledgement_));
189 bytes_sent_since_last_acknowledgement_ = 0;
193 // static
194 bool MidiHost::IsValidWebMIDIData(const std::vector<uint8>& data) {
195 bool in_sysex = false;
196 size_t waiting_data_length = 0;
197 for (size_t i = 0; i < data.size(); ++i) {
198 const uint8 current = data[i];
199 if (IsSystemRealTimeMessage(current))
200 continue; // Real time message can be placed at any point.
201 if (waiting_data_length > 0) {
202 if (!IsDataByte(current))
203 return false; // Error: |current| should have been data byte.
204 --waiting_data_length;
205 continue; // Found data byte as expected.
207 if (in_sysex) {
208 if (data[i] == kEndOfSysExByte)
209 in_sysex = false;
210 else if (!IsDataByte(current))
211 return false; // Error: |current| should have been data byte.
212 continue; // Found data byte as expected.
214 if (current == kSysExByte) {
215 in_sysex = true;
216 continue; // Found SysEX
218 waiting_data_length = media::GetMidiMessageLength(current);
219 if (waiting_data_length == 0)
220 return false; // Error: |current| should have been a valid status byte.
221 --waiting_data_length; // Found status byte
223 return waiting_data_length == 0 && !in_sysex;
226 } // namespace content