Don't send a SHChangeNotify for creating an app icon when creating a shortcut.
[chromium-blink-merge.git] / media / midi / midi_manager_usb.cc
blobeab41dae8efe5bc14b3fb55f55ce90b3047d735e
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/usb_midi_descriptor_parser.h"
13 #include "media/midi/usb_midi_device.h"
14 #include "media/midi/usb_midi_input_stream.h"
15 #include "media/midi/usb_midi_jack.h"
16 #include "media/midi/usb_midi_output_stream.h"
18 namespace media {
20 MidiManagerUsb::MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> factory)
21 : device_factory_(factory.Pass()) {
24 MidiManagerUsb::~MidiManagerUsb() {
27 void MidiManagerUsb::StartInitialization() {
28 Initialize(
29 base::Bind(&MidiManager::CompleteInitialization, base::Unretained(this)));
32 void MidiManagerUsb::Initialize(
33 base::Callback<void(MidiResult result)> callback) {
34 initialize_callback_ = callback;
35 // This is safe because EnumerateDevices cancels the operation on destruction.
36 device_factory_->EnumerateDevices(
37 this,
38 base::Bind(&MidiManagerUsb::OnEnumerateDevicesDone,
39 base::Unretained(this)));
42 void MidiManagerUsb::DispatchSendMidiData(MidiManagerClient* client,
43 uint32_t port_index,
44 const std::vector<uint8>& data,
45 double timestamp) {
46 if (port_index >= output_streams_.size()) {
47 // |port_index| is provided by a renderer so we can't believe that it is
48 // in the valid range.
49 return;
51 output_streams_[port_index]->Send(data);
52 client->AccumulateMidiBytesSent(data.size());
55 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device,
56 int endpoint_number,
57 const uint8* data,
58 size_t size,
59 base::TimeTicks time) {
60 if (!input_stream_)
61 return;
62 input_stream_->OnReceivedData(device,
63 endpoint_number,
64 data,
65 size,
66 time);
69 void MidiManagerUsb::OnDeviceAttached(scoped_ptr<UsbMidiDevice> device) {
70 int device_id = devices_.size();
71 devices_.push_back(device.release());
72 AddPorts(devices_.back(), device_id);
75 void MidiManagerUsb::OnDeviceDetached(size_t index) {
76 if (index >= devices_.size()) {
77 return;
79 UsbMidiDevice* device = devices_[index];
80 for (size_t i = 0; i < output_streams_.size(); ++i) {
81 if (output_streams_[i]->jack().device == device) {
82 SetOutputPortState(i, MIDI_PORT_DISCONNECTED);
85 const std::vector<UsbMidiJack>& input_jacks = input_stream_->jacks();
86 for (size_t i = 0; i < input_jacks.size(); ++i) {
87 if (input_jacks[i].device == device) {
88 SetInputPortState(i, MIDI_PORT_DISCONNECTED);
93 void MidiManagerUsb::OnReceivedData(size_t jack_index,
94 const uint8* data,
95 size_t size,
96 base::TimeTicks time) {
97 ReceiveMidiData(jack_index, data, size, time);
101 void MidiManagerUsb::OnEnumerateDevicesDone(bool result,
102 UsbMidiDevice::Devices* devices) {
103 if (!result) {
104 initialize_callback_.Run(MIDI_INITIALIZATION_ERROR);
105 return;
107 input_stream_.reset(new UsbMidiInputStream(this));
108 devices->swap(devices_);
109 for (size_t i = 0; i < devices_.size(); ++i) {
110 if (!AddPorts(devices_[i], i)) {
111 initialize_callback_.Run(MIDI_INITIALIZATION_ERROR);
112 return;
115 initialize_callback_.Run(MIDI_OK);
118 bool MidiManagerUsb::AddPorts(UsbMidiDevice* device, int device_id) {
119 UsbMidiDescriptorParser parser;
120 std::vector<uint8> descriptor = device->GetDescriptor();
121 const uint8* data = descriptor.size() > 0 ? &descriptor[0] : NULL;
122 std::vector<UsbMidiJack> jacks;
123 bool parse_result = parser.Parse(device,
124 data,
125 descriptor.size(),
126 &jacks);
127 if (!parse_result)
128 return false;
130 for (size_t j = 0; j < jacks.size(); ++j) {
131 if (jacks[j].direction() == UsbMidiJack::DIRECTION_OUT) {
132 output_streams_.push_back(new UsbMidiOutputStream(jacks[j]));
133 // TODO(yhirano): Set appropriate properties.
134 // TODO(yhiran): Port ID should contain product ID / vendor ID.
135 // Port ID must be unique in a MIDI manager. This (and the below) ID
136 // setting is sufficiently unique although there is no user-friendly
137 // meaning.
138 MidiPortInfo port;
139 port.state = MIDI_PORT_OPENED;
140 port.id = base::StringPrintf("port-%d-%ld",
141 device_id,
142 static_cast<long>(j));
143 AddOutputPort(port);
144 } else {
145 DCHECK_EQ(jacks[j].direction(), UsbMidiJack::DIRECTION_IN);
146 input_stream_->Add(jacks[j]);
147 // TODO(yhirano): Set appropriate properties.
148 MidiPortInfo port;
149 port.state = MIDI_PORT_OPENED;
150 port.id = base::StringPrintf("port-%d-%ld",
151 device_id,
152 static_cast<long>(j));
153 AddInputPort(port);
156 return true;
159 } // namespace media