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"
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"
20 using media::MIDIManager;
21 using media::MIDIPortInfoList;
23 // The total number of bytes which we're allowed to send to the OS
24 // before knowing that they have been successfully sent.
25 static const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB.
27 // We keep track of the number of bytes successfully sent to
28 // the hardware. Every once in a while we report back to the renderer
29 // the number of bytes sent since the last report. This threshold determines
30 // how many bytes will be sent before reporting back to the renderer.
31 static const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB.
33 static const uint8 kSysExMessage = 0xf0;
37 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager)
38 : renderer_process_id_(renderer_process_id),
39 midi_manager_(midi_manager),
40 sent_bytes_in_flight_(0),
41 bytes_sent_since_last_acknowledgement_(0) {
44 MIDIHost::~MIDIHost() {
46 midi_manager_->EndSession(this);
49 void MIDIHost::OnDestruct() const {
50 BrowserThread::DeleteOnIOThread::Destruct(this);
53 ///////////////////////////////////////////////////////////////////////////////
54 // IPC Messages handler
55 bool MIDIHost::OnMessageReceived(const IPC::Message& message,
56 bool* message_was_ok) {
58 IPC_BEGIN_MESSAGE_MAP_EX(MIDIHost, message, *message_was_ok)
59 IPC_MESSAGE_HANDLER(MIDIHostMsg_StartSession, OnStartSession)
60 IPC_MESSAGE_HANDLER(MIDIHostMsg_SendData, OnSendData)
61 IPC_MESSAGE_UNHANDLED(handled = false)
62 IPC_END_MESSAGE_MAP_EX()
67 void MIDIHost::OnStartSession(int client_id) {
68 MIDIPortInfoList input_ports;
69 MIDIPortInfoList output_ports;
71 // Initialize devices and register to receive MIDI data.
74 success = midi_manager_->StartSession(this);
76 input_ports = midi_manager_->input_ports();
77 output_ports = midi_manager_->output_ports();
81 Send(new MIDIMsg_SessionStarted(
88 void MIDIHost::OnSendData(uint32 port,
89 const std::vector<uint8>& data,
97 base::AutoLock auto_lock(in_flight_lock_);
99 // Sanity check that we won't send too much.
100 if (sent_bytes_in_flight_ > kMaxInFlightBytes ||
101 data.size() > kMaxInFlightBytes ||
102 data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
105 if (data[0] >= kSysExMessage) {
106 // Blink running in a renderer checks permission to raise a SecurityError in
107 // JavaScript. The actual permission check for security perposes happens
108 // here in the browser process.
109 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage(
110 renderer_process_id_)) {
111 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI"));
112 BadMessageReceived();
117 midi_manager_->DispatchSendMIDIData(
123 sent_bytes_in_flight_ += data.size();
126 void MIDIHost::ReceiveMIDIData(
131 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData");
133 // Check a process security policy to receive a system exclusive message.
134 if (length > 0 && data[0] >= kSysExMessage) {
135 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage(
136 renderer_process_id_)) {
137 // MIDI devices may send a system exclusive messages even if the renderer
138 // doesn't have a permission to receive it. Don't kill the renderer as
139 // OnSendData() does.
144 // Send to the renderer.
145 std::vector<uint8> v(data, data + length);
146 Send(new MIDIMsg_DataReceived(port, v, timestamp));
149 void MIDIHost::AccumulateMIDIBytesSent(size_t n) {
151 base::AutoLock auto_lock(in_flight_lock_);
152 if (n <= sent_bytes_in_flight_)
153 sent_bytes_in_flight_ -= n;
156 if (bytes_sent_since_last_acknowledgement_ + n >=
157 bytes_sent_since_last_acknowledgement_)
158 bytes_sent_since_last_acknowledgement_ += n;
160 if (bytes_sent_since_last_acknowledgement_ >=
161 kAcknowledgementThresholdBytes) {
162 Send(new MIDIMsg_AcknowledgeSentData(
163 bytes_sent_since_last_acknowledgement_));
164 bytes_sent_since_last_acknowledgement_ = 0;
168 } // namespace content