Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / media / midi / midi_manager_usb.cc
blob346098c5a851f67d262eb5a6475fefc6d8956825
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
3 // found in the LICENSE file.
5 #include "media/midi/midi_manager_usb.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "media/midi/midi_scheduler.h"
13 #include "media/midi/usb_midi_descriptor_parser.h"
14 #include "media/midi/usb_midi_device.h"
15 #include "media/midi/usb_midi_input_stream.h"
16 #include "media/midi/usb_midi_jack.h"
17 #include "media/midi/usb_midi_output_stream.h"
19 namespace media {
21 MidiManagerUsb::MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> factory)
22 : device_factory_(factory.Pass()) {
25 MidiManagerUsb::~MidiManagerUsb() {
28 void MidiManagerUsb::StartInitialization() {
29 Initialize(
30 base::Bind(&MidiManager::CompleteInitialization, base::Unretained(this)));
33 void MidiManagerUsb::Initialize(
34 base::Callback<void(MidiResult result)> callback) {
35 initialize_callback_ = callback;
36 scheduler_.reset(new MidiScheduler(this));
37 // This is safe because EnumerateDevices cancels the operation on destruction.
38 device_factory_->EnumerateDevices(
39 this,
40 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone,
41 base::Unretained(this)));
44 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client,
45 uint32_t port_index,
46 const std::vector<uint8>& data,
47 double timestamp) {
48 if (port_index >= output_streams_.size()) {
49 // |port_index| is provided by a renderer so we can't believe that it is
50 // in the valid range.
51 return;
53 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted.
54 // The task posted to the MidiScheduler will be disposed safely on deleting
55 // the scheduler.
56 scheduler_->PostSendDataTask(
57 client, data.size(), timestamp,
58 base::Bind(&UsbMidiOutputStream::Send,
59 base::Unretained(output_streams_[port_index]), data));
62 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device,
63 int endpoint_number,
64 const uint8* data,
65 size_t size,
66 base::TimeTicks time) {
67 if (!input_stream_)
68 return;
69 input_stream_->OnReceivedData(device,
70 endpoint_number,
71 data,
72 size,
73 time);
76 void MidiManagerUsb::OnDeviceAttached(scoped_ptr<UsbMidiDevice> device) {
77 int device_id = devices_.size();
78 devices_.push_back(device.release());
79 AddPorts(devices_.back(), device_id);
82 void MidiManagerUsb::OnDeviceDetached(size_t index) {
83 if (index >= devices_.size()) {
84 return;
86 UsbMidiDevice* device = devices_[index];
87 for (size_t i = 0; i < output_streams_.size(); ++i) {
88 if (output_streams_[i]->jack().device == device) {
89 SetOutputPortState(i, MIDI_PORT_DISCONNECTED);
92 const std::vector<UsbMidiJack>& input_jacks = input_stream_->jacks();
93 for (size_t i = 0; i < input_jacks.size(); ++i) {
94 if (input_jacks[i].device == device) {
95 SetInputPortState(i, MIDI_PORT_DISCONNECTED);
100 void MidiManagerUsb::OnReceivedData(size_t jack_index,
101 const uint8* data,
102 size_t size,
103 base::TimeTicks time) {
104 ReceiveMidiData(jack_index, data, size, time);
108 void MidiManagerUsb::OnEnumerateDevicesDone(bool result,
109 UsbMidiDevice::Devices* devices) {
110 if (!result) {
111 initialize_callback_.Run(MIDI_INITIALIZATION_ERROR);
112 return;
114 input_stream_.reset(new UsbMidiInputStream(this));
115 devices->swap(devices_);
116 for (size_t i = 0; i < devices_.size(); ++i) {
117 if (!AddPorts(devices_[i], i)) {
118 initialize_callback_.Run(MIDI_INITIALIZATION_ERROR);
119 return;
122 initialize_callback_.Run(MIDI_OK);
125 bool MidiManagerUsb::AddPorts(UsbMidiDevice* device, int device_id) {
126 UsbMidiDescriptorParser parser;
127 std::vector<uint8> descriptor = device->GetDescriptors();
128 const uint8* data = descriptor.size() > 0 ? &descriptor[0] : NULL;
129 std::vector<UsbMidiJack> jacks;
130 bool parse_result = parser.Parse(device,
131 data,
132 descriptor.size(),
133 &jacks);
134 if (!parse_result)
135 return false;
137 std::string manufacturer(device->GetManufacturer());
138 std::string product_name(device->GetProductName());
139 std::string version(device->GetDeviceVersion());
141 for (size_t j = 0; j < jacks.size(); ++j) {
142 // Port ID must be unique in a MIDI manager. This ID setting is
143 // sufficiently unique although there is no user-friendly meaning.
144 // TODO(yhirano): Use a hashed string as ID.
145 std::string id(
146 base::StringPrintf("port-%d-%ld", device_id, static_cast<long>(j)));
147 if (jacks[j].direction() == UsbMidiJack::DIRECTION_OUT) {
148 output_streams_.push_back(new UsbMidiOutputStream(jacks[j]));
149 AddOutputPort(MidiPortInfo(id, manufacturer, product_name, version,
150 MIDI_PORT_OPENED));
151 } else {
152 DCHECK_EQ(jacks[j].direction(), UsbMidiJack::DIRECTION_IN);
153 input_stream_->Add(jacks[j]);
154 AddInputPort(MidiPortInfo(id, manufacturer, product_name, version,
155 MIDI_PORT_OPENED));
158 return true;
161 } // namespace media