| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/media/midi_host.h" | 5 #include "media/midi/midi_service_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 10 #include "base/process/process.h" | 8 #include "base/logging.h" |
| 11 #include "content/browser/browser_main_loop.h" | |
| 12 #include "content/browser/child_process_security_policy_impl.h" | 9 #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" | 10 #include "media/midi/midi_message_util.h" |
| 21 | 11 |
| 22 using media::MidiManager; | 12 namespace media { |
| 23 using media::MidiPortInfoList; | |
| 24 | |
| 25 namespace content { | |
| 26 namespace { | 13 namespace { |
| 27 | 14 |
| 28 // The total number of bytes which we're allowed to send to the OS | 15 // The total number of bytes which we're allowed to send to the OS |
| 29 // before knowing that they have been successfully sent. | 16 // before knowing that they have been successfully sent. |
| 30 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. | 17 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. |
| 31 | 18 |
| 32 // We keep track of the number of bytes successfully sent to | 19 // 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 | 20 // 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 | 21 // 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. | 22 // how many bytes will be sent before reporting back to the renderer. |
| 36 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. | 23 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. |
| 37 | 24 |
| 38 bool IsDataByte(uint8 data) { | 25 bool IsDataByte(uint8 data) { |
| 39 return (data & 0x80) == 0; | 26 return (data & 0x80) == 0; |
| 40 } | 27 } |
| 41 | 28 |
| 42 bool IsSystemRealTimeMessage(uint8 data) { | 29 bool IsSystemRealTimeMessage(uint8 data) { |
| 43 return 0xf8 <= data && data <= 0xff; | 30 return 0xf8 <= data && data <= 0xff; |
| 44 } | 31 } |
| 45 | 32 |
| 46 } // namespace | 33 } // namespace |
| 47 | 34 |
| 48 using media::kSysExByte; | 35 // static |
| 49 using media::kEndOfSysExByte; | 36 void MidiServiceImpl::Create( |
| 37 int renderer_process_id, |
| 38 MidiManager* midi_manager, |
| 39 mojo::InterfaceRequest<MidiService> request) { |
| 40 new MidiServiceImpl(renderer_process_id, midi_manager, request.Pass()); |
| 41 } |
| 50 | 42 |
| 51 MidiHost::MidiHost(int renderer_process_id, media::MidiManager* midi_manager) | 43 MidiServiceImpl::MidiServiceImpl( |
| 52 : BrowserMessageFilter(MidiMsgStart), | 44 int renderer_process_id, |
| 45 MidiManager* midi_manager, |
| 46 mojo::InterfaceRequest<MidiService> request) |
| 47 : binding_(this, request.Pass()), |
| 53 renderer_process_id_(renderer_process_id), | 48 renderer_process_id_(renderer_process_id), |
| 54 has_sys_ex_permission_(false), | 49 midi_manager_(midi_manager) { |
| 55 is_session_requested_(false), | |
| 56 midi_manager_(midi_manager), | |
| 57 sent_bytes_in_flight_(0), | |
| 58 bytes_sent_since_last_acknowledgement_(0) { | |
| 59 CHECK(midi_manager_); | 50 CHECK(midi_manager_); |
| 60 } | 51 } |
| 61 | 52 |
| 62 MidiHost::~MidiHost() { | 53 MidiServiceImpl::~MidiServiceImpl() { |
| 63 // Close an open session, or abort opening a session. | |
| 64 if (is_session_requested_) | 54 if (is_session_requested_) |
| 65 midi_manager_->EndSession(this); | 55 midi_manager_->EndSession(this); |
| 66 } | 56 } |
| 67 | 57 |
| 68 void MidiHost::OnDestruct() const { | 58 void MidiServiceImpl::StartSession( |
| 69 BrowserThread::DeleteOnIOThread::Destruct(this); | 59 MidiServiceClientPtr client, const StartSessionCallback& callback) { |
| 70 } | 60 client_ = client.Pass(); |
| 71 | 61 start_session_callback_ = callback; |
| 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() | |
| 81 | |
| 82 return handled; | |
| 83 } | |
| 84 | |
| 85 void MidiHost::OnStartSession() { | |
| 86 is_session_requested_ = true; | 62 is_session_requested_ = true; |
| 87 midi_manager_->StartSession(this); | 63 midi_manager_->StartSession(this); |
| 88 } | 64 } |
| 89 | 65 |
| 90 void MidiHost::OnSendData(uint32 port, | 66 void MidiServiceImpl::EndSession() { |
| 91 const std::vector<uint8>& data, | 67 is_session_requested_ = false; |
| 92 double timestamp) { | 68 midi_manager_->EndSession(this); |
| 93 if (data.empty()) | 69 } |
| 70 |
| 71 void MidiServiceImpl::SendData(uint32_t port, |
| 72 mojo::Array<uint8_t> data, |
| 73 double timestamp) { |
| 74 if (data.size() == 0) |
| 94 return; | 75 return; |
| 95 | 76 |
| 77 const std::vector<uint8_t>& raw_data = data.storage(); |
| 96 // Blink running in a renderer checks permission to raise a SecurityError | 78 // Blink running in a renderer checks permission to raise a SecurityError |
| 97 // in JavaScript. The actual permission check for security purposes | 79 // in JavaScript. The actual permission check for security purposes |
| 98 // happens here in the browser process. | 80 // happens here in the browser process. |
| 99 if (!has_sys_ex_permission_ && | 81 if (!has_sys_ex_permission_ && |
| 100 std::find(data.begin(), data.end(), kSysExByte) != data.end()) { | 82 std::find(raw_data.begin(), raw_data.end(), kSysExByte) |
| 101 RecordAction(base::UserMetricsAction("BadMessageTerminate_MIDI")); | 83 != raw_data.end()) { |
| 102 BadMessageReceived(); | 84 // TODO(bashi): We no longer use Chromium IPC. What should we do when the |
| 85 // check fails? |
| 103 return; | 86 return; |
| 104 } | 87 } |
| 105 | 88 |
| 106 if (!IsValidWebMIDIData(data)) | 89 if (!IsValidWebMIDIData(data)) |
| 107 return; | 90 return; |
| 108 | 91 |
| 109 { | 92 { |
| 110 base::AutoLock auto_lock(in_flight_lock_); | 93 base::AutoLock auto_lock(in_flight_lock_); |
| 111 // Sanity check that we won't send too much data. | 94 // Sanity check that we won't send too much data. |
| 112 // TODO(yukawa): Consider to send an error event back to the renderer | 95 // TODO(yukawa): Consider to send an error event back to the renderer |
| 113 // after some future discussion in W3C. | 96 // after some future discussion in W3C. |
| 114 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes) | 97 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes) |
| 115 return; | 98 return; |
| 116 sent_bytes_in_flight_ += data.size(); | 99 sent_bytes_in_flight_ += data.size(); |
| 117 } | 100 } |
| 118 midi_manager_->DispatchSendMidiData(this, port, data, timestamp); | 101 midi_manager_->DispatchSendMidiData(this, port, raw_data, timestamp); |
| 119 } | 102 } |
| 120 | 103 |
| 121 void MidiHost::OnEndSession() { | 104 void MidiServiceImpl::CompleteStartSession(media::MidiResult result) { |
| 122 is_session_requested_ = false; | |
| 123 midi_manager_->EndSession(this); | |
| 124 } | |
| 125 | |
| 126 void MidiHost::CompleteStartSession(media::MidiResult result) { | |
| 127 DCHECK(is_session_requested_); | 105 DCHECK(is_session_requested_); |
| 128 if (result == media::MIDI_OK) { | 106 if (result == media::MIDI_OK) { |
| 129 // ChildSecurityPolicy is set just before OnStartSession by | 107 // ChildSecurityPolicy is set just before OnStartSession by |
| 130 // MidiDispatcherHost. So we can safely cache the policy. | 108 // MidiDispatcherHost. So we can safely cache the policy. |
| 131 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> | 109 has_sys_ex_permission_ = |
| 110 content::ChildProcessSecurityPolicyImpl::GetInstance()-> |
| 132 CanSendMidiSysExMessage(renderer_process_id_); | 111 CanSendMidiSysExMessage(renderer_process_id_); |
| 133 } | 112 } |
| 134 Send(new MidiMsg_SessionStarted(result)); | 113 start_session_callback_.Run(static_cast<MidiResultMojo>(result)); |
| 135 } | 114 } |
| 136 | 115 |
| 137 void MidiHost::AddInputPort(const media::MidiPortInfo& info) { | 116 void MidiServiceImpl::AddInputPort(const media::MidiPortInfo& info) { |
| 138 base::AutoLock auto_lock(messages_queues_lock_); | 117 base::AutoLock auto_lock(messages_queues_lock_); |
| 139 // MidiMessageQueue is created later in ReceiveMidiData(). | 118 // MidiMessageQueue is created later in ReceiveMidiData(). |
| 140 received_messages_queues_.push_back(nullptr); | 119 received_messages_queues_.push_back(nullptr); |
| 141 Send(new MidiMsg_AddInputPort(info)); | 120 MidiPortInfoMojoPtr info_mojo(MidiPortInfoMojo::New()); |
| 121 info_mojo->id = info.id; |
| 122 info_mojo->manufacturer = info.manufacturer; |
| 123 info_mojo->name = info.name; |
| 124 info_mojo->version = info.version; |
| 125 client_->AddInputPort(info_mojo.Pass()); |
| 142 } | 126 } |
| 143 | 127 |
| 144 void MidiHost::AddOutputPort(const media::MidiPortInfo& info) { | 128 void MidiServiceImpl::AddOutputPort(const media::MidiPortInfo& info) { |
| 145 Send(new MidiMsg_AddOutputPort(info)); | 129 MidiPortInfoMojoPtr info_mojo(MidiPortInfoMojo::New()); |
| 130 info_mojo->id = info.id; |
| 131 info_mojo->manufacturer = info.manufacturer; |
| 132 info_mojo->name = info.name; |
| 133 info_mojo->version = info.version; |
| 134 client_->AddOutputPort(info_mojo.Pass()); |
| 146 } | 135 } |
| 147 | 136 |
| 148 void MidiHost::ReceiveMidiData( | 137 void MidiServiceImpl::ReceiveMidiData( |
| 149 uint32 port, | 138 uint32 port, |
| 150 const uint8* data, | 139 const uint8* data, |
| 151 size_t length, | 140 size_t length, |
| 152 double timestamp) { | 141 double timestamp) { |
| 153 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData"); | 142 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData"); |
| 154 | 143 |
| 155 base::AutoLock auto_lock(messages_queues_lock_); | 144 base::AutoLock auto_lock(messages_queues_lock_); |
| 156 if (received_messages_queues_.size() <= port) | 145 if (received_messages_queues_.size() <= port) |
| 157 return; | 146 return; |
| 158 | 147 |
| 159 // Lazy initialization | 148 // Lazy initialization |
| 160 if (received_messages_queues_[port] == nullptr) | 149 if (received_messages_queues_[port] == nullptr) |
| 161 received_messages_queues_[port] = new media::MidiMessageQueue(true); | 150 received_messages_queues_[port] = new media::MidiMessageQueue(true); |
| 162 | 151 |
| 163 received_messages_queues_[port]->Add(data, length); | 152 received_messages_queues_[port]->Add(data, length); |
| 164 std::vector<uint8> message; | 153 std::vector<uint8> message; |
| 165 while (true) { | 154 while (true) { |
| 166 received_messages_queues_[port]->Get(&message); | 155 received_messages_queues_[port]->Get(&message); |
| 167 if (message.empty()) | 156 if (message.empty()) |
| 168 break; | 157 break; |
| 169 | 158 |
| 170 // MIDI devices may send a system exclusive messages even if the renderer | 159 // MIDI devices may send a system exclusive messages even if the renderer |
| 171 // doesn't have a permission to receive it. Don't kill the renderer as | 160 // doesn't have a permission to receive it. Don't kill the renderer as |
| 172 // OnSendData() does. | 161 // OnSendData() does. |
| 173 if (message[0] == kSysExByte && !has_sys_ex_permission_) | 162 if (message[0] == kSysExByte && !has_sys_ex_permission_) |
| 174 continue; | 163 continue; |
| 175 | 164 |
| 176 // Send to the renderer. | 165 // Send to the renderer. |
| 177 Send(new MidiMsg_DataReceived(port, message, timestamp)); | 166 mojo::Array<uint8_t> array = mojo::Array<uint8_t>::From(message); |
| 167 client_->DataReceived(port, array.Pass(), timestamp); |
| 178 } | 168 } |
| 179 } | 169 } |
| 180 | 170 |
| 181 void MidiHost::AccumulateMidiBytesSent(size_t n) { | 171 void MidiServiceImpl::AccumulateMidiBytesSent(size_t n) { |
| 182 { | 172 { |
| 183 base::AutoLock auto_lock(in_flight_lock_); | 173 base::AutoLock auto_lock(in_flight_lock_); |
| 184 if (n <= sent_bytes_in_flight_) | 174 if (n <= sent_bytes_in_flight_) |
| 185 sent_bytes_in_flight_ -= n; | 175 sent_bytes_in_flight_ -= n; |
| 186 } | 176 } |
| 187 | 177 |
| 188 if (bytes_sent_since_last_acknowledgement_ + n >= | 178 if (bytes_sent_since_last_acknowledgement_ + n >= |
| 189 bytes_sent_since_last_acknowledgement_) | 179 bytes_sent_since_last_acknowledgement_) |
| 190 bytes_sent_since_last_acknowledgement_ += n; | 180 bytes_sent_since_last_acknowledgement_ += n; |
| 191 | 181 |
| 192 if (bytes_sent_since_last_acknowledgement_ >= | 182 if (bytes_sent_since_last_acknowledgement_ >= |
| 193 kAcknowledgementThresholdBytes) { | 183 kAcknowledgementThresholdBytes) { |
| 194 Send(new MidiMsg_AcknowledgeSentData( | 184 client_->AcknowledgeSentData( |
| 195 bytes_sent_since_last_acknowledgement_)); | 185 bytes_sent_since_last_acknowledgement_); |
| 196 bytes_sent_since_last_acknowledgement_ = 0; | 186 bytes_sent_since_last_acknowledgement_ = 0; |
| 197 } | 187 } |
| 198 } | 188 } |
| 199 | 189 |
| 200 // static | 190 // static |
| 201 bool MidiHost::IsValidWebMIDIData(const std::vector<uint8>& data) { | 191 bool MidiServiceImpl::IsValidWebMIDIData(const std::vector<uint8_t>& data) { |
| 202 bool in_sysex = false; | 192 bool in_sysex = false; |
| 203 size_t waiting_data_length = 0; | 193 size_t waiting_data_length = 0; |
| 204 for (size_t i = 0; i < data.size(); ++i) { | 194 for (size_t i = 0; i < data.size(); ++i) { |
| 205 const uint8 current = data[i]; | 195 const uint8 current = data[i]; |
| 206 if (IsSystemRealTimeMessage(current)) | 196 if (IsSystemRealTimeMessage(current)) |
| 207 continue; // Real time message can be placed at any point. | 197 continue; // Real time message can be placed at any point. |
| 208 if (waiting_data_length > 0) { | 198 if (waiting_data_length > 0) { |
| 209 if (!IsDataByte(current)) | 199 if (!IsDataByte(current)) |
| 210 return false; // Error: |current| should have been data byte. | 200 return false; // Error: |current| should have been data byte. |
| 211 --waiting_data_length; | 201 --waiting_data_length; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 223 continue; // Found SysEX | 213 continue; // Found SysEX |
| 224 } | 214 } |
| 225 waiting_data_length = media::GetMidiMessageLength(current); | 215 waiting_data_length = media::GetMidiMessageLength(current); |
| 226 if (waiting_data_length == 0) | 216 if (waiting_data_length == 0) |
| 227 return false; // Error: |current| should have been a valid status byte. | 217 return false; // Error: |current| should have been a valid status byte. |
| 228 --waiting_data_length; // Found status byte | 218 --waiting_data_length; // Found status byte |
| 229 } | 219 } |
| 230 return waiting_data_length == 0 && !in_sysex; | 220 return waiting_data_length == 0 && !in_sysex; |
| 231 } | 221 } |
| 232 | 222 |
| 233 } // namespace content | 223 } // namespace media |
| OLD | NEW |