Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / media / midi_host.cc
blob56f4099f60b62d41ef6475902608781061a23e9d
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/renderer_host/media/midi_host.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/debug/trace_event.h"
10 #include "base/process.h"
11 #include "content/browser/browser_main_loop.h"
12 #include "content/browser/media/media_internals.h"
13 #include "content/common/media/midi_messages.h"
14 #include "content/public/browser/content_browser_client.h"
15 #include "content/public/browser/media_observer.h"
16 #include "media/midi/midi_manager.h"
18 using media::MIDIManager;
19 using media::MIDIPortInfoList;
21 namespace content {
23 MIDIHost::MIDIHost(media::MIDIManager* midi_manager)
24 : midi_manager_(midi_manager) {
27 MIDIHost::~MIDIHost() {
28 if (midi_manager_)
29 midi_manager_->ReleaseAccess(this);
32 void MIDIHost::OnChannelClosing() {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
35 BrowserMessageFilter::OnChannelClosing();
38 void MIDIHost::OnDestruct() const {
39 BrowserThread::DeleteOnIOThread::Destruct(this);
42 ///////////////////////////////////////////////////////////////////////////////
43 // IPC Messages handler
44 bool MIDIHost::OnMessageReceived(const IPC::Message& message,
45 bool* message_was_ok) {
46 bool handled = true;
47 IPC_BEGIN_MESSAGE_MAP_EX(MIDIHost, message, *message_was_ok)
48 IPC_MESSAGE_HANDLER(MIDIHostMsg_RequestAccess, OnRequestAccess)
49 IPC_MESSAGE_HANDLER(MIDIHostMsg_SendData, OnSendData)
50 IPC_MESSAGE_UNHANDLED(handled = false)
51 IPC_END_MESSAGE_MAP_EX()
53 return handled;
56 void MIDIHost::OnRequestAccess(int client_id, int access) {
57 MIDIPortInfoList input_ports;
58 MIDIPortInfoList output_ports;
60 // Ask permission and register to receive MIDI data.
61 bool approved = false;
62 if (midi_manager_) {
63 approved = midi_manager_->RequestAccess(this, access);
64 if (approved) {
65 input_ports = midi_manager_->input_ports();
66 output_ports = midi_manager_->output_ports();
70 Send(new MIDIMsg_AccessApproved(
71 client_id,
72 access,
73 approved,
74 input_ports,
75 output_ports));
78 void MIDIHost::OnSendData(int port,
79 const std::vector<uint8>& data,
80 double timestamp) {
81 // TODO(crogers): we need to post this to a dedicated thread for
82 // sending MIDI. For now, we will not implement the sending of MIDI data.
83 NOTIMPLEMENTED();
84 // if (midi_manager_)
85 // midi_manager_->SendMIDIData(port, data.data(), data.size(), timestamp);
88 void MIDIHost::ReceiveMIDIData(
89 int port_index,
90 const uint8* data,
91 size_t length,
92 double timestamp) {
93 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData");
94 // Send to the renderer.
95 std::vector<uint8> v(data, data + length);
96 Send(new MIDIMsg_DataReceived(port_index, v, timestamp));
99 } // namespace content