Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / media / midi_host.cc
blob349f302976e6c3d3dcd64e9a44a49bc3fe3af1bd
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/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 : BrowserMessageFilter(MidiMsgStart),
53 renderer_process_id_(renderer_process_id),
54 has_sys_ex_permission_(false),
55 is_session_requested_(false),
56 midi_manager_(midi_manager),
57 sent_bytes_in_flight_(0),
58 bytes_sent_since_last_acknowledgement_(0),
59 output_port_count_(0) {
60 CHECK(midi_manager_);
63 MidiHost::~MidiHost() {
64 // Close an open session, or abort opening a session.
65 if (is_session_requested_)
66 midi_manager_->EndSession(this);
69 void MidiHost::OnDestruct() const {
70 BrowserThread::DeleteOnIOThread::Destruct(this);
73 // IPC Messages handler
74 bool MidiHost::OnMessageReceived(const IPC::Message& message) {
75 bool handled = true;
76 IPC_BEGIN_MESSAGE_MAP(MidiHost, message)
77 IPC_MESSAGE_HANDLER(MidiHostMsg_StartSession, OnStartSession)
78 IPC_MESSAGE_HANDLER(MidiHostMsg_SendData, OnSendData)
79 IPC_MESSAGE_HANDLER(MidiHostMsg_EndSession, OnEndSession)
80 IPC_MESSAGE_UNHANDLED(handled = false)
81 IPC_END_MESSAGE_MAP()
83 return handled;
86 void MidiHost::OnStartSession() {
87 is_session_requested_ = true;
88 midi_manager_->StartSession(this);
91 void MidiHost::OnSendData(uint32 port,
92 const std::vector<uint8>& data,
93 double timestamp) {
95 base::AutoLock auto_lock(output_port_count_lock_);
96 if (output_port_count_ <= port) {
97 RecordAction(base::UserMetricsAction("BadMessageTerminate_MIDIPort"));
98 BadMessageReceived();
99 return;
103 if (data.empty())
104 return;
106 // Blink running in a renderer checks permission to raise a SecurityError
107 // in JavaScript. The actual permission check for security purposes
108 // happens here in the browser process.
109 if (!has_sys_ex_permission_ &&
110 std::find(data.begin(), data.end(), kSysExByte) != data.end()) {
111 RecordAction(base::UserMetricsAction("BadMessageTerminate_MIDI"));
112 BadMessageReceived();
113 return;
116 if (!IsValidWebMIDIData(data))
117 return;
120 base::AutoLock auto_lock(in_flight_lock_);
121 // Sanity check that we won't send too much data.
122 // TODO(yukawa): Consider to send an error event back to the renderer
123 // after some future discussion in W3C.
124 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
125 return;
126 sent_bytes_in_flight_ += data.size();
128 midi_manager_->DispatchSendMidiData(this, port, data, timestamp);
131 void MidiHost::OnEndSession() {
132 is_session_requested_ = false;
133 midi_manager_->EndSession(this);
136 void MidiHost::CompleteStartSession(media::MidiResult result) {
137 DCHECK(is_session_requested_);
138 if (result == media::MIDI_OK) {
139 // ChildSecurityPolicy is set just before OnStartSession by
140 // MidiDispatcherHost. So we can safely cache the policy.
141 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()->
142 CanSendMidiSysExMessage(renderer_process_id_);
144 Send(new MidiMsg_SessionStarted(result));
147 void MidiHost::AddInputPort(const media::MidiPortInfo& info) {
148 base::AutoLock auto_lock(messages_queues_lock_);
149 // MidiMessageQueue is created later in ReceiveMidiData().
150 received_messages_queues_.push_back(nullptr);
151 Send(new MidiMsg_AddInputPort(info));
154 void MidiHost::AddOutputPort(const media::MidiPortInfo& info) {
155 base::AutoLock auto_lock(output_port_count_lock_);
156 output_port_count_++;
157 Send(new MidiMsg_AddOutputPort(info));
160 void MidiHost::SetInputPortState(uint32 port, media::MidiPortState state) {
161 Send(new MidiMsg_SetInputPortState(port, state));
164 void MidiHost::SetOutputPortState(uint32 port, media::MidiPortState state) {
165 Send(new MidiMsg_SetOutputPortState(port, state));
168 void MidiHost::ReceiveMidiData(
169 uint32 port,
170 const uint8* data,
171 size_t length,
172 double timestamp) {
173 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData");
175 base::AutoLock auto_lock(messages_queues_lock_);
176 if (received_messages_queues_.size() <= port)
177 return;
179 // Lazy initialization
180 if (received_messages_queues_[port] == nullptr)
181 received_messages_queues_[port] = new media::MidiMessageQueue(true);
183 received_messages_queues_[port]->Add(data, length);
184 std::vector<uint8> message;
185 while (true) {
186 received_messages_queues_[port]->Get(&message);
187 if (message.empty())
188 break;
190 // MIDI devices may send a system exclusive messages even if the renderer
191 // doesn't have a permission to receive it. Don't kill the renderer as
192 // OnSendData() does.
193 if (message[0] == kSysExByte && !has_sys_ex_permission_)
194 continue;
196 // Send to the renderer.
197 Send(new MidiMsg_DataReceived(port, message, timestamp));
201 void MidiHost::AccumulateMidiBytesSent(size_t n) {
203 base::AutoLock auto_lock(in_flight_lock_);
204 if (n <= sent_bytes_in_flight_)
205 sent_bytes_in_flight_ -= n;
208 if (bytes_sent_since_last_acknowledgement_ + n >=
209 bytes_sent_since_last_acknowledgement_)
210 bytes_sent_since_last_acknowledgement_ += n;
212 if (bytes_sent_since_last_acknowledgement_ >=
213 kAcknowledgementThresholdBytes) {
214 Send(new MidiMsg_AcknowledgeSentData(
215 bytes_sent_since_last_acknowledgement_));
216 bytes_sent_since_last_acknowledgement_ = 0;
220 // static
221 bool MidiHost::IsValidWebMIDIData(const std::vector<uint8>& data) {
222 bool in_sysex = false;
223 size_t waiting_data_length = 0;
224 for (size_t i = 0; i < data.size(); ++i) {
225 const uint8 current = data[i];
226 if (IsSystemRealTimeMessage(current))
227 continue; // Real time message can be placed at any point.
228 if (waiting_data_length > 0) {
229 if (!IsDataByte(current))
230 return false; // Error: |current| should have been data byte.
231 --waiting_data_length;
232 continue; // Found data byte as expected.
234 if (in_sysex) {
235 if (data[i] == kEndOfSysExByte)
236 in_sysex = false;
237 else if (!IsDataByte(current))
238 return false; // Error: |current| should have been data byte.
239 continue; // Found data byte as expected.
241 if (current == kSysExByte) {
242 in_sysex = true;
243 continue; // Found SysEX
245 waiting_data_length = media::GetMidiMessageLength(current);
246 if (waiting_data_length == 0)
247 return false; // Error: |current| should have been a valid status byte.
248 --waiting_data_length; // Found status byte
250 return waiting_data_length == 0 && !in_sysex;
253 } // namespace content