| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/media/midi_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/process/process.h" | 9 #include "base/process/process.h" |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 MidiHost::MidiHost(int renderer_process_id, | 49 MidiHost::MidiHost(int renderer_process_id, |
| 50 media::midi::MidiManager* midi_manager) | 50 media::midi::MidiManager* midi_manager) |
| 51 : BrowserMessageFilter(MidiMsgStart), | 51 : BrowserMessageFilter(MidiMsgStart), |
| 52 renderer_process_id_(renderer_process_id), | 52 renderer_process_id_(renderer_process_id), |
| 53 has_sys_ex_permission_(false), | 53 has_sys_ex_permission_(false), |
| 54 is_session_requested_(false), | 54 is_session_requested_(false), |
| 55 midi_manager_(midi_manager), | 55 midi_manager_(midi_manager), |
| 56 sent_bytes_in_flight_(0), | 56 sent_bytes_in_flight_(0), |
| 57 bytes_sent_since_last_acknowledgement_(0), | 57 bytes_sent_since_last_acknowledgement_(0), |
| 58 output_port_count_(0) { | 58 output_port_count_(0) { |
| 59 CHECK(midi_manager_); | 59 DCHECK(midi_manager_); |
| 60 } | 60 } |
| 61 | 61 |
| 62 MidiHost::~MidiHost() { | 62 MidiHost::~MidiHost() { |
| 63 // Close an open session, or abort opening a session. | 63 // Close an open session, or abort opening a session. |
| 64 if (is_session_requested_) | 64 if (is_session_requested_ && midi_manager_) |
| 65 midi_manager_->EndSession(this); | 65 midi_manager_->EndSession(this); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void MidiHost::OnDestruct() const { | 68 void MidiHost::OnDestruct() const { |
| 69 BrowserThread::DeleteOnIOThread::Destruct(this); | 69 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // IPC Messages handler | 72 // IPC Messages handler |
| 73 bool MidiHost::OnMessageReceived(const IPC::Message& message) { | 73 bool MidiHost::OnMessageReceived(const IPC::Message& message) { |
| 74 bool handled = true; | 74 bool handled = true; |
| 75 IPC_BEGIN_MESSAGE_MAP(MidiHost, message) | 75 IPC_BEGIN_MESSAGE_MAP(MidiHost, message) |
| 76 IPC_MESSAGE_HANDLER(MidiHostMsg_StartSession, OnStartSession) | 76 IPC_MESSAGE_HANDLER(MidiHostMsg_StartSession, OnStartSession) |
| 77 IPC_MESSAGE_HANDLER(MidiHostMsg_SendData, OnSendData) | 77 IPC_MESSAGE_HANDLER(MidiHostMsg_SendData, OnSendData) |
| 78 IPC_MESSAGE_HANDLER(MidiHostMsg_EndSession, OnEndSession) | 78 IPC_MESSAGE_HANDLER(MidiHostMsg_EndSession, OnEndSession) |
| 79 IPC_MESSAGE_UNHANDLED(handled = false) | 79 IPC_MESSAGE_UNHANDLED(handled = false) |
| 80 IPC_END_MESSAGE_MAP() | 80 IPC_END_MESSAGE_MAP() |
| 81 | 81 |
| 82 return handled; | 82 return handled; |
| 83 } | 83 } |
| 84 | 84 |
| 85 void MidiHost::OnStartSession() { | 85 void MidiHost::OnStartSession() { |
| 86 is_session_requested_ = true; | 86 is_session_requested_ = true; |
| 87 midi_manager_->StartSession(this); | 87 if (midi_manager_) |
| 88 midi_manager_->StartSession(this); |
| 88 } | 89 } |
| 89 | 90 |
| 90 void MidiHost::OnSendData(uint32 port, | 91 void MidiHost::OnSendData(uint32 port, |
| 91 const std::vector<uint8>& data, | 92 const std::vector<uint8>& data, |
| 92 double timestamp) { | 93 double timestamp) { |
| 93 { | 94 { |
| 94 base::AutoLock auto_lock(output_port_count_lock_); | 95 base::AutoLock auto_lock(output_port_count_lock_); |
| 95 if (output_port_count_ <= port) { | 96 if (output_port_count_ <= port) { |
| 96 bad_message::ReceivedBadMessage(this, bad_message::MH_INVALID_MIDI_PORT); | 97 bad_message::ReceivedBadMessage(this, bad_message::MH_INVALID_MIDI_PORT); |
| 97 return; | 98 return; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 115 | 116 |
| 116 { | 117 { |
| 117 base::AutoLock auto_lock(in_flight_lock_); | 118 base::AutoLock auto_lock(in_flight_lock_); |
| 118 // Sanity check that we won't send too much data. | 119 // Sanity check that we won't send too much data. |
| 119 // TODO(yukawa): Consider to send an error event back to the renderer | 120 // TODO(yukawa): Consider to send an error event back to the renderer |
| 120 // after some future discussion in W3C. | 121 // after some future discussion in W3C. |
| 121 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes) | 122 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes) |
| 122 return; | 123 return; |
| 123 sent_bytes_in_flight_ += data.size(); | 124 sent_bytes_in_flight_ += data.size(); |
| 124 } | 125 } |
| 125 midi_manager_->DispatchSendMidiData(this, port, data, timestamp); | 126 if (midi_manager_) |
| 127 midi_manager_->DispatchSendMidiData(this, port, data, timestamp); |
| 126 } | 128 } |
| 127 | 129 |
| 128 void MidiHost::OnEndSession() { | 130 void MidiHost::OnEndSession() { |
| 129 is_session_requested_ = false; | 131 is_session_requested_ = false; |
| 130 midi_manager_->EndSession(this); | 132 if (midi_manager_) |
| 133 midi_manager_->EndSession(this); |
| 131 } | 134 } |
| 132 | 135 |
| 133 void MidiHost::CompleteStartSession(media::midi::Result result) { | 136 void MidiHost::CompleteStartSession(media::midi::Result result) { |
| 134 DCHECK(is_session_requested_); | 137 DCHECK(is_session_requested_); |
| 135 if (result == media::midi::Result::OK) { | 138 if (result == media::midi::Result::OK) { |
| 136 // ChildSecurityPolicy is set just before OnStartSession by | 139 // ChildSecurityPolicy is set just before OnStartSession by |
| 137 // MidiDispatcherHost. So we can safely cache the policy. | 140 // MidiDispatcherHost. So we can safely cache the policy. |
| 138 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> | 141 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> |
| 139 CanSendMidiSysExMessage(renderer_process_id_); | 142 CanSendMidiSysExMessage(renderer_process_id_); |
| 140 } | 143 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 bytes_sent_since_last_acknowledgement_ += n; | 212 bytes_sent_since_last_acknowledgement_ += n; |
| 210 | 213 |
| 211 if (bytes_sent_since_last_acknowledgement_ >= | 214 if (bytes_sent_since_last_acknowledgement_ >= |
| 212 kAcknowledgementThresholdBytes) { | 215 kAcknowledgementThresholdBytes) { |
| 213 Send(new MidiMsg_AcknowledgeSentData( | 216 Send(new MidiMsg_AcknowledgeSentData( |
| 214 bytes_sent_since_last_acknowledgement_)); | 217 bytes_sent_since_last_acknowledgement_)); |
| 215 bytes_sent_since_last_acknowledgement_ = 0; | 218 bytes_sent_since_last_acknowledgement_ = 0; |
| 216 } | 219 } |
| 217 } | 220 } |
| 218 | 221 |
| 222 void MidiHost::Detached() { |
| 223 midi_manager_ = nullptr; |
| 224 } |
| 225 |
| 219 // static | 226 // static |
| 220 bool MidiHost::IsValidWebMIDIData(const std::vector<uint8>& data) { | 227 bool MidiHost::IsValidWebMIDIData(const std::vector<uint8>& data) { |
| 221 bool in_sysex = false; | 228 bool in_sysex = false; |
| 222 size_t waiting_data_length = 0; | 229 size_t waiting_data_length = 0; |
| 223 for (size_t i = 0; i < data.size(); ++i) { | 230 for (size_t i = 0; i < data.size(); ++i) { |
| 224 const uint8 current = data[i]; | 231 const uint8 current = data[i]; |
| 225 if (IsSystemRealTimeMessage(current)) | 232 if (IsSystemRealTimeMessage(current)) |
| 226 continue; // Real time message can be placed at any point. | 233 continue; // Real time message can be placed at any point. |
| 227 if (waiting_data_length > 0) { | 234 if (waiting_data_length > 0) { |
| 228 if (!IsDataByte(current)) | 235 if (!IsDataByte(current)) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 243 } | 250 } |
| 244 waiting_data_length = media::midi::GetMidiMessageLength(current); | 251 waiting_data_length = media::midi::GetMidiMessageLength(current); |
| 245 if (waiting_data_length == 0) | 252 if (waiting_data_length == 0) |
| 246 return false; // Error: |current| should have been a valid status byte. | 253 return false; // Error: |current| should have been a valid status byte. |
| 247 --waiting_data_length; // Found status byte | 254 --waiting_data_length; // Found status byte |
| 248 } | 255 } |
| 249 return waiting_data_length == 0 && !in_sysex; | 256 return waiting_data_length == 0 && !in_sysex; |
| 250 } | 257 } |
| 251 | 258 |
| 252 } // namespace content | 259 } // namespace content |
| OLD | NEW |