| Index: content/renderer/media/midi_message_filter.cc
|
| diff --git a/content/renderer/media/midi_message_filter.cc b/content/renderer/media/midi_message_filter.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..61f2953c29a478537e7084da10da3fa6c6a2501d
|
| --- /dev/null
|
| +++ b/content/renderer/media/midi_message_filter.cc
|
| @@ -0,0 +1,189 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/renderer/media/midi_message_filter.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/debug/trace_event.h"
|
| +#include "base/message_loop_proxy.h"
|
| +#include "base/utf_string_conversions.h"
|
| +#include "content/common/media/midi_messages.h"
|
| +#include "content/renderer/render_thread_impl.h"
|
| +#include "ipc/ipc_logging.h"
|
| +
|
| +using media::MIDIPortInfoList;
|
| +using base::AutoLock;
|
| +
|
| +namespace content {
|
| +
|
| +int MIDIMessageFilter::next_available_id_ = 0;
|
| +
|
| +MIDIMessageFilter::MIDIMessageFilter(
|
| + const scoped_refptr<base::MessageLoopProxy>& io_message_loop)
|
| + : channel_(NULL),
|
| + io_message_loop_(io_message_loop) {
|
| +}
|
| +
|
| +MIDIMessageFilter::~MIDIMessageFilter() {}
|
| +
|
| +void MIDIMessageFilter::Send(IPC::Message* message) {
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| + if (!channel_) {
|
| + delete message;
|
| + } else {
|
| + channel_->Send(message);
|
| + }
|
| +}
|
| +
|
| +bool MIDIMessageFilter::OnMessageReceived(const IPC::Message& message) {
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| + bool handled = true;
|
| + IPC_BEGIN_MESSAGE_MAP(MIDIMessageFilter, message)
|
| + IPC_MESSAGE_HANDLER(MIDIMsg_AccessApproved, OnAccessApproved)
|
| + IPC_MESSAGE_HANDLER(MIDIMsg_DataReceived, OnDataReceived)
|
| + IPC_MESSAGE_UNHANDLED(handled = false)
|
| + IPC_END_MESSAGE_MAP()
|
| + return handled;
|
| +}
|
| +
|
| +void MIDIMessageFilter::OnFilterAdded(IPC::Channel* channel) {
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| + channel_ = channel;
|
| +}
|
| +
|
| +void MIDIMessageFilter::OnFilterRemoved() {
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| +
|
| + // Once removed, a filter will not be used again. At this time all
|
| + // delegates must be notified so they release their reference.
|
| + OnChannelClosing();
|
| +}
|
| +
|
| +void MIDIMessageFilter::OnChannelClosing() {
|
| + DCHECK(io_message_loop_->BelongsToCurrentThread());
|
| + channel_ = NULL;
|
| +}
|
| +
|
| +void MIDIMessageFilter::RequestAccess(
|
| + WebKit::WebMIDIAccessor::Client* client, int access) {
|
| + AutoLock auto_lock(clients_lock_);
|
| +
|
| + // Get the main thread message loop.
|
| + if (!client_message_loop_)
|
| + client_message_loop_ = base::MessageLoopProxy::current();
|
| + else
|
| + CHECK(client_message_loop_ == base::MessageLoopProxy::current());
|
| +
|
| + // Generate and keep track of a "client id" which is sent to the browser
|
| + // to ask permission to talk to MIDI hardware.
|
| + // This id is handed back when we receive the answer in OnAccessApproved().
|
| + if (clients_.find(client) == clients_.end()) {
|
| + int client_id = next_available_id_++;
|
| + clients_[client] = client_id;
|
| + Send(new MIDIHostMsg_RequestAccess(client_id, access));
|
| + }
|
| +};
|
| +
|
| +void MIDIMessageFilter::RemoveClient(WebKit::WebMIDIAccessor::Client* client) {
|
| + AutoLock auto_lock(clients_lock_);
|
| + ClientsMap::iterator i = clients_.find(client);
|
| + if (i != clients_.end())
|
| + clients_.erase(i);
|
| +};
|
| +
|
| +// Received from browser.
|
| +
|
| +void MIDIMessageFilter::OnAccessApproved(
|
| + int client_id,
|
| + int access,
|
| + bool success,
|
| + MIDIPortInfoList inputs,
|
| + MIDIPortInfoList outputs) {
|
| + if (client_message_loop_) {
|
| + // Handle on the main JS thread.
|
| + client_message_loop_->PostTask(FROM_HERE,
|
| + base::Bind(&MIDIMessageFilter::HandleAccessApproved, this,
|
| + client_id, access, success, inputs, outputs));
|
| + }
|
| +}
|
| +
|
| +void MIDIMessageFilter::HandleAccessApproved(
|
| + int client_id,
|
| + int access,
|
| + bool success,
|
| + MIDIPortInfoList inputs,
|
| + MIDIPortInfoList outputs) {
|
| + AutoLock auto_lock(clients_lock_);
|
| +
|
| + WebKit::WebMIDIAccessor::Client* client = GetClientFromId(client_id);
|
| + if (!client)
|
| + return;
|
| +
|
| + if (success) {
|
| + // Add the client's input and output ports.
|
| + for (size_t i = 0; i < inputs.size(); ++i) {
|
| + client->addInputPort(
|
| + UTF8ToUTF16(inputs[i].id_),
|
| + UTF8ToUTF16(inputs[i].manufacturer_),
|
| + UTF8ToUTF16(inputs[i].name_),
|
| + UTF8ToUTF16(inputs[i].version_));
|
| + }
|
| +
|
| + for (size_t i = 0; i < outputs.size(); ++i) {
|
| + client->addOutputPort(
|
| + UTF8ToUTF16(outputs[i].id_),
|
| + UTF8ToUTF16(outputs[i].manufacturer_),
|
| + UTF8ToUTF16(outputs[i].name_),
|
| + UTF8ToUTF16(outputs[i].version_));
|
| + }
|
| + }
|
| +
|
| + client->accessApproved(success);
|
| +}
|
| +
|
| +WebKit::WebMIDIAccessor::Client*
|
| +MIDIMessageFilter::GetClientFromId(int client_id) {
|
| + // Iterating like this seems inefficient, but in practice there generally
|
| + // will be very few clients (usually one). Additionally, this lookup
|
| + // usually happens one time during page load. So the performance hit is
|
| + // negligible.
|
| + for (ClientsMap::iterator i = clients_.begin(); i != clients_.end(); ++i) {
|
| + if ((*i).second == client_id)
|
| + return (*i).first;
|
| + }
|
| + return NULL;
|
| +}
|
| +
|
| +void MIDIMessageFilter::OnDataReceived(int port,
|
| + const std::vector<uint8>& data,
|
| + double timestamp) {
|
| + TRACE_EVENT0("midi", "MIDIMessageFilter::OnDataReceived");
|
| +
|
| + if (client_message_loop_) {
|
| + client_message_loop_->PostTask(FROM_HERE,
|
| + base::Bind(&MIDIMessageFilter::HandleDataReceived, this,
|
| + port, data, timestamp));
|
| + }
|
| +}
|
| +
|
| +void MIDIMessageFilter::HandleDataReceived(int port,
|
| + const std::vector<uint8>& data,
|
| + double timestamp) {
|
| + TRACE_EVENT0("midi", "MIDIMessageFilter::HandleDataReceived");
|
| +
|
| + AutoLock auto_lock(clients_lock_);
|
| + for (ClientsMap::iterator i = clients_.begin(); i != clients_.end(); ++i)
|
| + (*i).first->receiveMIDIData(port, data.data(), data.size(), timestamp);
|
| +}
|
| +
|
| +void MIDIMessageFilter::SendMIDIData(int port_index,
|
| + const uint8* data,
|
| + size_t length,
|
| + double timestamp) {
|
| + // Send to the browser.
|
| + std::vector<uint8> v(data, data + length);
|
| + Send(new MIDIHostMsg_SendData(port_index, v, timestamp));
|
| +}
|
| +
|
| +} // namespace content
|
|
|