| 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/renderer/media/midi_message_filter.h" | 5 #include "content/renderer/media/midi_message_filter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "content/common/media/midi_messages.h" | 11 #include "content/common/media/midi_messages.h" |
| 12 #include "content/renderer/render_thread_impl.h" | 12 #include "content/renderer/render_thread_impl.h" |
| 13 #include "ipc/ipc_logging.h" | 13 #include "ipc/ipc_logging.h" |
| 14 | 14 |
| 15 using media::MidiPortInfoList; | 15 using media::MidiPortInfoList; |
| 16 using base::AutoLock; | 16 using base::AutoLock; |
| 17 | 17 |
| 18 // The maximum number of bytes which we're allowed to send to the browser | 18 // The maximum number of bytes which we're allowed to send to the browser |
| 19 // before getting acknowledgement back from the browser that they've been | 19 // before getting acknowledgement back from the browser that they've been |
| 20 // successfully sent. | 20 // successfully sent. |
| 21 static const size_t kMaxUnacknowledgedBytesSent = 10 * 1024 * 1024; // 10 MB. | 21 static const size_t kMaxUnacknowledgedBytesSent = 10 * 1024 * 1024; // 10 MB. |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 | 24 |
| 25 MidiMessageFilter::MidiMessageFilter( | 25 MidiMessageFilter::MidiMessageFilter( |
| 26 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) | 26 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) |
| 27 : channel_(NULL), | 27 : sender_(NULL), |
| 28 io_message_loop_(io_message_loop), | 28 io_message_loop_(io_message_loop), |
| 29 main_message_loop_(base::MessageLoopProxy::current()), | 29 main_message_loop_(base::MessageLoopProxy::current()), |
| 30 next_available_id_(0), | 30 next_available_id_(0), |
| 31 unacknowledged_bytes_sent_(0) { | 31 unacknowledged_bytes_sent_(0) { |
| 32 } | 32 } |
| 33 | 33 |
| 34 MidiMessageFilter::~MidiMessageFilter() {} | 34 MidiMessageFilter::~MidiMessageFilter() {} |
| 35 | 35 |
| 36 void MidiMessageFilter::Send(IPC::Message* message) { | 36 void MidiMessageFilter::Send(IPC::Message* message) { |
| 37 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 37 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 38 if (!channel_) { | 38 if (!sender_) { |
| 39 delete message; | 39 delete message; |
| 40 } else { | 40 } else { |
| 41 channel_->Send(message); | 41 sender_->Send(message); |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool MidiMessageFilter::OnMessageReceived(const IPC::Message& message) { | 45 bool MidiMessageFilter::OnMessageReceived(const IPC::Message& message) { |
| 46 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 46 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 47 bool handled = true; | 47 bool handled = true; |
| 48 IPC_BEGIN_MESSAGE_MAP(MidiMessageFilter, message) | 48 IPC_BEGIN_MESSAGE_MAP(MidiMessageFilter, message) |
| 49 IPC_MESSAGE_HANDLER(MidiMsg_SessionStarted, OnSessionStarted) | 49 IPC_MESSAGE_HANDLER(MidiMsg_SessionStarted, OnSessionStarted) |
| 50 IPC_MESSAGE_HANDLER(MidiMsg_DataReceived, OnDataReceived) | 50 IPC_MESSAGE_HANDLER(MidiMsg_DataReceived, OnDataReceived) |
| 51 IPC_MESSAGE_HANDLER(MidiMsg_AcknowledgeSentData, OnAcknowledgeSentData) | 51 IPC_MESSAGE_HANDLER(MidiMsg_AcknowledgeSentData, OnAcknowledgeSentData) |
| 52 IPC_MESSAGE_UNHANDLED(handled = false) | 52 IPC_MESSAGE_UNHANDLED(handled = false) |
| 53 IPC_END_MESSAGE_MAP() | 53 IPC_END_MESSAGE_MAP() |
| 54 return handled; | 54 return handled; |
| 55 } | 55 } |
| 56 | 56 |
| 57 void MidiMessageFilter::OnFilterAdded(IPC::Channel* channel) { | 57 void MidiMessageFilter::OnFilterAdded(IPC::Sender* sender) { |
| 58 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 58 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 59 channel_ = channel; | 59 sender_ = sender; |
| 60 } | 60 } |
| 61 | 61 |
| 62 void MidiMessageFilter::OnFilterRemoved() { | 62 void MidiMessageFilter::OnFilterRemoved() { |
| 63 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 63 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 64 | 64 |
| 65 // Once removed, a filter will not be used again. At this time all | 65 // Once removed, a filter will not be used again. At this time all |
| 66 // delegates must be notified so they release their reference. | 66 // delegates must be notified so they release their reference. |
| 67 OnChannelClosing(); | 67 OnChannelClosing(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void MidiMessageFilter::OnChannelClosing() { | 70 void MidiMessageFilter::OnChannelClosing() { |
| 71 DCHECK(io_message_loop_->BelongsToCurrentThread()); | 71 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 72 channel_ = NULL; | 72 sender_ = NULL; |
| 73 } | 73 } |
| 74 | 74 |
| 75 void MidiMessageFilter::StartSession(blink::WebMIDIAccessorClient* client) { | 75 void MidiMessageFilter::StartSession(blink::WebMIDIAccessorClient* client) { |
| 76 // Generate and keep track of a "client id" which is sent to the browser | 76 // Generate and keep track of a "client id" which is sent to the browser |
| 77 // to ask permission to talk to MIDI hardware. | 77 // to ask permission to talk to MIDI hardware. |
| 78 // This id is handed back when we receive the answer in OnAccessApproved(). | 78 // This id is handed back when we receive the answer in OnAccessApproved(). |
| 79 if (clients_.find(client) == clients_.end()) { | 79 if (clients_.find(client) == clients_.end()) { |
| 80 int client_id = next_available_id_++; | 80 int client_id = next_available_id_++; |
| 81 clients_[client] = client_id; | 81 clients_[client] = client_id; |
| 82 | 82 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 return; | 227 return; |
| 228 } | 228 } |
| 229 | 229 |
| 230 unacknowledged_bytes_sent_ += n; | 230 unacknowledged_bytes_sent_ += n; |
| 231 | 231 |
| 232 // Send to the browser. | 232 // Send to the browser. |
| 233 Send(new MidiHostMsg_SendData(port, data, timestamp)); | 233 Send(new MidiHostMsg_SendData(port, data, timestamp)); |
| 234 } | 234 } |
| 235 | 235 |
| 236 } // namespace content | 236 } // namespace content |
| OLD | NEW |