Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / media / midi_host.cc
blobc26a719471887ba6e885690627c00634714fb6c2
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/media/midi_host.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/process/process.h"
10 #include "base/trace_event/trace_event.h"
11 #include "content/browser/bad_message.h"
12 #include "content/browser/browser_main_loop.h"
13 #include "content/browser/child_process_security_policy_impl.h"
14 #include "content/browser/media/media_internals.h"
15 #include "content/common/media/midi_messages.h"
16 #include "content/public/browser/content_browser_client.h"
17 #include "content/public/browser/media_observer.h"
18 #include "content/public/browser/user_metrics.h"
19 #include "media/midi/midi_manager.h"
20 #include "media/midi/midi_message_queue.h"
21 #include "media/midi/midi_message_util.h"
23 namespace content {
24 namespace {
26 // The total number of bytes which we're allowed to send to the OS
27 // before knowing that they have been successfully sent.
28 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB.
30 // We keep track of the number of bytes successfully sent to
31 // the hardware. Every once in a while we report back to the renderer
32 // the number of bytes sent since the last report. This threshold determines
33 // how many bytes will be sent before reporting back to the renderer.
34 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB.
36 bool IsDataByte(uint8 data) {
37 return (data & 0x80) == 0;
40 bool IsSystemRealTimeMessage(uint8 data) {
41 return 0xf8 <= data && data <= 0xff;
44 } // namespace
46 using media::midi::kSysExByte;
47 using media::midi::kEndOfSysExByte;
49 MidiHost::MidiHost(int renderer_process_id,
50 media::midi::MidiManager* midi_manager)
51 : BrowserMessageFilter(MidiMsgStart),
52 renderer_process_id_(renderer_process_id),
53 has_sys_ex_permission_(false),
54 is_session_requested_(false),
55 midi_manager_(midi_manager),
56 sent_bytes_in_flight_(0),
57 bytes_sent_since_last_acknowledgement_(0),
58 output_port_count_(0) {
59 CHECK(midi_manager_);
62 MidiHost::~MidiHost() {
63 // Close an open session, or abort opening a session.
64 if (is_session_requested_)
65 midi_manager_->EndSession(this);
68 void MidiHost::OnDestruct() const {
69 BrowserThread::DeleteOnIOThread::Destruct(this);
72 // IPC Messages handler
73 bool MidiHost::OnMessageReceived(const IPC::Message& message) {
74 bool handled = true;
75 IPC_BEGIN_MESSAGE_MAP(MidiHost, message)
76 IPC_MESSAGE_HANDLER(MidiHostMsg_StartSession, OnStartSession)
77 IPC_MESSAGE_HANDLER(MidiHostMsg_SendData, OnSendData)
78 IPC_MESSAGE_HANDLER(MidiHostMsg_EndSession, OnEndSession)
79 IPC_MESSAGE_UNHANDLED(handled = false)
80 IPC_END_MESSAGE_MAP()
82 return handled;
85 void MidiHost::OnStartSession() {
86 is_session_requested_ = true;
87 midi_manager_->StartSession(this);
90 void MidiHost::OnSendData(uint32 port,
91 const std::vector<uint8>& data,
92 double timestamp) {
94 base::AutoLock auto_lock(output_port_count_lock_);
95 if (output_port_count_ <= port) {
96 bad_message::ReceivedBadMessage(this, bad_message::MH_INVALID_MIDI_PORT);
97 return;
101 if (data.empty())
102 return;
104 // Blink running in a renderer checks permission to raise a SecurityError
105 // in JavaScript. The actual permission check for security purposes
106 // happens here in the browser process.
107 if (!has_sys_ex_permission_ &&
108 std::find(data.begin(), data.end(), kSysExByte) != data.end()) {
109 bad_message::ReceivedBadMessage(this, bad_message::MH_SYS_EX_PERMISSION);
110 return;
113 if (!IsValidWebMIDIData(data))
114 return;
117 base::AutoLock auto_lock(in_flight_lock_);
118 // Sanity check that we won't send too much data.
119 // TODO(yukawa): Consider to send an error event back to the renderer
120 // after some future discussion in W3C.
121 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
122 return;
123 sent_bytes_in_flight_ += data.size();
125 midi_manager_->DispatchSendMidiData(this, port, data, timestamp);
128 void MidiHost::OnEndSession() {
129 is_session_requested_ = false;
130 midi_manager_->EndSession(this);
133 void MidiHost::CompleteStartSession(media::midi::Result result) {
134 DCHECK(is_session_requested_);
135 if (result == media::midi::Result::OK) {
136 // ChildSecurityPolicy is set just before OnStartSession by
137 // MidiDispatcherHost. So we can safely cache the policy.
138 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()->
139 CanSendMidiSysExMessage(renderer_process_id_);
141 Send(new MidiMsg_SessionStarted(result));
144 void MidiHost::AddInputPort(const media::midi::MidiPortInfo& info) {
145 base::AutoLock auto_lock(messages_queues_lock_);
146 // MidiMessageQueue is created later in ReceiveMidiData().
147 received_messages_queues_.push_back(nullptr);
148 Send(new MidiMsg_AddInputPort(info));
151 void MidiHost::AddOutputPort(const media::midi::MidiPortInfo& info) {
152 base::AutoLock auto_lock(output_port_count_lock_);
153 output_port_count_++;
154 Send(new MidiMsg_AddOutputPort(info));
157 void MidiHost::SetInputPortState(uint32 port,
158 media::midi::MidiPortState state) {
159 Send(new MidiMsg_SetInputPortState(port, state));
162 void MidiHost::SetOutputPortState(uint32 port,
163 media::midi::MidiPortState state) {
164 Send(new MidiMsg_SetOutputPortState(port, state));
167 void MidiHost::ReceiveMidiData(
168 uint32 port,
169 const uint8* data,
170 size_t length,
171 double timestamp) {
172 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData");
174 base::AutoLock auto_lock(messages_queues_lock_);
175 if (received_messages_queues_.size() <= port)
176 return;
178 // Lazy initialization
179 if (received_messages_queues_[port] == nullptr)
180 received_messages_queues_[port] = new media::midi::MidiMessageQueue(true);
182 received_messages_queues_[port]->Add(data, length);
183 std::vector<uint8> message;
184 while (true) {
185 received_messages_queues_[port]->Get(&message);
186 if (message.empty())
187 break;
189 // MIDI devices may send a system exclusive messages even if the renderer
190 // doesn't have a permission to receive it. Don't kill the renderer as
191 // OnSendData() does.
192 if (message[0] == kSysExByte && !has_sys_ex_permission_)
193 continue;
195 // Send to the renderer.
196 Send(new MidiMsg_DataReceived(port, message, timestamp));
200 void MidiHost::AccumulateMidiBytesSent(size_t n) {
202 base::AutoLock auto_lock(in_flight_lock_);
203 if (n <= sent_bytes_in_flight_)
204 sent_bytes_in_flight_ -= n;
207 if (bytes_sent_since_last_acknowledgement_ + n >=
208 bytes_sent_since_last_acknowledgement_)
209 bytes_sent_since_last_acknowledgement_ += n;
211 if (bytes_sent_since_last_acknowledgement_ >=
212 kAcknowledgementThresholdBytes) {
213 Send(new MidiMsg_AcknowledgeSentData(
214 bytes_sent_since_last_acknowledgement_));
215 bytes_sent_since_last_acknowledgement_ = 0;
219 // static
220 bool MidiHost::IsValidWebMIDIData(const std::vector<uint8>& data) {
221 bool in_sysex = false;
222 size_t waiting_data_length = 0;
223 for (size_t i = 0; i < data.size(); ++i) {
224 const uint8 current = data[i];
225 if (IsSystemRealTimeMessage(current))
226 continue; // Real time message can be placed at any point.
227 if (waiting_data_length > 0) {
228 if (!IsDataByte(current))
229 return false; // Error: |current| should have been data byte.
230 --waiting_data_length;
231 continue; // Found data byte as expected.
233 if (in_sysex) {
234 if (data[i] == kEndOfSysExByte)
235 in_sysex = false;
236 else if (!IsDataByte(current))
237 return false; // Error: |current| should have been data byte.
238 continue; // Found data byte as expected.
240 if (current == kSysExByte) {
241 in_sysex = true;
242 continue; // Found SysEX
244 waiting_data_length = media::midi::GetMidiMessageLength(current);
245 if (waiting_data_length == 0)
246 return false; // Error: |current| should have been a valid status byte.
247 --waiting_data_length; // Found status byte
249 return waiting_data_length == 0 && !in_sysex;
252 } // namespace content